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"; | |
}; | |
} |
Has anyone figured out how to get this to work without that awesome before an after script? Its so weird that setting a simple 'suspend-then-hibernate' still won't work on NixOS.
Has anyone figured out how to get this to work without that awesome before an after script? Its so weird that setting a simple 'suspend-then-hibernate' still won't work on NixOS.
Read my 2 replies above. Suspend-then-hibernate works.
@jyap808 thanks a lot for the details :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To fill in all the background to what I found out, it was specifically fixed in Systemd v253 (released February 15, 2023 but depends on when your distro updated to it).
Release page: https://github.com/systemd/systemd/releases/tag/v253
There are a couple more lid options in NixOS:
https://search.nixos.org/options?channel=24.05&from=0&size=50&sort=relevance&type=packages&query=services.logind.lidSwitch
services.logind.lidSwitchExternalPower
defaults to what is set forservices.logind.lidSwitch
so I’m fine with this setting.services.logind.lidSwitchDocked
defaults toignore
which I like as well since I’m often docked to an external monitor so this means I can put the lid down and it doesn’t Suspend.