fork/home-manager

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

Basile Clémentaccounts.email: fix runbox.com TLS setup (#4408)886ea1d2

master
13.1 KiB526 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.accounts.email;
8
9  gpgModule = types.submodule {
10    options = {
11      key = mkOption {
12        type = types.str;
13        description = ''
14          The key to use as listed in {command}`gpg --list-keys`.
15        '';
16      };
17
18      signByDefault = mkOption {
19        type = types.bool;
20        default = false;
21        description = "Sign messages by default.";
22      };
23
24      encryptByDefault = mkOption {
25        type = types.bool;
26        default = false;
27        description = "Encrypt outgoing messages by default.";
28      };
29    };
30  };
31
32  signatureModule = types.submodule {
33    options = {
34      text = mkOption {
35        type = types.str;
36        default = "";
37        example = ''
38          --
39          Luke Skywalker
40          May the force be with you.
41        '';
42        description = ''
43          Signature content.
44        '';
45      };
46
47      delimiter = mkOption {
48        type = types.str;
49        default = ''
50          --
51        '';
52        example = literalExpression ''
53          ~*~*~*~*~*~*~*~*~*~*~*~
54        '';
55        description = ''
56          The delimiter used between the document and the signature.
57        '';
58      };
59
60      command = mkOption {
61        type = with types; nullOr path;
62        default = null;
63        example = literalExpression ''
64          pkgs.writeScript "signature" "echo This is my signature"
65        '';
66        description = "A command that generates a signature.";
67      };
68
69      showSignature = mkOption {
70        type = types.enum [ "append" "attach" "none" ];
71        default = "none";
72        description = "Method to communicate the signature.";
73      };
74    };
75  };
76
77  tlsModule = types.submodule {
78    options = {
79      enable = mkOption {
80        type = types.bool;
81        default = true;
82        description = ''
83          Whether to enable TLS/SSL.
84        '';
85      };
86
87      useStartTls = mkOption {
88        type = types.bool;
89        default = false;
90        description = ''
91          Whether to use STARTTLS.
92        '';
93      };
94
95      certificatesFile = mkOption {
96        type = types.nullOr types.path;
97        default = config.accounts.email.certificatesFile;
98        defaultText = "config.accounts.email.certificatesFile";
99        description = ''
100          Path to file containing certificate authorities that should
101          be used to validate the connection authenticity. If
102          `null` then the system default is used.
103          Note, if set then the system default may still be accepted.
104        '';
105      };
106    };
107  };
108
109  imapModule = types.submodule {
110    options = {
111      host = mkOption {
112        type = types.str;
113        example = "imap.example.org";
114        description = ''
115          Hostname of IMAP server.
116        '';
117      };
118
119      port = mkOption {
120        type = types.nullOr types.port;
121        default = null;
122        example = 993;
123        description = ''
124          The port on which the IMAP server listens. If
125          `null` then the default port is used.
126        '';
127      };
128
129      tls = mkOption {
130        type = tlsModule;
131        default = { };
132        description = ''
133          Configuration for secure connections.
134        '';
135      };
136    };
137  };
138
139  jmapModule = types.submodule {
140    options = {
141      host = mkOption {
142        type = types.nullOr types.str;
143        default = null;
144        example = "jmap.example.org";
145        description = ''
146          Hostname of JMAP server.
147
148          If both this option and [](#opt-accounts.email.accounts._name_.jmap.sessionUrl) are specified,
149          `host` is preferred by applications when establishing a
150          session.
151        '';
152      };
153
154      sessionUrl = mkOption {
155        type = types.nullOr types.str;
156        default = null;
157        example = "https://jmap.example.org:443/.well-known/jmap";
158        description = ''
159          URL for the JMAP Session resource.
160
161          If both this option and [](#opt-accounts.email.accounts._name_.jmap.host) are specified,
162          `host` is preferred by applications when establishing a
163          session.
164        '';
165      };
166    };
167  };
168
169  smtpModule = types.submodule {
170    options = {
171      host = mkOption {
172        type = types.str;
173        example = "smtp.example.org";
174        description = ''
175          Hostname of SMTP server.
176        '';
177      };
178
179      port = mkOption {
180        type = types.nullOr types.port;
181        default = null;
182        example = 465;
183        description = ''
184          The port on which the SMTP server listens. If
185          `null` then the default port is used.
186        '';
187      };
188
189      tls = mkOption {
190        type = tlsModule;
191        default = { };
192        description = ''
193          Configuration for secure connections.
194        '';
195      };
196    };
197  };
198
199  maildirModule = types.submodule ({ config, ... }: {
200    options = {
201      path = mkOption {
202        type = types.str;
203        description = ''
204          Path to maildir directory where mail for this account is
205          stored. This is relative to the base maildir path.
206        '';
207      };
208
209      absPath = mkOption {
210        type = types.path;
211        readOnly = true;
212        internal = true;
213        default = "${cfg.maildirBasePath}/${config.path}";
214        description = ''
215          A convenience option whose value is the absolute path of
216          this maildir.
217        '';
218      };
219    };
220  });
221
222  mailAccountOpts = { name, config, ... }: {
223    options = {
224      name = mkOption {
225        type = types.str;
226        readOnly = true;
227        description = ''
228          Unique identifier of the account. This is set to the
229          attribute name of the account configuration.
230        '';
231      };
232
233      primary = mkOption {
234        type = types.bool;
235        default = false;
236        description = ''
237          Whether this is the primary account. Only one account may be
238          set as primary.
239        '';
240      };
241
242      flavor = mkOption {
243        type = types.enum [
244          "plain"
245          "gmail.com"
246          "runbox.com"
247          "fastmail.com"
248          "yandex.com"
249          "outlook.office365.com"
250        ];
251        default = "plain";
252        description = ''
253          Some email providers have peculiar behavior that require
254          special treatment. This option is therefore intended to
255          indicate the nature of the provider.
256
257          When this indicates a specific provider then, for example,
258          the IMAP, SMTP, and JMAP server configuration may be set
259          automatically.
260        '';
261      };
262
263      address = mkOption {
264        type = types.strMatching ".*@.*";
265        example = "jane.doe@example.org";
266        description = "The email address of this account.";
267      };
268
269      aliases = mkOption {
270        type = types.listOf (types.strMatching ".*@.*");
271        default = [ ];
272        example = [ "webmaster@example.org" "admin@example.org" ];
273        description = "Alternative email addresses of this account.";
274      };
275
276      realName = mkOption {
277        type = types.str;
278        example = "Jane Doe";
279        description = "Name displayed when sending mails.";
280      };
281
282      userName = mkOption {
283        type = types.nullOr types.str;
284        default = null;
285        description = ''
286          The server username of this account. This will be used as
287          the SMTP, IMAP, and JMAP user name.
288        '';
289      };
290
291      passwordCommand = mkOption {
292        type = types.nullOr (types.either types.str (types.listOf types.str));
293        default = null;
294        apply = p: if isString p then splitString " " p else p;
295        example = "secret-tool lookup email me@example.org";
296        description = ''
297          A command, which when run writes the account password on
298          standard output.
299        '';
300      };
301
302      folders = mkOption {
303        type = types.submodule {
304          options = {
305            inbox = mkOption {
306              type = types.str;
307              default = "Inbox";
308              description = ''
309                Relative path of the inbox mail.
310              '';
311            };
312
313            sent = mkOption {
314              type = types.nullOr types.str;
315              default = "Sent";
316              description = ''
317                Relative path of the sent mail folder.
318              '';
319            };
320
321            drafts = mkOption {
322              type = types.nullOr types.str;
323              default = "Drafts";
324              description = ''
325                Relative path of the drafts mail folder.
326              '';
327            };
328
329            trash = mkOption {
330              type = types.str;
331              default = "Trash";
332              description = ''
333                Relative path of the deleted mail folder.
334              '';
335            };
336          };
337        };
338        default = { };
339        description = ''
340          Standard email folders.
341        '';
342      };
343
344      imap = mkOption {
345        type = types.nullOr imapModule;
346        default = null;
347        description = ''
348          The IMAP configuration to use for this account.
349        '';
350      };
351
352      jmap = mkOption {
353        type = types.nullOr jmapModule;
354        default = null;
355        description = ''
356          The JMAP configuration to use for this account.
357        '';
358      };
359
360      signature = mkOption {
361        type = signatureModule;
362        default = { };
363        description = ''
364          Signature configuration.
365        '';
366      };
367
368      gpg = mkOption {
369        type = types.nullOr gpgModule;
370        default = null;
371        description = ''
372          GPG configuration.
373        '';
374      };
375
376      smtp = mkOption {
377        type = types.nullOr smtpModule;
378        default = null;
379        description = ''
380          The SMTP configuration to use for this account.
381        '';
382      };
383
384      maildir = mkOption {
385        type = types.nullOr maildirModule;
386        defaultText = { path = "\${name}"; };
387        description = ''
388          Maildir configuration for this account.
389        '';
390      };
391    };
392
393    config = mkMerge [
394      {
395        name = name;
396        maildir = mkOptionDefault { path = "${name}"; };
397      }
398
399      (mkIf (config.flavor == "yandex.com") {
400        userName = mkDefault config.address;
401
402        imap = {
403          host = "imap.yandex.com";
404          port = 993;
405          tls.enable = true;
406        };
407
408        smtp = {
409          host = "smtp.yandex.com";
410          port = 465;
411          tls.enable = true;
412        };
413      })
414
415      (mkIf (config.flavor == "outlook.office365.com") {
416        userName = mkDefault config.address;
417
418        imap = {
419          host = "outlook.office365.com";
420          port = 993;
421          tls.enable = true;
422        };
423
424        smtp = {
425          host = "smtp.office365.com";
426          port = 587;
427          tls = {
428            enable = true;
429            useStartTls = true;
430          };
431        };
432      })
433
434      (mkIf (config.flavor == "fastmail.com") {
435        userName = mkDefault config.address;
436
437        imap = {
438          host = "imap.fastmail.com";
439          port = 993;
440        };
441
442        smtp = {
443          host = "smtp.fastmail.com";
444          port = if config.smtp.tls.useStartTls then 587 else 465;
445        };
446
447        jmap = {
448          host = "fastmail.com";
449          sessionUrl = "https://jmap.fastmail.com/.well-known/jmap";
450        };
451      })
452
453      (mkIf (config.flavor == "gmail.com") {
454        userName = mkDefault config.address;
455
456        imap = {
457          host = "imap.gmail.com";
458          port = 993;
459        };
460
461        smtp = {
462          host = "smtp.gmail.com";
463          port = if config.smtp.tls.useStartTls then 587 else 465;
464        };
465      })
466
467      (mkIf (config.flavor == "runbox.com") {
468        imap = {
469          host = "mail.runbox.com";
470          port = 993;
471        };
472
473        smtp = {
474          host = "mail.runbox.com";
475          port = if config.smtp.tls.useStartTls then 587 else 465;
476        };
477      })
478    ];
479  };
480
481in {
482  options.accounts.email = {
483    certificatesFile = mkOption {
484      type = types.nullOr types.path;
485      default = "/etc/ssl/certs/ca-certificates.crt";
486      description = ''
487        Path to default file containing certificate authorities that
488        should be used to validate the connection authenticity. This
489        path may be overridden on a per-account basis.
490      '';
491    };
492
493    maildirBasePath = mkOption {
494      type = types.str;
495      default = "${config.home.homeDirectory}/Maildir";
496      defaultText = "$HOME/Maildir";
497      apply = p:
498        if hasPrefix "/" p then p else "${config.home.homeDirectory}/${p}";
499      description = ''
500        The base directory for account maildir directories. May be a
501        relative path, in which case it is relative the home
502        directory.
503      '';
504    };
505
506    accounts = mkOption {
507      type = types.attrsOf (types.submodule mailAccountOpts);
508      default = { };
509      description = "List of email accounts.";
510    };
511  };
512
513  config = mkIf (cfg.accounts != { }) {
514    assertions = [
515      (let
516        primaries =
517          catAttrs "name" (filter (a: a.primary) (attrValues cfg.accounts));
518      in {
519        assertion = length primaries == 1;
520        message = "Must have exactly one primary mail account but found "
521          + toString (length primaries) + optionalString (length primaries > 1)
522          (", namely " + concatStringsSep ", " primaries);
523      })
524    ];
525  };
526}