fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
7.0 KiB242 linesraw
1# alot config loader is sensitive to leading space !
2{ config, lib, pkgs, ... }:
3
4with lib;
5
6let
7
8  cfg = config.programs.alot;
9
10  enabledAccounts =
11    filter (a: a.notmuch.enable) (attrValues config.accounts.email.accounts);
12
13  # sorted: primary first
14  alotAccounts = sort (a: b: !(a.primary -> b.primary)) enabledAccounts;
15
16  boolStr = v: if v then "True" else "False";
17
18  mkKeyValue = key: value:
19    let value' = if isBool value then boolStr value else toString value;
20    in "${key} = ${value'}";
21
22  mk2ndLevelSectionName = name: "[" + name + "]";
23
24  tagSubmodule = types.submodule {
25    options = {
26      translated = mkOption {
27        type = types.nullOr types.str;
28        description = ''
29          Fixed string representation for this tag. The tag can be
30          hidden from view, if the key translated is set to
31          `""`, the empty string.
32        '';
33      };
34
35      translation = mkOption {
36        type = types.nullOr types.str;
37        default = null;
38        description = ''
39          A pair of strings that define a regular substitution to
40          compute the string representation on the fly using
41          `re.sub`.
42        '';
43      };
44
45      normal = mkOption {
46        type = types.nullOr types.str;
47        default = null;
48        example = "'','', 'white','light red', 'white','#d66'";
49        description = ''
50          How to display the tag when unfocused.
51          See <https://alot.readthedocs.io/en/latest/configuration/theming.html#tagstring-formatting>.
52        '';
53      };
54
55      focus = mkOption {
56        type = types.nullOr types.str;
57        default = null;
58        description = "How to display the tag when focused.";
59      };
60    };
61  };
62
63  accountStr = account:
64    with account;
65    concatStringsSep "\n" ([ "[[${name}]]" ]
66      ++ mapAttrsToList (n: v: n + "=" + v) ({
67        address = address;
68        realname = realName;
69        sendmail_command =
70          optionalString (alot.sendMailCommand != null) alot.sendMailCommand;
71      } // optionalAttrs (folders.sent != null) {
72        sent_box = "maildir" + "://" + maildir.absPath + "/" + folders.sent;
73      } // optionalAttrs (folders.drafts != null) {
74        draft_box = "maildir" + "://" + maildir.absPath + "/" + folders.drafts;
75      } // optionalAttrs (aliases != [ ]) {
76        aliases = concatStringsSep "," aliases;
77      } // optionalAttrs (gpg != null) {
78        gpg_key = gpg.key;
79        encrypt_by_default = if gpg.encryptByDefault then "all" else "none";
80        sign_by_default = boolStr gpg.signByDefault;
81      } // optionalAttrs (signature.showSignature != "none") {
82        signature = pkgs.writeText "signature.txt" signature.text;
83        signature_as_attachment = boolStr (signature.showSignature == "attach");
84      }) ++ [ alot.extraConfig ] ++ [ "[[[abook]]]" ]
85      ++ mapAttrsToList (n: v: n + "=" + v) alot.contactCompletion);
86
87  configFile = let
88    bindingsToStr = attrSet:
89      concatStringsSep "\n" (mapAttrsToList (n: v: "${n} = ${v}") attrSet);
90  in ''
91    # Generated by Home Manager.
92    # See http://alot.readthedocs.io/en/latest/configuration/config_options.html
93
94    ${generators.toKeyValue { inherit mkKeyValue; } cfg.settings}
95    ${cfg.extraConfig}
96    [tags]
97  '' + (let
98    submoduleToAttrs = m:
99      filterAttrs (name: v: name != "_module" && v != null) m;
100  in generators.toINI { mkSectionName = mk2ndLevelSectionName; }
101  (mapAttrs (name: x: submoduleToAttrs x) cfg.tags)) + ''
102    [bindings]
103    ${bindingsToStr cfg.bindings.global}
104
105    [[bufferlist]]
106    ${bindingsToStr cfg.bindings.bufferlist}
107    [[search]]
108    ${bindingsToStr cfg.bindings.search}
109    [[envelope]]
110    ${bindingsToStr cfg.bindings.envelope}
111    [[taglist]]
112    ${bindingsToStr cfg.bindings.taglist}
113    [[thread]]
114    ${bindingsToStr cfg.bindings.thread}
115
116    [accounts]
117
118    ${concatStringsSep "\n\n" (map accountStr alotAccounts)}
119  '';
120
121in {
122  options = {
123    programs.alot = {
124      enable = mkOption {
125        type = types.bool;
126        default = false;
127        example = true;
128        description = ''
129          Whether to enable the Alot mail user agent. Alot uses the
130          Notmuch email system and will therefore be automatically
131          enabled for each email account that is managed by Notmuch.
132        '';
133      };
134
135      hooks = mkOption {
136        type = types.lines;
137        default = "";
138        description = ''
139          Content of the hooks file.
140        '';
141      };
142
143      bindings = mkOption {
144        type = types.submodule {
145          options = {
146            global = mkOption {
147              type = types.attrsOf types.str;
148              default = { };
149              description = "Global keybindings.";
150            };
151
152            bufferlist = mkOption {
153              type = types.attrsOf types.str;
154              default = { };
155              description = "Bufferlist mode keybindings.";
156            };
157
158            search = mkOption {
159              type = types.attrsOf types.str;
160              default = { };
161              description = "Search mode keybindings.";
162            };
163
164            envelope = mkOption {
165              type = types.attrsOf types.str;
166              default = { };
167              description = "Envelope mode keybindings.";
168            };
169
170            taglist = mkOption {
171              type = types.attrsOf types.str;
172              default = { };
173              description = "Taglist mode keybindings.";
174            };
175
176            thread = mkOption {
177              type = types.attrsOf types.str;
178              default = { };
179              description = "Thread mode keybindings.";
180            };
181          };
182        };
183        default = { };
184        description = ''
185          Keybindings.
186        '';
187      };
188
189      tags = mkOption {
190        type = types.attrsOf tagSubmodule;
191        default = { };
192        description = "How to display the tags.";
193      };
194
195      settings = mkOption {
196        type = with types;
197          let primitive = either (either (either str int) bool) float;
198          in attrsOf primitive;
199        default = {
200          initial_command = "search tag:inbox AND NOT tag:killed";
201          auto_remove_unread = true;
202          handle_mouse = true;
203          prefer_plaintext = true;
204        };
205        example = literalExpression ''
206          {
207            auto_remove_unread = true;
208            ask_subject = false;
209            thread_indent_replies = 2;
210          }
211        '';
212        description = ''
213          Configuration options added to alot configuration file.
214        '';
215      };
216
217      extraConfig = mkOption {
218        type = types.lines;
219        default = "";
220        description = ''
221          Extra lines added to alot configuration file.
222        '';
223      };
224    };
225
226    accounts.email.accounts = mkOption {
227      type = with types; attrsOf (submodule (import ./alot-accounts.nix pkgs));
228    };
229  };
230
231  config = mkIf cfg.enable {
232    home.packages = [ pkgs.alot ];
233
234    xdg.configFile."alot/config".text = configFile;
235
236    xdg.configFile."alot/hooks.py" = mkIf (cfg.hooks != "") {
237      text = ''
238        # Generated by Home Manager.
239      '' + cfg.hooks;
240    };
241  };
242}