From 8588887c161025b71aa9943ea16f2b066650da28 Mon Sep 17 00:00:00 2001 From: iFargle Date: Tue, 16 Jan 2024 21:23:09 +0900 Subject: [PATCH] Test more hibernation fixes --- nixos/hosts/nixos-framework/default.nix | 1 + nixos/hosts/nixos-framework/hibernate.nix | 46 +++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 nixos/hosts/nixos-framework/hibernate.nix diff --git a/nixos/hosts/nixos-framework/default.nix b/nixos/hosts/nixos-framework/default.nix index 621ba52f..0ef790b3 100644 --- a/nixos/hosts/nixos-framework/default.nix +++ b/nixos/hosts/nixos-framework/default.nix @@ -3,6 +3,7 @@ inputs.nixos-hardware.nixosModules.framework-13-7040-amd (modulesPath + "/installer/scan/not-detected.nix") ./disks.nix + ./hibernate.nix ../../common/services/tailscale-autoconnect.nix ../../common/modules/secureboot.nix ../../common/modules/udev-rules.nix diff --git a/nixos/hosts/nixos-framework/hibernate.nix b/nixos/hosts/nixos-framework/hibernate.nix new file mode 100644 index 00000000..2875a022 --- /dev/null +++ b/nixos/hosts/nixos-framework/hibernate.nix @@ -0,0 +1,46 @@ +{ config, pkgs, ... }: + +let + hibernateEnvironment = { + HIBERNATE_SECONDS = "10"; + 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 only if not on AC power"; + wantedBy = [ "suspend.target" ]; + before = [ "systemd-suspend.service" ]; + environment = hibernateEnvironment; + script = '' + if [ $(cat /sys/class/power_supply/ACAD/online) -eq 0 ]; then + curtime=$(date +%s) + echo "$curtime $1" >> /tmp/autohibernate.log + echo "$curtime" > $HIBERNATE_LOCK + ${pkgs.utillinux}/bin/rtcwake -m no -s $HIBERNATE_SECONDS + else + echo "System is on AC power, skipping wake-up scheduling for hibernation." >> /tmp/autohibernate.log + fi + ''; + 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"; + }; + +} \ No newline at end of file