fork/home-manager

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

Damien Cassouaerc: cleanup unused bindings35cbed7a

master
6.9 KiB233 linesraw
1{ config, lib, confSections, confSection, ... }:
2
3with lib;
4
5let
6  mapAttrNames = f: attr:
7    listToAttrs (attrValues (mapAttrs (k: v: {
8      name = f k;
9      value = v;
10    }) attr));
11
12  addAccountName = name: k: "${k}:account=${name}";
13
14  oauth2Params = mkOption {
15    type = with types;
16      nullOr (submodule {
17        options = {
18          token_endpoint = mkOption {
19            type = nullOr str;
20            default = null;
21            description = "The OAuth2 token endpoint.";
22          };
23          client_id = mkOption {
24            type = nullOr str;
25            default = null;
26            description = "The OAuth2 client identifier.";
27          };
28          client_secret = mkOption {
29            type = nullOr str;
30            default = null;
31            description = "The OAuth2 client secret.";
32          };
33          scope = mkOption {
34            type = nullOr str;
35            default = null;
36            description = "The OAuth2 requested scope.";
37          };
38        };
39      });
40    default = null;
41    example = { token_endpoint = "<token_endpoint>"; };
42    description = ''
43      Sets the oauth2 params if authentication mechanism oauthbearer or
44      xoauth2 is used.
45      See {manpage}`aerc-imap(5)`.
46    '';
47  };
48
49in {
50  type = mkOption {
51    type = types.attrsOf (types.submodule {
52      options.aerc = {
53        enable = mkEnableOption "aerc";
54        extraAccounts = mkOption {
55          type = confSection;
56          default = { };
57          example =
58            literalExpression ''{ source = "maildir://~/Maildir/example"; }'';
59          description = ''
60            Extra config added to the configuration section for this account in
61            {file}`$HOME/.config/aerc/accounts.conf`.
62            See {manpage}`aerc-accounts(5)`.
63          '';
64        };
65
66        extraBinds = mkOption {
67          type = confSections;
68          default = { };
69          example = literalExpression
70            ''{ messages = { d = ":move ''${folder.trash}<Enter>"; }; }'';
71          description = ''
72            Extra bindings specific to this account, added to
73            {file}`$HOME/.config/aerc/binds.conf`.
74            See {manpage}`aerc-binds(5)`.
75          '';
76        };
77
78        extraConfig = mkOption {
79          type = confSections;
80          default = { };
81          example = literalExpression "{ ui = { sidebar-width = 25; }; }";
82          description = ''
83            Config specific to this account, added to {file}`$HOME/.config/aerc/aerc.conf`.
84            Aerc only supports per-account UI configuration.
85            For other sections of {file}`$HOME/.config/aerc/aerc.conf`,
86            use `programs.aerc.extraConfig`.
87            See {manpage}`aerc-config(5)`.
88          '';
89        };
90
91        imapAuth = mkOption {
92          type = with types; nullOr (enum [ "oauthbearer" "xoauth2" ]);
93          default = null;
94          example = "auth";
95          description = ''
96            Sets the authentication mechanism if imap is used as the incoming
97            method.
98            See {manpage}`aerc-imap(5)`.
99          '';
100        };
101
102        imapOauth2Params = oauth2Params;
103
104        smtpAuth = mkOption {
105          type = with types;
106            nullOr (enum [ "none" "plain" "login" "oauthbearer" "xoauth2" ]);
107          default = "plain";
108          example = "auth";
109          description = ''
110            Sets the authentication mechanism if smtp is used as the outgoing
111            method.
112            See {manpage}`aerc-smtp(5)`.
113          '';
114        };
115
116        smtpOauth2Params = oauth2Params;
117      };
118    });
119  };
120
121  mkAccount = name: account:
122    let
123      nullOrMap = f: v: if v == null then v else f v;
124
125      optPort = port: if port != null then ":${toString port}" else "";
126
127      optAttr = k: v:
128        if v != null && v != [ ] && v != "" then { ${k} = v; } else { };
129
130      optPwCmd = k: p:
131        optAttr "${k}-cred-cmd" (nullOrMap (concatStringsSep " ") p);
132
133      useOauth = auth: builtins.elem auth [ "oauthbearer" "xoauth2" ];
134
135      oauthParams = { auth, params }:
136        if useOauth auth && params != null && params != { } then
137          "?" + builtins.concatStringsSep "&" lib.attrsets.mapAttrsToList
138          (k: v: k + "=" + lib.strings.escapeURL v) params
139        else
140          "";
141
142      mkConfig = {
143        maildir = cfg: {
144          source =
145            "maildir://${config.accounts.email.maildirBasePath}/${cfg.maildir.path}";
146        };
147
148        imap = { userName, imap, passwordCommand, aerc, ... }@cfg:
149          let
150            loginMethod' =
151              if cfg.aerc.imapAuth != null then "+${cfg.aerc.imapAuth}" else "";
152
153            oauthParams' = oauthParams {
154              auth = cfg.aerc.imapAuth;
155              params = cfg.aerc.imapOauth2Params;
156            };
157
158            protocol = if imap.tls.enable then
159              if imap.tls.useStartTls then "imap" else "imaps${loginMethod'}"
160            else
161              "imap+insecure";
162
163            port' = optPort imap.port;
164
165          in {
166            source =
167              "${protocol}://${userName}@${imap.host}${port'}${oauthParams'}";
168          } // optPwCmd "source" passwordCommand;
169
170        smtp = { userName, smtp, passwordCommand, ... }@cfg:
171          let
172            loginMethod' =
173              if cfg.aerc.smtpAuth != null then "+${cfg.aerc.smtpAuth}" else "";
174
175            oauthParams' = oauthParams {
176              auth = cfg.aerc.smtpAuth;
177              params = cfg.aerc.smtpOauth2Params;
178            };
179
180            protocol = if smtp.tls.enable then
181              if smtp.tls.useStartTls then
182                "smtp${loginMethod'}"
183              else
184                "smtps${loginMethod'}"
185            else
186              "smtp+insecure${loginMethod'}";
187
188            port' = optPort smtp.port;
189
190          in {
191            outgoing =
192              "${protocol}://${userName}@${smtp.host}${port'}${oauthParams'}";
193          } // optPwCmd "outgoing" passwordCommand;
194
195        msmtp = cfg: {
196          outgoing = "msmtpq --read-envelope-from --read-recipients";
197        };
198
199      };
200
201      basicCfg = account:
202        {
203          from = "${account.realName} <${account.address}>";
204        } // (optAttr "copy-to" account.folders.sent)
205        // (optAttr "default" account.folders.inbox)
206        // (optAttr "postpone" account.folders.drafts)
207        // (optAttr "aliases" account.aliases);
208
209      sourceCfg = account:
210        if account.mbsync.enable || account.offlineimap.enable then
211          mkConfig.maildir account
212        else if account.imap != null then
213          mkConfig.imap account
214        else
215          { };
216
217      outgoingCfg = account:
218        if account.msmtp.enable then
219          mkConfig.msmtp account
220        else if account.smtp != null then
221          mkConfig.smtp account
222        else
223          { };
224
225    in (basicCfg account) // (sourceCfg account) // (outgoingCfg account)
226    // account.aerc.extraAccounts;
227
228  mkAccountConfig = name: account:
229    mapAttrNames (addAccountName name) account.aerc.extraConfig;
230
231  mkAccountBinds = name: account:
232    mapAttrNames (addAccountName name) account.aerc.extraBinds;
233}