fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

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