diff --git a/bin/notify-battery b/bin/notify-battery deleted file mode 100755 index e9e77b4..0000000 --- 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 311a75a..5aa18c3 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 481fd23..0a90690 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 d8cb037..e1aa504 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 0000000..7355f4f --- /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 + ''; +}