60 lines
1.2 KiB
Nix
60 lines
1.2 KiB
Nix
{ config, pkgs, ... }:
|
|
|
|
let
|
|
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 \
|
|
-t 0 \
|
|
-u critical \
|
|
"Battery Low" \
|
|
"Battery is at ''${LEVEL}%"
|
|
else
|
|
${pkgs.dunst}/bin/dunstify \
|
|
-C 9991 2>/dev/null || true
|
|
fi
|
|
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";
|
|
OnUnitActiveSec = "15s";
|
|
};
|
|
|
|
Install = {
|
|
WantedBy = [ "timers.target" ];
|
|
};
|
|
};
|
|
}
|