pinwheel: Add low battery manager

This commit is contained in:
Alexander Heldt
2024-07-24 18:08:00 +02:00
parent 7f42c36a76
commit 22f4587281
2 changed files with 84 additions and 3 deletions

View File

@@ -14,7 +14,9 @@ in
hyprland.enable = true; hyprland.enable = true;
swaylock.enable = true; swaylock.enable = true;
physlock.enable = false; physlock.enable = false;
power.enable = true; power.enable = true;
lowbat.enable = true;
wezterm.enable = false; wezterm.enable = false;
foot.enable = true; foot.enable = true;

View File

@@ -1,16 +1,36 @@
{ lib, config, ... }: { pkgs, lib, config, ... }:
let let
enabled = config.mod.power.enable; enabled = config.mod.power.enable;
lowbat = config.mod.lowbat;
in in
{ {
options = { options = {
mod.power = { mod.power = {
enable = lib.mkEnableOption "enable power module"; 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 { config = {
services = { services = lib.mkIf enabled {
upower = { upower = {
enable = true; 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
'';
};
};
};
}; };
} }