1{ 2 config, 3 lib, 4 pkgs, 5 ... 6}: 7 8let 9 cfg = config.custom.battery; 10in 11{ 12 options.custom.battery.enable = lib.mkEnableOption "battery"; 13 14 config = lib.mkIf cfg.enable { 15 systemd.user.services.battery_monitor = { 16 Install = { 17 WantedBy = [ "default.target" ]; 18 }; 19 Service = { 20 ExecStart = pkgs.writeScript "battery_monitor.sh" '' 21 #!${pkgs.bash}/bin/bash 22 23 battery=/sys/class/power_supply/BAT0 24 25 while : 26 do 27 status="$(${pkgs.coreutils}/bin/cat $battery/status)" 28 capacity="$(${pkgs.coreutils}/bin/cat $battery/capacity)" 29 30 if [ "$status" = Discharging -a "$capacity" -lt 5 ]; then 31 ${pkgs.util-linux}/bin/logger "Critical battery threshold" 32 ${pkgs.systemd}/bin/systemctl hibernate 33 elif [ "$status" = Discharging -a "$capacity" -lt 10 ]; then 34 ${pkgs.libnotify}/bin/notify-send "warning: battery at $capacity%" 35 fi 36 ${pkgs.coreutils}/bin/sleep 60 37 done 38 ''; 39 Restart = "always"; 40 RestartSec = 10; 41 Type = "simple"; 42 }; 43 }; 44 }; 45}