fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
7.1 KiB234 linesraw
1{ config, options, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.services.polybar;
8  opt = options.services.polybar;
9
10  eitherStrBoolIntList = with types;
11    either str (either bool (either int (listOf str)));
12
13  # Convert a key/val pair to the insane format that polybar uses.
14  # Each input key/val pair may return several output key/val pairs.
15  convertPolybarKeyVal = key: val:
16    # Convert { foo = [ "a" "b" ]; }
17    # to {
18    #   foo-0 = "a";
19    #   foo-1 = "b";
20    # }
21    if isList val then
22      concatLists (imap0 (i: convertPolybarKeyVal "${key}-${toString i}") val)
23      # Convert {
24      #   foo.text = "a";
25      #   foo.font = 1;
26      # } to {
27      #   foo = "a";
28      #   foo-font = 1;
29      # }
30    else if isAttrs val && !lib.isDerivation val then
31      concatLists (mapAttrsToList
32        (k: convertPolybarKeyVal (if k == "text" then key else "${key}-${k}"))
33        val)
34      # Base case
35    else
36      [ (nameValuePair key val) ];
37
38  convertPolybarSection = _: attrs:
39    listToAttrs (concatLists (mapAttrsToList convertPolybarKeyVal attrs));
40
41  # Converts an attrset to INI text, quoting values as expected by polybar.
42  # This does no more fancy conversion.
43  toPolybarIni = generators.toINI {
44    mkKeyValue = key: value:
45      let
46        quoted = v:
47          if hasPrefix " " v || hasSuffix " " v then ''"${v}"'' else v;
48
49        value' = if isBool value then
50          (if value then "true" else "false")
51        else if (isString value && key != "include-file") then
52          quoted value
53        else
54          toString value;
55      in "${key}=${value'}";
56  };
57
58  configFile = let
59    isDeclarativeConfig = cfg.settings != opt.settings.default || cfg.config
60      != opt.config.default || cfg.extraConfig != opt.extraConfig.default;
61  in if isDeclarativeConfig then
62    pkgs.writeText "polybar.conf" ''
63      ${toPolybarIni cfg.config}
64      ${toPolybarIni (mapAttrs convertPolybarSection cfg.settings)}
65      ${cfg.extraConfig}
66    ''
67  else
68    null;
69
70in {
71  options = {
72    services.polybar = {
73      enable = mkEnableOption "Polybar status bar";
74
75      package = mkOption {
76        type = types.package;
77        default = pkgs.polybar;
78        defaultText = literalExpression "pkgs.polybar";
79        description = "Polybar package to install.";
80        example = literalExpression ''
81          pkgs.polybar.override {
82            i3GapsSupport = true;
83            alsaSupport = true;
84            iwSupport = true;
85            githubSupport = true;
86          }
87        '';
88      };
89
90      config = mkOption {
91        type = types.coercedTo types.path
92          (p: { "section/base" = { include-file = "${p}"; }; })
93          (types.attrsOf (types.attrsOf eitherStrBoolIntList));
94        description = ''
95          Polybar configuration. Can be either path to a file, or set of attributes
96          that will be used to create the final configuration.
97          See also {option}`services.polybar.settings` for a more nix-friendly format.
98        '';
99        default = { };
100        example = literalExpression ''
101          {
102            "bar/top" = {
103              monitor = "\''${env:MONITOR:eDP1}";
104              width = "100%";
105              height = "3%";
106              radius = 0;
107              modules-center = "date";
108            };
109
110            "module/date" = {
111              type = "internal/date";
112              internal = 5;
113              date = "%d.%m.%y";
114              time = "%H:%M";
115              label = "%time%  %date%";
116            };
117          }
118        '';
119      };
120
121      settings = mkOption {
122        type = with types;
123          let ty = oneOf [ bool int float str (listOf ty) (attrsOf ty) ];
124          in attrsOf (attrsOf ty // { description = "attribute sets"; });
125        description = ''
126          Polybar configuration. This takes a nix attrset and converts it to the
127          strange data format that polybar uses.
128          Each entry will be converted to a section in the output file.
129          Several things are treated specially: nested keys are converted
130          to dash-separated keys; the special `text` key is ignored as a nested key,
131          to allow mixing different levels of nesting; and lists are converted to
132          polybar's `foo-0, foo-1, ...` format.
133
134          For example:
135          ```nix
136          "module/volume" = {
137            type = "internal/pulseaudio";
138            format.volume = "<ramp-volume> <label-volume>";
139            label.muted.text = "🔇";
140            label.muted.foreground = "#666";
141            ramp.volume = ["🔈" "🔉" "🔊"];
142            click.right = "pavucontrol &";
143          }
144          ```
145          becomes:
146          ```ini
147          [module/volume]
148          type=internal/pulseaudio
149          format-volume=<ramp-volume> <label-volume>
150          label-muted=🔇
151          label-muted-foreground=#666
152          ramp-volume-0=🔈
153          ramp-volume-1=🔉
154          ramp-volume-2=🔊
155          click-right=pavucontrol &
156          ```
157        '';
158        default = { };
159        example = literalExpression ''
160          {
161            "module/volume" = {
162              type = "internal/pulseaudio";
163              format.volume = "<ramp-volume> <label-volume>";
164              label.muted.text = "🔇";
165              label.muted.foreground = "#666";
166              ramp.volume = ["🔈" "🔉" "🔊"];
167              click.right = "pavucontrol &";
168            };
169          }
170        '';
171      };
172
173      extraConfig = mkOption {
174        type = types.lines;
175        description = "Additional configuration to add.";
176        default = "";
177        example = ''
178          [module/date]
179          type = internal/date
180          interval = 5
181          date = "%d.%m.%y"
182          time = %H:%M
183          format-prefix-foreground = \''${colors.foreground-alt}
184          label = %time%  %date%
185        '';
186      };
187
188      script = mkOption {
189        type = types.lines;
190        description = ''
191          This script will be used to start the polybars.
192          Set all necessary environment variables here and start all bars.
193          It can be assumed that {command}`polybar` executable is in the {env}`PATH`.
194
195          Note, this script must start all bars in the background and then terminate.
196        '';
197        example = "polybar bar &";
198      };
199    };
200  };
201
202  config = mkIf cfg.enable {
203    assertions = [
204      (lib.hm.assertions.assertPlatform "services.polybar" pkgs
205        lib.platforms.linux)
206    ];
207
208    meta.maintainers = with maintainers; [ h7x4 ];
209
210    home.packages = [ cfg.package ];
211    xdg.configFile."polybar/config.ini" =
212      mkIf (configFile != null) { source = configFile; };
213
214    systemd.user.services.polybar = {
215      Unit = {
216        Description = "Polybar status bar";
217        PartOf = [ "tray.target" ];
218        X-Restart-Triggers = mkIf (configFile != null) "${configFile}";
219      };
220
221      Service = {
222        Type = "forking";
223        Environment = "PATH=${cfg.package}/bin:/run/wrappers/bin";
224        ExecStart =
225          let scriptPkg = pkgs.writeShellScriptBin "polybar-start" cfg.script;
226          in "${scriptPkg}/bin/polybar-start";
227        Restart = "on-failure";
228      };
229
230      Install = { WantedBy = [ "tray.target" ]; };
231    };
232  };
233
234}