aboutsummaryrefslogtreecommitdiff
path: root/home-manager/modules/programs/rofi.nix
blob: f344e88e2ff3497dfdf2f48c11fe343421f3f75e (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
{ config, lib, pkgs, ... }:

with lib;
with builtins;

let

  cfg = config.programs.rofi;

  colorOption = description:
    mkOption {
      type = types.str;
      description = description;
    };

  rowColorSubmodule = types.submodule {
    options = {
      background = colorOption "Background color";
      foreground = colorOption "Foreground color";
      backgroundAlt = colorOption "Alternative background color";
      highlight = mkOption {
        type = types.submodule {
          options = {
            background = colorOption "Highlight background color";
            foreground = colorOption "Highlight foreground color";
          };
        };
        description = "Color settings for highlighted row.";
      };
    };
  };

  windowColorSubmodule = types.submodule {
    options = {
      background = colorOption "Window background color";
      border = colorOption "Window border color";
      separator = colorOption "Separator color";
    };
  };

  colorsSubmodule = types.submodule {
    options = {
      window = mkOption {
        default = null;
        type = windowColorSubmodule;
        description = "Window color settings.";
      };
      rows = mkOption {
        default = null;
        type = types.submodule {
          options = {
            normal = mkOption {
              default = null;
              type = types.nullOr rowColorSubmodule;
              description = "Normal row color settings.";
            };
            active = mkOption {
              default = null;
              type = types.nullOr rowColorSubmodule;
              description = "Active row color settings.";
            };
            urgent = mkOption {
              default = null;
              type = types.nullOr rowColorSubmodule;
              description = "Urgent row color settings.";
            };
          };
        };
        description = "Rows color settings.";
      };
    };
  };

  valueToString = value:
    if isBool value then (if value then "true" else "else") else toString value;

  windowColorsToString = window:
    concatStringsSep ", " (with window; [ background border separator ]);

  rowsColorsToString = rows: ''
    ${optionalString (rows.normal != null)
    (setOption "color-normal" (rowColorsToString rows.normal))}
    ${optionalString (rows.active != null)
    (setOption "color-active" (rowColorsToString rows.active))}
    ${optionalString (rows.urgent != null)
    (setOption "color-urgent" (rowColorsToString rows.urgent))}
  '';

  rowColorsToString = row:
    concatStringsSep ", " (with row; [
      background
      foreground
      backgroundAlt
      highlight.background
      highlight.foreground
    ]);

  setOption = name: value:
    optionalString (value != null) "rofi.${name}: ${valueToString value}";

  setColorScheme = colors:
    optionalString (colors != null) ''
      ${optionalString (colors.window != null) setOption "color-window"
      (windowColorsToString colors.window)}
      ${optionalString (colors.rows != null) (rowsColorsToString colors.rows)}
    '';

  locationsMap = {
    center = 0;
    top-left = 1;
    top = 2;
    top-right = 3;
    right = 4;
    bottom-right = 5;
    bottom = 6;
    bottom-left = 7;
    left = 8;
  };

  themeName = if (cfg.theme == null) then
    null
  else if (lib.isString cfg.theme) then
    cfg.theme
  else
    lib.removeSuffix ".rasi" (baseNameOf cfg.theme);

  themePath = if (lib.isString cfg.theme) then null else cfg.theme;

in {
  options.programs.rofi = {
    enable = mkEnableOption
      "Rofi: A window switcher, application launcher and dmenu replacement";

    width = mkOption {
      default = null;
      type = types.nullOr types.int;
      description = "Window width";
      example = 100;
    };

    lines = mkOption {
      default = null;
      type = types.nullOr types.int;
      description = "Number of lines";
      example = 10;
    };

    borderWidth = mkOption {
      default = null;
      type = types.nullOr types.int;
      description = "Border width";
      example = 1;
    };

    rowHeight = mkOption {
      default = null;
      type = types.nullOr types.int;
      description = "Row height (in chars)";
      example = 1;
    };

    padding = mkOption {
      default = null;
      type = types.nullOr types.int;
      description = "Padding";
      example = 400;
    };

    font = mkOption {
      default = null;
      type = types.nullOr types.str;
      example = "Droid Sans Mono 14";
      description = "Font to use.";
    };

    scrollbar = mkOption {
      default = null;
      type = types.nullOr types.bool;
      description = "Whether to show a scrollbar.";
    };

    terminal = mkOption {
      default = null;
      type = types.nullOr types.str;
      description = ''
        Path to the terminal which will be used to run console applications
      '';
      example = "\${pkgs.gnome3.gnome_terminal}/bin/gnome-terminal";
    };

    separator = mkOption {
      default = null;
      type = types.nullOr (types.enum [ "none" "dash" "solid" ]);
      description = "Separator style";
      example = "solid";
    };

    cycle = mkOption {
      default = null;
      type = types.nullOr types.bool;
      description = "Whether to cycle through the results list.";
    };

    fullscreen = mkOption {
      default = null;
      type = types.nullOr types.bool;
      description = "Whether to run rofi fullscreen.";
    };

    location = mkOption {
      default = "center";
      type = types.enum (builtins.attrNames locationsMap);
      description = "The location rofi appears on the screen.";
    };

    xoffset = mkOption {
      default = 0;
      type = types.int;
      description = ''
        Offset in the x-axis in pixels relative to the chosen location.
      '';
    };

    yoffset = mkOption {
      default = 0;
      type = types.int;
      description = ''
        Offset in the y-axis in pixels relative to the chosen location.
      '';
    };

    colors = mkOption {
      default = null;
      type = types.nullOr colorsSubmodule;
      description = ''
        Color scheme settings. Colors can be specified in CSS color
        formats. This option may become deprecated in the future and
        therefore the <varname>programs.rofi.theme</varname> option
        should be used whenever possible.
      '';
      example = literalExample ''
        colors = {
          window = {
            background = "argb:583a4c54";
            border = "argb:582a373e";
            separator = "#c3c6c8";
          };

          rows = {
            normal = {
              background = "argb:58455a64";
              foreground = "#fafbfc";
              backgroundAlt = "argb:58455a64";
              highlight = {
                background = "#00bcd4";
                foreground = "#fafbfc";
              };
            };
          };
        };
      '';
    };

    theme = mkOption {
      default = null;
      type = with types; nullOr (either str path);
      example = "Arc";
      description = ''
        Name of theme or path to theme file in rasi format. Available
        named themes can be viewed using the
        <command>rofi-theme-selector</command> tool.
      '';
    };

    configPath = mkOption {
      default = "${config.xdg.configHome}/rofi/config";
      defaultText = "$XDG_CONFIG_HOME/rofi/config";
      type = types.str;
      description = "Path where to put generated configuration file.";
    };

    extraConfig = mkOption {
      default = "";
      type = types.lines;
      description = "Additional configuration to add.";
    };

  };

  config = mkIf cfg.enable {
    assertions = [{
      assertion = cfg.theme == null || cfg.colors == null;
      message = ''
        Cannot use the rofi options 'theme' and 'colors' simultaneously.
      '';
    }];

    home.packages = [ pkgs.rofi ];

    home.file."${cfg.configPath}".text = ''
      ${setOption "width" cfg.width}
      ${setOption "lines" cfg.lines}
      ${setOption "font" cfg.font}
      ${setOption "bw" cfg.borderWidth}
      ${setOption "eh" cfg.rowHeight}
      ${setOption "padding" cfg.padding}
      ${setOption "separator-style" cfg.separator}
      ${setOption "hide-scrollbar"
      (if (cfg.scrollbar != null) then (!cfg.scrollbar) else cfg.scrollbar)}
      ${setOption "terminal" cfg.terminal}
      ${setOption "cycle" cfg.cycle}
      ${setOption "fullscreen" cfg.fullscreen}
      ${setOption "location" (builtins.getAttr cfg.location locationsMap)}
      ${setOption "xoffset" cfg.xoffset}
      ${setOption "yoffset" cfg.yoffset}

      ${setColorScheme cfg.colors}
      ${setOption "theme" themeName}

      ${cfg.extraConfig}
    '';

    xdg.dataFile = mkIf (themePath != null) {
      "rofi/themes/${themeName}.rasi".source = themePath;
    };
  };
}