🚀 hibernate when battery is low

This commit is contained in:
Moritz Böhme 2022-03-08 22:43:38 +01:00
parent 0028ad3ecd
commit 616acb44e3
No known key found for this signature in database
GPG key ID: 213820E2795F5CF5

View file

@ -54,6 +54,26 @@
services.tlp.enable = true; services.tlp.enable = true;
powerManagement.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 # This value determines the NixOS release from which the default
# settings for stateful data, like file locations and database versions # settings for stateful data, like file locations and database versions
# on your system were taken. Its perfectly fine and recommended to leave # on your system were taken. Its perfectly fine and recommended to leave
@ -63,4 +83,3 @@
system.stateVersion = "21.05"; # Did you read the comment? system.stateVersion = "21.05"; # Did you read the comment?
} }