aboutsummaryrefslogtreecommitdiff
path: root/modules/services/cbatticon.nix
blob: 0de69c5f9ec26cb7ab878f09301592c904cd948b (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.cbatticon;

  package = pkgs.cbatticon;

  makeCommand = commandName: commandArg:
    optional (commandArg != null)
    (let cmd = pkgs.writeShellScript commandName commandArg;
    in "--${commandName} ${cmd}");

  commandLine = concatStringsSep " " ([ "${package}/bin/cbatticon" ]
    ++ makeCommand "command-critical-level" cfg.commandCriticalLevel
    ++ makeCommand "command-left-click" cfg.commandLeftClick
    ++ optional (cfg.iconType != null) "--icon-type ${cfg.iconType}"
    ++ optional (cfg.lowLevelPercent != null)
    "--low-level ${toString cfg.lowLevelPercent}"
    ++ optional (cfg.criticalLevelPercent != null)
    "--critical-level ${toString cfg.criticalLevelPercent}"
    ++ optional (cfg.updateIntervalSeconds != null)
    "--update-interval ${toString cfg.updateIntervalSeconds}"
    ++ optional (cfg.hideNotification != null && cfg.hideNotification)
    "--hide-notification");

in {
  meta.maintainers = [ maintainers.pmiddend ];

  options = {
    services.cbatticon = {
      enable = mkEnableOption "cbatticon";

      commandCriticalLevel = mkOption {
        type = types.nullOr types.lines;
        default = null;
        example = ''
          notify-send "battery critical!"
        '';
        description = ''
          Command to execute when the critical battery level is reached.
        '';
      };

      commandLeftClick = mkOption {
        type = types.nullOr types.lines;
        default = null;
        description = ''
          Command to execute when left clicking on the tray icon.
        '';
      };

      iconType = mkOption {
        type =
          types.nullOr (types.enum [ "standard" "notification" "symbolic" ]);
        default = null;
        example = "symbolic";
        description = "Icon type to display in the system tray.";
      };

      lowLevelPercent = mkOption {
        type = types.nullOr (types.ints.between 0 100);
        default = null;
        example = 20;
        description = ''
          Low level percentage of the battery in percent (without the
          percent symbol).
        '';
      };

      criticalLevelPercent = mkOption {
        type = types.nullOr (types.ints.between 0 100);
        default = null;
        example = 5;
        description = ''
          Critical level percentage of the battery in percent (without
          the percent symbol).
        '';
      };

      updateIntervalSeconds = mkOption {
        type = types.nullOr types.ints.positive;
        default = null;
        example = 5;
        description = ''
          Number of seconds between updates of the battery information.
        '';
      };

      hideNotification = mkOption {
        type = types.nullOr types.bool;
        default = null;
        description = "Hide the notification popups.";
      };
    };
  };

  config = mkIf cfg.enable {
    home.packages = [ package ];

    systemd.user.services.cbatticon = {
      Unit = {
        Description = "cbatticon system tray battery icon";
        After = [ "graphical-session-pre.target" ];
        PartOf = [ "graphical-session.target" ];
      };

      Install = { WantedBy = [ "graphical-session.target" ]; };

      Service = {
        ExecStart = commandLine;
        Restart = "on-abort";
      };
    };
  };
}