fork/home-manager

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

Janikprograms.khal4a26e210

master
4.2 KiB163 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.accounts.calendar;
8
9  localModule = name:
10    types.submodule {
11      options = {
12        path = mkOption {
13          type = types.str;
14          default = "${cfg.basePath}/${name}";
15          defaultText = "‹accounts.calendar.basePath›/‹name›";
16          description = "The path of the storage.";
17        };
18
19        type = mkOption {
20          type = types.enum [ "filesystem" "singlefile" ];
21          description = "The type of the storage.";
22        };
23
24        fileExt = mkOption {
25          type = types.nullOr types.str;
26          default = null;
27          description = "The file extension to use.";
28        };
29
30        encoding = mkOption {
31          type = types.nullOr types.str;
32          default = null;
33          description = ''
34            File encoding for items, both content and file name.
35            Defaults to UTF-8.
36          '';
37        };
38      };
39    };
40
41  remoteModule = types.submodule {
42    options = {
43      type = mkOption {
44        type = types.enum [ "caldav" "http" "google_calendar" ];
45        description = "The type of the storage.";
46      };
47
48      url = mkOption {
49        type = types.nullOr types.str;
50        default = null;
51        description = "The URL of the storage.";
52      };
53
54      userName = mkOption {
55        type = types.nullOr types.str;
56        default = null;
57        description = "User name for authentication.";
58      };
59
60      # userNameCommand = mkOption {
61      #   type = types.nullOr (types.listOf types.str);
62      #   default = null;
63      #   example = [ "~/get-username.sh" ];
64      #   description = ''
65      #     A command that prints the user name to standard output.
66      #   '';
67      # };
68
69      passwordCommand = mkOption {
70        type = types.nullOr (types.listOf types.str);
71        default = null;
72        example = [ "pass" "caldav" ];
73        description = ''
74          A command that prints the password to standard output.
75        '';
76      };
77    };
78  };
79
80  calendarOpts = { name, config, ... }: {
81    options = {
82      name = mkOption {
83        type = types.str;
84        readOnly = true;
85        description = ''
86          Unique identifier of the calendar. This is set to the
87          attribute name of the calendar configuration.
88        '';
89      };
90
91      primary = mkOption {
92        type = types.bool;
93        default = false;
94        description = ''
95          Whether this is the primary account. Only one account may be
96          set as primary.
97        '';
98      };
99
100      primaryCollection = mkOption {
101        type = types.str;
102        description = ''
103          The primary collection of the account. Required when an
104          account has multiple collections.
105        '';
106      };
107
108      local = mkOption {
109        type = types.nullOr (localModule name);
110        default = null;
111        description = ''
112          Local configuration for the calendar.
113        '';
114      };
115
116      remote = mkOption {
117        type = types.nullOr remoteModule;
118        default = null;
119        description = ''
120          Remote configuration for the calendar.
121        '';
122      };
123    };
124
125    config = { name = name; };
126  };
127
128in {
129  options.accounts.calendar = {
130    basePath = mkOption {
131      type = types.str;
132      apply = p:
133        if hasPrefix "/" p then p else "${config.home.homeDirectory}/${p}";
134      description = ''
135        The base directory in which to save calendars. May be a
136        relative path, in which case it is relative the home
137        directory.
138      '';
139    };
140
141    accounts = mkOption {
142      type = types.attrsOf (types.submodule [
143        calendarOpts
144        (import ../programs/vdirsyncer-accounts.nix)
145        (import ../programs/khal-accounts.nix)
146        (import ../programs/khal-calendar-accounts.nix)
147      ]);
148      default = { };
149      description = "List of calendars.";
150    };
151  };
152  config = mkIf (cfg.accounts != { }) {
153    assertions = let
154      primaries =
155        catAttrs "name" (filter (a: a.primary) (attrValues cfg.accounts));
156    in [{
157      assertion = length primaries <= 1;
158      message = "Must have at most one primary calendar account but found "
159        + toString (length primaries) + ", namely "
160        + concatStringsSep ", " primaries;
161    }];
162  };
163}