fork/home-manager
git clone https://git.julia.blue/fork/home-manager
75790441
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7cfg = config . programs . vdirsyncer ; 8 9vdirsyncerCalendarAccounts = filterAttrs ( _ :v :v . vdirsyncer . enable ) 10( mapAttrs' ( n :v :nameValuePair ( "calendar_" + n ) v ) 11config . accounts . calendar . accounts ); 12 13vdirsyncerContactAccounts = filterAttrs ( _ :v :v . vdirsyncer . enable ) 14( mapAttrs' ( n :v :nameValuePair ( "contacts_" + n ) v ) 15config . accounts . contact . accounts ); 16 17vdirsyncerAccounts = vdirsyncerCalendarAccounts // vdirsyncerContactAccounts ; 18 19wrap = s :''" ${ s } "'' ; 20 21listString = l :"[ ${ concatStringsSep ", " l } ]" ; 22 23boolString = b :if b then "true" else "false" ; 24 25localStorage = a : 26filterAttrs ( _ :v :v != null ) 27(( getAttrs [ "type" "fileExt" "encoding" ] a . local ) // { 28path = a . local . path ; 29postHook = pkgs . writeShellScriptBin "post-hook" a . vdirsyncer . postHook 30+ "/bin/post-hook" ; 31}); 32 33remoteStorage = a : 34filterAttrs ( _ :v :v != null ) (( getAttrs [ 35"type" 36"url" 37"userName" 38#"userNameCommand" 39"passwordCommand" 40] a . remote ) // ( if a . vdirsyncer == null then 41{ } 42else 43getAttrs [ 44"itemTypes" 45"verify" 46"verifyFingerprint" 47"auth" 48"authCert" 49"userAgent" 50"tokenFile" 51"clientIdCommand" 52"clientSecretCommand" 53"timeRange" 54] a . vdirsyncer )); 55 56pair = a : 57with a . vdirsyncer ; 58filterAttrs ( k :v :k == "collections" || ( v != null && v != [ ])) 59( getAttrs [ "collections" "conflictResolution" "metadata" "partialSync" ] 60a . vdirsyncer ); 61 62pairs = mapAttrs ( _ :v :pair v ) vdirsyncerAccounts ; 63localStorages = mapAttrs ( _ :v :localStorage v ) vdirsyncerAccounts ; 64remoteStorages = mapAttrs ( _ :v :remoteStorage v ) vdirsyncerAccounts ; 65 66optionString = n :v : 67if ( n == "type" ) then 68''type = " ${ v } "'' 69else if ( n == "path" ) then 70''path = " ${ v } "'' 71else if ( n == "fileExt" ) then 72''fileext = " ${ v } "'' 73else if ( n == "encoding" ) then 74''encoding = " ${ v } "'' 75else if ( n == "postHook" ) then 76''post_hook = " ${ v } "'' 77else if ( n == "url" ) then 78''url = " ${ v } "'' 79else if ( n == "timeRange" ) then '' 80start_date = " ${ v . start } " 81end_date = " ${ v . end } "'' else if ( n == "itemTypes" ) then 82"item_types = ${ listString ( map wrap v )} " 83else if ( n == "userName" ) then 84''username = " ${ v } "'' 85else if ( n == "userNameCommand" ) then 86"username.fetch = ${ listString ( map wrap ([ "command" ] ++ v ))} " 87else if ( n == "password" ) then 88''password = " ${ v } "'' 89else if ( n == "passwordCommand" ) then 90"password.fetch = ${ listString ( map wrap ([ "command" ] ++ v ))} " 91else if ( n == "passwordPrompt" ) then 92''password.fetch = ["prompt", " ${ v } "]'' 93else if ( n == "verify" ) then 94"verify = ${ if v then "true" else "false" } " 95else if ( n == "verifyFingerprint" ) then 96''verify_fingerprint = " ${ v } "'' 97else if ( n == "auth" ) then 98''auth = " ${ v } "'' 99else if ( n == "authCert" && isString ( v )) then 100''auth_cert = " ${ v } "'' 101else if ( n == "authCert" ) then 102"auth_cert = ${ listString ( map wrap v )} " 103else if ( n == "userAgent" ) then 104''useragent = " ${ v } "'' 105else if ( n == "tokenFile" ) then 106''token_file = " ${ v } "'' 107else if ( n == "clientId" ) then 108''client_id = " ${ v } "'' 109else if ( n == "clientIdCommand" ) then 110"client_id.fetch = ${ listString ( map wrap ([ "command" ] ++ v ))} " 111else if ( n == "clientSecret" ) then 112''client_secret = " ${ v } "'' 113else if ( n == "clientSecretCommand" ) then 114"client_secret.fetch = ${ listString ( map wrap ([ "command" ] ++ v ))} " 115else if ( n == "metadata" ) then 116"metadata = ${ listString ( map wrap v )} " 117else if ( n == "partialSync" ) then 118''partial_sync = " ${ v } "'' 119else if ( n == "collections" ) then 120let 121contents = 122map ( c :if ( isString c ) then ''" ${ c } "'' else listString ( map wrap c )) 123v ; 124in "collections = ${ 125if (( isNull v ) || v == [ ]) then "null" else listString contents 126} " 127else if ( n == "conflictResolution" ) then 128if v == "remote wins" then 129''conflict_resolution = "a wins"'' 130else if v == "local wins" then 131''conflict_resolution = "b wins"'' 132else 133"conflict_resolution = ${ listString ( map wrap ([ "command" ] ++ v ))} " 134else 135throw "Unrecognized option: ${ n } " ; 136 137attrsString = a :concatStringsSep "\n" ( mapAttrsToList optionString a ); 138 139pairString = n :v :'' 140[pair ${ n } ] 141a = " ${ n } _remote" 142b = " ${ n } _local" 143${ attrsString v } 144'' ; 145 146configFile = pkgs . writeText "config" '' 147[general] 148status_path = " ${ cfg . statusPath } " 149 150### Pairs 151 152${ concatStringsSep "\n" ( mapAttrsToList pairString pairs )} 153 154### Local storages 155 156${ concatStringsSep "\n\n" 157( mapAttrsToList ( n : v : "[storage ${ n } _local]" + "\n" + attrsString v ) 158localStorages )} 159 160### Remote storages 161 162${ concatStringsSep "\n\n" 163( mapAttrsToList ( n : v : "[storage ${ n } _remote]" + "\n" + attrsString v ) 164remoteStorages )} 165'' ; 166 167in { 168options = { 169programs . vdirsyncer = { 170enable = mkEnableOption "vdirsyncer" ; 171 172package = mkOption { 173type = types . package ; 174default = pkgs . vdirsyncer ; 175defaultText = "pkgs.vdirsyncer" ; 176description = '' 177vdirsyncer package to use. 178'' ; 179}; 180 181statusPath = mkOption { 182type = types . str ; 183default = " ${ config . xdg . dataHome } /vdirsyncer/status" ; 184defaultText = "$XDG_DATA_HOME/vdirsyncer/status" ; 185description = '' 186A directory where vdirsyncer will store some additional data for the next sync. 187 188For more information, see the 189[vdirsyncer manual](https://vdirsyncer.pimutils.org/en/stable/config.html#general-section). 190'' ; 191}; 192}; 193}; 194 195config = mkIf cfg . enable { 196assertions = let 197 198requiredOptions = t : 199if ( t == "caldav" || t == "carddav" || t == "http" ) then 200[ "url" ] 201else if ( t == "filesystem" ) then [ 202"path" 203"fileExt" 204] else if ( t == "singlefile" ) then 205[ "path" ] 206else if ( t == "google_calendar" || t == "google_contacts" ) then [ 207"tokenFile" 208"clientId" 209"clientSecret" 210] else 211throw "Unrecognized storage type: ${ t } " ; 212 213allowedOptions = let 214remoteOptions = [ 215"userName" 216"userNameCommand" 217"password" 218"passwordCommand" 219"passwordPrompt" 220"verify" 221"verifyFingerprint" 222"auth" 223"authCert" 224"userAgent" 225]; 226in t : 227if ( t == "caldav" ) then 228[ "timeRange" "itemTypes" ] ++ remoteOptions 229else if ( t == "carddav" || t == "http" ) then 230remoteOptions 231else if ( t == "filesystem" ) then [ 232"fileExt" 233"encoding" 234"postHook" 235] else if ( t == "singlefile" ) then 236[ "encoding" ] 237else if ( t == "google_calendar" ) then [ 238"timeRange" 239"itemTypes" 240"clientIdCommand" 241"clientSecretCommand" 242] else if ( t == "google_contacts" ) then [ 243"clientIdCommand" 244"clientSecretCommand" 245] else 246throw "Unrecognized storage type: ${ t } " ; 247 248assertStorage = n :v : 249let allowed = allowedOptions v . type ++ ( requiredOptions v . type ); 250in mapAttrsToList ( a :v' : 251[{ 252assertion = ( elem a allowed ); 253message = '' 254Storage ${ n } is of type ${ v . type } . Option 255${ a } is not allowed for this type. 256'' ; 257}] ++ ( let 258required = 259filter ( a :! hasAttr " ${ a } Command" v ) ( requiredOptions v . type ); 260in map ( a :[{ 261assertion = hasAttr a v ; 262message = '' 263Storage ${ n } is of type ${ v . type } , but required 264option ${ a } is not set. 265'' ; 266}]) required )) ( removeAttrs v [ "type" "_module" ]); 267 268storageAssertions = flatten ( mapAttrsToList assertStorage localStorages ) 269++ flatten ( mapAttrsToList assertStorage remoteStorages ); 270 271in storageAssertions ; 272home . packages = [ cfg . package ]; 273xdg . configFile . "vdirsyncer/config" . source = configFile ; 274}; 275}