diff options
author | Akshay <[email protected]> | 2021-09-26 16:56:36 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2021-09-26 16:56:36 +0100 |
commit | 5cdb4e421a809de51c3ebe8404e50d732721238b (patch) | |
tree | 73b71617c41b3e13edbf26035e821bb884d30441 /services/battery.nix |
init
Diffstat (limited to 'services/battery.nix')
-rw-r--r-- | services/battery.nix | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/services/battery.nix b/services/battery.nix new file mode 100644 index 0000000..a871e8f --- /dev/null +++ b/services/battery.nix | |||
@@ -0,0 +1,48 @@ | |||
1 | { config, lib, pkgs, ... }: | ||
2 | |||
3 | with lib; | ||
4 | let | ||
5 | cfg = config.services.battery-alert; | ||
6 | bat = pkgs.writeScriptBin "bat" | ||
7 | '' | ||
8 | ''; | ||
9 | in | ||
10 | { | ||
11 | options.services.battery-alert = { | ||
12 | enable = mkOption { | ||
13 | type = types.bool; | ||
14 | default = false; | ||
15 | description = '' | ||
16 | If enabled, NixOS will periodically check battery levels and report | ||
17 | if it is below a threshold value. | ||
18 | ''; | ||
19 | }; | ||
20 | }; | ||
21 | |||
22 | config = { | ||
23 | systemd.user.timers.battery-alert = mkIf cfg.enable { | ||
24 | description = "Periodically check battery status and alert if required"; | ||
25 | timerConfig.OnBootSec = "1m"; | ||
26 | timerConfig.OnUnitInactiveSec = "1m"; | ||
27 | timerConfig.Unit = "battery-alert.service"; | ||
28 | wantedBy = [ "timers.target" ]; | ||
29 | }; | ||
30 | |||
31 | systemd.user.services.battery-alert = { | ||
32 | description = "Check battery levels"; | ||
33 | path = [ pkgs.libnotify pkgs.coreutils ]; | ||
34 | serviceConfig = { | ||
35 | PassEnvironment = "DISPLAY XAUTHORITY"; | ||
36 | }; | ||
37 | script = '' | ||
38 | bat_status=$( ${pkgs.coreutils}/bin/cat /sys/class/power_supply/BAT0/capacity ) | ||
39 | charging_status=$( ${pkgs.coreutils}/bin/cat /sys/class/power_supply/BAT0/status ) | ||
40 | |||
41 | # if [[ $bat_status -ge 10 ]]; then | ||
42 | ${pkgs.libnotify}/bin/notify-send "Battery low: $bat_status%" "$charging_status" | ||
43 | # fi | ||
44 | ''; | ||
45 | }; | ||
46 | |||
47 | }; | ||
48 | } | ||