From 616acb44e362a6ab055fed7d5a60b8296e97f788 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Tue, 8 Mar 2022 22:43:38 +0100 Subject: [PATCH] :rocket: hibernate when battery is low --- hosts/nixos-laptop/default.nix | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/hosts/nixos-laptop/default.nix b/hosts/nixos-laptop/default.nix index a97f5f8..93be824 100644 --- a/hosts/nixos-laptop/default.nix +++ b/hosts/nixos-laptop/default.nix @@ -54,6 +54,26 @@ services.tlp.enable = true; powerManagement.enable = true; + # Hibernare on low battery + systemd.timers.hibernate-on-low-battery = { + wantedBy = [ "multi-user.target" ]; + timerConfig = { + OnUnitActiveSec = "120"; + OnBootSec = "120"; + }; + }; + systemd.services.hibernate-on-low-battery = let + batteryLevelSufficient = let batteryPath = "/sys/class/power_supply/BATT"; + in pkgs.writeShellScriptBin "battery-level-sufficient" '' + test "$(cat ${batteryPath}/status)" != Discharging \ + || test "$(cat ${batteryPath}/capacity)" -ge 5 + ''; + in { + serviceConfig.Type = "oneshot"; + onFailure = [ "hibernate.target" ]; + script = "${batteryLevelSufficient}/bin/battery-level-sufficient"; + }; + # This value determines the NixOS release from which the default # settings for stateful data, like file locations and database versions # on your system were taken. It‘s perfectly fine and recommended to leave @@ -63,4 +83,3 @@ system.stateVersion = "21.05"; # Did you read the comment? } -