From 1d40bf6c1d05ec2e6e735bd13c48c97c3c7fb706 Mon Sep 17 00:00:00 2001
From: Oliver Davies <oliver@oliverdavies.dev>
Date: Wed, 27 Nov 2024 19:00:42 +0000
Subject: [PATCH] Fix battery notifications

---
 bin/notify-battery            | 34 --------------------------
 nix/home/opdavies/default.nix |  1 +
 nix/modules/nixos/i3.nix      |  2 +-
 nix/pkgs/default.nix          |  1 +
 nix/pkgs/notify-battery.nix   | 46 +++++++++++++++++++++++++++++++++++
 5 files changed, 49 insertions(+), 35 deletions(-)
 delete mode 100755 bin/notify-battery
 create mode 100644 nix/pkgs/notify-battery.nix

diff --git a/bin/notify-battery b/bin/notify-battery
deleted file mode 100755
index e9e77b4c..00000000
--- a/bin/notify-battery
+++ /dev/null
@@ -1,34 +0,0 @@
-#!/usr/bin/env bash
-
-set -o errexit
-set -o nounset
-set -o pipefail
-
-export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus"
-export DISPLAY=:0
-
-# Battery percentage at which to notify.
-BATTERY_DISCHARGING=$(acpi -b | grep "Battery 0" | grep -c "Discharging")
-BATTERY_LEVEL=$(acpi -b | grep "Battery 0" | grep -P -o '[0-9]+(?=%)')
-WARNING_LEVEL=20
-
-# Use two files to store whether we've shown a notification or not (to prevent multiple notifications).
-EMPTY_FILE=/tmp/battery-empty
-FULL_FILE=/tmp/battery-full
-
-# Reset notifications if the computer is charging/discharging.
-if [ "$BATTERY_DISCHARGING" -eq 1 ] && [ -f $FULL_FILE ]; then
-  rm $FULL_FILE
-elif [ "$BATTERY_DISCHARGING" -eq 0 ] && [ -f $EMPTY_FILE ]; then
-  rm $EMPTY_FILE
-fi
-
-# If the battery is charging and is full (and has not shown notification yet).
-if [ "$BATTERY_LEVEL" -gt 95 ] && [ "$BATTERY_DISCHARGING" -eq 0 ] && [ ! -f $FULL_FILE ]; then
-  notify-send "Battery Charged" "Battery is fully charged." -r 9991
-  touch $FULL_FILE
-# If the battery is low and is not charging (and has not shown notification yet).
-elif [ "$BATTERY_LEVEL" -le $WARNING_LEVEL ] && [ "$BATTERY_DISCHARGING" -eq 1 ] && [ ! -f $EMPTY_FILE ]; then
-  notify-send "Low Battery" "${BATTERY_LEVEL}% of battery remaining." -u critical -r 9991 -t 0
-  touch $EMPTY_FILE
-fi
diff --git a/nix/home/opdavies/default.nix b/nix/home/opdavies/default.nix
index 311a75a0..5aa18c31 100644
--- a/nix/home/opdavies/default.nix
+++ b/nix/home/opdavies/default.nix
@@ -63,6 +63,7 @@ in
         gscan2pdf
         handbrake
         meslo-lg
+        notify-battery
         obs-studio
         okular
         pamixer
diff --git a/nix/modules/nixos/i3.nix b/nix/modules/nixos/i3.nix
index 481fd235..0a906908 100644
--- a/nix/modules/nixos/i3.nix
+++ b/nix/modules/nixos/i3.nix
@@ -17,7 +17,7 @@ in
       enable = true;
 
       systemCronJobs = [
-        "* * * * * opdavies /home/opdavies/bin/notify-battery"
+        "* * * * * opdavies ${pkgs.notify-battery}/bin/notify-battery"
       ];
     };
 
diff --git a/nix/pkgs/default.nix b/nix/pkgs/default.nix
index d8cb0376..e1aa5047 100644
--- a/nix/pkgs/default.nix
+++ b/nix/pkgs/default.nix
@@ -5,6 +5,7 @@ let
 in
 {
   build-glove80 = callPackage ./build-glove80.nix { };
+  notify-battery = callPackage ./notify-battery.nix { };
 
   vimPlugins = prev.vimPlugins // {
     conf-vim = callPackage ./vim-plugins/conf-vim.nix { };
diff --git a/nix/pkgs/notify-battery.nix b/nix/pkgs/notify-battery.nix
new file mode 100644
index 00000000..7355f4f3
--- /dev/null
+++ b/nix/pkgs/notify-battery.nix
@@ -0,0 +1,46 @@
+{ pkgs, ... }:
+
+pkgs.writeShellApplication {
+  name = "notify-battery";
+
+  runtimeInputs = with pkgs; [
+    acpi
+    bash
+    coreutils
+    dbus
+  ];
+
+  text = ''
+    set +o nounset
+    set +o pipefail
+
+    export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus"
+    export DISPLAY=:0
+
+    # Battery percentage at which to notify.
+    BATTERY_DISCHARGING=$(acpi -b | grep "Battery 0" | grep -c "Discharging")
+    BATTERY_LEVEL=$(acpi -b | grep "Battery 0" | grep -P -o '[0-9]+(?=%)')
+    WARNING_LEVEL=20
+
+    # Use two files to store whether we've shown a notification or not (to prevent multiple notifications).
+    EMPTY_FILE=/tmp/battery-empty
+    FULL_FILE=/tmp/battery-full
+
+    # Reset notifications if the computer is charging/discharging.
+    if [ "$BATTERY_DISCHARGING" -eq 1 ] && [ -f $FULL_FILE ]; then
+      rm $FULL_FILE
+    elif [ "$BATTERY_DISCHARGING" -eq 0 ] && [ -f $EMPTY_FILE ]; then
+      rm $EMPTY_FILE
+    fi
+
+    # If the battery is charging and is full (and has not shown notification yet).
+    if [ "$BATTERY_LEVEL" -gt 95 ] && [ "$BATTERY_DISCHARGING" -eq 0 ] && [ ! -f $FULL_FILE ]; then
+      notify-send "Battery Charged" "Battery is fully charged." -r 9991
+      touch $FULL_FILE
+    # If the battery is low and is not charging (and has not shown notification yet).
+    elif [ "$BATTERY_LEVEL" -le $WARNING_LEVEL ] && [ "$BATTERY_DISCHARGING" -eq 1 ] && [ ! -f $EMPTY_FILE ]; then
+      notify-send "Low Battery" "''${BATTERY_LEVEL}% of battery remaining." -u critical -r 9991 -t 0
+      touch $EMPTY_FILE
+    fi
+  '';
+}