fork/home-manager
git clone https://git.julia.blue/fork/home-manager
1232d0e1
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7cfg = config . programs . mbsync ; 8 9# Accounts for which mbsync is enabled. 10mbsyncAccounts = 11filter ( a :a . mbsync . enable ) ( attrValues config . accounts . . accounts ); 12 13# Given a SINGLE group's channels attribute set, return true if ANY of the channel's 14# patterns use the invalidOption attribute set value name. 15channelInvalidOption = channels :invalidOption : 16any ( c :c ) ( mapAttrsToList ( c :hasAttr invalidOption ) channels ); 17 18# Given a SINGLE account's groups attribute set, return true if ANY of the account's group's channel's patterns use the invalidOption attribute set value name. 19groupInvalidOption = groups :invalidOption : 20any ( g :g ) ( mapAttrsToList ( groupName :groupVals : 21channelInvalidOption groupVals . channels invalidOption ) groups ); 22 23# Given all accounts (ensure that accounts passed in here ARE mbsync-using accounts) 24# return true if ANY of the account's groups' channels' patterns use the 25# invalidOption attribute set value name. 26accountInvalidOption = accounts :invalidOption : 27any ( a :a ) 28( map ( account :groupInvalidOption account . mbsync . groups invalidOption ) 29mbsyncAccounts ); 30 31genTlsConfig = tls : 32{ 33SSLType = if ! tls . enable then 34"None" 35else if tls . useStartTls then 36"STARTTLS" 37else 38"IMAPS" ; 39} // optionalAttrs ( tls . enable && tls . certificatesFile != null ) { 40CertificateFile = toString tls . certificatesFile ; 41}; 42 43imports = [ 44( mkRenamedOptionModule [ "programs" "mbsync" "masterSlaveMapping" ] [ 45"programs" 46"mbsync" 47"nearFarMapping" 48]) 49]; 50 51nearFarMapping = { 52none = "None" ; 53imap = "Far" ; 54maildir = "Near" ; 55both = "Both" ; 56}; 57 58genSection = header :entries : 59let 60escapeValue = escape [ ''"'' ]; 61hasSpace = v :builtins . match ".* .*" v != null ; 62genValue = n :v : 63if isList v then 64concatMapStringsSep " " ( genValue n ) v 65else if isBool v then 66lib . hm . booleans . yesNo v 67else if isInt v then 68toString v 69else if isString v && hasSpace v then 70''" ${ escapeValue v } "'' 71else if isString v then 72v 73else 74let prettyV = lib . generators . toPretty { } v ; 75in throw "mbsync: unexpected value for option ${ n } : ' ${ prettyV } '" ; 76in '' 77${ header } 78${ concatStringsSep "\n" 79( mapAttrsToList ( n : v : " ${ n } ${ genValue n v } " ) entries )} 80'' ; 81 82genAccountConfig = account : 83with account ; 84genSection "IMAPAccount ${ name } " ({ 85Host = imap . host ; 86User = userName ; 87PassCmd = toString passwordCommand ; 88} // genTlsConfig imap . tls 89// optionalAttrs ( imap . port != null ) { Port = toString imap . port ; } 90// mbsync . extraConfig . account ) + "\n" 91+ genSection "IMAPStore ${ name } -remote" 92({ Account = name ; } // mbsync . extraConfig . remote ) + "\n" 93+ genSection "MaildirStore ${ name } -local" ({ 94Inbox = " ${ maildir . absPath } / ${ folders . inbox } " ; 95} // optionalAttrs 96( mbsync . subFolders != "Maildir++" || mbsync . flatten != null ) { 97Path = " ${ maildir . absPath } /" ; 98} // optionalAttrs ( mbsync . flatten == null ) { 99SubFolders = mbsync . subFolders ; 100} // optionalAttrs ( mbsync . flatten != null ) { Flatten = mbsync . flatten ; } 101// mbsync . extraConfig . local ) + "\n" + genChannels account ; 102 103genChannels = account : 104with account ; 105if mbsync . groups == { } then 106genAccountWideChannel account 107else 108genGroupChannelConfig name mbsync . groups + "\n" 109+ genAccountGroups mbsync . groups ; 110 111# Used when no channels are specified for this account. This will create a 112# single channel for the entire account that is then further refined within 113# the Group for synchronization. 114genAccountWideChannel = account : 115with account ; 116genSection "Channel ${ name } " ({ 117Far = ": ${ name } -remote:" ; 118Near = ": ${ name } -local:" ; 119Patterns = mbsync . patterns ; 120Create = nearFarMapping .${ mbsync . create }; 121Remove = nearFarMapping .${ mbsync . remove }; 122Expunge = nearFarMapping .${ mbsync . expunge }; 123SyncState = "*" ; 124} // mbsync . extraConfig . channel ) + "\n" ; 125 126# Given the attr set of groups, return a string of channels that will direct 127# mail to the proper directories, according to the pattern used in channel's 128# "far" pattern definition. 129genGroupChannelConfig = storeName :groups : 130let 131# Given the name of the group this channel is part of and the channel 132# itself, generate the string for the desired configuration. 133genChannelString = groupName :channel : 134let 135escapeValue = escape [ ''\"'' ]; 136hasSpace = v :builtins . match ".* .*" v != null ; 137# Given a list of patterns, will return the string requested. 138# Only prints if the pattern is NOT the empty list, the default. 139genChannelPatterns = patterns : 140if ( length patterns ) != 0 then 141"Pattern " + concatStringsSep " " 142( map ( pat :if hasSpace pat then escapeValue pat else pat ) 143patterns ) + "\n" 144else 145"" ; 146in genSection "Channel ${ groupName } - ${ channel . name } " ({ 147Far = ": ${ storeName } -remote: ${ channel . farPattern } " ; 148Near = ": ${ storeName } -local: ${ channel . nearPattern } " ; 149} // channel . extraConfig ) + genChannelPatterns channel . patterns ; 150# Given the group name, and a attr set of channels within that group, 151# Generate a list of strings for each channels' configuration. 152genChannelStrings = groupName :channels : 153optionals ( channels != { }) 154( mapAttrsToList ( channelName :info :genChannelString groupName info ) 155channels ); 156# Given a group, return a string that configures all the channels within 157# the group. 158genGroupsChannels = group : 159concatStringsSep "\n" ( genChannelStrings group . name group . channels ); 160# Generate all channel configurations for all groups for this account. 161in concatStringsSep "\n" 162( remove "" ( mapAttrsToList ( name :group :genGroupsChannels group ) groups )); 163 164# Given the attr set of groups, return a string which maps channels to groups 165genAccountGroups = groups : 166let 167# Given the name of the group and the attribute set of channels, make 168# make "Channel <grpName>-<chnName>" for each channel to list os strings 169genChannelStrings = groupName :channels : 170mapAttrsToList ( name :info :"Channel ${ groupName } - ${ name } " ) channels ; 171# Take in 1 group, if the group has channels specified, construct the 172# "Group <grpName>" header and each of the channels. 173genGroupChannelString = group : 174flatten ( optionals ( group . channels != { }) ([ "Group ${ group . name } " ] 175++ ( genChannelStrings group . name group . channels ))); 176# Given set of groups, generates list of strings, where each string is one 177# of the groups and its constituent channels. 178genGroupsStrings = mapAttrsToList ( name :info : 179concatStringsSep "\n" ( genGroupChannelString groups .${ name })) groups ; 180# Join all non-empty groups. 181combined = concatStringsSep "\n\n" ( remove "" genGroupsStrings ) + "\n" ; 182in combined ; 183 184genGroupConfig = name :channels : 185let 186genGroupChannel = n :boxes :"Channel ${ n } : ${ concatStringsSep "," boxes } " ; 187in "\n" + concatStringsSep "\n" 188([ "Group ${ name } " ] ++ mapAttrsToList genGroupChannel channels ); 189 190in { 191meta . maintainers = [ maintainers . KarlJoad ]; 192 193options = { 194programs . mbsync = { 195enable = mkEnableOption "mbsync IMAP4 and Maildir mailbox synchronizer" ; 196 197package = mkOption { 198type = types . package ; 199default = pkgs . isync ; 200defaultText = literalExpression "pkgs.isync" ; 201example = literalExpression "pkgs.isync" ; 202description = "The package to use for the mbsync binary." ; 203}; 204 205groups = mkOption { 206type = types . attrsOf ( types . attrsOf ( types . listOf types . str )); 207default = { }; 208example = literalExpression '' 209{ 210inboxes = { 211account1 = [ "Inbox" ]; 212account2 = [ "Inbox" ]; 213}; 214} 215'' ; 216description = '' 217Definition of groups. 218'' ; 219}; 220 221extraConfig = mkOption { 222type = types . lines ; 223default = "" ; 224description = '' 225Extra configuration lines to add to the mbsync configuration. 226'' ; 227}; 228}; 229 230accounts . . accounts = mkOption { 231type = with types ; attrsOf ( submodule ( import ./mbsync-accounts.nix )); 232}; 233}; 234 235config = mkIf cfg . enable ( mkMerge [ 236{ 237assertions = let 238checkAccounts = pred :msg : 239let badAccounts = filter pred mbsyncAccounts ; 240in { 241assertion = badAccounts == [ ]; 242message = "mbsync: ${ msg } for accounts: " 243+ concatMapStringsSep ", " ( a :a . name ) badAccounts ; 244}; 245in [ 246( checkAccounts ( a :a . maildir == null ) "Missing maildir configuration" ) 247( checkAccounts ( a :a . imap == null ) "Missing IMAP configuration" ) 248( checkAccounts ( a :a . passwordCommand == null ) "Missing passwordCommand" ) 249( checkAccounts ( a :a . userName == null ) "Missing username" ) 250]; 251} 252 253( mkIf ( accountInvalidOption mbsyncAccounts "masterPattern" ) { 254warnings = [ 255"mbsync channels no longer use masterPattern. Use farPattern in its place." 256]; 257}) 258 259( mkIf ( accountInvalidOption mbsyncAccounts "slavePattern" ) { 260warnings = [ 261"mbsync channels no longer use slavePattern. Use nearPattern in its place." 262]; 263}) 264 265{ 266home . packages = [ cfg . package ]; 267 268programs . notmuch . new . ignore = [ ".uidvalidity" ".mbsyncstate" ]; 269 270home . file . ".mbsyncrc" . text = let 271accountsConfig = map genAccountConfig mbsyncAccounts ; 272# Only generate this kind of Group configuration if there are ANY accounts 273# that do NOT have a per-account groups/channels option(s) specified. 274groupsConfig = 275if any ( account :account . mbsync . groups == { }) mbsyncAccounts then 276mapAttrsToList genGroupConfig cfg . groups 277else 278[ ]; 279in '' 280# Generated by Home Manager. 281 282'' 283+ concatStringsSep "\n" ( optional ( cfg . extraConfig != "" ) cfg . extraConfig ) 284+ concatStringsSep "\n\n" accountsConfig 285+ concatStringsSep "\n" groupsConfig ; 286 287home . activation = mkIf ( mbsyncAccounts != [ ]) { 288createMaildir = 289hm . dag . entryBetween [ "linkGeneration" ] [ "writeBoundary" ] '' 290$DRY_RUN_CMD mkdir -m700 -p $VERBOSE_ARG ${ 291concatMapStringsSep " " ( a : a . maildir . absPath ) mbsyncAccounts 292} 293'' ; 294}; 295} 296]); 297}