Hibernation

From NixOS Wiki
Jump to: navigation, search

Hibernation

NixOS is the first Linux that successfully provided laptop hibernation for me. This guide can help you set it up.

I pursued hibernation because I have an off-brand laptop that doesn't sleep well. The battery continues to drain rapidly when sleeping. I previously tried Ubuntu and Tuxedo OS during the 2024 time frame.

If your laptop sleeps well and preserves its battery, disregard hibernation.

Presumptions

Please extrapolate for other NixOS versions, user environments, or hardware.

  • NixOS 24.11
    • Classic channels configuration
    • ext4 OS partition
    • No swap partition
    • Essentially a clean, default install. I did not enable heritage options such as grub or initd.
  • KDE Plasma 6 / Wayland
  • 2024 laptop computer: BSLAY 16-inch AMD R7-8845HS (AliExpress)

HOWTO

1) Setup a swap file

Add something like this to configuration.nix. The value should be the size of your RAM.
  swapDevices = [
    {
      device = "/var/lib/swapfile";
      size = 32 * 1024; # 32GB in MB
    }
  ];

2) Switch

sudo nixos-rebuild switch

3) In a command shell, determine your root partition UUID

lsblk -f
Example:
❯ lsblk -f
NAME        FSTYPE FSVER LABEL UUID                                 FSAVAIL FSUSE% MOUNTPOINTS
nvme0n1                                                                            
├─nvme0n1p1 vfat   FAT32       7B0D-CE40                             349.6M    32% /boot
└─nvme0n1p2 ext4   1.0         a6a36731-4e78-4c8b-a8dd-12ed48fe4fce      3T    11% /nix/store
                               ------------------------------------

4) In a command shell, determine your swap file offset

sudo filefrag -v /var/lib/swapfile | head
Example:
❯ sudo filefrag -v /var/lib/swapfile | head                                              
Filesystem type is: ef53
File size of /var/lib/swapfile is 68719476736 (16777216 blocks of 4096 bytes)
 ext:     logical_offset:        physical_offset: length:   expected: flags:
   0:        0..    2047:  576098304.. 576100351:   2048:            
                           ---------
   1:     2048..    4095:   74756096..  74758143:   2048:  576100352:
   2:     4096..    6143:   71784448..  71786495:   2048:   74758144:
   3:     6144..   12287:   71788544..  71794687:   6144:   71786496:
   4:    12288..   71679:  100237312.. 100296703:  59392:   71794688:
   5:    71680..  434175:  100300800.. 100663295: 362496:  100296704:
   6:   434176..  466943:  100204544.. 100237311:  32768:  100663296:

5) Enable hibernation

Add the following to configuration.nix and replace the <offset> and <uuid-of-root-partition> values with your own:
boot.kernelParams = ["resume_offset=<offset>"];

boot.resumeDevice = "/dev/disk/by-uuid/<uuid-of-root-partition>";

powerManagement.enable = true;

swapDevices = [
  {
    device = "/var/lib/swapfile";
    size = 32 * 1024; # 32GB in MB
  }
];

6) Apply your NixOS configuration

Tip: Use 'boot' instead of 'switch' when you change any boot or kernel options.
sudo nixos-rebuild boot
sudo reboot

7) Login to KDE and set your hibernation preferences

System Settings > Power Management

8) Test hibernation

Application Launcher > Hibernate