Power Management: Difference between revisions

Onny (talk | contribs)
Add notes on hibernation
m remove some whitespace and fix typo
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{expansion}}
This article covers configurations related to power management in terms of energy saving modes of various devices and components.


== Suspend ==
== Configuration ==
 
=== Hard drives ===
Following snippet configures [[Udev]] rules which automatically run the program <code>hdparm</code> to enable power saving modes for hard disks, especially rotational drives mapped to <code>/dev/sd*</code>.<syntaxhighlight lang="nix">
services.udev.extraRules =
  let
    mkRule = as: lib.concatStringsSep ", " as;
    mkRules = rs: lib.concatStringsSep "\n" rs;
  in mkRules ([( mkRule [
    ''ACTION=="add|change"''
    ''SUBSYSTEM=="block"''
    ''KERNEL=="sd[a-z]"''
    ''ATTR{queue/rotational}=="1"''
    ''RUN+="${pkgs.hdparm}/bin/hdparm -B 90 -S 41 /dev/%k"''
  ])]);
</syntaxhighlight>The <code>hdparm</code> parameters <code>-B</code> and <code>-S</code> define power saving modes and in case of <code>-S</code> the standby (spindown) timeout. The number 41 means therefore: Turn off the motor after 205 = 41*5 seconds.


=== Suspend hooks ===
=== Suspend hooks ===
NixOS provides the {{nixos:option|powerManagement.resumeCommands}} option which defines commands that are added to a global script that will be executed after resuming.
NixOS provides the {{nixos:option|powerManagement.resumeCommands}} option which defines commands that are added to a global script that will be executed after resuming.


Line 27: Line 41:
</syntaxhighlight>
</syntaxhighlight>


== Hibernation ==
=== Hibernation ===
Hibernation requires a configured swap device. See [https://nixos.org/manual/nixos/stable/#ch-installation installation instructions] on how to create a swap partition. An example configuration could look like this:<syntaxhighlight lang="nix">
 
Hibernation requires a configured swap device. See [https://nixos.org/manual/nixos/stable/#ch-installation installation instructions] on how to create a swap partition.
 
Please note that <code>resumeDevice</code> must match the output of  <code>swapon -s</code> especially if you're dealing with mapped volumes (LUKS, logical volumes, logical volumes under LUKS, etc.). If you're using a swapfile, you must also [https://search.nixos.org/options?channel=unstable&show=boot.resumeDevice&from=0&size=50&sort=relevance&type=packages&query=resume+offset specify the offset to it.]
 
Therefore, an example configuration could look like this:<syntaxhighlight lang="nix">
/*
  I'm hibernating into a logical volume that's also under LUKS. Pretty cool, right?
*/
 
swapDevices = [
swapDevices = [
   {
   {
     device = "/dev/hda7";
     device = "/dev/VG/SWAP";
   }
   }
];
];
boot.resumeDevice = "/dev/hda7";
</syntaxhighlight>Please note that encrypted swap devices or swap files are not yet supported for hibernation.


Test and use hibernation with following command:<syntaxhighlight lang="nix">
boot.resumeDevice = "/dev/dm-7";
</syntaxhighlight>Derived from a system with the following output from <code>swapon -s</code> :<syntaxhighlight>
Filename                                Type            Size          Used            Priority
/dev/dm-7                              partition      67108860      00
/dev/zram0                              partition      32881148      032767
 
</syntaxhighlight>
 
Test and use hibernation with the following command:<syntaxhighlight lang="nix">
systemctl hibernate
systemctl hibernate
</syntaxhighlight>
</syntaxhighlight>
Line 47: Line 76:
systemd.sleep.extraConfig = ''
systemd.sleep.extraConfig = ''
   HibernateDelaySec=1h
   HibernateDelaySec=1h
'';
</syntaxhighlight>
Or, to disable suspend entirely, consider a configuration like this:
<syntaxhighlight lang="nix">
systemd.sleep.extraConfig = ''
  AllowSuspend=no
  AllowHibernation=no
  AllowHybridSleep=no
  AllowSuspendThenHibernate=no
'';
'';
</syntaxhighlight>
</syntaxhighlight>
Line 123: Line 162:


* {{manual:nixos|sec=#sec-rebooting|chapter=Chapter 23. Rebooting and Shutting Down}}
* {{manual:nixos|sec=#sec-rebooting|chapter=Chapter 23. Rebooting and Shutting Down}}
[[Category:Configuration]]