fork/home-manager

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

Robert Helgessonwaybar: fix service After value8aac47a1

master
9.6 KiB326 linesraw
1{ config, lib, pkgs, ... }:
2
3let
4  inherit (lib)
5    all filterAttrs hasAttr isStorePath literalExpression optionalAttrs types;
6  inherit (lib.options) mkEnableOption mkOption;
7  inherit (lib.modules) mkIf mkMerge;
8
9  cfg = config.programs.waybar;
10
11  jsonFormat = pkgs.formats.json { };
12
13  mkMargin = name:
14    mkOption {
15      type = types.nullOr types.int;
16      default = null;
17      example = 10;
18      description = "Margin value without unit.";
19    };
20
21  waybarBarConfig = with types;
22    submodule {
23      freeformType = jsonFormat.type;
24
25      options = {
26        layer = mkOption {
27          type = nullOr (enum [ "top" "bottom" ]);
28          default = null;
29          description = ''
30            Decide if the bar is displayed in front (`"top"`)
31            of the windows or behind (`"bottom"`).
32          '';
33          example = "top";
34        };
35
36        output = mkOption {
37          type = nullOr (either str (listOf str));
38          default = null;
39          example = literalExpression ''
40            [ "DP-1" "!DP-2" "!DP-3" ]
41          '';
42          description = ''
43            Specifies on which screen this bar will be displayed.
44            Exclamation mark(!) can be used to exclude specific output.
45          '';
46        };
47
48        position = mkOption {
49          type = nullOr (enum [ "top" "bottom" "left" "right" ]);
50          default = null;
51          example = "right";
52          description = "Bar position relative to the output.";
53        };
54
55        height = mkOption {
56          type = nullOr ints.unsigned;
57          default = null;
58          example = 5;
59          description =
60            "Height to be used by the bar if possible. Leave blank for a dynamic value.";
61        };
62
63        width = mkOption {
64          type = nullOr ints.unsigned;
65          default = null;
66          example = 5;
67          description =
68            "Width to be used by the bar if possible. Leave blank for a dynamic value.";
69        };
70
71        modules-left = mkOption {
72          type = listOf str;
73          default = [ ];
74          description = "Modules that will be displayed on the left.";
75          example = literalExpression ''
76            [ "sway/workspaces" "sway/mode" "wlr/taskbar" ]
77          '';
78        };
79
80        modules-center = mkOption {
81          type = listOf str;
82          default = [ ];
83          description = "Modules that will be displayed in the center.";
84          example = literalExpression ''
85            [ "sway/window" ]
86          '';
87        };
88
89        modules-right = mkOption {
90          type = listOf str;
91          default = [ ];
92          description = "Modules that will be displayed on the right.";
93          example = literalExpression ''
94            [ "mpd" "custom/mymodule#with-css-id" "temperature" ]
95          '';
96        };
97
98        modules = mkOption {
99          type = jsonFormat.type;
100          visible = false;
101          default = null;
102          description = "Modules configuration.";
103          example = literalExpression ''
104            {
105              "sway/window" = {
106                max-length = 50;
107              };
108              "clock" = {
109                format-alt = "{:%a, %d. %b  %H:%M}";
110              };
111            }
112          '';
113        };
114
115        margin = mkOption {
116          type = nullOr str;
117          default = null;
118          description = "Margins value using the CSS format without units.";
119          example = "20 5";
120        };
121
122        margin-left = mkMargin "left";
123        margin-right = mkMargin "right";
124        margin-bottom = mkMargin "bottom";
125        margin-top = mkMargin "top";
126
127        name = mkOption {
128          type = nullOr str;
129          default = null;
130          description =
131            "Optional name added as a CSS class, for styling multiple waybars.";
132          example = "waybar-1";
133        };
134
135        gtk-layer-shell = mkOption {
136          type = nullOr bool;
137          default = null;
138          example = false;
139          description =
140            "Option to disable the use of gtk-layer-shell for popups.";
141        };
142      };
143    };
144in {
145  meta.maintainers = with lib.maintainers; [ berbiche ];
146
147  options.programs.waybar = with lib.types; {
148    enable = mkEnableOption "Waybar";
149
150    package = mkOption {
151      type = package;
152      default = pkgs.waybar;
153      defaultText = literalExpression "pkgs.waybar";
154      description = ''
155        Waybar package to use. Set to `null` to use the default package.
156      '';
157    };
158
159    settings = mkOption {
160      type = either (listOf waybarBarConfig) (attrsOf waybarBarConfig);
161      default = [ ];
162      description = ''
163        Configuration for Waybar, see <https://github.com/Alexays/Waybar/wiki/Configuration>
164        for supported values.
165      '';
166      example = literalExpression ''
167        {
168          mainBar = {
169            layer = "top";
170            position = "top";
171            height = 30;
172            output = [
173              "eDP-1"
174              "HDMI-A-1"
175            ];
176            modules-left = [ "sway/workspaces" "sway/mode" "wlr/taskbar" ];
177            modules-center = [ "sway/window" "custom/hello-from-waybar" ];
178            modules-right = [ "mpd" "custom/mymodule#with-css-id" "temperature" ];
179
180            "sway/workspaces" = {
181              disable-scroll = true;
182              all-outputs = true;
183            };
184            "custom/hello-from-waybar" = {
185              format = "hello {}";
186              max-length = 40;
187              interval = "once";
188              exec = pkgs.writeShellScript "hello-from-waybar" '''
189                echo "from within waybar"
190              ''';
191            };
192          };
193        }
194      '';
195    };
196
197    systemd.enable = mkEnableOption "Waybar systemd integration";
198
199    systemd.target = mkOption {
200      type = str;
201      default = "graphical-session.target";
202      example = "sway-session.target";
203      description = ''
204        The systemd target that will automatically start the Waybar service.
205
206        When setting this value to `"sway-session.target"`,
207        make sure to also enable {option}`wayland.windowManager.sway.systemd.enable`,
208        otherwise the service may never be started.
209      '';
210    };
211
212    style = mkOption {
213      type = nullOr (either path lines);
214      default = null;
215      description = ''
216        CSS style of the bar.
217
218        See <https://github.com/Alexays/Waybar/wiki/Configuration>
219        for the documentation.
220
221        If the value is set to a path literal, then the path will be used as the css file.
222      '';
223      example = ''
224        * {
225          border: none;
226          border-radius: 0;
227          font-family: Source Code Pro;
228        }
229        window#waybar {
230          background: #16191C;
231          color: #AAB2BF;
232        }
233        #workspaces button {
234          padding: 0 5px;
235        }
236      '';
237    };
238  };
239
240  config = let
241    # Removes nulls because Waybar ignores them.
242    # This is not recursive.
243    removeTopLevelNulls = filterAttrs (_: v: v != null);
244
245    # Makes the actual valid configuration Waybar accepts
246    # (strips our custom settings before converting to JSON)
247    makeConfiguration = configuration:
248      let
249        # The "modules" option is not valid in the JSON
250        # as its descendants have to live at the top-level
251        settingsWithoutModules = removeAttrs configuration [ "modules" ];
252        settingsModules =
253          optionalAttrs (configuration.modules != null) configuration.modules;
254      in removeTopLevelNulls (settingsWithoutModules // settingsModules);
255
256    # Allow using attrs for settings instead of a list in order to more easily override
257    settings = if builtins.isAttrs cfg.settings then
258      lib.attrValues cfg.settings
259    else
260      cfg.settings;
261
262    # The clean list of configurations
263    finalConfiguration = map makeConfiguration settings;
264
265    configSource = jsonFormat.generate "waybar-config.json" finalConfiguration;
266
267  in mkIf cfg.enable (mkMerge [
268    {
269      assertions = [
270        (lib.hm.assertions.assertPlatform "programs.waybar" pkgs
271          lib.platforms.linux)
272        ({
273          assertion =
274            if lib.versionAtLeast config.home.stateVersion "22.05" then
275              all (x: !hasAttr "modules" x || x.modules == null) settings
276            else
277              true;
278          message = ''
279            The `programs.waybar.settings.[].modules` option has been removed.
280            It is now possible to declare modules in the configuration without nesting them under the `modules` option.
281          '';
282        })
283      ];
284
285      home.packages = [ cfg.package ];
286
287      xdg.configFile."waybar/config" = mkIf (settings != [ ]) {
288        source = configSource;
289        onChange = ''
290          ${pkgs.procps}/bin/pkill -u $USER -USR2 waybar || true
291        '';
292      };
293
294      xdg.configFile."waybar/style.css" = mkIf (cfg.style != null) {
295        source = if builtins.isPath cfg.style || isStorePath cfg.style then
296          cfg.style
297        else
298          pkgs.writeText "waybar/style.css" cfg.style;
299        onChange = ''
300          ${pkgs.procps}/bin/pkill -u $USER -USR2 waybar || true
301        '';
302      };
303    }
304
305    (mkIf cfg.systemd.enable {
306      systemd.user.services.waybar = {
307        Unit = {
308          Description =
309            "Highly customizable Wayland bar for Sway and Wlroots based compositors.";
310          Documentation = "https://github.com/Alexays/Waybar/wiki";
311          PartOf = [ "graphical-session.target" ];
312          After = [ "graphical-session-pre.target" ];
313        };
314
315        Service = {
316          ExecStart = "${cfg.package}/bin/waybar";
317          ExecReload = "${pkgs.coreutils}/bin/kill -SIGUSR2 $MAINPID";
318          Restart = "on-failure";
319          KillMode = "mixed";
320        };
321
322        Install = { WantedBy = [ cfg.systemd.target ]; };
323      };
324    })
325  ]);
326}