fork/home-manager

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

Masanori Oginohimalaya: update a link to ticketa146ab6a

master
9.2 KiB270 linesraw
1{ config, lib, pkgs, ... }:
2
3let
4  # aliases
5  inherit (config.programs) himalaya;
6  tomlFormat = pkgs.formats.toml { };
7
8  # attrs util that removes entries containing a null value
9  compactAttrs = lib.filterAttrs (_: val: !isNull val);
10
11  # Needed for notmuch config, because the DB is here, and not in each account's dir
12  maildirBasePath = config.accounts.email.maildirBasePath;
13
14  # make a himalaya config from a home-manager email account config
15  mkAccountConfig = _: account:
16    let
17      # Use notmuch if it's enabled, otherwise fallback to IMAP then maildir
18      # Maildir is always set, so there's no easy way to detect if it's being used
19      notmuchEnabled = account.notmuch.enable;
20      imapEnabled = !isNull account.imap && !notmuchEnabled;
21      maildirEnabled = !isNull account.maildir && !imapEnabled
22        && !notmuchEnabled;
23
24      globalConfig = {
25        email = account.address;
26        display-name = account.realName;
27        default = account.primary;
28        folder-aliases = {
29          inbox = account.folders.inbox;
30          sent = account.folders.sent;
31          drafts = account.folders.drafts;
32          trash = account.folders.trash;
33        };
34      };
35
36      signatureConfig =
37        lib.optionalAttrs (account.signature.showSignature == "append") {
38          # TODO: signature cannot be attached yet
39          # https://todo.sr.ht/~soywod/pimalaya/27
40          signature = account.signature.text;
41          signature-delim = account.signature.delimiter;
42        };
43
44      imapConfig = lib.optionalAttrs imapEnabled (compactAttrs {
45        backend = "imap";
46        imap-host = account.imap.host;
47        imap-port = account.imap.port;
48        imap-ssl = account.imap.tls.enable;
49        imap-starttls = account.imap.tls.useStartTls;
50        imap-login = account.userName;
51        imap-auth = "passwd";
52        imap-passwd.cmd = builtins.concatStringsSep " " account.passwordCommand;
53      });
54
55      maildirConfig = lib.optionalAttrs maildirEnabled (compactAttrs {
56        backend = "maildir";
57        maildir-root-dir = account.maildir.absPath;
58      });
59
60      notmuchConfig = lib.optionalAttrs notmuchEnabled (compactAttrs {
61        backend = "notmuch";
62        notmuch-db-path = maildirBasePath;
63      });
64
65      smtpConfig = lib.optionalAttrs (!isNull account.smtp) (compactAttrs {
66        sender = "smtp";
67        smtp-host = account.smtp.host;
68        smtp-port = account.smtp.port;
69        smtp-ssl = account.smtp.tls.enable;
70        smtp-starttls = account.smtp.tls.useStartTls;
71        smtp-login = account.userName;
72        smtp-auth = "passwd";
73        smtp-passwd.cmd = builtins.concatStringsSep " " account.passwordCommand;
74      });
75
76      sendmailConfig =
77        lib.optionalAttrs (isNull account.smtp && !isNull account.msmtp) {
78          sender = "sendmail";
79          sendmail-cmd = "${pkgs.msmtp}/bin/msmtp";
80        };
81
82      config = globalConfig // signatureConfig // imapConfig // maildirConfig
83        // notmuchConfig // smtpConfig // sendmailConfig;
84
85    in lib.recursiveUpdate config account.himalaya.settings;
86
87  # make a systemd service config from a name and a description
88  mkServiceConfig = name: desc:
89    let
90      inherit (config.services."himalaya-${name}") enable environment settings;
91      optionalArg = key:
92        if (key ? settings && !isNull settings."${key}") then
93          [ "--${key} ${settings."${key}"}" ]
94        else
95          [ ];
96    in {
97      "himalaya-${name}" = lib.mkIf enable {
98        Unit = {
99          Description = desc;
100          After = [ "network.target" ];
101        };
102        Install = { WantedBy = [ "default.target" ]; };
103        Service = {
104          ExecStart = lib.concatStringsSep " "
105            ([ "${himalaya.package}/bin/himalaya" ] ++ optionalArg "account"
106              ++ [ name ] ++ optionalArg "keepalive");
107          ExecSearchPath = "/bin";
108          Environment =
109            lib.mapAttrsToList (key: val: "${key}=${val}") environment;
110          Restart = "always";
111          RestartSec = 10;
112        };
113      };
114    };
115
116in {
117  meta.maintainers = with lib.hm.maintainers; [ soywod toastal ];
118
119  options = {
120    programs.himalaya = {
121      enable = lib.mkEnableOption "the Himalaya email client";
122      package = lib.mkPackageOption pkgs "himalaya" { };
123      settings = lib.mkOption {
124        type = lib.types.submodule { freeformType = tomlFormat.type; };
125        default = { };
126        description = ''
127          Himalaya global configuration.
128          See <https://pimalaya.org/himalaya/cli/configuration/global.html> for supported values.
129        '';
130      };
131    };
132
133    services = {
134      himalaya-notify = {
135        enable = lib.mkEnableOption "the Himalaya new emails notifier service";
136
137        environment = lib.mkOption {
138          type = with lib.types; attrsOf str;
139          default = { };
140          example = lib.literalExpression ''
141            {
142              "PASSWORD_STORE_DIR" = "~/.password-store";
143            }
144          '';
145          description = ''
146            Extra environment variables to be exported in the service.
147          '';
148        };
149
150        settings = {
151          account = lib.mkOption {
152            type = with lib.types; nullOr str;
153            default = null;
154            example = "gmail";
155            description = ''
156              Name of the account the notifier should be started for. If
157              no account is given, the default one is used.
158            '';
159          };
160
161          keepalive = lib.mkOption {
162            type = with lib.types; nullOr int;
163            default = null;
164            example = "500";
165            description = ''
166              Notifier lifetime of the IDLE session (in seconds). 
167            '';
168          };
169        };
170      };
171
172      himalaya-watch = {
173        enable =
174          lib.mkEnableOption "the Himalaya folder changes watcher service";
175
176        environment = lib.mkOption {
177          type = with lib.types; attrsOf str;
178          default = { };
179          example = lib.literalExpression ''
180            {
181              "PASSWORD_STORE_DIR" = "~/.password-store";
182            }
183          '';
184          description = ''
185            Extra environment variables to be exported in the service.
186          '';
187        };
188
189        settings = {
190          account = lib.mkOption {
191            type = with lib.types; nullOr str;
192            default = null;
193            example = "gmail";
194            description = ''
195              Name of the account the watcher should be started for. If
196              no account is given, the default one is used.
197            '';
198          };
199
200          keepalive = lib.mkOption {
201            type = with lib.types; nullOr int;
202            default = null;
203            example = "500";
204            description = ''
205              Watcher lifetime of the IDLE session (in seconds). 
206            '';
207          };
208        };
209      };
210    };
211
212    accounts.email.accounts = lib.mkOption {
213      type = lib.types.attrsOf (lib.types.submodule {
214        options.himalaya = {
215          enable = lib.mkEnableOption "Himalaya for this email account";
216
217          # TODO: remove me for the next release
218          backend = lib.mkOption {
219            type = with lib.types; nullOr str;
220            default = null;
221            description = ''
222              Specifying {option}`accounts.email.accounts.*.himalaya.backend` is deprecated,
223              set {option}`accounts.email.accounts.*.himalaya.settings.backend` instead.
224            '';
225          };
226
227          # TODO: remove me for the next release
228          sender = lib.mkOption {
229            type = with lib.types; nullOr str;
230            description = ''
231              Specifying {option}`accounts.email.accounts.*.himalaya.sender` is deprecated,
232              set {option}'accounts.email.accounts.*.himalaya.settings.sender' instead.
233            '';
234          };
235
236          settings = lib.mkOption {
237            type = lib.types.submodule { freeformType = tomlFormat.type; };
238            default = { };
239            description = ''
240              Himalaya configuration for this email account.
241              See <https://pimalaya.org/himalaya/cli/configuration/account.html> for supported values.
242            '';
243          };
244        };
245      });
246    };
247  };
248
249  config = lib.mkIf himalaya.enable {
250    home.packages = [ himalaya.package ];
251
252    xdg.configFile."himalaya/config.toml".source = let
253      enabledAccounts = lib.filterAttrs (_: account: account.himalaya.enable)
254        config.accounts.email.accounts;
255      accountsConfig = lib.mapAttrs mkAccountConfig enabledAccounts;
256      globalConfig = compactAttrs himalaya.settings;
257      allConfig = globalConfig // accountsConfig;
258    in tomlFormat.generate "himalaya-config.toml" allConfig;
259
260    systemd.user.services = { }
261      // mkServiceConfig "notify" "Himalaya new emails notifier service"
262      // mkServiceConfig "watch" "Himalaya folder changes watcher service";
263
264    # TODO: remove me for the next release
265    warnings = (lib.optional ("backend" ? himalaya && !isNull himalaya.backend)
266      "Specifying 'accounts.email.accounts.*.himalaya.backend' is deprecated, set 'accounts.email.accounts.*.himalaya.settings.backend' instead")
267      ++ (lib.optional ("sender" ? himalaya && !isNull himalaya.sender)
268        "Specifying 'accounts.email.accounts.*.himalaya.sender' is deprecated, set 'accounts.email.accounts.*.himalaya.settings.sender' instead.");
269  };
270}