fork/home-manager
git clone https://git.julia.blue/fork/home-manager
886ea1d2
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7cfg = config . accounts . ; 8 9gpgModule = types . submodule { 10options = { 11key = mkOption { 12type = types . str ; 13description = '' 14The key to use as listed in {command}`gpg --list-keys`. 15'' ; 16}; 17 18signByDefault = mkOption { 19type = types . bool ; 20default = false ; 21description = "Sign messages by default." ; 22}; 23 24encryptByDefault = mkOption { 25type = types . bool ; 26default = false ; 27description = "Encrypt outgoing messages by default." ; 28}; 29}; 30}; 31 32signatureModule = types . submodule { 33options = { 34text = mkOption { 35type = types . str ; 36default = "" ; 37example = '' 38-- 39Luke Skywalker 40May the force be with you. 41'' ; 42description = '' 43Signature content. 44'' ; 45}; 46 47delimiter = mkOption { 48type = types . str ; 49default = '' 50-- 51'' ; 52example = literalExpression '' 53~*~*~*~*~*~*~*~*~*~*~*~ 54'' ; 55description = '' 56The delimiter used between the document and the signature. 57'' ; 58}; 59 60command = mkOption { 61type = with types ; nullOr path ; 62default = null ; 63example = literalExpression '' 64pkgs.writeScript "signature" "echo This is my signature" 65'' ; 66description = "A command that generates a signature." ; 67}; 68 69showSignature = mkOption { 70type = types . enum [ "append" "attach" "none" ]; 71default = "none" ; 72description = "Method to communicate the signature." ; 73}; 74}; 75}; 76 77tlsModule = types . submodule { 78options = { 79enable = mkOption { 80type = types . bool ; 81default = true ; 82description = '' 83Whether to enable TLS/SSL. 84'' ; 85}; 86 87useStartTls = mkOption { 88type = types . bool ; 89default = false ; 90description = '' 91Whether to use STARTTLS. 92'' ; 93}; 94 95certificatesFile = mkOption { 96type = types . nullOr types . path ; 97default = config . accounts . . certificatesFile ; 98defaultText = "config.accounts.email.certificatesFile" ; 99description = '' 100Path to file containing certificate authorities that should 101be used to validate the connection authenticity. If 102`null` then the system default is used. 103Note, if set then the system default may still be accepted. 104'' ; 105}; 106}; 107}; 108 109imapModule = types . submodule { 110options = { 111host = mkOption { 112type = types . str ; 113example = "imap.example.org" ; 114description = '' 115Hostname of IMAP server. 116'' ; 117}; 118 119port = mkOption { 120type = types . nullOr types . port ; 121default = null ; 122example = 993 ; 123description = '' 124The port on which the IMAP server listens. If 125`null` then the default port is used. 126'' ; 127}; 128 129tls = mkOption { 130type = tlsModule ; 131default = { }; 132description = '' 133Configuration for secure connections. 134'' ; 135}; 136}; 137}; 138 139jmapModule = types . submodule { 140options = { 141host = mkOption { 142type = types . nullOr types . str ; 143default = null ; 144example = "jmap.example.org" ; 145description = '' 146Hostname of JMAP server. 147 148If both this option and [](#opt-accounts.email.accounts._name_.jmap.sessionUrl) are specified, 149`host` is preferred by applications when establishing a 150session. 151'' ; 152}; 153 154sessionUrl = mkOption { 155type = types . nullOr types . str ; 156default = null ; 157example = "https://jmap.example.org:443/.well-known/jmap" ; 158description = '' 159URL for the JMAP Session resource. 160 161If both this option and [](#opt-accounts.email.accounts._name_.jmap.host) are specified, 162`host` is preferred by applications when establishing a 163session. 164'' ; 165}; 166}; 167}; 168 169smtpModule = types . submodule { 170options = { 171host = mkOption { 172type = types . str ; 173example = "smtp.example.org" ; 174description = '' 175Hostname of SMTP server. 176'' ; 177}; 178 179port = mkOption { 180type = types . nullOr types . port ; 181default = null ; 182example = 465 ; 183description = '' 184The port on which the SMTP server listens. If 185`null` then the default port is used. 186'' ; 187}; 188 189tls = mkOption { 190type = tlsModule ; 191default = { }; 192description = '' 193Configuration for secure connections. 194'' ; 195}; 196}; 197}; 198 199maildirModule = types . submodule ({ config , ...} :{ 200options = { 201path = mkOption { 202type = types . str ; 203description = '' 204Path to maildir directory where mail for this account is 205stored. This is relative to the base maildir path. 206'' ; 207}; 208 209absPath = mkOption { 210type = types . path ; 211readOnly = true ; 212internal = true ; 213default = " ${ cfg . maildirBasePath } / ${ config . path } " ; 214description = '' 215A convenience option whose value is the absolute path of 216this maildir. 217'' ; 218}; 219}; 220}); 221 222mailAccountOpts = { name , config , ...} :{ 223options = { 224name = mkOption { 225type = types . str ; 226readOnly = true ; 227description = '' 228Unique identifier of the account. This is set to the 229attribute name of the account configuration. 230'' ; 231}; 232 233primary = mkOption { 234type = types . bool ; 235default = false ; 236description = '' 237Whether this is the primary account. Only one account may be 238set as primary. 239'' ; 240}; 241 242flavor = mkOption { 243type = types . enum [ 244"plain" 245"gmail.com" 246"runbox.com" 247"fastmail.com" 248"yandex.com" 249"outlook.office365.com" 250]; 251default = "plain" ; 252description = '' 253Some email providers have peculiar behavior that require 254special treatment. This option is therefore intended to 255indicate the nature of the provider. 256 257When this indicates a specific provider then, for example, 258the IMAP, SMTP, and JMAP server configuration may be set 259automatically. 260'' ; 261}; 262 263address = mkOption { 264type = types . strMatching ".*@.*" ; 265example = "jane.doe@example.org" ; 266description = "The email address of this account." ; 267}; 268 269aliases = mkOption { 270type = types . listOf ( types . strMatching ".*@.*" ); 271default = [ ]; 272example = [ "webmaster@example.org" "admin@example.org" ]; 273description = "Alternative email addresses of this account." ; 274}; 275 276realName = mkOption { 277type = types . str ; 278example = "Jane Doe" ; 279description = "Name displayed when sending mails." ; 280}; 281 282userName = mkOption { 283type = types . nullOr types . str ; 284default = null ; 285description = '' 286The server username of this account. This will be used as 287the SMTP, IMAP, and JMAP user name. 288'' ; 289}; 290 291passwordCommand = mkOption { 292type = types . nullOr ( types . either types . str ( types . listOf types . str )); 293default = null ; 294apply = p :if isString p then splitString " " p else p ; 295example = "secret-tool lookup email me@example.org" ; 296description = '' 297A command, which when run writes the account password on 298standard output. 299'' ; 300}; 301 302folders = mkOption { 303type = types . submodule { 304options = { 305inbox = mkOption { 306type = types . str ; 307default = "Inbox" ; 308description = '' 309Relative path of the inbox mail. 310'' ; 311}; 312 313sent = mkOption { 314type = types . nullOr types . str ; 315default = "Sent" ; 316description = '' 317Relative path of the sent mail folder. 318'' ; 319}; 320 321drafts = mkOption { 322type = types . nullOr types . str ; 323default = "Drafts" ; 324description = '' 325Relative path of the drafts mail folder. 326'' ; 327}; 328 329trash = mkOption { 330type = types . str ; 331default = "Trash" ; 332description = '' 333Relative path of the deleted mail folder. 334'' ; 335}; 336}; 337}; 338default = { }; 339description = '' 340Standard email folders. 341'' ; 342}; 343 344imap = mkOption { 345type = types . nullOr imapModule ; 346default = null ; 347description = '' 348The IMAP configuration to use for this account. 349'' ; 350}; 351 352jmap = mkOption { 353type = types . nullOr jmapModule ; 354default = null ; 355description = '' 356The JMAP configuration to use for this account. 357'' ; 358}; 359 360signature = mkOption { 361type = signatureModule ; 362default = { }; 363description = '' 364Signature configuration. 365'' ; 366}; 367 368gpg = mkOption { 369type = types . nullOr gpgModule ; 370default = null ; 371description = '' 372GPG configuration. 373'' ; 374}; 375 376smtp = mkOption { 377type = types . nullOr smtpModule ; 378default = null ; 379description = '' 380The SMTP configuration to use for this account. 381'' ; 382}; 383 384maildir = mkOption { 385type = types . nullOr maildirModule ; 386defaultText = { path = "\${name}" ; }; 387description = '' 388Maildir configuration for this account. 389'' ; 390}; 391}; 392 393config = mkMerge [ 394{ 395name = name ; 396maildir = mkOptionDefault { path = " ${ name } " ; }; 397} 398 399( mkIf ( config . flavor == "yandex.com" ) { 400userName = mkDefault config . address ; 401 402imap = { 403host = "imap.yandex.com" ; 404port = 993 ; 405tls . enable = true ; 406}; 407 408smtp = { 409host = "smtp.yandex.com" ; 410port = 465 ; 411tls . enable = true ; 412}; 413}) 414 415( mkIf ( config . flavor == "outlook.office365.com" ) { 416userName = mkDefault config . address ; 417 418imap = { 419host = "outlook.office365.com" ; 420port = 993 ; 421tls . enable = true ; 422}; 423 424smtp = { 425host = "smtp.office365.com" ; 426port = 587 ; 427tls = { 428enable = true ; 429useStartTls = true ; 430}; 431}; 432}) 433 434( mkIf ( config . flavor == "fastmail.com" ) { 435userName = mkDefault config . address ; 436 437imap = { 438host = "imap.fastmail.com" ; 439port = 993 ; 440}; 441 442smtp = { 443host = "smtp.fastmail.com" ; 444port = if config . smtp . tls . useStartTls then 587 else 465 ; 445}; 446 447jmap = { 448host = "fastmail.com" ; 449sessionUrl = "https://jmap.fastmail.com/.well-known/jmap" ; 450}; 451}) 452 453( mkIf ( config . flavor == "gmail.com" ) { 454userName = mkDefault config . address ; 455 456imap = { 457host = "imap.gmail.com" ; 458port = 993 ; 459}; 460 461smtp = { 462host = "smtp.gmail.com" ; 463port = if config . smtp . tls . useStartTls then 587 else 465 ; 464}; 465}) 466 467( mkIf ( config . flavor == "runbox.com" ) { 468imap = { 469host = "mail.runbox.com" ; 470port = 993 ; 471}; 472 473smtp = { 474host = "mail.runbox.com" ; 475port = if config . smtp . tls . useStartTls then 587 else 465 ; 476}; 477}) 478]; 479}; 480 481in { 482options . accounts . = { 483certificatesFile = mkOption { 484type = types . nullOr types . path ; 485default = "/etc/ssl/certs/ca-certificates.crt" ; 486description = '' 487Path to default file containing certificate authorities that 488should be used to validate the connection authenticity. This 489path may be overridden on a per-account basis. 490'' ; 491}; 492 493maildirBasePath = mkOption { 494type = types . str ; 495default = " ${ config . home . homeDirectory } /Maildir" ; 496defaultText = "$HOME/Maildir" ; 497apply = p : 498if hasPrefix "/" p then p else " ${ config . home . homeDirectory } / ${ p } " ; 499description = '' 500The base directory for account maildir directories. May be a 501relative path, in which case it is relative the home 502directory. 503'' ; 504}; 505 506accounts = mkOption { 507type = types . attrsOf ( types . submodule mailAccountOpts ); 508default = { }; 509description = "List of email accounts." ; 510}; 511}; 512 513config = mkIf ( cfg . accounts != { }) { 514assertions = [ 515( let 516primaries = 517catAttrs "name" ( filter ( a :a . primary ) ( attrValues cfg . accounts )); 518in { 519assertion = length primaries == 1 ; 520message = "Must have exactly one primary mail account but found " 521+ toString ( length primaries ) + optionalString ( length primaries > 1 ) 522( ", namely " + concatStringsSep ", " primaries ); 523}) 524]; 525}; 526}