From ae3d890e87a123b8efdb6e6cee38581e8aa212fe Mon Sep 17 00:00:00 2001 From: Tomas Rivera <137088692+Totorile1@users.noreply.github.com> Date: Thu, 26 Feb 2026 18:11:46 +0100 Subject: [PATCH] removed batsignal. Used a bash script for battery notifications --- hosts/laptop/home.nix | 2 +- modules/scripts/batterynotify.nix | 72 +++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 modules/scripts/batterynotify.nix diff --git a/hosts/laptop/home.nix b/hosts/laptop/home.nix index b06a6c1..bfcbbbd 100755 --- a/hosts/laptop/home.nix +++ b/hosts/laptop/home.nix @@ -37,7 +37,7 @@ home.stateVersion = "25.11"; # Please read the comment before changing. home.packages = [ #scritps (pkgs.callPackage ../../modules/scripts/dontkillsteam.nix {}) - +(pkgs.callPackage ../../modules/scripts/batterynotify.nix {}) # # Adds the 'hello' command to your environment. It prints a friendly # # "Hello, world!" when run. # pkgs.hello diff --git a/modules/scripts/batterynotify.nix b/modules/scripts/batterynotify.nix new file mode 100644 index 0000000..ef14f48 --- /dev/null +++ b/modules/scripts/batterynotify.nix @@ -0,0 +1,72 @@ +{ writeShellApplication +, libnotify +, systemd +}: + +writeShellApplication { + name = "custom-batterynotify"; + + runtimeInputs = [ + libnotify # provides notify-send + systemd # provides systemctl + ]; + + text = '' + set -euo pipefail + + LOW="''${BATTERY_LOW_THRESHOLD:-20}" + CRITICAL="''${BATTERY_CRITICAL_THRESHOLD:-5}" + FULL="''${BATTERY_FULL_THRESHOLD:-95}" + INTERVAL="''${BATTERY_INTERVAL:-30}" + ACTION="''${BATTERY_CRITICAL_ACTION:-systemctl suspend}" + + notify() { + notify-send -a "Battery" -u "$1" "$2" "$3" + } + + get_info() { + total=0 + count=0 + + for bat in /sys/class/power_supply/BAT*; do + [ -f "$bat/capacity" ] || continue + capacity=$(<"$bat/capacity") + status=$(<"$bat/status") + total=$((total + capacity)) + count=$((count + 1)) + done + + [ "$count" -gt 0 ] || exit 0 + percentage=$((total / count)) + } + + last_state="" + + while true; do + get_info + state="$status-$percentage" + + if [[ "$status" == "Discharging" && "$percentage" -le "$CRITICAL" ]]; then + notify critical "Battery Critically Low" "$percentage% — suspending." + $ACTION + + elif [[ "$status" == "Discharging" && "$percentage" -le "$LOW" ]]; then + if [[ "$last_state" != "low" ]]; then + notify normal "Battery Low" "$percentage% remaining." + last_state="low" + fi + + elif [[ "$status" != "Discharging" && "$percentage" -ge "$FULL" ]]; then + if [[ "$last_state" != "full" ]]; then + notify normal "Battery Full" "$percentage% — unplug charger." + last_state="full" + fi + + else + last_state="" + fi + + sleep "$INTERVAL" + done + ''; +}