{ config, lib, pkgs, ... }: with lib; let cfg = config.services.battery-alert; bat = pkgs.writeScriptBin "bat" '' ''; in { options.services.battery-alert = { enable = mkOption { type = types.bool; default = false; description = '' If enabled, NixOS will periodically check battery levels and report if it is below a threshold value. ''; }; }; config = { systemd.user.timers.battery-alert = mkIf cfg.enable { description = "Periodically check battery status and alert if required"; timerConfig.OnBootSec = "1m"; timerConfig.OnUnitInactiveSec = "1m"; timerConfig.Unit = "battery-alert.service"; wantedBy = [ "timers.target" ]; }; systemd.user.services.battery-alert = { description = "Check battery levels"; path = [ pkgs.libnotify pkgs.coreutils ]; serviceConfig = { PassEnvironment = "DISPLAY XAUTHORITY"; }; script = '' bat_status=$( ${pkgs.coreutils}/bin/cat /sys/class/power_supply/BAT0/capacity ) charging_status=$( ${pkgs.coreutils}/bin/cat /sys/class/power_supply/BAT0/status ) # if [[ $bat_status -ge 10 ]]; then ${pkgs.libnotify}/bin/notify-send "Battery low: $bat_status%" "$charging_status" # fi ''; }; }; }