Add hinernation on low battery feature

This commit is contained in:
2026-07-09 18:46:21 +01:00
parent 27759c824a
commit 35e165b801
+19 -10
View File
@@ -1,11 +1,21 @@
{ config, pkgs, ... }:
let
batteryNotify = pkgs.writeShellScript "battery-notify" ''
battery = pkgs.writeShellScript "battery" ''
BAT="/sys/class/power_supply/BAT1"
LEVEL=$(cat "$BAT/capacity")
STATUS=$(cat "$BAT/status")
has_notifications() {
${pkgs.dbus}/bin/busctl --user status org.freedesktop.Notifications >/dev/null 2>&1
}
if [ "$STATUS" = "Discharging" ] && [ "$LEVEL" -lt 3 ]; then
${pkgs.systemd}/bin/systemctl hibernate
exit 0
fi
if has_notifications; then
if [ "$STATUS" = "Discharging" ] && [ "$LEVEL" -lt 20 ]; then
${pkgs.dunst}/bin/dunstify \
-r 9991 \
@@ -17,30 +27,29 @@ let
${pkgs.dunst}/bin/dunstify \
-C 9991 2>/dev/null || true
fi
fi
'';
in
{
systemd.user.services.battery-notify = {
systemd.user.services.battery = {
Unit = {
Description = "Low battery notification";
After = [ "dunst.service" ];
Requires = [ "dunst.service" ];
Description = "Battery management";
};
Service = {
Type = "oneshot";
ExecStart = batteryNotify;
ExecStart = battery;
};
};
systemd.user.timers.battery-notify = {
systemd.user.timers.battery = {
Unit = {
Description = "Check battery level every 20 seconds";
Description = "Check battery level every 15 seconds";
};
Timer = {
OnBootSec = "20s";
OnUnitActiveSec = "20s";
OnBootSec = "15s";
OnUnitActiveSec = "15s";
};
Install = {