aboutsummaryrefslogtreecommitdiff
path: root/services/battery.nix
blob: a871e8fffe392f81d6c0dec6a9e4d41f37dd0dee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{ 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
      '';
    };

  };
}