fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Silvio Ankermanncbatticon: Add support for batteryIdf540f30f

master
3.7 KiB133 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.services.cbatticon;
8
9  package = pkgs.cbatticon;
10
11  makeCommand = commandName: commandArg:
12    optional (commandArg != null)
13    (let cmd = pkgs.writeShellScript commandName commandArg;
14    in "--${commandName} ${cmd}");
15
16  commandLine = concatStringsSep " " ([ "${package}/bin/cbatticon" ]
17    ++ makeCommand "command-critical-level" cfg.commandCriticalLevel
18    ++ makeCommand "command-left-click" cfg.commandLeftClick
19    ++ optional (cfg.iconType != null) "--icon-type ${cfg.iconType}"
20    ++ optional (cfg.lowLevelPercent != null)
21    "--low-level ${toString cfg.lowLevelPercent}"
22    ++ optional (cfg.criticalLevelPercent != null)
23    "--critical-level ${toString cfg.criticalLevelPercent}"
24    ++ optional (cfg.updateIntervalSeconds != null)
25    "--update-interval ${toString cfg.updateIntervalSeconds}"
26    ++ optional (cfg.hideNotification != null && cfg.hideNotification)
27    "--hide-notification" ++ optional (cfg.batteryId != null) cfg.batteryId);
28
29in {
30  meta.maintainers = [ maintainers.pmiddend ];
31
32  options = {
33    services.cbatticon = {
34      enable = mkEnableOption "cbatticon";
35
36      commandCriticalLevel = mkOption {
37        type = types.nullOr types.lines;
38        default = null;
39        example = ''
40          notify-send "battery critical!"
41        '';
42        description = ''
43          Command to execute when the critical battery level is reached.
44        '';
45      };
46
47      commandLeftClick = mkOption {
48        type = types.nullOr types.lines;
49        default = null;
50        description = ''
51          Command to execute when left clicking on the tray icon.
52        '';
53      };
54
55      iconType = mkOption {
56        type =
57          types.nullOr (types.enum [ "standard" "notification" "symbolic" ]);
58        default = null;
59        example = "symbolic";
60        description = "Icon type to display in the system tray.";
61      };
62
63      lowLevelPercent = mkOption {
64        type = types.nullOr (types.ints.between 0 100);
65        default = null;
66        example = 20;
67        description = ''
68          Low level percentage of the battery in percent (without the
69          percent symbol).
70        '';
71      };
72
73      criticalLevelPercent = mkOption {
74        type = types.nullOr (types.ints.between 0 100);
75        default = null;
76        example = 5;
77        description = ''
78          Critical level percentage of the battery in percent (without
79          the percent symbol).
80        '';
81      };
82
83      updateIntervalSeconds = mkOption {
84        type = types.nullOr types.ints.positive;
85        default = null;
86        example = 5;
87        description = ''
88          Number of seconds between updates of the battery information.
89        '';
90      };
91
92      hideNotification = mkOption {
93        type = types.nullOr types.bool;
94        default = null;
95        description = "Hide the notification popups.";
96      };
97
98      batteryId = mkOption {
99        type = types.nullOr types.str;
100        default = null;
101        description = ''
102          ID of the battery to monitor. List with {command}`cbatticon -p`.
103          Defaults to the first entry in the list.
104        '';
105      };
106    };
107  };
108
109  config = mkIf cfg.enable {
110    assertions = [
111      (lib.hm.assertions.assertPlatform "services.cbatticon" pkgs
112        lib.platforms.linux)
113    ];
114
115    home.packages = [ package ];
116
117    systemd.user.services.cbatticon = {
118      Unit = {
119        Description = "cbatticon system tray battery icon";
120        Requires = [ "tray.target" ];
121        After = [ "graphical-session-pre.target" "tray.target" ];
122        PartOf = [ "graphical-session.target" ];
123      };
124
125      Install = { WantedBy = [ "graphical-session.target" ]; };
126
127      Service = {
128        ExecStart = commandLine;
129        Restart = "on-abort";
130      };
131    };
132  };
133}