From 78226909f94b2cdb73e7ab9935b0aef18b6444a4 Mon Sep 17 00:00:00 2001 From: fleaz Date: Sun, 7 Jan 2024 16:45:47 +0100 Subject: [PATCH] modules/sus-then-hib: Switch to systemds suspend-then-hibernate --- modules/sus-then-hib.nix | 43 ++++++++-------------------------------- 1 file changed, 8 insertions(+), 35 deletions(-) diff --git a/modules/sus-then-hib.nix b/modules/sus-then-hib.nix index afabea5..eeeacc6 100644 --- a/modules/sus-then-hib.nix +++ b/modules/sus-then-hib.nix @@ -1,38 +1,11 @@ -{ 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 - 5)) ] ; then - systemctl hibernate - else - ${pkgs.utillinux}/bin/rtcwake -m no -s 1 - fi - ''; - serviceConfig.Type = "simple"; +{ config, pkgs, ... }: +{ + systemd.sleep.extraConfig = "HibernateDelaySec=1h"; + + services.logind = { + lidSwitch = "suspend-then-hibernate"; + suspendKey = "suspend-then-hibernate"; + powerKey = "suspend-then-hibernate"; }; }