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