From f4b0269b083f7dfec6787ac47eef7a3fad167bbf Mon Sep 17 00:00:00 2001 From: Alexander Heldt Date: Sun, 15 Nov 2026 14:46:38 +0100 Subject: [PATCH] pinwheel: Hibernate instead of suspend when the battery is low Adds a swap file to hold the hibernation image. The low battery timer and UPower's critical action now hibernate, and SUPER+SHIFT+x uses `suspend-then-hibernate` so a suspended laptop hibernates before the battery runs out. --- hosts/pinwheel/modules/hyprlock/default.nix | 2 +- hosts/pinwheel/modules/power/default.nix | 22 ++++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/hosts/pinwheel/modules/hyprlock/default.nix b/hosts/pinwheel/modules/hyprlock/default.nix index 76ff1c1..9cd743b 100644 --- a/hosts/pinwheel/modules/hyprlock/default.nix +++ b/hosts/pinwheel/modules/hyprlock/default.nix @@ -93,7 +93,7 @@ in _args = [ "SUPER + SHIFT + x" (lib.generators.mkLuaInline - ''hl.dsp.exec_cmd("${pause-music}; ${pkgs.hyprlock}/bin/hyprlock & sleep 0.5; systemctl suspend")'' + ''hl.dsp.exec_cmd("${pause-music}; ${pkgs.hyprlock}/bin/hyprlock & sleep 0.5; systemctl suspend-then-hibernate")'' ) ]; } diff --git a/hosts/pinwheel/modules/power/default.nix b/hosts/pinwheel/modules/power/default.nix index c61b28a..6081bac 100644 --- a/hosts/pinwheel/modules/power/default.nix +++ b/hosts/pinwheel/modules/power/default.nix @@ -27,17 +27,25 @@ in description = "Notify low battery at this level"; }; - suspendCapacity = lib.mkOption { + hibernateCapacity = lib.mkOption { default = 10; - description = "Warn at this level and then suspend if power is not plugged in"; + description = "Warn at this level and then hibernate if power is not plugged in"; }; }; }; config = { + swapDevices = lib.mkIf enabled [ + { + device = "/var/lib/swapfile"; + size = 64 * 1024; # MiB + } + ]; + services = lib.mkIf enabled { upower = { enable = true; + criticalPowerAction = "Hibernate"; }; logind.settings.Login = { @@ -62,7 +70,7 @@ in timers = { "monitor-low-battery" = { unitConfig = { - Description = "monitors battery level and suspends computer if battery is low"; + Description = "monitors battery level and hibernates computer if battery is low"; }; timerConfig = { @@ -78,7 +86,7 @@ in services = { "monitor-low-battery" = { unitConfig = { - Description = "monitors battery level and suspends computer if battery is low"; + Description = "monitors battery level and hibernates computer if battery is low"; }; serviceConfig = { @@ -105,13 +113,13 @@ in notify-send --expire-time=0 --urgency=critical "Battery Low" fi - if [[ $BATTERY_CAPACITY -le ${builtins.toString lowbat.suspendCapacity} && $BATTERY_STATUS = "Discharging" ]]; then - notify-send --expire-time=0 --urgency=critical "Battery Critically Low" "Suspending in 60 seconds if power is not plugged in" + if [[ $BATTERY_CAPACITY -le ${builtins.toString lowbat.hibernateCapacity} && $BATTERY_STATUS = "Discharging" ]]; then + notify-send --expire-time=0 --urgency=critical "Battery Critically Low" "Hibernating in 60 seconds if power is not plugged in" sleep 60s BATTERY_STATUS=$(cat /sys/class/power_supply/${lowbat.battery}/status) if [[ $BATTERY_STATUS = "Discharging" ]]; then - ${pause-music}; ${pkgs.hyprlock}/bin/hyprlock & sleep 0.5; systemctl suspend + ${pause-music}; ${pkgs.hyprlock}/bin/hyprlock & sleep 0.5; systemctl hibernate fi fi '';