fork/home-manager

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

Genevieveimapnotify: Use JSON type for extraConfig (#4238)f5b03feb

master
4.0 KiB133 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.services.imapnotify;
8
9  safeName = lib.replaceStrings [ "@" ":" "\\" "[" "]" ] [ "-" "-" "-" "" "" ];
10
11  configName = account: "imapnotify-${safeName account.name}-config.json";
12
13  imapnotifyAccounts =
14    filter (a: a.imapnotify.enable) (attrValues config.accounts.email.accounts);
15
16  genAccountUnit = account:
17    let name = safeName account.name;
18    in {
19      name = "imapnotify-${name}";
20      value = {
21        Unit = { Description = "imapnotify for ${name}"; };
22
23        Service = {
24          # Use the nix store path for config to ensure service restarts when it changes
25          ExecStart =
26            "${getExe cfg.package} -conf '${genAccountConfig account}'";
27          Restart = "always";
28          RestartSec = 30;
29          Type = "simple";
30        } // optionalAttrs account.notmuch.enable {
31          Environment =
32            "NOTMUCH_CONFIG=${config.xdg.configHome}/notmuch/default/config";
33        };
34
35        Install = { WantedBy = [ "default.target" ]; };
36      };
37    };
38
39  genAccountAgent = account:
40    let name = safeName account.name;
41    in {
42      name = "imapnotify-${name}";
43      value = {
44        enable = true;
45        config = {
46          # Use the nix store path for config to ensure service restarts when it changes
47          ProgramArguments =
48            [ "${getExe cfg.package}" "-conf" "${genAccountConfig account}" ];
49          KeepAlive = true;
50          ThrottleInterval = 30;
51          ExitTimeOut = 0;
52          ProcessType = "Background";
53          RunAtLoad = true;
54        } // optionalAttrs account.notmuch.enable {
55          EnvironmentVariables.NOTMUCH_CONFIG =
56            "${config.xdg.configHome}/notmuch/default/config";
57        };
58      };
59    };
60
61  genAccountConfig = account:
62    pkgs.writeText (configName account) (let
63      port = if account.imap.port != null then
64        account.imap.port
65      else if account.imap.tls.enable then
66        993
67      else
68        143;
69
70      toJSON = builtins.toJSON;
71    in toJSON ({
72      inherit (account.imap) host;
73      inherit port;
74      tls = account.imap.tls.enable;
75      username = account.userName;
76      passwordCmd =
77        lib.concatMapStringsSep " " lib.escapeShellArg account.passwordCommand;
78      inherit (account.imapnotify) boxes;
79    } // optionalAttrs (account.imapnotify.onNotify != "") {
80      onNewMail = account.imapnotify.onNotify;
81    } // optionalAttrs (account.imapnotify.onNotifyPost != "") {
82      onNewMailPost = account.imapnotify.onNotifyPost;
83    } // account.imapnotify.extraConfig));
84
85in {
86  meta.maintainers = [ maintainers.nickhu ];
87
88  options = {
89    services.imapnotify = {
90      enable = mkEnableOption "imapnotify";
91
92      package = mkOption {
93        type = types.package;
94        default = pkgs.goimapnotify;
95        defaultText = literalExpression "pkgs.goimapnotify";
96        example = literalExpression "pkgs.imapnotify";
97        description = "The imapnotify package to use";
98      };
99    };
100
101    accounts.email.accounts = mkOption {
102      type = with types;
103        attrsOf
104        (submodule (import ./imapnotify-accounts.nix { inherit pkgs lib; }));
105    };
106  };
107
108  config = mkIf cfg.enable {
109    assertions = let
110      checkAccounts = pred: msg:
111        let badAccounts = filter pred imapnotifyAccounts;
112        in {
113          assertion = badAccounts == [ ];
114          message = "imapnotify: Missing ${msg} for accounts: "
115            + concatMapStringsSep ", " (a: a.name) badAccounts;
116        };
117    in [
118      (checkAccounts (a: a.maildir == null) "maildir configuration")
119      (checkAccounts (a: a.imap == null) "IMAP configuration")
120      (checkAccounts (a: a.passwordCommand == null) "password command")
121      (checkAccounts (a: a.userName == null) "username")
122    ];
123
124    systemd.user.services = listToAttrs (map genAccountUnit imapnotifyAccounts);
125
126    launchd.agents = listToAttrs (map genAccountAgent imapnotifyAccounts);
127
128    xdg.configFile = listToAttrs (map (account: {
129      name = "imapnotify/${configName account}";
130      value.source = genAccountConfig account;
131    }) imapnotifyAccounts);
132  };
133}