Jump to content

Laptop: Difference between revisions

From NixOS Wiki
imported>AmnesiaAmesia
m Corrected spelling mistake
Klinger (talk | contribs)
- reordered headings (moved all headings one heading up). Added the 3 options for closing the lid.
 
(19 intermediate revisions by 8 users not shown)
Line 1: Line 1:
== Power management ==
= Closing the lid =
NixOS has several tools to help you manage the power on your system and it also have a stock feature for power management. To enable the stock NixOS power management tool, which allow for managing hibernate and suspend states you can write <code>powerManagement.enable = true;</code>. This tool is compatible with the other tools mentioned, but the other tools may however overwrite the settings set.
<syntaxhighlight lang="nixos">
services.logind.lidSwitch = "poweroff";
services.logind.lidSwitchExternalPower = "lock";
services.logind.lidSwitchDocked = "ignore";


=== CPU performance scaling ===
# one of "ignore", "poweroff", "reboot", "halt", "kexec", "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate", "lock"
==== thermald ====
</syntaxhighlight>These three options can be used to configure how a laptop should behave when the lid is closed. In this example, it normally shuts down. If power is connected, only the screen is locked. If another screen is connected instead, nothing happens.
Thermald proactively prevents overheating on intel cpus and work well with other tools. The tool can enabled <code>services.thermald.enable = true;</code>


==== tlp ====
= Power management =
A common tool used to save power on laptops is tlp, which has sensible defaults for most laptops. To enable tlp you simply just write <code>services.tlp.enable = true;</code> in your configraion.nix. However, if you need a specific configuration, you can do as shown in the example here below.
NixOS has several tools to help you manage the power on your system and it also has a stock feature for power management. To enable the stock NixOS power management tool which allows for managing hibernate and suspend states you can write <code>powerManagement.enable = true;</code>. This tool is compatible with the other tools mentioned, but the other tools may overwrite this setting.
 
== CPU performance scaling ==
=== thermald ===
Thermald proactively prevents overheating on Intel CPUs and works well with other tools. Enabled by: <code>services.thermald.enable = true;</code>
 
=== TLP ===
A common tool used to save power on laptops is [https://linrunner.de/tlp/index.html TLP], which has sensible defaults for most laptops. To enable TLP you simply just write <code>services.tlp.enable = true;</code> in your <code>configuration.nix</code>. However, if you need a specific configuration, you can do as shown in the example below.


<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
services.tlp = {
services.tlp = {
      enable = true;
  enable = true;
      settings = {
  settings = {
        CPU_SCALING_GOVERNOR_ON_AC = "performance";
    CPU_SCALING_GOVERNOR_ON_AC = "performance";
        CPU_SCALING_GOVERNOR_ON_BAT = "powersave";
    CPU_SCALING_GOVERNOR_ON_BAT = "powersave";


        CPU_ENERGY_PERF_POLICY_ON_BAT = "powersave";
    CPU_ENERGY_PERF_POLICY_ON_BAT = "power";
        CPU_ENERGY_PERF_POLICY_ON_AC = "performance";
    CPU_ENERGY_PERF_POLICY_ON_AC = "performance";


        CPU_SCALING_MIN_FREQ_ON_AC = 800000;
    CPU_MIN_PERF_ON_AC = 0;
        CPU_SCALING_MAX_FREQ_ON_AC = 5000000;
    CPU_MAX_PERF_ON_AC = 100;
        CPU_SCALING_MIN_FREQ_ON_BAT = 800000;
    CPU_MIN_PERF_ON_BAT = 0;
        CPU_SCALING_MAX_FREQ_ON_BAT = 2600000;
    CPU_MAX_PERF_ON_BAT = 20;
      };
 
    };
    # Optional helps save long term battery health
    START_CHARGE_THRESH_BAT0 = 40; # 40 and bellow it starts to charge
    STOP_CHARGE_THRESH_BAT0 = 80; # 80 and above it stops charging
  };
};
</syntaxHighlight>
</syntaxHighlight>


This example enables tlp and sets the minimum and maximum frequencies for the cpu based on whether it is plugged into power or not. It also changes the cpu scaling governor based on this.
This example enables TLP and sets the minimum and maximum frequencies for the CPU based on whether it is plugged into power or not. It also changes the CPU scaling governor based on this.


==== auto-cpufreq ====
=== auto-cpufreq ===
Another tool used for power management is auto-cpufreq which aims to replace tlp. When using auto-cpufreq it is therefore recommended to disable tlp as these tools are conflicting with each other. However, NixOS does allow for using both at the same time, and you therefore run them in tandem at your own risk. To enable the service, just add <code>services.auto-cpufreq.enable = true;</code> to your configuration.nix
Another tool used for power management is [https://github.com/AdnanHodzic/auto-cpufreq auto-cpufreq] which aims to replace TLP. When using auto-cpufreq it is therefore recommended to disable TLP as these tools are conflicting with each other. However, NixOS does allow for using both at the same time, and you can therefore run them in tandem at your own risk. To enable the service, add <code>services.auto-cpufreq.enable = true;</code> to your <code>configuration.nix</code>.


Example of how to configure auto-cpufreq:
Example of how to configure auto-cpufreq:
Line 37: Line 50:
services.auto-cpufreq.settings = {
services.auto-cpufreq.settings = {
   battery = {
   battery = {
    governor = "powersave";
    governor = "powersave";
    turbo = "never";
    turbo = "never";
   };
   };
   charger = {
   charger = {
    governor = "performance";
    governor = "performance";
    turbo = "auto";
    turbo = "auto";
   };
   };
};
};
</syntaxHighlight>
</syntaxHighlight>
Alternatively, if you have [[Flakes]] enabled you can also use the flake directly provided by the auto-cpufreq authors to get a more up-to-date version.  They offer a detailed explanation how to add it to your system on their [https://github.com/AdnanHodzic/auto-cpufreq?tab=readme-ov-file#nixos GitHub page].
To summarize:</br>
1) add the flake as an input to your <code>flake.nix</code> file and enable the module:
<syntaxhighlight lang="nix">
# flake.nix
{
  inputs =
    {
      # ---Snip---
      auto-cpufreq = {
        url = "github:AdnanHodzic/auto-cpufreq";
        inputs.nixpkgs.follows = "nixpkgs";
      };
      # ---Snip---
    };
  outputs = inputs@{ nixpkgs, auto-cpufreq, ... }: {
    nixosConfigurations.HOSTNAME = nixpkgs.lib.nixosSystem {
      specialArgs = {
        inherit inputs;
      };
      modules = [
        ./configuration.nix
        auto-cpufreq.nixosModules.default
      ];
    };
  };
}
</syntaxhighlight>2) Then enable it in your <code>configuration.nix</code> file:
<syntaxhighlight lang="nix">
# configuration.nix
{ inputs, pkgs, ... }: {
  # ---Snip---
  programs.auto-cpufreq.enable = true;
  # optionally, you can configure your auto-cpufreq settings, if you have any
  programs.auto-cpufreq.settings = {
    charger = {
      governor = "performance";
      turbo = "auto";
    };
    battery = {
      governor = "powersave";
      turbo = "auto";
    };
  };
  # ---Snip---
}
</syntaxhighlight>
Since v2.0 auto-cpufreq also includes a GUI that lets you temporarily override the CPU frequency governor setting.


==== Powertop ====
=== Powertop ===
Powertop is a power analysis tool, but it also has a feature referred to as auto-tune which will enable power saving settings on your device. These power saving settings should be almost the same as those enabled by tlp, although you powertop enables usb autosuspend per default. This can make your input devices such as the keyboard unresponsive for some time when it has been suspended. To enable powetop write  <code>powerManagement.powertop.enable = true;</code> and it should be noted that this also enables the auto-tune feature of powertop.
Powertop is a power analysis tool, but it also has a feature called auto-tune which will enable power saving settings on your device. These power saving settings should be almost the same as those enabled by tlp, although Powertop enables USB auto-suspend by default. This can make your input devices such as the keyboard unresponsive for some time when it has been suspended.  


== Hardware support ==
To enable Powertop: <code>powerManagement.powertop.enable = true;</code>.  
=== Hybrid graphics ===
Many laptops have both a dedicated and a discrete GPU. To use your laptop effictively you have to manage both GPU's. For guidance on how to configure the GPU's please refer to the dedicated wiki-pages for your configuration. If you want to have the option to run your laptop with and without the discrete GPU to save power you can either disable it in the bios (if possible) or you can use NIx's feature to define specilisations. Using specilisations will give you two boot entries on each rebuild of your system.


Example of a nvidia specilisation:
This also enables the auto-tune feature of Powertop.
<syntaxHighlight lang=nix>
 
specialisation = {  
= Hardware support =
  nvidia.configuration = {  
== Hybrid graphics ==
    # Nvidia Configuration  
Many laptops have both a dedicated and a discrete GPU. To use your laptop effectively you have to manage both GPUs. For guidance on how to configure the GPUs, refer to the dedicated wiki-pages for your configuration. If you want to have the option to run your laptop with and without the discrete GPU to save power, you can either disable it in the bios (if possible) or you can use Nix's feature to define specialisations to give you two boot entries on each rebuild of your system.
    services.xserver.videoDrivers = [ "nvidia" ];  
 
    hardware.opengl.enable = true;  
Example of a Nvidia specialisation:
 
<syntaxhighlight lang="nix">
    # Optionally, you may need to select the appropriate driver version for your specific GPU.  
specialisation = {
    hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.stable;  
  nvidia.configuration = {
 
    # Nvidia Configuration
    # nvidia-drm.modeset=1 is required for some wayland compositors, e.g. sway  
    services.xserver.videoDrivers = [ "nvidia" ];
    hardware.nvidia.modesetting.enable = true;  
    hardware.graphics.enable = true;
 
 
    hardware.nvidia.prime = {  
    # Optionally, you may need to select the appropriate driver version for your specific GPU.
      sync.enable = true;  
    hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.stable;
 
 
      # Bus ID of the NVIDIA GPU. You can find it using lspci, either under 3D or VGA  
    # nvidia-drm.modeset=1 is required for some wayland compositors, e.g. sway
      nvidiaBusId = "PCI:1:0:0";  
    hardware.nvidia.modesetting.enable = true;
 
 
      # Bus ID of the Intel GPU. You can find it using lspci, either under 3D or VGA  
    hardware.nvidia.prime = {
      intelBusId = "PCI:0:2:0";  
      sync.enable = true;
    };
 
      # Bus ID of the NVIDIA GPU. You can find it using lspci, either under 3D or VGA
      nvidiaBusId = "PCI:1:0:0";
 
      # Bus ID of the Intel GPU. You can find it using lspci, either under 3D or VGA
      intelBusId = "PCI:0:2:0";
    };
   };
   };
};
};
</syntaxHighlight>
</syntaxhighlight>


== Troubleshooting ==
= Troubleshooting =


==== My laptops runs hot when on power, but not on battery ====
=== Laptop runs hot when on power, but not on battery ===
If you use tlp and experience this issue a simple solution can be to tell tlp to always run in battery mode.
If you use tlp and experience this issue a solution can be to tell tlp to always run in battery mode.


<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
services.tlp = {
services.tlp = {
    enable = true;
  enable = true;
    settings = {
  settings = {
      TLP_DEFAULT_MODE = "BAT";
    TLP_DEFAULT_MODE = "BAT";
      TLP_PERSISTENT_DEFAULT = 1;
    TLP_PERSISTENT_DEFAULT = 1;
    };
  };
};  
};
</syntaxHighlight>
</syntaxHighlight>
= See also =
* [[Power Management]]
[[Category:Hardware]]
[[Category:Cookbook]]

Latest revision as of 13:29, 23 March 2025

Closing the lid

services.logind.lidSwitch = "poweroff";
services.logind.lidSwitchExternalPower = "lock";
services.logind.lidSwitchDocked = "ignore";

# one of "ignore", "poweroff", "reboot", "halt", "kexec", "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate", "lock"

These three options can be used to configure how a laptop should behave when the lid is closed. In this example, it normally shuts down. If power is connected, only the screen is locked. If another screen is connected instead, nothing happens.

Power management

NixOS has several tools to help you manage the power on your system and it also has a stock feature for power management. To enable the stock NixOS power management tool which allows for managing hibernate and suspend states you can write powerManagement.enable = true;. This tool is compatible with the other tools mentioned, but the other tools may overwrite this setting.

CPU performance scaling

thermald

Thermald proactively prevents overheating on Intel CPUs and works well with other tools. Enabled by: services.thermald.enable = true;

TLP

A common tool used to save power on laptops is TLP, which has sensible defaults for most laptops. To enable TLP you simply just write services.tlp.enable = true; in your configuration.nix. However, if you need a specific configuration, you can do as shown in the example below.

services.tlp = {
  enable = true;
  settings = {
    CPU_SCALING_GOVERNOR_ON_AC = "performance";
    CPU_SCALING_GOVERNOR_ON_BAT = "powersave";

    CPU_ENERGY_PERF_POLICY_ON_BAT = "power";
    CPU_ENERGY_PERF_POLICY_ON_AC = "performance";

    CPU_MIN_PERF_ON_AC = 0;
    CPU_MAX_PERF_ON_AC = 100;
    CPU_MIN_PERF_ON_BAT = 0;
    CPU_MAX_PERF_ON_BAT = 20;

    # Optional helps save long term battery health
    START_CHARGE_THRESH_BAT0 = 40; # 40 and bellow it starts to charge
    STOP_CHARGE_THRESH_BAT0 = 80;  # 80 and above it stops charging
  };
};

This example enables TLP and sets the minimum and maximum frequencies for the CPU based on whether it is plugged into power or not. It also changes the CPU scaling governor based on this.

auto-cpufreq

Another tool used for power management is auto-cpufreq which aims to replace TLP. When using auto-cpufreq it is therefore recommended to disable TLP as these tools are conflicting with each other. However, NixOS does allow for using both at the same time, and you can therefore run them in tandem at your own risk. To enable the service, add services.auto-cpufreq.enable = true; to your configuration.nix.

Example of how to configure auto-cpufreq:

services.auto-cpufreq.enable = true;
services.auto-cpufreq.settings = {
  battery = {
    governor = "powersave";
    turbo = "never";
  };
  charger = {
    governor = "performance";
    turbo = "auto";
  };
};

Alternatively, if you have Flakes enabled you can also use the flake directly provided by the auto-cpufreq authors to get a more up-to-date version. They offer a detailed explanation how to add it to your system on their GitHub page.

To summarize:
1) add the flake as an input to your flake.nix file and enable the module:

# flake.nix
{
  inputs =
    {
      # ---Snip---
      auto-cpufreq = {
        url = "github:AdnanHodzic/auto-cpufreq";
        inputs.nixpkgs.follows = "nixpkgs";
      };
      # ---Snip---
    };

  outputs = inputs@{ nixpkgs, auto-cpufreq, ... }: {
    nixosConfigurations.HOSTNAME = nixpkgs.lib.nixosSystem {
      specialArgs = {
        inherit inputs;
      };
      modules = [
        ./configuration.nix
        auto-cpufreq.nixosModules.default
      ];
    };
  };
}

2) Then enable it in your configuration.nix file:

# configuration.nix
{ inputs, pkgs, ... }: {
  # ---Snip---

  programs.auto-cpufreq.enable = true;

  # optionally, you can configure your auto-cpufreq settings, if you have any
  programs.auto-cpufreq.settings = {
    charger = {
      governor = "performance";
      turbo = "auto";
    };
    battery = {
      governor = "powersave";
      turbo = "auto";
    };
  };
  # ---Snip---
}

Since v2.0 auto-cpufreq also includes a GUI that lets you temporarily override the CPU frequency governor setting.

Powertop

Powertop is a power analysis tool, but it also has a feature called auto-tune which will enable power saving settings on your device. These power saving settings should be almost the same as those enabled by tlp, although Powertop enables USB auto-suspend by default. This can make your input devices such as the keyboard unresponsive for some time when it has been suspended.

To enable Powertop: powerManagement.powertop.enable = true;.

This also enables the auto-tune feature of Powertop.

Hardware support

Hybrid graphics

Many laptops have both a dedicated and a discrete GPU. To use your laptop effectively you have to manage both GPUs. For guidance on how to configure the GPUs, refer to the dedicated wiki-pages for your configuration. If you want to have the option to run your laptop with and without the discrete GPU to save power, you can either disable it in the bios (if possible) or you can use Nix's feature to define specialisations to give you two boot entries on each rebuild of your system.

Example of a Nvidia specialisation:

specialisation = {
  nvidia.configuration = {
    # Nvidia Configuration
    services.xserver.videoDrivers = [ "nvidia" ];
    hardware.graphics.enable = true;

    # Optionally, you may need to select the appropriate driver version for your specific GPU.
    hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.stable;

    # nvidia-drm.modeset=1 is required for some wayland compositors, e.g. sway
    hardware.nvidia.modesetting.enable = true;

    hardware.nvidia.prime = {
      sync.enable = true;

      # Bus ID of the NVIDIA GPU. You can find it using lspci, either under 3D or VGA
      nvidiaBusId = "PCI:1:0:0";

      # Bus ID of the Intel GPU. You can find it using lspci, either under 3D or VGA
      intelBusId = "PCI:0:2:0";
    };
  };
};

Troubleshooting

Laptop runs hot when on power, but not on battery

If you use tlp and experience this issue a solution can be to tell tlp to always run in battery mode.

services.tlp = {
  enable = true;
  settings = {
    TLP_DEFAULT_MODE = "BAT";
    TLP_PERSISTENT_DEFAULT = 1;
  };
};

See also