aboutsummaryrefslogtreecommitdiff
path: root/home-manager/modules/programs/neomutt.nix
blob: 85af0353b6c55c9423b7640b0017c51cb8a78a4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.programs.neomutt;

  neomuttAccounts =
    filter (a: a.neomutt.enable) (attrValues config.accounts.email.accounts);

  sidebarModule = types.submodule {
    options = {
      enable = mkEnableOption "sidebar support";

      width = mkOption {
        type = types.int;
        default = 22;
        description = "Width of the sidebar";
      };

      shortPath = mkOption {
        type = types.bool;
        default = true;
        description = ''
          By default sidebar shows the full path of the mailbox, but
          with this enabled only the relative name is shown.
        '';
      };

      format = mkOption {
        type = types.str;
        default = "%B%?F? [%F]?%* %?N?%N/?%S";
        description = ''
          Sidebar format. Check neomutt documentation for details.
        '';
      };
    };
  };

  bindModule = types.submodule {
    options = {
      map = mkOption {
        type = types.enum [
          "alias"
          "attach"
          "browser"
          "compose"
          "editor"
          "generic"
          "index"
          "mix"
          "pager"
          "pgp"
          "postpone"
          "query"
          "smime"
        ];
        default = "index";
        description = "Select the menu to bind the command to.";
      };

      key = mkOption {
        type = types.str;
        example = "<left>";
        description = "The key to bind.";
      };

      action = mkOption {
        type = types.str;
        example = "<enter-command>toggle sidebar_visible<enter><refresh>";
        description = "Specify the action to take.";
      };
    };
  };

  yesno = x: if x then "yes" else "no";
  setOption = n: v: if v == null then "unset ${n}" else "set ${n}=${v}";
  escape = replaceStrings [ "%" ] [ "%25" ];

  accountFilename = account: config.xdg.configHome + "/neomutt/" + account.name;

  genCommonFolderHooks = account:
    with account; {
      from = "'${address}'";
      realname = "'${realName}'";
      spoolfile = "'+${folders.inbox}'";
      record = if folders.sent == null then null else "'+${folders.sent}'";
      postponed = "'+${folders.drafts}'";
      trash = "'+${folders.trash}'";
    };

  mtaSection = account:
    with account;
    let passCmd = concatStringsSep " " passwordCommand;
    in if neomutt.sendMailCommand != null then {
      sendmail = "'${neomutt.sendMailCommand}'";
    } else
      let
        smtpProto = if smtp.tls.enable then "smtps" else "smtp";
        smtpBaseUrl = "${smtpProto}://${escape userName}@${smtp.host}";
      in {
        smtp_url = "'${smtpBaseUrl}'";
        smtp_pass = "'`${passCmd}`'";
      };

  genMaildirAccountConfig = account:
    with account;
    let
      folderHook = mapAttrsToList setOption (genCommonFolderHooks account // {
        folder = "'${account.maildir.absPath}'";
      }) ++ optional (neomutt.extraConfig != "") neomutt.extraConfig;
    in ''
      ${concatStringsSep "\n" folderHook}
    '';

  registerAccount = account:
    with account; ''
      # register account ${name}
      mailboxes "${account.maildir.absPath}/${folders.inbox}"
      folder-hook ${account.maildir.absPath}/ " \
          source ${accountFilename account} "
    '';

  mraSection = account:
    with account;
    if account.maildir != null then
      genMaildirAccountConfig account
    else
      throw "Only maildir is supported at the moment";

  optionsStr = attrs: concatStringsSep "\n" (mapAttrsToList setOption attrs);

  sidebarSection = ''
    # Sidebar
    set sidebar_visible = yes
    set sidebar_short_path = ${yesno cfg.sidebar.shortPath}
    set sidebar_width = ${toString cfg.sidebar.width}
    set sidebar_format = '${cfg.sidebar.format}'
  '';

  bindSection = concatMapStringsSep "\n"
    (bind: ''bind ${bind.map} ${bind.key} "${bind.action}"'') cfg.binds;

  macroSection = concatMapStringsSep "\n"
    (bind: ''macro ${bind.map} ${bind.key} "${bind.action}"'') cfg.macros;

  mailCheckSection = ''
    set mail_check_stats
    set mail_check_stats_interval = ${toString cfg.checkStatsInterval}
  '';

  notmuchSection = account:
    with account; ''
      # notmuch section
      set nm_default_uri = "notmuch://${config.accounts.email.maildirBasePath}"
      virtual-mailboxes "My INBOX" "notmuch://?query=tag:inbox"
    '';

  accountStr = account:
    with account;
    ''
      # Generated by Home Manager.
      set ssl_force_tls = yes
      set certificate_file=${config.accounts.email.certificatesFile}

      # GPG section
      set crypt_use_gpgme = yes
      set crypt_autosign = ${yesno (gpg.signByDefault or false)}
      set pgp_use_gpg_agent = yes
      set mbox_type = ${if maildir != null then "Maildir" else "mbox"}
      set sort = "${cfg.sort}"

      # MTA section
      ${optionsStr (mtaSection account)}

      ${optionalString (cfg.checkStatsInterval != null) mailCheckSection}

      ${optionalString cfg.sidebar.enable sidebarSection}

      # MRA section
      ${mraSection account}

      # Extra configuration
      ${account.neomutt.extraConfig}
    '' + optionalString (account.signature.showSignature != "none") ''
      set signature = ${pkgs.writeText "signature.txt" account.signature.text}
    '' + optionalString account.notmuch.enable (notmuchSection account);

in {
  options = {
    programs.neomutt = {
      enable = mkEnableOption "the NeoMutt mail client";

      sidebar = mkOption {
        type = sidebarModule;
        default = { };
        description = "Options related to the sidebar.";
      };

      binds = mkOption {
        type = types.listOf bindModule;
        default = [ ];
        description = "List of keybindings.";
      };

      macros = mkOption {
        type = types.listOf bindModule;
        default = [ ];
        description = "List of macros.";
      };

      sort = mkOption {
        type = types.enum [
          "date"
          "date-received"
          "from"
          "mailbox-order"
          "score"
          "size"
          "spam"
          "subject"
          "threads"
          "to"
        ];
        default = "threads";
        description = "Sorting method on messages.";
      };

      vimKeys = mkOption {
        type = types.bool;
        default = false;
        description = "Enable vim-like bindings.";
      };

      checkStatsInterval = mkOption {
        type = types.nullOr types.int;
        default = null;
        example = 60;
        description = "Enable and set the interval of automatic mail check.";
      };

      editor = mkOption {
        type = types.str;
        default = "$EDITOR";
        description = "Select the editor used for writing mail.";
      };

      settings = mkOption {
        type = types.attrsOf types.str;
        default = { };
        description = "Extra configuration appended to the end.";
      };

      extraConfig = mkOption {
        type = types.lines;
        default = "";
        description = "Extra configuration appended to the end.";
      };
    };
  };

  config = mkIf cfg.enable {
    home.packages = [ pkgs.neomutt ];
    home.file = let
      rcFile = account: {
        "${accountFilename account}".text = accountStr account;
      };
    in foldl' (a: b: a // b) { } (map rcFile neomuttAccounts);

    xdg.configFile."neomutt/neomuttrc" = mkIf (neomuttAccounts != [ ]) {
      text = let primary = filter (a: a.primary) neomuttAccounts;
      in ''
        # Generated by Home Manager.
        set header_cache = "${config.xdg.cacheHome}/neomutt/headers/"
        set message_cachedir = "${config.xdg.cacheHome}/neomutt/messages/"
        set editor = "${cfg.editor}"
        set implicit_autoview = yes

        alternative_order text/enriched text/plain text

        set delete = yes

        # Binds
        ${bindSection}

        # Macros
        ${macroSection}

        ${optionalString cfg.vimKeys
        "source ${pkgs.neomutt}/share/doc/neomutt/vim-keys/vim-keys.rc"}

        # Extra configuration
        ${optionsStr cfg.settings}

        ${cfg.extraConfig}
      '' + concatMapStringsSep "\n" registerAccount neomuttAccounts +
      # source primary account
      "source ${accountFilename (builtins.head primary)}";
    };
  };
}