Laptop

From NixOS Wiki
Revision as of 09:27, 27 September 2023 by imported>IgorM (Added categories)

Power management

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 powerManagement.enable = true;. This tool is compatible with the other tools mentioned, but the other tools may however overwrite the settings set.

CPU performance scaling

thermald

Thermald proactively prevents overheating on intel cpus and work well with other tools. The tool can enabled 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 configraion.nix. However, if you need a specific configuration, you can do as shown in the example here 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;
      };
};

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 therefore run them in tandem at your own risk. To enable the service, just 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";
  };
};

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 powerManagement.powertop.enable = true; and it should be noted that 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 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:

specialisation = { 
   nvidia.configuration = { 
     # Nvidia Configuration 
     services.xserver.videoDrivers = [ "nvidia" ]; 
     hardware.opengl.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

My laptops 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.

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