From 3ba1690e3a0ab555d8075e984a1750cb692764a8 Mon Sep 17 00:00:00 2001 From: Swarsel Date: Sun, 15 Dec 2024 15:00:44 +0100 Subject: [PATCH] feat: add low battery indicator --- SwarselSystems.org | 41 ++++++++++++++++++++++++++++ profiles/common/nixos/default.nix | 1 + profiles/common/nixos/lowbattery.nix | 26 ++++++++++++++++++ profiles/common/nixos/packages.nix | 4 +++ 4 files changed, 72 insertions(+) create mode 100644 profiles/common/nixos/lowbattery.nix diff --git a/SwarselSystems.org b/SwarselSystems.org index 5778d1b..07cdef4 100644 --- a/SwarselSystems.org +++ b/SwarselSystems.org @@ -3062,6 +3062,7 @@ This section is for setting things that should be used on hosts that are using t # ./safeeyes.nix ./distrobox.nix ./lid.nix + ./lowbattery.nix ]; nixpkgs.config.permittedInsecurePackages = [ @@ -3149,8 +3150,12 @@ Mostly used to install some compilers and lsp's that I want to have available wh # secure boot sbctl + # nix package database nix-index + # proc info + acpi + # better make for general tasks just @@ -4570,6 +4575,42 @@ This turns off the display when the lid is closed. } #+end_src +**** Low battery notification +:PROPERTIES: +:CUSTOM_ID: h:adf894d7-b3c6-4b8b-b13f-c28b3a5e1e17 +:END: + +Since I hide the waybar completely during normal operation, I run the risk of not noticing when my battery is about to run out. This module sends a notification when the battery level falls below 10%. Written by [[https://gist.github.com/cafkafk][cafkafk]]. + +#+begin_src nix :tangle profiles/common/nixos/lowbattery.nix + { pkgs, lib, ... }: + { + systemd.user.services."battery-low" = { + enable = true; + description = "Notify user if battery is below 10%"; + partOf = [ "graphical-session.target" ]; + wantedBy = [ "graphical-session.target" ]; + serviceConfig = { + Type = "simple"; + ExecStart = pkgs.writeShellScript "battery-low-notification" + '' + if (( 10 >= $(${lib.getExe pkgs.acpi} -b | head -n 1 | ${lib.getExe pkgs.ripgrep} -o "\d+%" | ${lib.getExe pkgs.ripgrep} -o "\d+"))); + then ${lib.getExe pkgs.libnotify} --urgency=critical "low battery" "$(${lib.getExe pkgs.acpi} -b | head -n 1 | ${lib.getExe pkgs.ripgrep} -o "\d+%")"; + fi; + ''; + }; + }; + systemd.user.timers."battery-low" = { + wantedBy = [ "timers.target" ]; + timerConfig = { + # Every Minute + OnCalendar = "*-*-* *:*:00"; + Unit = "battery-low.service"; + }; + }; + } +#+end_src + *** Server :PROPERTIES: :CUSTOM_ID: h:e492c24a-83a0-4bcb-a084-706f49318651 diff --git a/profiles/common/nixos/default.nix b/profiles/common/nixos/default.nix index 4d034af..05e8540 100644 --- a/profiles/common/nixos/default.nix +++ b/profiles/common/nixos/default.nix @@ -38,6 +38,7 @@ _: # ./safeeyes.nix ./distrobox.nix ./lid.nix + ./lowbattery.nix ]; nixpkgs.config.permittedInsecurePackages = [ diff --git a/profiles/common/nixos/lowbattery.nix b/profiles/common/nixos/lowbattery.nix new file mode 100644 index 0000000..3c41b4f --- /dev/null +++ b/profiles/common/nixos/lowbattery.nix @@ -0,0 +1,26 @@ +{ pkgs, lib, ... }: +{ + systemd.user.services."battery-low" = { + enable = true; + description = "Notify user if battery is below 10%"; + partOf = [ "graphical-session.target" ]; + wantedBy = [ "graphical-session.target" ]; + serviceConfig = { + Type = "simple"; + ExecStart = pkgs.writeShellScript "battery-low-notification" + '' + if (( 10 >= $(${lib.getExe pkgs.acpi} -b | head -n 1 | ${lib.getExe pkgs.ripgrep} -o "\d+%" | ${lib.getExe pkgs.ripgrep} -o "\d+"))); + then ${lib.getExe pkgs.libnotify} --urgency=critical "low battery" "$(${lib.getExe pkgs.acpi} -b | head -n 1 | ${lib.getExe pkgs.ripgrep} -o "\d+%")"; + fi; + ''; + }; + }; + systemd.user.timers."battery-low" = { + wantedBy = [ "timers.target" ]; + timerConfig = { + # Every Minute + OnCalendar = "*-*-* *:*:00"; + Unit = "battery-low.service"; + }; + }; +} diff --git a/profiles/common/nixos/packages.nix b/profiles/common/nixos/packages.nix index f09506d..127eebd 100644 --- a/profiles/common/nixos/packages.nix +++ b/profiles/common/nixos/packages.nix @@ -26,8 +26,12 @@ # secure boot sbctl + # nix package database nix-index + # proc info + acpi + # better make for general tasks just