fork/home-manager

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

Franz Pletzneomutt: fix STARTTLS408ba131

master
11.2 KiB395 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6  cfg = config.programs.neomutt;
7
8  neomuttAccounts =
9    filter (a: a.neomutt.enable) (attrValues config.accounts.email.accounts);
10
11  sidebarModule = types.submodule {
12    options = {
13      enable = mkEnableOption "sidebar support";
14
15      width = mkOption {
16        type = types.int;
17        default = 22;
18        description = "Width of the sidebar";
19      };
20
21      shortPath = mkOption {
22        type = types.bool;
23        default = true;
24        description = ''
25          By default sidebar shows the full path of the mailbox, but
26          with this enabled only the relative name is shown.
27        '';
28      };
29
30      format = mkOption {
31        type = types.str;
32        default = "%D%?F? [%F]?%* %?N?%N/?%S";
33        description = ''
34          Sidebar format. Check neomutt documentation for details.
35        '';
36      };
37    };
38  };
39
40  sortOptions = [
41    "date"
42    "date-received"
43    "from"
44    "mailbox-order"
45    "score"
46    "size"
47    "spam"
48    "subject"
49    "threads"
50    "to"
51  ];
52
53  bindModule = types.submodule {
54    options = {
55      map = mkOption {
56        type = let
57          menus = [
58            "alias"
59            "attach"
60            "browser"
61            "compose"
62            "editor"
63            "generic"
64            "index"
65            "mix"
66            "pager"
67            "pgp"
68            "postpone"
69            "query"
70            "smime"
71          ];
72        in with types; either (enum menus) (listOf (enum menus));
73        default = "index";
74        description = "Select the menu to bind the command to.";
75      };
76
77      key = mkOption {
78        type = types.str;
79        example = "<left>";
80        description = "The key to bind.";
81      };
82
83      action = mkOption {
84        type = types.str;
85        example = "<enter-command>toggle sidebar_visible<enter><refresh>";
86        description = "Specify the action to take.";
87      };
88    };
89  };
90
91  mkNotmuchVirtualboxes = virtualMailboxes:
92    "${concatStringsSep "\n" (map ({ name, query, limit, type }:
93      ''
94        virtual-mailboxes "${name}" "notmuch://?query=${lib.escapeURL query}${
95          optionalString (limit != null) "&limit=${toString limit}"
96        }${optionalString (type != null) "&type=${type}"}"'')
97      virtualMailboxes)}";
98
99  setOption = n: v: if v == null then "unset ${n}" else "set ${n}=${v}";
100  escape = replaceStrings [ "%" ] [ "%25" ];
101
102  accountFilename = account: config.xdg.configHome + "/neomutt/" + account.name;
103
104  genCommonFolderHooks = account:
105    with account; {
106      from = "'${address}'";
107      realname = "'${realName}'";
108      spoolfile = "'+${folders.inbox}'";
109      record = if folders.sent == null then null else "'+${folders.sent}'";
110      postponed = "'+${folders.drafts}'";
111      trash = "'+${folders.trash}'";
112    };
113
114  mtaSection = account:
115    with account;
116    let passCmd = concatStringsSep " " passwordCommand;
117    in if neomutt.sendMailCommand != null then {
118      sendmail = "'${neomutt.sendMailCommand}'";
119    } else
120      let
121        smtpProto =
122          if smtp.tls.enable && !smtp.tls.useStartTls then "smtps" else "smtp";
123        smtpPort = if smtp.port != null then ":${toString smtp.port}" else "";
124        smtpBaseUrl =
125          "${smtpProto}://${escape userName}@${smtp.host}${smtpPort}";
126      in {
127        smtp_url = "'${smtpBaseUrl}'";
128        smtp_pass = ''"`${passCmd}`"'';
129      };
130
131  genMaildirAccountConfig = account:
132    with account;
133    let
134      folderHook = mapAttrsToList setOption (genCommonFolderHooks account
135        // optionalAttrs cfg.changeFolderWhenSourcingAccount {
136          folder = "'${account.maildir.absPath}'";
137        });
138    in ''
139      ${concatStringsSep "\n" folderHook}
140    '';
141
142  registerAccount = account:
143    let
144      mailboxes = if account.neomutt.mailboxName == null then
145        "mailboxes"
146      else
147        ''named-mailboxes "${account.neomutt.mailboxName}"'';
148      extraMailboxes = concatMapStringsSep "\n" (extra:
149        if isString extra then
150          ''mailboxes "${account.maildir.absPath}/${extra}"''
151        else if extra.name == null then
152          ''mailboxes "${account.maildir.absPath}/${extra.mailbox}"''
153        else
154          ''
155            named-mailboxes "${extra.name}" "${account.maildir.absPath}/${extra.mailbox}"'')
156        account.neomutt.extraMailboxes;
157    in with account; ''
158      # register account ${name}
159      ${mailboxes} "${maildir.absPath}/${folders.inbox}"
160      ${extraMailboxes}
161      folder-hook ${maildir.absPath}/ " \
162          source ${accountFilename account} "
163    '';
164
165  mraSection = account:
166    with account;
167    if account.maildir != null then
168      genMaildirAccountConfig account
169    else
170      throw "Only maildir is supported at the moment";
171
172  optionsStr = attrs: concatStringsSep "\n" (mapAttrsToList setOption attrs);
173
174  sidebarSection = ''
175    # Sidebar
176    set sidebar_visible = yes
177    set sidebar_short_path = ${lib.hm.booleans.yesNo cfg.sidebar.shortPath}
178    set sidebar_width = ${toString cfg.sidebar.width}
179    set sidebar_format = '${cfg.sidebar.format}'
180  '';
181
182  genBindMapper = bindType:
183    concatMapStringsSep "\n" (bind:
184      ''
185        ${bindType} ${
186          concatStringsSep "," (toList bind.map)
187        } ${bind.key} "${bind.action}"'');
188
189  bindSection = (genBindMapper "bind") cfg.binds;
190
191  macroSection = (genBindMapper "macro") cfg.macros;
192
193  mailCheckSection = ''
194    set mail_check_stats
195    set mail_check_stats_interval = ${toString cfg.checkStatsInterval}
196  '';
197
198  notmuchSection = account:
199    let virtualMailboxes = account.notmuch.neomutt.virtualMailboxes;
200    in with account; ''
201      # notmuch section
202      set nm_default_uri = "notmuch://${config.accounts.email.maildirBasePath}"
203      ${optionalString
204      (notmuch.neomutt.enable && builtins.length virtualMailboxes > 0)
205      (mkNotmuchVirtualboxes virtualMailboxes)}
206    '';
207
208  accountStr = account:
209    with account;
210    let
211      signature = if account.signature.showSignature == "none" then
212        "unset signature"
213      else if account.signature.command != null then
214        ''set signature = "${account.signature.command}|"''
215      else
216        "set signature = ${
217          pkgs.writeText "signature.txt" account.signature.text
218        }";
219    in ''
220      # Generated by Home Manager.
221      set ssl_force_tls = ${
222        lib.hm.booleans.yesNo (smtp.tls.enable || smtp.tls.useStartTls)
223      }
224      set certificate_file=${toString config.accounts.email.certificatesFile}
225
226      # GPG section
227      set crypt_use_gpgme = yes
228      set crypt_autosign = ${lib.hm.booleans.yesNo (gpg.signByDefault or false)}
229      set crypt_opportunistic_encrypt = ${
230        lib.hm.booleans.yesNo (gpg.encryptByDefault or false)
231      }
232      set pgp_use_gpg_agent = yes
233      set mbox_type = ${if maildir != null then "Maildir" else "mbox"}
234      set sort = "${cfg.sort}"
235
236      # MTA section
237      ${optionsStr (mtaSection account)}
238
239      ${optionalString (cfg.checkStatsInterval != null) mailCheckSection}
240
241      ${optionalString cfg.sidebar.enable sidebarSection}
242
243      # MRA section
244      ${mraSection account}
245
246      # Extra configuration
247      ${account.neomutt.extraConfig}
248
249      ${signature}
250    ''
251    + optionalString (account.notmuch.enable && account.notmuch.neomutt.enable)
252    (notmuchSection account);
253
254in {
255  options = {
256    programs.neomutt = {
257      enable = mkEnableOption "the NeoMutt mail client";
258
259      package = mkOption {
260        type = types.package;
261        default = pkgs.neomutt;
262        defaultText = literalExpression "pkgs.neomutt";
263        description = "The neomutt package to use.";
264      };
265
266      sidebar = mkOption {
267        type = sidebarModule;
268        default = { };
269        description = "Options related to the sidebar.";
270      };
271
272      binds = mkOption {
273        type = types.listOf bindModule;
274        default = [ ];
275        description = "List of keybindings.";
276      };
277
278      macros = mkOption {
279        type = types.listOf bindModule;
280        default = [ ];
281        description = "List of macros.";
282      };
283
284      sort = mkOption {
285        # allow users to choose any option from sortOptions, or any option prefixed with "reverse-"
286        type = types.enum
287          (sortOptions ++ (map (option: "reverse-" + option) sortOptions));
288        default = "threads";
289        description = "Sorting method on messages.";
290      };
291
292      vimKeys = mkOption {
293        type = types.bool;
294        default = false;
295        description = "Enable vim-like bindings.";
296      };
297
298      checkStatsInterval = mkOption {
299        type = types.nullOr types.int;
300        default = null;
301        example = 60;
302        description = "Enable and set the interval of automatic mail check.";
303      };
304
305      editor = mkOption {
306        type = types.str;
307        default = "$EDITOR";
308        description = "Select the editor used for writing mail.";
309      };
310
311      settings = mkOption {
312        type = types.attrsOf types.str;
313        default = { };
314        description = "Extra configuration appended to the end.";
315      };
316
317      changeFolderWhenSourcingAccount =
318        mkEnableOption "changing the folder when sourcing an account" // {
319          default = true;
320        };
321
322      extraConfig = mkOption {
323        type = types.lines;
324        default = "";
325        description = "Extra configuration appended to the end.";
326      };
327    };
328
329    accounts.email.accounts = mkOption {
330      type = with types; attrsOf (submodule (import ./neomutt-accounts.nix));
331    };
332  };
333
334  config = mkIf cfg.enable {
335    home.packages = [ cfg.package ];
336    home.file = let
337      rcFile = account: {
338        "${accountFilename account}".text = accountStr account;
339      };
340    in foldl' (a: b: a // b) { } (map rcFile neomuttAccounts);
341
342    xdg.configFile."neomutt/neomuttrc" = mkIf (neomuttAccounts != [ ]) {
343      text = let
344        # Find the primary account, if it has neomutt enabled;
345        # otherwise use the first neomutt account as primary.
346        primary =
347          head (filter (a: a.primary) neomuttAccounts ++ neomuttAccounts);
348      in ''
349        # Generated by Home Manager.
350        set header_cache = "${config.xdg.cacheHome}/neomutt/headers/"
351        set message_cachedir = "${config.xdg.cacheHome}/neomutt/messages/"
352        set editor = "${cfg.editor}"
353        set implicit_autoview = yes
354
355        alternative_order text/enriched text/plain text
356
357        set delete = yes
358
359        # Binds
360        ${bindSection}
361
362        # Macros
363        ${macroSection}
364
365        ${optionalString cfg.vimKeys
366        "source ${pkgs.neomutt}/share/doc/neomutt/vim-keys/vim-keys.rc"}
367
368        # Register accounts
369        ${concatMapStringsSep "\n" registerAccount neomuttAccounts}
370
371        # Source primary account
372        source ${accountFilename primary}
373
374        # Extra configuration
375        ${optionsStr cfg.settings}
376
377        ${cfg.extraConfig}
378      '';
379    };
380
381    assertions = [{
382      assertion =
383        ((filter (b: (length (toList b.map)) == 0) (cfg.binds ++ cfg.macros))
384          == [ ]);
385      message =
386        "The 'programs.neomutt.(binds|macros).map' list must contain at least one element.";
387    }];
388
389    warnings =
390      let hasOldBinds = binds: (filter (b: !(isList b.map)) binds) != [ ];
391      in mkIf (hasOldBinds (cfg.binds ++ cfg.macros)) [
392        "Specifying 'programs.neomutt.(binds|macros).map' as a string is deprecated, use a list of strings instead. See https://github.com/nix-community/home-manager/pull/1885."
393      ];
394  };
395}