fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
7.7 KiB295 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.rofi;
8
9  mkValueString = value:
10    if isBool value then
11      if value then "true" else "false"
12    else if isInt value then
13      toString value
14    else if (value._type or "") == "literal" then
15      value.value
16    else if isString value then
17      ''"${value}"''
18    else if isList value then
19      "[ ${strings.concatStringsSep "," (map mkValueString value)} ]"
20    else
21      abort "Unhandled value type ${builtins.typeOf value}";
22
23  mkKeyValue = { sep ? ": ", end ? ";" }:
24    name: value:
25    "${name}${sep}${mkValueString value}${end}";
26
27  mkRasiSection = name: value:
28    if isAttrs value then
29      let
30        toRasiKeyValue = generators.toKeyValue { mkKeyValue = mkKeyValue { }; };
31        # Remove null values so the resulting config does not have empty lines
32        configStr = toRasiKeyValue (filterAttrs (_: v: v != null) value);
33      in ''
34        ${name} {
35        ${configStr}}
36      ''
37    else
38      (mkKeyValue {
39        sep = " ";
40        end = "";
41      } name value) + "\n";
42
43  toRasi = attrs:
44    concatStringsSep "\n" (concatMap (mapAttrsToList mkRasiSection) [
45      (filterAttrs (n: _: n == "@theme") attrs)
46      (filterAttrs (n: _: n == "@import") attrs)
47      (removeAttrs attrs [ "@theme" "@import" ])
48    ]);
49
50  locationsMap = {
51    center = 0;
52    top-left = 1;
53    top = 2;
54    top-right = 3;
55    right = 4;
56    bottom-right = 5;
57    bottom = 6;
58    bottom-left = 7;
59    left = 8;
60  };
61
62  primitive = with types; (oneOf [ str int bool rasiLiteral ]);
63
64  # Either a `section { foo: "bar"; }` or a `@import/@theme "some-text"`
65  configType = with types;
66    (either (attrsOf (either primitive (listOf primitive))) str);
67
68  rasiLiteral = types.submodule {
69    options = {
70      _type = mkOption {
71        type = types.enum [ "literal" ];
72        internal = true;
73      };
74
75      value = mkOption {
76        type = types.str;
77        internal = true;
78      };
79    };
80  } // {
81    description = "Rasi literal string";
82  };
83
84  themeType = with types; attrsOf configType;
85
86  themeName = if (cfg.theme == null) then
87    null
88  else if (isString cfg.theme) then
89    cfg.theme
90  else if (isAttrs cfg.theme) then
91    "custom"
92  else
93    removeSuffix ".rasi" (baseNameOf cfg.theme);
94
95  themePath = if (isString cfg.theme) then
96    null
97  else if (isAttrs cfg.theme) then
98    "custom"
99  else
100    cfg.theme;
101
102in {
103  options.programs.rofi = {
104    enable = mkEnableOption
105      "Rofi: A window switcher, application launcher and dmenu replacement";
106
107    package = mkOption {
108      default = pkgs.rofi;
109      type = types.package;
110      description = ''
111        Package providing the {command}`rofi` binary.
112      '';
113      example = literalExpression ''
114        pkgs.rofi.override { plugins = [ pkgs.rofi-emoji ]; };
115      '';
116    };
117
118    finalPackage = mkOption {
119      type = types.package;
120      readOnly = true;
121      description = ''
122        Resulting customized rofi package.
123      '';
124    };
125
126    plugins = mkOption {
127      default = [ ];
128      type = types.listOf types.package;
129      description = ''
130        List of rofi plugins to be installed.
131      '';
132      example = literalExpression "[ pkgs.rofi-calc ]";
133    };
134
135    font = mkOption {
136      default = null;
137      type = types.nullOr types.str;
138      example = "Droid Sans Mono 14";
139      description = "Font to use.";
140    };
141
142    terminal = mkOption {
143      default = null;
144      type = types.nullOr types.str;
145      description = ''
146        Path to the terminal which will be used to run console applications
147      '';
148      example = "\${pkgs.gnome.gnome_terminal}/bin/gnome-terminal";
149    };
150
151    cycle = mkOption {
152      default = null;
153      type = types.nullOr types.bool;
154      description = "Whether to cycle through the results list.";
155    };
156
157    location = mkOption {
158      default = "center";
159      type = types.enum (attrNames locationsMap);
160      description = "The location rofi appears on the screen.";
161    };
162
163    xoffset = mkOption {
164      default = 0;
165      type = types.int;
166      description = ''
167        Offset in the x-axis in pixels relative to the chosen location.
168      '';
169    };
170
171    yoffset = mkOption {
172      default = 0;
173      type = types.int;
174      description = ''
175        Offset in the y-axis in pixels relative to the chosen location.
176      '';
177    };
178
179    theme = mkOption {
180      default = null;
181      type = with types; nullOr (oneOf [ str path themeType ]);
182      example = literalExpression ''
183        let
184          # Use `mkLiteral` for string-like values that should show without
185          # quotes, e.g.:
186          # {
187          #   foo = "abc"; => foo: "abc";
188          #   bar = mkLiteral "abc"; => bar: abc;
189          # };
190          inherit (config.lib.formats.rasi) mkLiteral;
191        in {
192          "*" = {
193            background-color = mkLiteral "#000000";
194            foreground-color = mkLiteral "rgba ( 250, 251, 252, 100 % )";
195            border-color = mkLiteral "#FFFFFF";
196            width = 512;
197          };
198
199          "#inputbar" = {
200            children = map mkLiteral [ "prompt" "entry" ];
201          };
202
203          "#textbox-prompt-colon" = {
204            expand = false;
205            str = ":";
206            margin = mkLiteral "0px 0.3em 0em 0em";
207            text-color = mkLiteral "@foreground-color";
208          };
209        }
210      '';
211      description = ''
212        Name of theme or path to theme file in rasi format or attribute set with
213        theme configuration. Available named themes can be viewed using the
214        {command}`rofi-theme-selector` tool.
215      '';
216    };
217
218    configPath = mkOption {
219      default = "${config.xdg.configHome}/rofi/config.rasi";
220      defaultText = "$XDG_CONFIG_HOME/rofi/config.rasi";
221      type = types.str;
222      description = "Path where to put generated configuration file.";
223    };
224
225    extraConfig = mkOption {
226      default = { };
227      example = literalExpression ''
228        {
229          modi = "drun,emoji,ssh";
230          kb-primary-paste = "Control+V,Shift+Insert";
231          kb-secondary-paste = "Control+v,Insert";
232        }
233      '';
234      type = configType;
235      description = "Additional configuration to add.";
236    };
237
238  };
239
240  imports = let
241    mkRemovedOptionRofi = option:
242      (mkRemovedOptionModule [ "programs" "rofi" option ]
243        "Please use a Rofi theme instead.");
244  in map mkRemovedOptionRofi [
245    "width"
246    "lines"
247    "borderWidth"
248    "rowHeight"
249    "padding"
250    "separator"
251    "scrollbar"
252    "fullscreen"
253    "colors"
254  ];
255
256  config = mkIf cfg.enable {
257    assertions =
258      [ (hm.assertions.assertPlatform "programs.rofi" pkgs platforms.linux) ];
259
260    lib.formats.rasi.mkLiteral = value: {
261      _type = "literal";
262      inherit value;
263    };
264
265    programs.rofi.finalPackage = let
266      rofiWithPlugins = cfg.package.override
267        (old: { plugins = (old.plugins or [ ]) ++ cfg.plugins; });
268    in if builtins.hasAttr "override" cfg.package && cfg.plugins != [ ] then
269      rofiWithPlugins
270    else
271      cfg.package;
272
273    home.packages = [ cfg.finalPackage ];
274
275    home.file."${cfg.configPath}".text = toRasi {
276      configuration = ({
277        font = cfg.font;
278        terminal = cfg.terminal;
279        cycle = cfg.cycle;
280        location = (getAttr cfg.location locationsMap);
281        xoffset = cfg.xoffset;
282        yoffset = cfg.yoffset;
283      } // cfg.extraConfig);
284      # @theme must go after configuration but attrs are output in alphabetical order ('@' first)
285    } + (optionalString (themeName != null) (toRasi { "@theme" = themeName; }));
286
287    xdg.dataFile = mkIf (themePath != null) (if themePath == "custom" then {
288      "rofi/themes/${themeName}.rasi".text = toRasi cfg.theme;
289    } else {
290      "rofi/themes/${themeName}.rasi".source = themePath;
291    });
292  };
293
294  meta.maintainers = with maintainers; [ thiagokokada ];
295}