diff --git a/home/rachel.nix b/home/rachel.nix index 16ab1e9..cca1742 100644 --- a/home/rachel.nix +++ b/home/rachel.nix @@ -534,4 +534,51 @@ in rec { enable = true; }; }; + + systemd.user= { + timers = { + battery-check = { + Unit.Description = "Warn at low battery levels"; + Timer = { + OnBootSec = "1min"; + OnUnitActiveSec = "30s"; + Unit = "battery-check.service"; + }; + }; + }; + services = { + battery-check = { + Unit.Description = "Warn at low battery levels"; + Service = let batMon = pkgs.writeShellScript "botMon" '' + PATH="$PATH:${pkgs.lib.makeBinPath [ + pkgs.acpi + pkgs.dunst + pkgs.gnugrep + pkgs.gawk + pkgs.systemd + ]}" + acpi -b | grep "Battery 0" | awk -F'[,:%]' '{print $2, $3}' | { + read -r status capacity + + if [ "$status" = Discharging -a "$capacity" -lt 2 ]; then + dunstify -u critical -a batMon \ + -h string:x-dunst-stack-tag:batMon \ + -h int:value:"$capacity" "Battery Critical: ''${capacity}% + Hibernating" + sleep 5 + systemctl hibernate + elif [ "$status" = Discharging -a "$capacity" -lt 5 ]; then + dunstify -u critical -a batMon \ + -h string:x-dunst-stack-tag:batMon \ + -h int:value:"$capacity" "Battery Critical: ''${capacity}%" + fi + } + ''; + in { + ExecStart = "${batMon}"; + }; + }; + }; + }; + } diff --git a/home/xinitrc b/home/xinitrc index dbfc758..e5cd1c6 100644 --- a/home/xinitrc +++ b/home/xinitrc @@ -17,4 +17,5 @@ systemctl restart --user dunst systemctl restart --user picom systemctl restart --user polybar systemctl restart --user network-manager-applet +systemctl restart --user battery-check.timer exec berry