Files
dotfiles/home/battery/default.nix
T
2026-07-10 20:19:04 +01:00

55 lines
1.1 KiB
Nix

{ config, pkgs, ... }:
let
battery = pkgs.writeShellScript "battery" ''
BAT="/sys/class/power_supply/BAT1"
LEVEL=$(cat "$BAT/capacity")
STATUS=$(cat "$BAT/status")
if [ "$STATUS" = "Discharging" ] && [ "$LEVEL" -lt 3 ]; then
${pkgs.systemd}/bin/systemctl hibernate
exit 0
fi
if [ "$STATUS" = "Discharging" ] && [ "$LEVEL" -lt 20 ]; then
${pkgs.dunst}/bin/dunstify \
-r 9991 \
-t 0 \
-u critical \
"Battery Low" \
"Battery is at ''${LEVEL}%" 2>/dev/null || true
else
${pkgs.dunst}/bin/dunstify \
-C 9991 2>/dev/null || true
fi
'';
in
{
systemd.user.services.battery = {
Unit = {
Description = "Battery management";
};
Service = {
Type = "oneshot";
ExecStart = battery;
};
};
systemd.user.timers.battery = {
Unit = {
Description = "Check battery level every 15 seconds";
};
Timer = {
OnBootSec = "15s";
OnUnitInactiveSec = "15s";
AccuracySec = "1s";
};
Install = {
WantedBy = [ "timers.target" ];
};
};
}