I found a post about suspending and then going into hibernate that included a really clever script. Turns out that with NixOS this is even esaier to coordinate as you have systemd so can have a before
and after
service. I just include this in my /etc/nixos/configuration.nix
file and nixos-rebuild
; then a systemctl suspend
or a close of the lid will cause the hibernate timer to be set.
Last active
February 22, 2025 21:05
-
-
Save mattdenner/befcf099f5cfcc06ea04dcdd4969a221 to your computer and use it in GitHub Desktop.
Suspend and then hibernate after 60 minutes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ config, pkgs, ... }: let | |
hibernateEnvironment = { | |
HIBERNATE_SECONDS = "3600"; | |
HIBERNATE_LOCK = "/var/run/autohibernate.lock"; | |
}; | |
in { | |
systemd.services."awake-after-suspend-for-a-time" = { | |
description = "Sets up the suspend so that it'll wake for hibernation"; | |
wantedBy = [ "suspend.target" ]; | |
before = [ "systemd-suspend.service" ]; | |
environment = hibernateEnvironment; | |
script = '' | |
curtime=$(date +%s) | |
echo "$curtime $1" >> /tmp/autohibernate.log | |
echo "$curtime" > $HIBERNATE_LOCK | |
${pkgs.utillinux}/bin/rtcwake -m no -s $HIBERNATE_SECONDS | |
''; | |
serviceConfig.Type = "simple"; | |
}; | |
systemd.services."hibernate-after-recovery" = { | |
description = "Hibernates after a suspend recovery due to timeout"; | |
wantedBy = [ "suspend.target" ]; | |
after = [ "systemd-suspend.service" ]; | |
environment = hibernateEnvironment; | |
script = '' | |
curtime=$(date +%s) | |
sustime=$(cat $HIBERNATE_LOCK) | |
rm $HIBERNATE_LOCK | |
if [ $(($curtime - $sustime)) -ge $HIBERNATE_SECONDS ] ; then | |
systemctl hibernate | |
else | |
${pkgs.utillinux}/bin/rtcwake -m no -s 1 | |
fi | |
''; | |
serviceConfig.Type = "simple"; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Read my 2 replies above. Suspend-then-hibernate works.