fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
7.6 KiB293 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6  cfg = config.programs.lieer;
7
8  lieerAccounts =
9    filter (a: a.lieer.enable) (attrValues config.accounts.email.accounts);
10
11  nonGmailAccounts =
12    map (a: a.name) (filter (a: a.flavor != "gmail.com") lieerAccounts);
13
14  nonGmailConfigHelp =
15    map (name: ''accounts.email.accounts.${name}.flavor = "gmail.com";'')
16    nonGmailAccounts;
17
18  missingNotmuchAccounts = map (a: a.name)
19    (filter (a: !a.notmuch.enable && a.lieer.notmuchSetupWarning)
20      lieerAccounts);
21
22  notmuchConfigHelp =
23    map (name: "accounts.email.accounts.${name}.notmuch.enable = true;")
24    missingNotmuchAccounts;
25
26  settingsFormat = pkgs.formats.json { };
27
28  configFile = account: {
29    name = "${account.maildir.absPath}/.gmailieer.json";
30    value.source = settingsFormat.generate "lieer-${account.address}.json"
31      ({ account = account.address; } // account.lieer.settings);
32  };
33
34  settingsOpts = {
35    drop_non_existing_label = mkOption {
36      type = types.bool;
37      default = false;
38      description = ''
39        Allow missing labels on the Gmail side to be dropped.
40      '';
41    };
42
43    file_extension = mkOption {
44      type = types.str;
45      default = "";
46      example = "mbox";
47      description = ''
48        Extension to include in local file names, which can be useful
49        for indexing with third-party programs.
50      '';
51    };
52
53    ignore_empty_history = mkOption {
54      type = types.bool;
55      default = false;
56      description = ''
57        Work around a Gmail API quirk where an empty change history
58        is sometimes returned.
59
60        See this
61        [GitHub issue](https://github.com/gauteh/lieer/issues/120)
62        for more details.
63      '';
64    };
65
66    ignore_remote_labels = mkOption {
67      type = types.listOf types.str;
68      default = [
69        "CATEGORY_FORUMS"
70        "CATEGORY_PROMOTIONS"
71        "CATEGORY_UPDATES"
72        "CATEGORY_SOCIAL"
73        "CATEGORY_PERSONAL"
74      ];
75      description = ''
76        Set Gmail labels to ignore when syncing from remote labels to
77        local tags (before translations).
78      '';
79    };
80
81    ignore_tags = mkOption {
82      type = types.listOf types.str;
83      default = [ ];
84      description = ''
85        Set labels to ignore when syncing from local tags to
86        remote labels (after translations).
87      '';
88    };
89
90    local_trash_tag = mkOption {
91      type = types.str;
92      default = "trash";
93      description = ''
94        Local tag to which the remote Gmail 'TRASH' label is translated.
95      '';
96    };
97
98    remove_local_messages = mkOption {
99      type = types.bool;
100      default = true;
101      description = ''
102        Remove local messages that have been deleted on the remote.
103      '';
104    };
105
106    replace_slash_with_dot = mkOption {
107      type = types.bool;
108      default = false;
109      description = ''
110        Replace '/' with '.' in Gmail labels.
111      '';
112    };
113
114    timeout = mkOption {
115      type = types.ints.unsigned;
116      default = 600;
117      description = ''
118        HTTP timeout in seconds. 0 means forever or system timeout.
119      '';
120    };
121  };
122
123  syncOpts = {
124    enable = mkEnableOption "lieer synchronization service";
125
126    frequency = mkOption {
127      type = types.str;
128      default = "*:0/5";
129      description = ''
130        How often to synchronize the account.
131
132        This value is passed to the systemd timer configuration as the
133        onCalendar option. See
134        {manpage}`systemd.time(7)`
135        for more information about the format.
136      '';
137    };
138  };
139
140  lieerOpts = {
141    enable = mkEnableOption "lieer Gmail synchronization for notmuch";
142
143    notmuchSetupWarning = mkOption {
144      type = types.bool;
145      default = true;
146      description = ''
147        Warn if Notmuch is not also enabled for this account.
148
149        This can safely be disabled if {command}`notmuch init`
150        has been used to configure this account outside of Home
151        Manager.
152      '';
153    };
154
155    settings = mkOption {
156      type = types.submodule {
157        freeformType = settingsFormat.type;
158        options = settingsOpts;
159      };
160      default = { };
161      description = ''
162        Settings which are applied to {file}`.gmailieer.json`
163        for the account.
164
165        See the [lieer manual](https://github.com/gauteh/lieer/)
166        for documentation of settings not explicitly covered by this module.
167      '';
168    };
169
170    sync = syncOpts;
171  };
172
173  lieerModule = types.submodule {
174    imports = [
175      (mkRenamedOptionModule [ "lieer" "dropNonExistingLabels" ] [
176        "lieer"
177        "settings"
178        "drop_non_existing_label"
179      ])
180      (mkRenamedOptionModule [ "lieer" "ignoreTagsRemote" ] [
181        "lieer"
182        "settings"
183        "ignore_remote_labels"
184      ])
185      (mkRenamedOptionModule [ "lieer" "ignoreTagsLocal" ] [
186        "lieer"
187        "settings"
188        "ignore_tags"
189      ])
190      (mkRenamedOptionModule [ "lieer" "timeout" ] [
191        "lieer"
192        "settings"
193        "timeout"
194      ])
195      (mkRenamedOptionModule [ "lieer" "replaceSlashWithDot" ] [
196        "lieer"
197        "settings"
198        "replace_slash_with_dot"
199      ])
200    ];
201
202    options = {
203      lieer = lieerOpts;
204
205      warnings = mkOption {
206        type = types.listOf types.str;
207        default = [ ];
208        internal = true;
209        visible = false;
210      };
211    };
212  };
213
214  renamedOptions = account:
215    let prefix = [ "accounts" "email" "accounts" account.name "lieer" ];
216    in [
217      (mkRenamedOptionModule (prefix ++ [ "dropNonExistingLabels" ])
218        (prefix ++ [ "settings" "drop_non_existing_label" ]))
219      (mkRenamedOptionModule (prefix ++ [ "ignoreTagsRemote" ])
220        (prefix ++ [ "settings" "ignore_remote_labels" ]))
221      (mkRenamedOptionModule (prefix ++ [ "ignoreTagsLocal" ])
222        (prefix ++ [ "settings" "ignore_tags" ]))
223      (mkRenamedOptionModule (prefix ++ [ "timeout" ])
224        (prefix ++ [ "settings" "timeout" ]))
225      (mkRenamedOptionModule (prefix ++ [ "replaceSlashWithDot" ])
226        (prefix ++ [ "settings" "replace_slash_with_dot" ]))
227    ];
228
229in {
230  meta.maintainers = [ maintainers.tadfisher ];
231
232  options = {
233    programs.lieer = {
234      enable = mkEnableOption "lieer Gmail synchronization for notmuch";
235
236      package = mkOption {
237        type = types.package;
238        default = pkgs.lieer;
239        defaultText = "pkgs.lieer";
240        description = ''
241          lieer package to use.
242        '';
243      };
244    };
245
246    accounts.email.accounts =
247      mkOption { type = with types; attrsOf lieerModule; };
248  };
249
250  config = mkIf cfg.enable (mkMerge [
251    (mkIf (missingNotmuchAccounts != [ ]) {
252      warnings = [''
253        lieer is enabled for the following email accounts, but notmuch is not:
254
255            ${concatStringsSep "\n    " missingNotmuchAccounts}
256
257        Notmuch can be enabled with:
258
259            ${concatStringsSep "\n    " notmuchConfigHelp}
260
261        If you have configured notmuch outside of Home Manager, you can suppress this
262        warning with:
263
264            programs.lieer.notmuchSetupWarning = false;
265      ''];
266    })
267
268    {
269      assertions = [{
270        assertion = nonGmailAccounts == [ ];
271        message = ''
272          lieer is enabled for non-Gmail accounts:
273
274              ${concatStringsSep "\n    " nonGmailAccounts}
275
276          If these accounts are actually Gmail accounts, you can
277          fix this error with:
278
279              ${concatStringsSep "\n    " nonGmailConfigHelp}
280        '';
281      }];
282
283      warnings = flatten (map (account: account.warnings) lieerAccounts);
284
285      home.packages = [ cfg.package ];
286
287      # Notmuch should ignore non-mail files created by lieer.
288      programs.notmuch.new.ignore = [ "/.*[.](json|lock|bak)$/" ];
289
290      home.file = listToAttrs (map configFile lieerAccounts);
291    }
292  ]);
293}