fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
8.6 KiB315 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6  cfg = config.programs.mujmap;
7
8  mujmapAccounts =
9    filter (a: a.mujmap.enable) (attrValues config.accounts.email.accounts);
10
11  missingNotmuchAccounts = map (a: a.name)
12    (filter (a: !a.notmuch.enable && a.mujmap.notmuchSetupWarning)
13      mujmapAccounts);
14
15  notmuchConfigHelp =
16    map (name: "accounts.email.accounts.${name}.notmuch.enable = true;")
17    missingNotmuchAccounts;
18
19  settingsFormat = pkgs.formats.toml { };
20
21  filterNull = attrs: attrsets.filterAttrs (n: v: v != null) attrs;
22
23  configFile = account:
24    let
25      settings'' = if (account.jmap == null) then
26        { }
27      else
28        filterNull {
29          fqdn = account.jmap.host;
30          session_url = account.jmap.sessionUrl;
31        };
32
33      settings' = settings'' // {
34        username = account.userName;
35        password_command = escapeShellArgs account.passwordCommand;
36      } // filterNull account.mujmap.settings;
37
38      settings = if (hasAttr "fqdn" settings') then
39        (removeAttrs settings' [ "session_url" ])
40      else
41        settings';
42    in {
43      name = "${account.maildir.absPath}/mujmap.toml";
44      value.source = settingsFormat.generate
45        "mujmap-${lib.replaceStrings [ "@" ] [ "_at_" ] account.address}.toml"
46        settings;
47    };
48
49  tagsOpts = {
50    lowercase = mkOption {
51      type = types.bool;
52      default = false;
53      description = ''
54        If true, translate all mailboxes to lowercase names when mapping to notmuch
55        tags.
56      '';
57    };
58
59    directory_separator = mkOption {
60      type = types.str;
61      default = "/";
62      example = ".";
63      description = ''
64        Directory separator for mapping notmuch tags to maildirs.
65      '';
66    };
67
68    inbox = mkOption {
69      type = types.str;
70      default = "inbox";
71      description = ''
72        Tag for notmuch to use for messages stored in the mailbox labeled with the
73        `Inbox` name attribute.
74
75        If set to an empty string, this mailbox *and its child
76        mailboxes* are not synchronized with a tag.
77      '';
78    };
79
80    deleted = mkOption {
81      type = types.str;
82      default = "deleted";
83      description = ''
84        Tag for notmuch to use for messages stored in the mailbox labeled with the
85        `Trash` name attribute.
86
87        If set to an empty string, this mailbox *and its child
88        mailboxes* are not synchronized with a tag.
89      '';
90    };
91
92    sent = mkOption {
93      type = types.str;
94      default = "sent";
95      description = ''
96        Tag for notmuch to use for messages stored in the mailbox labeled with the
97        `Sent` name attribute.
98
99        If set to an empty string, this mailbox *and its child
100        mailboxes* are not synchronized with a tag.
101      '';
102    };
103
104    spam = mkOption {
105      type = types.str;
106      default = "spam";
107      description = ''
108        Tag for notmuch to use for messages stored in the mailbox labeled with the
109        `Junk` name attribute and/or with the `$Junk` keyword,
110        *except* for messages with the `$NotJunk` keyword.
111
112        If set to an empty string, this mailbox, *its child
113        mailboxes*, and these keywords are not synchronized with a tag.
114      '';
115    };
116
117    important = mkOption {
118      type = types.str;
119      default = "important";
120      description = ''
121        Tag for notmuch to use for messages stored in the mailbox labeled with the
122        `Important` name attribute and/or with the `$Important`
123        keyword.
124
125        If set to an empty string, this mailbox, *its child
126        mailboxes*, and these keywords are not synchronized with a tag.
127      '';
128    };
129
130    phishing = mkOption {
131      type = types.str;
132      default = "phishing";
133      description = ''
134        Tag for notmuch to use for the IANA `$Phishing` keyword.
135
136        If set to an empty string, this keyword is not synchronized with a tag.
137      '';
138    };
139  };
140
141  rootOpts = {
142    username = mkOption {
143      type = types.nullOr types.str;
144      default = null;
145      example = "alice@example.com";
146      description = ''
147        Username for basic HTTP authentication.
148
149        If `null`, defaults to
150        [](#opt-accounts.email.accounts._name_.userName).
151      '';
152    };
153
154    password_command = mkOption {
155      type = types.nullOr (types.either types.str (types.listOf types.str));
156      default = null;
157      apply = p: if isList p then escapeShellArgs p else p;
158      example = "pass alice@example.com";
159      description = ''
160        Shell command which will print a password to stdout for basic HTTP
161        authentication.
162
163        If `null`, defaults to
164        [](#opt-accounts.email.accounts._name_.passwordCommand).
165      '';
166    };
167
168    fqdn = mkOption {
169      type = types.nullOr types.str;
170      default = null;
171      example = "example.com";
172      description = ''
173        Fully qualified domain name of the JMAP service.
174
175        mujmap looks up the JMAP SRV record for this host to determine the JMAP session
176        URL. Mutually exclusive with
177        [](#opt-accounts.email.accounts._name_.mujmap.settings.session_url).
178
179        If `null`, defaults to
180        [](#opt-accounts.email.accounts._name_.jmap.host).
181      '';
182    };
183
184    session_url = mkOption {
185      type = types.nullOr types.str;
186      default = null;
187      example = "https://jmap.example.com/.well-known/jmap";
188      description = ''
189        Session URL to connect to.
190
191        Mutually exclusive with
192        [](#opt-accounts.email.accounts._name_.mujmap.settings.fqdn).
193
194        If `null`, defaults to
195        [](#opt-accounts.email.accounts._name_.jmap.sessionUrl).
196      '';
197    };
198
199    auto_create_new_mailboxes = mkOption {
200      type = types.bool;
201      default = true;
202      description = ''
203        Whether to create new mailboxes automatically on the server from notmuch
204        tags.
205      '';
206    };
207
208    cache_dir = mkOption {
209      type = types.nullOr types.str;
210      default = null;
211      description = ''
212        The cache directory in which to store mail files while they are being
213        downloaded. The default is operating-system specific.
214      '';
215    };
216
217    tags = mkOption {
218      type = types.submodule {
219        freeformType = settingsFormat.type;
220        options = tagsOpts;
221      };
222      default = { };
223      description = ''
224        Tag configuration.
225
226        Beware that there are quirks that require manual consideration if changing the
227        values of these files; please see
228        <https://github.com/elizagamedev/mujmap/blob/main/mujmap.toml.example>
229        for more details.
230      '';
231    };
232  };
233
234  mujmapOpts = {
235    enable = mkEnableOption "mujmap JMAP synchronization for notmuch";
236
237    notmuchSetupWarning = mkOption {
238      type = types.bool;
239      default = true;
240      description = ''
241        Warn if Notmuch is not also enabled for this account.
242
243        This can safely be disabled if {file}`mujmap.toml` is managed
244        outside of Home Manager.
245      '';
246    };
247
248    settings = mkOption {
249      type = types.submodule {
250        freeformType = settingsFormat.type;
251        options = rootOpts;
252      };
253      default = { };
254      description = ''
255        Settings which are applied to {file}`mujmap.toml`
256        for the account.
257
258        See the [mujmap project](https://github.com/elizagamedev/mujmap)
259        for documentation of settings not explicitly covered by this module.
260      '';
261    };
262  };
263
264  mujmapModule = types.submodule { options = { mujmap = mujmapOpts; }; };
265in {
266  meta.maintainers = with maintainers; [ elizagamedev ];
267
268  options = {
269    programs.mujmap = {
270      enable = mkEnableOption "mujmap Gmail synchronization for notmuch";
271
272      package = mkOption {
273        type = types.package;
274        default = pkgs.mujmap;
275        defaultText = "pkgs.mujmap";
276        description = ''
277          mujmap package to use.
278        '';
279      };
280    };
281
282    accounts.email.accounts =
283      mkOption { type = with types; attrsOf mujmapModule; };
284  };
285
286  config = mkIf cfg.enable (mkMerge [
287    (mkIf (missingNotmuchAccounts != [ ]) {
288      warnings = [''
289        mujmap is enabled for the following email accounts, but notmuch is not:
290
291            ${concatStringsSep "\n    " missingNotmuchAccounts}
292
293        Notmuch can be enabled with:
294
295            ${concatStringsSep "\n    " notmuchConfigHelp}
296
297        If you have configured notmuch outside of Home Manager, you can suppress this
298        warning with:
299
300            programs.mujmap.notmuchSetupWarning = false;
301      ''];
302    })
303
304    {
305      warnings = flatten (map (account: account.warnings) mujmapAccounts);
306
307      home.packages = [ cfg.package ];
308
309      # Notmuch should ignore non-mail files created by mujmap.
310      programs.notmuch.new.ignore = [ "/.*[.](toml|json|lock)$/" ];
311
312      home.file = listToAttrs (map configFile mujmapAccounts);
313    }
314  ]);
315}