Add hinernation on low battery feature

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