From 22f45872815848799a5415461e93952368d60375 Mon Sep 17 00:00:00 2001 From: Alexander Heldt Date: Wed, 24 Jul 2024 18:08:00 +0200 Subject: [PATCH] pinwheel: Add low battery manager --- hosts/pinwheel/modules/default.nix | 2 + hosts/pinwheel/modules/power/default.nix | 85 +++++++++++++++++++++++- 2 files changed, 84 insertions(+), 3 deletions(-) diff --git a/hosts/pinwheel/modules/default.nix b/hosts/pinwheel/modules/default.nix index 29fc882..0f5d693 100644 --- a/hosts/pinwheel/modules/default.nix +++ b/hosts/pinwheel/modules/default.nix @@ -14,7 +14,9 @@ in hyprland.enable = true; swaylock.enable = true; physlock.enable = false; + power.enable = true; + lowbat.enable = true; wezterm.enable = false; foot.enable = true; diff --git a/hosts/pinwheel/modules/power/default.nix b/hosts/pinwheel/modules/power/default.nix index 238c642..84640ca 100644 --- a/hosts/pinwheel/modules/power/default.nix +++ b/hosts/pinwheel/modules/power/default.nix @@ -1,16 +1,36 @@ -{ lib, config, ... }: +{ pkgs, lib, config, ... }: let enabled = config.mod.power.enable; + lowbat = config.mod.lowbat; in { options = { mod.power = { enable = lib.mkEnableOption "enable power module"; }; + + mod.lowbat = { + enable = lib.mkEnableOption "enable low battery manager"; + + battery = lib.mkOption { + default = "BAT0"; + description = "Battery to monitor"; + }; + + notifyCapacity = lib.mkOption { + default = 15; + description = "Notify low battery at this level"; + }; + + suspendCapacity = lib.mkOption { + default = 10; + description = "Warn at this level and then suspend if power is not plugged in"; + }; + }; }; - config = lib.mkIf enabled { - services = { + config = { + services = lib.mkIf enabled { upower = { enable = true; }; @@ -24,5 +44,64 @@ in }; }; }; + + systemd.user = lib.mkIf lowbat.enable { + timers = { + "monitor-low-battery" = { + unitConfig = { + Description = "monitors battery level and suspends computer if battery is low"; + }; + + timerConfig = { + Unit = "monitor-low-battery.service"; + OnCalendar = "*-*-* *:00/15:00"; # Every 15 minutes + Persistent = true; + }; + + wantedBy = ["timers.target"]; + }; + }; + + services = { + "monitor-low-battery" = { + unitConfig = { + Description = "monitors battery level and suspends computer if battery is low"; + }; + + serviceConfig = { + Type = "exec"; + }; + + path = [ + pkgs.coreutils # For `cat` + pkgs.libnotify + pkgs.swaylock + ]; + + script = let + pause-music = "${pkgs.playerctl}/bin/playerctl -p spotify pause"; + in '' + BATTERY_CAPACITY=$(cat /sys/class/power_supply/${lowbat.battery}/capacity) + BATTERY_STATUS=$(cat /sys/class/power_supply/${lowbat.battery}/status) + echo "Battery capacity: $BATTERY_CAPACITY" + echo "Battery status: $BATTERY_STATUS" + + if [[ $BATTERY_CAPACITY -le ${builtins.toString lowbat.notifyCapacity} && $BATTERY_STATUS = "Discharging" ]]; then + notify-send --expire-time=0 --urgency=critical "Battery Low" + fi + + if [[ $BATTERY_CAPACITY -le ${builtins.toString lowbat.suspendCapacity} && $BATTERY_STATUS = "Discharging" ]]; then + notify-send --expire-time=0 --urgency=critical "Battery Critically Low" "Suspending in 60 seconds if power is not plugged in" + sleep 60s + + BATTERY_STATUS=$(cat /sys/class/power_supply/${lowbat.battery}/status) + if [[ $BATTERY_STATUS = "Discharging" ]]; then + ${pause-music}; ${pkgs.swaylock}/bin/swaylock -f; systemctl suspend + fi + fi + ''; + }; + }; + }; }; }