Power Management

From NixOS Wiki
Revision as of 19:23, 17 May 2024 by Onny (talk | contribs) (Add notes on hibernation)

Suspend

Suspend hooks

NixOS provides the powerManagement.resumeCommands option which defines commands that are added to a global script that will be executed after resuming.

powerManagement.resumeCommands = ''
  echo "This should show up in the journal after resuming."
'';

It is also possible to use the post-resume target directly to make a service.

  systemd.services.your-service-name = { 
    description = "Service description here";
    wantedBy = [ "post-resume.target" ];
    after = [ "post-resume.target" ];
    script = ''
    echo "This should show up in the journal after resuming."
    '';
    serviceConfig.Type = "oneshot";
  };

Hibernation

Hibernation requires a configured swap device. See installation instructions on how to create a swap partition. An example configuration could look like this:

swapDevices = [
  {
    device = "/dev/hda7";
  }
];
boot.resumeDevice = "/dev/hda7";

Please note that encrypted swap devices or swap files are not yet supported for hibernation. Test and use hibernation with following command:

systemctl hibernate

Tips and tricks

Go into hibernate after specific suspend time

Using following configuration, your system will go from suspend into hibernate after 1 hour:

systemd.sleep.extraConfig = ''
  HibernateDelaySec=1h
'';

Troubleshooting

System immediately wakes up from suspend

Particularly in some Gigabyte motherboards with NVMe drives, the system may immediately wake up from being suspended. This can be worked around by disabling the wakeup triggers for the offending components:

Solution 1: Disabling wakeup triggers for all PCIe devices

If you don't need your system to wakeup via PCIe components you can simply disable it for all without needing to determine which component is causing problems.

services.udev.extraRules = ''
  ACTION=="add", SUBSYSTEM=="pci", DRIVER=="pcieport", ATTR{power/wakeup}="disabled"
'';
Solution 2: Disable a common NVMe interface

Specifically on Gigabyte motherboards you can try targetting only the NVMe ports.

services.udev.extraRules = ''
  ACTION=="add" SUBSYSTEM=="pci" ATTR{vendor}=="0x1022" ATTR{device}=="0x1483" ATTR{power/wakeup}="disabled"
'';
Solution 3: Disable a single device's wakeup triggers

If you wish to be more granular in what components should no longer be able to wakeup your system, you can find out which component is causing the wakeup events.

First, list all components and their current wakeup status:

$ cat /proc/acpi/wakeup
Device	S-state	  Status   Sysfs node
GP12	  S4	*enabled   pci:0000:00:07.1
GP13	  S4	*disabled  pci:0000:00:08.1
XHC0	  S4	*enabled   pci:0000:0a:00.3
GP30	  S4	*disabled
....
PT27	  S4	*disabled
PT28	  S4	*disabled
PT29	  S4	*disabled  pci:0000:03:09.0

You can temporarily toggle a device by writing its "Device" name back into /proc/acpi/wakeup

echo GPP0 | sudo tee /proc/acpi/wakeup

After finding out which component is causing unwanted wakeups you can use the sysfs id to find out the "vendor" and "device" fields:

$ cat /sys/class/pci_bus/0000:04/device/0000:04:00.0/vendor
0x1987
$ cat /sys/class/pci_bus/0000:04/device/0000:04:00.0/device
0x5013

And finally use those values in a udev rule:

services.udev.extraRules = ''
  ACTION=="add" SUBSYSTEM=="pci" ATTR{vendor}=="0x1987" ATTR{device}=="0x5013" ATTR{power/wakeup}="disabled"
'';

See also

External resources