aboutsummaryrefslogtreecommitdiff
path: root/home-manager/modules/programs/i3status.nix
blob: c1e12fe71d7b062c6d30ac83ca77045b8fcc5974 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.programs.i3status;

  enabledModules = filterAttrs (n: v: v.enable) cfg.modules;

  formatOrder = n: ''order += "${n}"'';

  formatModule = n: v:
    let
      formatLine = n: v:
        let
          formatValue = v:
            if isBool v then
              (if v then "true" else "false")
            else if isString v then
              ''"${v}"''
            else
              toString v;
        in "${n} = ${formatValue v}";
    in ''
      ${n} {
        ${concatStringsSep "\n  " (mapAttrsToList formatLine v)}
      }
    '';

  settingsType = with types; attrsOf (oneOf [ bool int str ]);

  sortAttrNamesByPosition = comparator: set:
    let pos = n: set."${n}".position;
    in sort (a: b: comparator (pos a) (pos b)) (attrNames set);
in {
  meta.maintainers = [ hm.maintainers.justinlovinger ];

  options.programs.i3status = {
    enable = mkEnableOption "i3status";

    enableDefault = mkOption {
      type = types.bool;
      default = true;
      description = ''
        Whether or not to enable
        the default configuration.
      '';
    };

    general = mkOption {
      type = settingsType;
      default = { };
      description = ''
        Configuration to add to i3status <filename>config</filename>
        <code>general</code> section.
        See
        <citerefentry>
         <refentrytitle>i3status</refentrytitle>
         <manvolnum>1</manvolnum>
        </citerefentry>
        for options.
      '';
      example = literalExample ''
        {
          colors = true;
          color_good = "#e0e0e0";
          color_degraded = "#d7ae00";
          color_bad = "#f69d6a";
          interval = 1;
        }
      '';
    };

    modules = mkOption {
      type = types.attrsOf (types.submodule {
        options = {
          enable = mkOption {
            type = types.bool;
            default = true;
            description = ''
              Whether or not to enable this module.
            '';
          };
          position = mkOption {
            type = with types; either int float;
            description = ''
              Position of this module in i3status <code>order</code>.
            '';
          };
          settings = mkOption {
            type = settingsType;
            default = { };
            description = ''
              Configuration to add to this i3status module.
              See
              <citerefentry>
               <refentrytitle>i3status</refentrytitle>
               <manvolnum>1</manvolnum>
              </citerefentry>
              for options.
            '';
            example = literalExample ''
              {
                format = "♪ %volume";
                format_muted = "♪ muted (%volume)";
                device = "pulse:1";
              }
            '';
          };
        };
      });
      default = { };
      description = ''
        Modules to add to i3status <filename>config</filename> file.
        See
        <citerefentry>
         <refentrytitle>i3status</refentrytitle>
         <manvolnum>1</manvolnum>
        </citerefentry>
        for options.
      '';
      example = literalExample ''
        {
          "volume master" = {
            position = 1;
            settings = {
              format = "♪ %volume";
              format_muted = "♪ muted (%volume)";
              device = "pulse:1";
            };
          };
          "disk /" = {
            position = 2;
            settings = {
              format = "/ %avail";
            };
          };
        }
      '';
    };
  };

  config = mkIf cfg.enable {
    programs.i3status = mkIf cfg.enableDefault {
      general = {
        colors = mkDefault true;
        interval = mkDefault 5;
      };

      modules = {
        ipv6 = { position = mkDefault 1; };

        "wireless _first_" = {
          position = mkDefault 2;
          settings = {
            format_up = mkDefault "W: (%quality at %essid) %ip";
            format_down = mkDefault "W: down";
          };
        };

        "ethernet _first_" = {
          position = mkDefault 3;
          settings = {
            format_up = mkDefault "E: %ip (%speed)";
            format_down = mkDefault "E: down";
          };
        };

        "battery all" = {
          position = mkDefault 4;
          settings = { format = mkDefault "%status %percentage %remaining"; };
        };

        "disk /" = {
          position = mkDefault 5;
          settings = { format = mkDefault "%avail"; };
        };

        load = {
          position = mkDefault 6;
          settings = { format = mkDefault "%1min"; };
        };

        memory = {
          position = mkDefault 7;
          settings = {
            format = mkDefault "%used | %available";
            threshold_degraded = mkDefault "1G";
            format_degraded = mkDefault "MEMORY < %available";
          };
        };

        "tztime local" = {
          position = mkDefault 8;
          settings = { format = mkDefault "%Y-%m-%d %H:%M:%S"; };
        };
      };
    };

    home.packages = [ pkgs.i3status ];

    xdg.configFile."i3status/config".text = concatStringsSep "\n" ([ ]
      ++ optional (cfg.general != { }) (formatModule "general" cfg.general)
      ++ map formatOrder (sortAttrNamesByPosition lessThan enabledModules)
      ++ mapAttrsToList formatModule
      (mapAttrs (n: v: v.settings) enabledModules));
  };
}