fork/home-manager
git clone https://git.julia.blue/fork/home-manager
75790441
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6inherit ( pkgs . stdenv . hostPlatform ) isDarwin ; 7 8cfg = config . programs . thunderbird ; 9 10enabledAccounts = attrValues 11( filterAttrs ( _ :a :a . thunderbird . enable ) config . accounts . . accounts ); 12 13enabledAccountsWithId = 14map ( a :a // { id = builtins . hashString "sha256" a . name ; }) enabledAccounts ; 15 16thunderbirdConfigPath = 17if isDarwin then "Library/Thunderbird" else ".thunderbird" ; 18 19thunderbirdProfilesPath = if isDarwin then 20" ${ thunderbirdConfigPath } /Profiles" 21else 22thunderbirdConfigPath ; 23 24profilesWithId = 25imap0 ( i :v :v // { id = toString i ; }) ( attrValues cfg . profiles ); 26 27profilesIni = foldl recursiveUpdate { 28General = { 29StartWithLastProfile = 1 ; 30Version = 2 ; 31}; 32} ( flip map profilesWithId ( profile :{ 33"Profile ${ profile . id } " = { 34Name = profile . name ; 35Path = if isDarwin then "Profiles/ ${ profile . name } " else profile . name ; 36IsRelative = 1 ; 37Default = if profile . isDefault then 1 else 0 ; 38}; 39})); 40 41toThunderbirdIdentity = account :address : 42# For backwards compatibility, the primary address reuses the account ID. 43let 44id = if address == account . address then 45account . id 46else 47builtins . hashString "sha256" address ; 48in { 49"mail.identity.id_ ${ id } .fullName" = account . realName ; 50"mail.identity.id_ ${ id } .useremail" = address ; 51"mail.identity.id_ ${ id } .valid" = true ; 52} // optionalAttrs ( account . gpg != null ) { 53"mail.identity.id_ ${ id } .attachPgpKey" = false ; 54"mail.identity.id_ ${ id } .autoEncryptDrafts" = true ; 55"mail.identity.id_ ${ id } .e2etechpref" = 0 ; 56"mail.identity.id_ ${ id } .encryptionpolicy" = 57if account . gpg . encryptByDefault then 2 else 0 ; 58"mail.identity.id_ ${ id } .is_gnupg_key_id" = true ; 59"mail.identity.id_ ${ id } .last_entered_external_gnupg_key_id" = 60account . gpg . key ; 61"mail.identity.id_ ${ id } .openpgp_key_id" = account . gpg . key ; 62"mail.identity.id_ ${ id } .protectSubject" = true ; 63"mail.identity.id_ ${ id } .sign_mail" = account . gpg . signByDefault ; 64} // account . thunderbird . perIdentitySettings id ; 65 66toThunderbirdAccount = account :profile : 67let 68id = account . id ; 69addresses = [ account . address ] ++ account . aliases ; 70in { 71"mail.account.account_ ${ id } .identities" = concatStringsSep "," 72([ "id_ ${ id } " ] 73++ map ( address :"id_ ${ builtins . hashString "sha256" address } " ) 74account . aliases ); 75"mail.account.account_ ${ id } .server" = "server_ ${ id } " ; 76} // optionalAttrs account . primary { 77"mail.accountmanager.defaultaccount" = "account_ ${ id } " ; 78} // optionalAttrs ( account . imap != null ) { 79"mail.server.server_ ${ id } .directory" = 80" ${ thunderbirdProfilesPath } / ${ profile . name } /ImapMail/ ${ id } " ; 81"mail.server.server_ ${ id } .directory-rel" = "[ProfD]ImapMail/ ${ id } " ; 82"mail.server.server_ ${ id } .hostname" = account . imap . host ; 83"mail.server.server_ ${ id } .login_at_startup" = true ; 84"mail.server.server_ ${ id } .name" = account . name ; 85"mail.server.server_ ${ id } .port" = 86if ( account . imap . port != null ) then account . imap . port else 143 ; 87"mail.server.server_ ${ id } .socketType" = if ! account . imap . tls . enable then 880 89else if account . imap . tls . useStartTls then 902 91else 923 ; 93"mail.server.server_ ${ id } .type" = "imap" ; 94"mail.server.server_ ${ id } .userName" = account . userName ; 95} // optionalAttrs ( account . smtp != null ) { 96"mail.identity.id_ ${ id } .smtpServer" = "smtp_ ${ id } " ; 97"mail.smtpserver.smtp_ ${ id } .authMethod" = 3 ; 98"mail.smtpserver.smtp_ ${ id } .hostname" = account . smtp . host ; 99"mail.smtpserver.smtp_ ${ id } .port" = 100if ( account . smtp . port != null ) then account . smtp . port else 587 ; 101"mail.smtpserver.smtp_ ${ id } .try_ssl" = if ! account . smtp . tls . enable then 1020 103else if account . smtp . tls . useStartTls then 1042 105else 1063 ; 107"mail.smtpserver.smtp_ ${ id } .username" = account . userName ; 108} // optionalAttrs ( account . smtp != null && account . primary ) { 109"mail.smtp.defaultserver" = "smtp_ ${ id } " ; 110} // builtins . foldl' ( a :b :a // b ) { } 111( builtins . map ( address :toThunderbirdIdentity account address ) addresses ) 112// account . thunderbird . settings id ; 113 114mkUserJs = prefs :extraPrefs :'' 115// Generated by Home Manager. 116 117${ concatStrings ( mapAttrsToList ( name : value : '' 118user_pref(" ${ name } ", ${ builtins . toJSON value } ); 119'' ) prefs )} 120${ extraPrefs } 121'' ; 122in { 123meta . maintainers = with hm . maintainers ; [ d-dervishi jkarlson ]; 124 125options = { 126programs . thunderbird = { 127enable = mkEnableOption "Thunderbird" ; 128 129package = mkOption { 130type = types . package ; 131default = pkgs . thunderbird ; 132defaultText = literalExpression "pkgs.thunderbird" ; 133example = literalExpression "pkgs.thunderbird-91" ; 134description = "The Thunderbird package to use." ; 135}; 136 137profiles = mkOption { 138type = with types ; 139attrsOf ( submodule ({ config , name , ...} :{ 140options = { 141name = mkOption { 142type = types . str ; 143default = name ; 144readOnly = true ; 145description = "This profile's name." ; 146}; 147 148isDefault = mkOption { 149type = types . bool ; 150default = false ; 151example = true ; 152description = '' 153Whether this is a default profile. There must be exactly one 154default profile. 155'' ; 156}; 157 158settings = mkOption { 159type = with types ; attrsOf ( oneOf [ bool int str ]); 160default = { }; 161example = literalExpression '' 162{ 163"mail.spellcheck.inline" = false; 164} 165'' ; 166description = '' 167Preferences to add to this profile's 168{file}`user.js`. 169'' ; 170}; 171 172withExternalGnupg = mkOption { 173type = types . bool ; 174default = false ; 175example = true ; 176description = "Allow using external GPG keys with GPGME." ; 177}; 178 179userChrome = mkOption { 180type = types . lines ; 181default = "" ; 182description = "Custom Thunderbird user chrome CSS." ; 183example = '' 184/* Hide tab bar in Thunderbird */ 185#tabs-toolbar { 186visibility: collapse !important; 187} 188'' ; 189}; 190 191userContent = mkOption { 192type = types . lines ; 193default = "" ; 194description = "Custom Thunderbird user content CSS." ; 195example = '' 196/* Hide scrollbar on Thunderbird pages */ 197*{scrollbar-width:none !important} 198'' ; 199}; 200 201extraConfig = mkOption { 202type = types . lines ; 203default = "" ; 204description = '' 205Extra preferences to add to {file}`user.js`. 206'' ; 207}; 208}; 209})); 210description = "Attribute set of Thunderbird profiles." ; 211}; 212 213settings = mkOption { 214type = with types ; attrsOf ( oneOf [ bool int str ]); 215default = { }; 216example = literalExpression '' 217{ 218"general.useragent.override" = ""; 219"privacy.donottrackheader.enabled" = true; 220} 221'' ; 222description = '' 223Attribute set of Thunderbird preferences to be added to 224all profiles. 225'' ; 226}; 227 228darwinSetupWarning = mkOption { 229type = types . bool ; 230default = true ; 231example = false ; 232visible = isDarwin ; 233readOnly = ! isDarwin ; 234description = '' 235Warn to set environment variables before using this module. Only 236relevant on Darwin. 237'' ; 238}; 239}; 240 241accounts . . accounts = mkOption { 242type = with types ; 243attrsOf ( submodule { 244options . thunderbird = { 245enable = 246mkEnableOption "the Thunderbird mail client for this account" ; 247 248profiles = mkOption { 249type = with types ; listOf str ; 250default = [ ]; 251example = literalExpression '' 252[ "profile1" "profile2" ] 253'' ; 254description = '' 255List of Thunderbird profiles for which this account should be 256enabled. If this list is empty (the default), this account will 257be enabled for all declared profiles. 258'' ; 259}; 260 261settings = mkOption { 262type = with types ; functionTo ( attrsOf ( oneOf [ bool int str ])); 263default = _ :{ }; 264defaultText = literalExpression "_: { }" ; 265example = literalExpression '' 266id: { 267"mail.server.server_''${id}.check_new_mail" = false; 268}; 269'' ; 270description = '' 271Extra settings to add to this Thunderbird account configuration. 272The {var}`id` given as argument is an automatically 273generated account identifier. 274'' ; 275}; 276 277perIdentitySettings = mkOption { 278type = with types ; functionTo ( attrsOf ( oneOf [ bool int str ])); 279default = _ :{ }; 280defaultText = literalExpression "_: { }" ; 281example = literalExpression '' 282id: { 283"mail.identity.id_''${id}.protectSubject" = false; 284"mail.identity.id_''${id}.autoEncryptDrafts" = false; 285}; 286'' ; 287description = '' 288Extra settings to add to each identity of this Thunderbird 289account configuration. The {var}`id` given as 290argument is an automatically generated identifier. 291'' ; 292}; 293}; 294}); 295}; 296}; 297 298config = mkIf cfg . enable { 299assertions = [ 300( let defaults = catAttrs "name" ( filter ( a :a . isDefault ) profilesWithId ); 301in { 302assertion = cfg . profiles == { } || length defaults == 1 ; 303message = "Must have exactly one default Thunderbird profile but found " 304+ toString ( length defaults ) + optionalString ( length defaults > 1 ) 305( ", namely " + concatStringsSep "," defaults ); 306}) 307 308( let 309profiles = catAttrs "name" profilesWithId ; 310selectedProfiles = 311concatMap ( a :a . thunderbird . profiles ) enabledAccounts ; 312in { 313assertion = ( intersectLists profiles selectedProfiles ) 314== selectedProfiles ; 315message = "Cannot enable an account for a non-declared profile. " 316+ "The declared profiles are " + ( concatStringsSep "," profiles ) 317+ ", but the used profiles are " 318+ ( concatStringsSep "," selectedProfiles ); 319}) 320]; 321 322warnings = optional ( isDarwin && cfg . darwinSetupWarning ) '' 323Thunderbird packages are not yet supported on Darwin. You can still use 324this module to manage your accounts and profiles by setting 325'programs.thunderbird.package' to a dummy value, for example using 326'pkgs.runCommand'. 327 328Note that this module requires you to set the following environment 329variables when using an installation of Thunderbird that is not provided 330by Nix: 331 332export MOZ_LEGACY_PROFILES=1 333export MOZ_ALLOW_DOWNGRADE=1 334'' ; 335 336home . packages = [ cfg . package ] 337++ optional ( any ( p :p . withExternalGnupg ) ( attrValues cfg . profiles )) 338pkgs . gpgme ; 339 340home . file = mkMerge ([{ 341" ${ thunderbirdConfigPath } /profiles.ini" = 342mkIf ( cfg . profiles != { }) { text = generators . toINI { } profilesIni ; }; 343}] ++ flip mapAttrsToList cfg . profiles ( name :profile :{ 344" ${ thunderbirdProfilesPath } / ${ name } /chrome/userChrome.css" = 345mkIf ( profile . userChrome != "" ) { text = profile . userChrome ; }; 346 347" ${ thunderbirdProfilesPath } / ${ name } /chrome/userContent.css" = 348mkIf ( profile . userContent != "" ) { text = profile . userContent ; }; 349 350" ${ thunderbirdProfilesPath } / ${ name } /user.js" = let 351accounts = filter ( a : 352a . thunderbird . profiles == [ ] 353|| any ( p :p == name ) a . thunderbird . profiles ) enabledAccountsWithId ; 354 355smtp = filter ( a :a . smtp != null ) accounts ; 356in { 357text = mkUserJs ( builtins . foldl' ( a :b :a // b ) { } ([ 358cfg . settings 359 360( optionalAttrs ( length accounts != 0 ) { 361"mail.accountmanager.accounts" = 362concatStringsSep "," ( map ( a :"account_ ${ a . id } " ) accounts ); 363}) 364 365( optionalAttrs ( length smtp != 0 ) { 366"mail.smtpservers" = 367concatStringsSep "," ( map ( a :"smtp_ ${ a . id } " ) smtp ); 368}) 369 370{ "mail.openpgp.allow_external_gnupg" = profile . withExternalGnupg ; } 371 372profile . settings 373] ++ ( map ( a :toThunderbirdAccount a profile ) accounts ))) 374profile . extraConfig ; 375}; 376})); 377}; 378}