From 2dbf80faf6e056cfbc097326c8fc655106ede7cb Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Wed, 20 Sep 2023 13:21:58 +0100 Subject: [PATCH] nixos/laptop: init --- hosts/hachune-nixos/default.nix | 39 ++++++++--------- hosts/sankyuu-nixos/default.nix | 21 +++------ nixos/default.nix | 2 +- nixos/{laptop.nix => laptop/default.nix} | 43 +++---------------- nixos/laptop/tlp.nix | 54 ++++++++++++++++++++++++ nixos/laptop/wireless.nix | 47 +++++++++++++++++++++ 6 files changed, 132 insertions(+), 74 deletions(-) rename nixos/{laptop.nix => laptop/default.nix} (50%) create mode 100644 nixos/laptop/tlp.nix create mode 100644 nixos/laptop/wireless.nix diff --git a/hosts/hachune-nixos/default.nix b/hosts/hachune-nixos/default.nix index 98bd8646..6345eff7 100644 --- a/hosts/hachune-nixos/default.nix +++ b/hosts/hachune-nixos/default.nix @@ -21,19 +21,25 @@ in mediaDir = "/mnt/media/${config.meta.username}"; }; - nixos.server = { - enable = true; - iperf3.enable = true; - jellyfin.enable = true; - networkd.enable = true; - plex.enable = true; - rtorrent.enable = true; - ssh.enable = true; - tailscale.enable = true; - duckdns-updater = { + nixos = { + laptop.tlp = { enable = true; - certs.enable = true; - domain = "hachune-nixos.duckdns.org"; + cpuFreqGovernor = "schedutil"; + }; + server = { + enable = true; + iperf3.enable = true; + jellyfin.enable = true; + networkd.enable = true; + plex.enable = true; + rtorrent.enable = true; + ssh.enable = true; + tailscale.enable = true; + duckdns-updater = { + enable = true; + certs.enable = true; + domain = "hachune-nixos.duckdns.org"; + }; }; }; @@ -47,14 +53,5 @@ in home-manager.users.${config.meta.username}.theme.wallpaper.path = pkgs.wallpapers.hatsune-miku_stylized-ultrawide; # Reduce power consumption - services.tlp = { - enable = true; - settings = { - CPU_SCALING_GOVERNOR_ON_AC = "schedutil"; - CPU_SCALING_GOVERNOR_ON_BAT = "schedutil"; - RUNTIME_PM_ON_AC = "auto"; - }; - }; - time.timeZone = "America/Sao_Paulo"; } diff --git a/hosts/sankyuu-nixos/default.nix b/hosts/sankyuu-nixos/default.nix index daadfb11..0faba0ef 100644 --- a/hosts/sankyuu-nixos/default.nix +++ b/hosts/sankyuu-nixos/default.nix @@ -55,6 +55,13 @@ in # The audio device from this notebook doesn't seem to like short buffers too much quantum = 128; }; + laptop.tlp = { + cpuFreqGovernor = "schedutil"; + batteryThreshold = { + start = 75; + stop = 80; + }; + }; }; programs.steam.gamescopeSession.args = [ @@ -67,20 +74,6 @@ in # Used for firmware updates services.fwupd.enable = true; - services.tlp.settings = { - # Set battery thresholds - START_CHARGE_THRESH_BAT0 = 75; - STOP_CHARGE_THRESH_BAT0 = 80; - # Use `tlp setcharge` to restore the charging thresholds - RESTORE_THRESHOLDS_ON_BAT = 1; - # Increase performance on AC - PLATFORM_PROFILE_ON_AC = "performance"; - PLATFORM_PROFILE_ON_BAT = "balanced"; - # Use schedutil governor - CPU_SCALING_GOVERNOR_ON_AC = "schedutil"; - CPU_SCALING_GOVERNOR_ON_BAT = "schedutil"; - }; - # https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/1849 systemd.services.fix-mic-light = { diff --git a/nixos/default.nix b/nixos/default.nix index 830651de..87ed479e 100644 --- a/nixos/default.nix +++ b/nixos/default.nix @@ -7,7 +7,7 @@ ./dev ./fonts.nix ./home.nix - ./laptop.nix + ./laptop ./minimal.nix ./server ]; diff --git a/nixos/laptop.nix b/nixos/laptop/default.nix similarity index 50% rename from nixos/laptop.nix rename to nixos/laptop/default.nix index 047e1614..4ce642be 100644 --- a/nixos/laptop.nix +++ b/nixos/laptop/default.nix @@ -1,46 +1,20 @@ { config, pkgs, lib, ... }: { + imports = [ + ./wireless.nix + ./tlp.nix + ]; + options.nixos.laptop.enable = lib.mkEnableOption "laptop config" // { default = (config.device.type == "laptop"); }; config = lib.mkIf config.nixos.laptop.enable { - networking = { - # Use Network Manager - networkmanager = { - enable = true; - wifi.backend = "iwd"; - }; - }; - # Configure hibernation boot.resumeDevice = lib.mkIf (config.swapDevices != [ ]) (lib.mkDefault (builtins.head config.swapDevices).device); - # Install laptop related packages - environment.systemPackages = with pkgs; [ iw ]; - - # Configure special hardware in laptops - hardware = { - # Enable bluetooth - bluetooth = { enable = true; }; - }; - - # Enable programs that need special configuration - programs = { - # Enable NetworkManager applet - nm-applet = { enable = true; }; - }; - - # Make nm-applet restart in case of failure - systemd.user.services.nm-applet = { - serviceConfig = { - RestartSec = 3; - Restart = "on-failure"; - }; - }; - # Enable laptop specific services services = { # Enable Blueman to manage Bluetooth @@ -56,13 +30,6 @@ lidSwitchExternalPower = lib.mkDefault "lock"; }; - # Use systemd-resolved for DNS - resolved = { - enable = true; - # Can make DNS lookups really slow - dnssec = "false"; - }; - # Reduce power consumption tlp.enable = true; }; diff --git a/nixos/laptop/tlp.nix b/nixos/laptop/tlp.nix new file mode 100644 index 00000000..b75fef8a --- /dev/null +++ b/nixos/laptop/tlp.nix @@ -0,0 +1,54 @@ +{ config, pkgs, lib, ... }: + +let + cfg = config.nixos.laptop.tlp; +in +{ + options.nixos.laptop.tlp = { + enable = lib.mkEnableOption "TLP config" // { + default = config.nixos.laptop.enable; + }; + cpuFreqGovernor = lib.mkOption { + default = config.powerManagement.cpuFreqGovernor; + types = lib.types.nullOr lib.types.string; + example = "schedutil"; + description = "CPU frequency governor to be set via TLP."; + }; + batteryThreshold = { + start = lib.mkOption { + default = 0; + types = lib.types.int; + description = "Start of battery charging threshold"; + }; + stop = lib.mkOption { + default = 0; + types = lib.types.int; + description = "Stop of battery charging threshold"; + }; + }; + }; + + config = lib.mkIf cfg.enable { + # Reduce power consumption + services.tlp = { + enable = true; + # https://linrunner.de/tlp/support/optimizing.html + settings = { + # Enable the platform profile low-power + PLATFORM_PROFILE_ON_BAT = lib.mkDefault "balanced"; + # Enable the platform profile performance + PLATFORM_PROFILE_ON_AC = lib.mkDefault "performance"; + # Enable runtime power management + RUNTIME_PM_ON_AC = lib.mkDefault "auto"; + # CPU frequency governor + CPU_SCALING_GOVERNOR_ON_AC = lib.mkIf (cfg.cpuFreqGovernor != null) cfg.cpuFreqGovernor; + CPU_SCALING_GOVERNOR_ON_BAT = lib.mkIf (cfg.cpuFreqGovernor != null) cfg.cpuFreqGovernor; + # Set battery thresholds + START_CHARGE_THRESH_BAT0 = cfg.batteryThreshold.start; + STOP_CHARGE_THRESH_BAT0 = cfg.batteryThreshold.stop; + # Use `tlp setcharge` to restore the charging thresholds + RESTORE_THRESHOLDS_ON_BAT = lib.mkDefault 1; + }; + }; + }; +} diff --git a/nixos/laptop/wireless.nix b/nixos/laptop/wireless.nix new file mode 100644 index 00000000..1bb06953 --- /dev/null +++ b/nixos/laptop/wireless.nix @@ -0,0 +1,47 @@ +{ config, pkgs, lib, ... }: + +{ + options.nixos.laptop.wireless.enable = lib.mkEnableOption "Wi-Fi/Bluetooth config" // { + default = config.nixos.laptop.enable; + }; + + config = lib.mkIf config.nixos.laptop.enable { + networking = { + # Use Network Manager + networkmanager = { + enable = true; + wifi.backend = "iwd"; + }; + }; + + # Install Wireless related packages + environment.systemPackages = with pkgs; [ iw ]; + + # Enable bluetooth + hardware.bluetooth.enable = true; + + # Enable NetworkManager applet + programs.nm-applet.enable = true; + + # Make nm-applet restart in case of failure + systemd.user.services.nm-applet = { + serviceConfig = { + RestartSec = 3; + Restart = "on-failure"; + }; + }; + + # Wireless related config + services = { + # Enable Blueman to manage Bluetooth + blueman.enable = true; + + # Use systemd-resolved for DNS + resolved = { + enable = true; + # Can make DNS lookups really slow + dnssec = "false"; + }; + }; + }; +}