One one of my nixos installation I’m using swapfile and noticed that hibernation never works.
I made sure that resume=/var/swap-file and resume_offset=myoffset are present in my kernel boot args.
I did calculate resume_offset using filefrag -v /var/swapfile | awk '{if($1=="0:"){print $4}}' as per information that i found on arch wiki Power management/Suspend and hibernate - ArchWiki.
I also noticed that in init script following lines:
# Clear the resume device.
if test -n "$resumeDevice"; then
mkswap "$resumeDevice" || echo 'Failed to clear saved image.'
fi
Does this mean that swap device will be always flushed?
Is the correct option, no need for resume=/var/swapfile in boot.kernelParams also. resumeDevice should be pointing at partition that swap file resides on.
I got it to work with a complex setup so I thought I would leave this here for the future generations.
My setup is Btrfs + LUKS2 + TPM unlock + swapfile. Long story short, I got to this setup by doing the recommended auto partitioning during the install, then a long time later I changed things up. I used a live USB NixOS, removed my swap partition and filled the space with the root partition in gparted (leaving a few megabytes at the end empty). I followed a guide to convert from ext4 to btrfs and another guide to upgrade to LUKS2 from the default LUKS1 and also switched to a stronger key algo there. The TPM enrolling is pretty simple too.
I only have 2 partitions, the boot and the root. During the boot sequence the encrypted root volume gets automatically unlocked using the TPM and mapped to /dev/dm-0. I found this with ll /dev/mapper/luks*
I have an 18G swapfile at /var/swapfile. It’s auto created by my NixOS config.
I used btrfs inspect-internal map-swapfile -r /var/swapfile to get the resume offset for the kernel parameter.
After building and rebooting I can finally hibernate with systemctl hibernate and my PC wakes up from keyboard or mouse input.
I still don’t know how to add a hibernate button to the shutdown menu and disable the wake from mouse/keyboard but it’s good progress for day.
I’m planning to try Suspend then hibernate - #5 by ericgundrum next