From 028a3702996273b4983c94fb24985d43c53ab3f6 Mon Sep 17 00:00:00 2001 From: Satria Date: Fri, 20 Mar 2026 06:49:51 +0700 Subject: [PATCH] battery notifier --- modules/hardware/misc/battery-power.nix | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/modules/hardware/misc/battery-power.nix b/modules/hardware/misc/battery-power.nix index bd8ee20..a8dbc96 100644 --- a/modules/hardware/misc/battery-power.nix +++ b/modules/hardware/misc/battery-power.nix @@ -1,4 +1,4 @@ -{ pkgs, resume-dev, ... }: { +{ pkgs, username, resume-dev, ... }: { powerManagement.powertop.enable = true; services = { @@ -10,6 +10,18 @@ echo 85 > /sys/class/power_supply/BAT*/charge_control_end_threshold || true ''}" ''; + cron = { + enable = true; + systemCronJobs = [ + "* * * * * ${username} bash -x ${pkgs.writeShellScript "low-battery-notifier" '' + BAT_PCT=`${pkgs.acpi}/bin/acpi -b | ${pkgs.gnugrep}/bin/grep -P -o '[0-9]+(?=%)'` + BAT_STA=`${pkgs.acpi}/bin/acpi -b | ${pkgs.gnugrep}/bin/grep -P -o '\w+(?=,)'` + echo "`date` battery status:$BAT_STA percentage:$BAT_PCT" + test $BAT_PCT -le 30 && test $BAT_PCT -gt 15 && test $BAT_STA = "Discharging" && DISPLAY=:0.0 ${pkgs.libnotify}/bin/notify-send -c device -u normal "Low Battery" "\$\{BAT_PCT}% remaining." + test $BAT_PCT -le 15 && test $BAT_STA = "Discharging" && DISPLAY=:0.0 ${pkgs.libnotify}/bin/notify-send -c device -u critical "Low Battery" "Shutdown at 10%." + ''} > /tmp/cron.batt.log 2>&1" + ]; + }; upower = { enable = true; percentageCritical = 15;