fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
10.4 KiB380 linesraw
1{ config, lib, pkgs, stdenv, ... }:
2
3with lib;
4
5let
6
7  cfg = config.services.twmn;
8
9  animationOpts = {
10    curve = mkOption {
11      type = types.ints.between 0 40;
12      default = 38;
13      example = 19;
14      description = ''
15        The qt easing-curve animation to use for the animation. See
16        [
17        QEasingCurve documentation](https://doc.qt.io/qt-5/qeasingcurve.html#Type-enum).
18      '';
19    };
20
21    duration = mkOption {
22      type = types.ints.unsigned;
23      default = 1000;
24      example = 618;
25      description = "The animation duration in milliseconds.";
26    };
27  };
28
29in {
30  meta.maintainers = [ hm.maintainers.austreelis ];
31
32  options.services.twmn = {
33    enable = mkEnableOption "twmn, a tiling window manager notification daemon";
34
35    duration = mkOption {
36      type = types.ints.unsigned;
37      default = 3000;
38      example = 5000;
39      description = ''
40        The time each notification remains visible, in milliseconds.
41      '';
42    };
43
44    extraConfig = mkOption {
45      type = types.attrs;
46      default = { };
47      example = literalExpression
48        ''{ main.activation_command = "\${pkgs.hello}/bin/hello"; }'';
49      description = ''
50        Extra configuration options to add to the twmnd config file. See
51        <https://github.com/sboli/twmn/blob/master/README.md>
52        for details.
53      '';
54    };
55
56    host = mkOption {
57      type = types.str;
58      default = "127.0.0.1";
59      example = "laptop.lan";
60      description = "Host address to listen on for notifications.";
61    };
62
63    icons = {
64      critical = mkOption {
65        type = types.nullOr types.path;
66        default = null;
67        description = "Path to the critical notifications' icon.";
68      };
69
70      info = mkOption {
71        type = types.nullOr types.path;
72        default = null;
73        description = "Path to the informative notifications' icon.";
74      };
75
76      warning = mkOption {
77        type = types.nullOr types.path;
78        default = null;
79        description = "Path to the warning notifications' icon.";
80      };
81    };
82
83    port = mkOption {
84      type = types.port;
85      default = 9797;
86      description = "UDP port to listen on for notifications.";
87    };
88
89    screen = mkOption {
90      type = types.nullOr types.int;
91      default = null;
92      example = 0;
93      description = ''
94        Screen number to display notifications on when using a multi-head
95        desktop.
96      '';
97    };
98
99    soundCommand = mkOption {
100      type = types.str;
101      default = "";
102      description = "Command to execute to play a notification's sound.";
103    };
104
105    text = {
106      color = mkOption {
107        type = types.str;
108        default = "#999999";
109        example = "lightgray";
110        description = ''
111          Notification's text color. RGB hex and keywords (e.g. `lightgray`)
112          are supported.
113        '';
114      };
115
116      font = {
117        package = mkOption {
118          type = types.nullOr types.package;
119          default = null;
120          example = literalExpression "pkgs.dejavu_fonts";
121          description = ''
122            Notification text's font package. If `null` then
123            the font is assumed to already be available in your profile.
124          '';
125        };
126
127        family = mkOption {
128          type = types.str;
129          default = "Sans";
130          example = "Noto Sans";
131          description = "Notification text's font family.";
132        };
133
134        size = mkOption {
135          type = types.ints.unsigned;
136          default = 13;
137          example = 42;
138          description = "Notification text's font size.";
139        };
140
141        variant = mkOption {
142          # These are the font variant supported by twmn
143          # See https://github.com/sboli/twmn/blob/master/README.md?plain=1#L42
144          type = types.enum [
145            "oblique"
146            "italic"
147            "ultra-light"
148            "light"
149            "medium"
150            "semi-bold"
151            "bold"
152            "ultra-bold"
153            "heavy"
154            "ultra-condensed"
155            "extra-condensed"
156            "condensed"
157            "semi-condensed"
158            "semi-expanded"
159            "expanded"
160            "extra-expanded"
161            "ultra-expanded"
162          ];
163          default = "medium";
164          example = "heavy";
165          description = "Notification text's font variant.";
166        };
167      };
168
169      maxLength = mkOption {
170        type = types.nullOr types.ints.unsigned;
171        default = null;
172        example = 80;
173        description = ''
174          Maximum length of the text before it is cut and suffixed with "...".
175          Never cuts if `null`.
176        '';
177      };
178    };
179
180    window = {
181      alwaysOnTop =
182        mkEnableOption "forcing the notification window to always be on top";
183
184      animation = {
185        easeIn = mkOption {
186          type = types.submodule { options = animationOpts; };
187          default = { };
188          example = literalExpression ''
189            {
190              curve = 19;
191              duration = 618;
192            }
193          '';
194          description = "Options for the notification appearance's animation.";
195        };
196
197        easeOut = mkOption {
198          type = types.submodule { options = animationOpts; };
199          default = { };
200          example = literalExpression ''
201            {
202              curve = 19;
203              duration = 618;
204            }
205          '';
206          description =
207            "Options for the notification disappearance's animation.";
208        };
209
210        bounce = {
211          enable = mkEnableOption
212            "notification bounce when displaying next notification directly";
213
214          duration = mkOption {
215            type = types.ints.unsigned;
216            default = 500;
217            example = 618;
218            description = "The bounce animation duration in milliseconds.";
219          };
220        };
221      };
222
223      color = mkOption {
224        type = types.str;
225        default = "#000000";
226        example = "lightgray";
227        description = ''
228          Notification's background color. RGB hex and keywords (e.g.
229          `lightgray`) are supported.
230        '';
231      };
232
233      height = mkOption {
234        type = types.ints.unsigned;
235        default = 18;
236        example = 42;
237        description = ''
238          Height of the slide bar. Useful to match your tiling window
239          manager's bar.
240        '';
241      };
242
243      offset = {
244        x = mkOption {
245          type = types.int;
246          default = 0;
247          example = 50;
248          description = ''
249            Offset of the notification's slide starting point in pixels on the
250            horizontal axis (positive is rightward).
251          '';
252        };
253
254        y = mkOption {
255          type = types.int;
256          default = 0;
257          example = -100;
258          description = ''
259            Offset of the notification's slide starting point in pixels on the
260            vertical axis (positive is upward).
261          '';
262        };
263      };
264
265      opacity = mkOption {
266        type = types.ints.between 0 100;
267        default = 100;
268        example = 80;
269        description = "The notification window's opacity.";
270      };
271
272      position = mkOption {
273        type = types.enum [
274          "tr"
275          "top_right"
276          "tl"
277          "top_left"
278          "br"
279          "bottom_right"
280          "bl"
281          "bottom_left"
282          "tc"
283          "top_center"
284          "bc"
285          "bottom_center"
286          "c"
287          "center"
288        ];
289        default = "top_right";
290        example = "bottom_left";
291        description = ''
292          Position of the notification slide. The notification will slide
293          in vertically from the border if placed in
294          `top_center` or `bottom_center`,
295          horizontally otherwise.
296        '';
297      };
298    };
299  };
300
301  #################
302  # Implementation
303
304  config = mkIf cfg.enable {
305    assertions = [
306      (lib.hm.assertions.assertPlatform "services.twmn" pkgs
307        lib.platforms.linux)
308    ];
309
310    home.packages =
311      lib.optional (!isNull cfg.text.font.package) cfg.text.font.package
312      ++ [ pkgs.twmn ];
313
314    xdg.configFile."twmn/twmn.conf".text = let
315      conf = recursiveUpdate {
316        gui = {
317          always_on_top = if cfg.window.alwaysOnTop then "true" else "false";
318          background_color = cfg.window.color;
319          bounce =
320            if cfg.window.animation.bounce.enable then "true" else "false";
321          bounce_duration = toString cfg.window.animation.bounce.duration;
322          font = cfg.text.font.family;
323          font_size = toString cfg.text.font.size;
324          font_variant = cfg.text.font.variant;
325          foreground_color = cfg.text.color;
326          height = toString cfg.window.height;
327          in_animation = toString cfg.window.animation.easeIn.curve;
328          in_animation_duration = toString cfg.window.animation.easeIn.duration;
329          max_length = toString
330            (if isNull cfg.text.maxLength then -1 else cfg.text.maxLength);
331          offset_x = with cfg.window.offset;
332            if x < 0 then toString x else "+${toString x}";
333          offset_y = with cfg.window.offset;
334            if y < 0 then toString y else "+${toString y}";
335          opacity = toString cfg.window.opacity;
336          out_animation = toString cfg.window.animation.easeOut.curve;
337          out_animation_duration =
338            toString cfg.window.animation.easeOut.duration;
339          position = cfg.window.position;
340          screen = toString cfg.screen;
341        };
342        # map null values to empty strings because formats.toml generator fails
343        # when encountering a null.
344        icons = mapAttrs (_: toString) cfg.icons;
345        main = {
346          duration = toString cfg.duration;
347          host = cfg.host;
348          port = toString cfg.port;
349          sound_command = cfg.soundCommand;
350        };
351      } cfg.extraConfig;
352
353      mkLine = name: value: "${name}=${value}";
354
355      mkSection = section: conf: ''
356        [${section}]
357        ${concatStringsSep "\n" (mapAttrsToList mkLine conf)}
358      '';
359    in concatStringsSep "\n" (mapAttrsToList mkSection conf) + "\n";
360
361    systemd.user.services.twmnd = {
362      Unit = {
363        Description = "twmn daemon";
364        After = [ "graphical-session-pre.target" ];
365        PartOf = [ "graphical-session.target" ];
366        X-Restart-Triggers =
367          [ "${config.xdg.configFile."twmn/twmn.conf".source}" ];
368      };
369
370      Install.WantedBy = [ "graphical-session.target" ];
371
372      Service = {
373        ExecStart = "${pkgs.twmn}/bin/twmnd";
374        Restart = "on-failure";
375        Type = "simple";
376        StandardOutput = "null";
377      };
378    };
379  };
380}