fork/home-manager
git clone https://git.julia.blue/fork/home-manager
75790441
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6cfg = config . programs . mujmap ; 7 8mujmapAccounts = 9filter ( a :a . mujmap . enable ) ( attrValues config . accounts . . accounts ); 10 11missingNotmuchAccounts = map ( a :a . name ) 12( filter ( a :! a . notmuch . enable && a . mujmap . notmuchSetupWarning ) 13mujmapAccounts ); 14 15notmuchConfigHelp = 16map ( name :"accounts.email.accounts. ${ name } .notmuch.enable = true;" ) 17missingNotmuchAccounts ; 18 19settingsFormat = pkgs . formats . toml { }; 20 21filterNull = attrs :attrsets . filterAttrs ( n :v :v != null ) attrs ; 22 23configFile = account : 24let 25settings'' = if ( account . jmap == null ) then 26{ } 27else 28filterNull { 29fqdn = account . jmap . host ; 30session_url = account . jmap . sessionUrl ; 31}; 32 33settings' = settings'' // { 34username = account . userName ; 35password_command = escapeShellArgs account . passwordCommand ; 36} // filterNull account . mujmap . settings ; 37 38settings = if ( hasAttr "fqdn" settings' ) then 39( removeAttrs settings' [ "session_url" ]) 40else 41settings' ; 42in { 43name = " ${ account . maildir . absPath } /mujmap.toml" ; 44value . source = settingsFormat . generate 45"mujmap- ${ lib . replaceStrings [ "@" ] [ "_at_" ] account . address } .toml" 46settings ; 47}; 48 49tagsOpts = { 50lowercase = mkOption { 51type = types . bool ; 52default = false ; 53description = '' 54If true, translate all mailboxes to lowercase names when mapping to notmuch 55tags. 56'' ; 57}; 58 59directory_separator = mkOption { 60type = types . str ; 61default = "/" ; 62example = "." ; 63description = '' 64Directory separator for mapping notmuch tags to maildirs. 65'' ; 66}; 67 68inbox = mkOption { 69type = types . str ; 70default = "inbox" ; 71description = '' 72Tag for notmuch to use for messages stored in the mailbox labeled with the 73`Inbox` name attribute. 74 75If set to an empty string, this mailbox *and its child 76mailboxes* are not synchronized with a tag. 77'' ; 78}; 79 80deleted = mkOption { 81type = types . str ; 82default = "deleted" ; 83description = '' 84Tag for notmuch to use for messages stored in the mailbox labeled with the 85`Trash` name attribute. 86 87If set to an empty string, this mailbox *and its child 88mailboxes* are not synchronized with a tag. 89'' ; 90}; 91 92sent = mkOption { 93type = types . str ; 94default = "sent" ; 95description = '' 96Tag for notmuch to use for messages stored in the mailbox labeled with the 97`Sent` name attribute. 98 99If set to an empty string, this mailbox *and its child 100mailboxes* are not synchronized with a tag. 101'' ; 102}; 103 104spam = mkOption { 105type = types . str ; 106default = "spam" ; 107description = '' 108Tag for notmuch to use for messages stored in the mailbox labeled with the 109`Junk` name attribute and/or with the `$Junk` keyword, 110*except* for messages with the `$NotJunk` keyword. 111 112If set to an empty string, this mailbox, *its child 113mailboxes*, and these keywords are not synchronized with a tag. 114'' ; 115}; 116 117important = mkOption { 118type = types . str ; 119default = "important" ; 120description = '' 121Tag for notmuch to use for messages stored in the mailbox labeled with the 122`Important` name attribute and/or with the `$Important` 123keyword. 124 125If set to an empty string, this mailbox, *its child 126mailboxes*, and these keywords are not synchronized with a tag. 127'' ; 128}; 129 130phishing = mkOption { 131type = types . str ; 132default = "phishing" ; 133description = '' 134Tag for notmuch to use for the IANA `$Phishing` keyword. 135 136If set to an empty string, this keyword is not synchronized with a tag. 137'' ; 138}; 139}; 140 141rootOpts = { 142username = mkOption { 143type = types . nullOr types . str ; 144default = null ; 145example = "alice@example.com" ; 146description = '' 147Username for basic HTTP authentication. 148 149If `null`, defaults to 150[](#opt-accounts.email.accounts._name_.userName). 151'' ; 152}; 153 154password_command = mkOption { 155type = types . nullOr ( types . either types . str ( types . listOf types . str )); 156default = null ; 157apply = p :if isList p then escapeShellArgs p else p ; 158example = "pass alice@example.com" ; 159description = '' 160Shell command which will print a password to stdout for basic HTTP 161authentication. 162 163If `null`, defaults to 164[](#opt-accounts.email.accounts._name_.passwordCommand). 165'' ; 166}; 167 168fqdn = mkOption { 169type = types . nullOr types . str ; 170default = null ; 171example = "example.com" ; 172description = '' 173Fully qualified domain name of the JMAP service. 174 175mujmap looks up the JMAP SRV record for this host to determine the JMAP session 176URL. Mutually exclusive with 177[](#opt-accounts.email.accounts._name_.mujmap.settings.session_url). 178 179If `null`, defaults to 180[](#opt-accounts.email.accounts._name_.jmap.host). 181'' ; 182}; 183 184session_url = mkOption { 185type = types . nullOr types . str ; 186default = null ; 187example = "https://jmap.example.com/.well-known/jmap" ; 188description = '' 189Session URL to connect to. 190 191Mutually exclusive with 192[](#opt-accounts.email.accounts._name_.mujmap.settings.fqdn). 193 194If `null`, defaults to 195[](#opt-accounts.email.accounts._name_.jmap.sessionUrl). 196'' ; 197}; 198 199auto_create_new_mailboxes = mkOption { 200type = types . bool ; 201default = true ; 202description = '' 203Whether to create new mailboxes automatically on the server from notmuch 204tags. 205'' ; 206}; 207 208cache_dir = mkOption { 209type = types . nullOr types . str ; 210default = null ; 211description = '' 212The cache directory in which to store mail files while they are being 213downloaded. The default is operating-system specific. 214'' ; 215}; 216 217tags = mkOption { 218type = types . submodule { 219freeformType = settingsFormat . type ; 220options = tagsOpts ; 221}; 222default = { }; 223description = '' 224Tag configuration. 225 226Beware that there are quirks that require manual consideration if changing the 227values of these files; please see 228<https://github.com/elizagamedev/mujmap/blob/main/mujmap.toml.example> 229for more details. 230'' ; 231}; 232}; 233 234mujmapOpts = { 235enable = mkEnableOption "mujmap JMAP synchronization for notmuch" ; 236 237notmuchSetupWarning = mkOption { 238type = types . bool ; 239default = true ; 240description = '' 241Warn if Notmuch is not also enabled for this account. 242 243This can safely be disabled if {file}`mujmap.toml` is managed 244outside of Home Manager. 245'' ; 246}; 247 248settings = mkOption { 249type = types . submodule { 250freeformType = settingsFormat . type ; 251options = rootOpts ; 252}; 253default = { }; 254description = '' 255Settings which are applied to {file}`mujmap.toml` 256for the account. 257 258See the [mujmap project](https://github.com/elizagamedev/mujmap) 259for documentation of settings not explicitly covered by this module. 260'' ; 261}; 262}; 263 264mujmapModule = types . submodule { options = { mujmap = mujmapOpts ; }; }; 265in { 266meta . maintainers = with maintainers ; [ elizagamedev ]; 267 268options = { 269programs . mujmap = { 270enable = mkEnableOption "mujmap Gmail synchronization for notmuch" ; 271 272package = mkOption { 273type = types . package ; 274default = pkgs . mujmap ; 275defaultText = "pkgs.mujmap" ; 276description = '' 277mujmap package to use. 278'' ; 279}; 280}; 281 282accounts . . accounts = 283mkOption { type = with types ; attrsOf mujmapModule ; }; 284}; 285 286config = mkIf cfg . enable ( mkMerge [ 287( mkIf ( missingNotmuchAccounts != [ ]) { 288warnings = [ '' 289mujmap is enabled for the following email accounts, but notmuch is not: 290 291${ concatStringsSep "\n " missingNotmuchAccounts } 292 293Notmuch can be enabled with: 294 295${ concatStringsSep "\n " notmuchConfigHelp } 296 297If you have configured notmuch outside of Home Manager, you can suppress this 298warning with: 299 300programs.mujmap.notmuchSetupWarning = false; 301'' ]; 302}) 303 304{ 305warnings = flatten ( map ( account :account . warnings ) mujmapAccounts ); 306 307home . packages = [ cfg . package ]; 308 309# Notmuch should ignore non-mail files created by mujmap. 310programs . notmuch . new . ignore = [ "/.*[.](toml|json|lock)$/" ]; 311 312home . file = listToAttrs ( map configFile mujmapAccounts ); 313} 314]); 315}