fork/home-manager

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

arjan-sprograms.khal: add `settings` option (#4375)a7eab56b

master
5.2 KiB199 linesraw
1# khal config loader is sensitive to leading space !
2{ config, lib, pkgs, ... }:
3
4with lib;
5
6let
7
8  cfg = config.programs.khal;
9
10  iniFormat = pkgs.formats.ini { };
11
12  khalCalendarAccounts =
13    filterAttrs (_: a: a.khal.enable) config.accounts.calendar.accounts;
14
15  khalContactAccounts = mapAttrs (_: v: v // { type = "birthdays"; })
16    (filterAttrs (_: a: a.khal.enable) config.accounts.contact.accounts);
17
18  khalAccounts = khalCalendarAccounts // khalContactAccounts;
19
20  primaryAccount = findSingle (a: a.primary) null null
21    (mapAttrsToList (n: v: v // { name = n; }) khalAccounts);
22
23  definedAttrs = filterAttrs (_: v: !isNull v);
24
25  toKeyValueIfDefined = attrs: generators.toKeyValue { } (definedAttrs attrs);
26
27  genCalendarStr = name: value:
28    concatStringsSep "\n" ([
29      "[[${name}]]"
30      "path = ${
31        value.local.path + "/"
32        + (optionalString (value.khal.type == "discover") value.khal.glob)
33      }"
34    ] ++ optional (value.khal.readOnly) "readonly = True" ++ [
35      (toKeyValueIfDefined (getAttrs [ "type" "color" "priority" ] value.khal))
36    ] ++ [ "\n" ]);
37
38  localeFormatOptions = let
39    T = lib.types;
40    suffix = ''
41      Format strings are for Python `strftime`, similarly to
42      {manpage}`strftime(3)`.
43    '';
44  in {
45    dateformat = mkOption {
46      type = T.str;
47      default = "%x";
48      description = ''
49        khal will display and understand all dates in this format.
50
51        ${suffix}
52      '';
53    };
54
55    timeformat = mkOption {
56      type = T.str;
57      default = "%X";
58      description = ''
59        khal will display and understand all times in this format.
60
61        ${suffix}
62      '';
63    };
64
65    datetimeformat = mkOption {
66      type = T.str;
67      default = "%c";
68      description = ''
69        khal will display and understand all datetimes in this format.
70
71        ${suffix}
72      '';
73    };
74
75    longdateformat = mkOption {
76      type = T.str;
77      default = "%x";
78      description = ''
79        khal will display and understand all dates in this format.
80        It should contain a year (e.g. `%Y`).
81
82        ${suffix}
83      '';
84    };
85
86    longdatetimeformat = mkOption {
87      type = T.str;
88      default = "%c";
89      description = ''
90        khal will display and understand all datetimes in this format.
91        It should contain a year (e.g. `%Y`).
92
93        ${suffix}
94      '';
95    };
96  };
97
98  localeOptions = let T = lib.types;
99  in localeFormatOptions // {
100    unicode_symbols = mkOption {
101      type = T.bool;
102      default = true;
103      description = ''
104        By default khal uses some Unicode symbols (as in "non-ASCII") as
105        indicators for things like repeating events.
106        If your font, encoding etc. does not support those symbols, set this
107        to false (this will enable ASCII-based replacements).
108      '';
109    };
110
111    default_timezone = mkOption {
112      type = T.nullOr T.str;
113      default = null;
114      description = ''
115        Default for new events or if khal does not understand the timezone
116        in an ical file.
117        If `null`, the timezone of your computer will be used.
118      '';
119    };
120
121    local_timezone = mkOption {
122      type = T.nullOr T.str;
123      default = null;
124      description = ''
125        khal will show all times in this timezone.
126        If `null`, the timezone of your computer will be used.
127      '';
128    };
129
130    firstweekday = mkOption {
131      type = T.ints.between 0 6;
132      default = 0;
133      description = ''
134        The first day of the week, where Monday is 0 and Sunday is 6.
135      '';
136    };
137
138    weeknumbers = mkOption {
139      type = T.enum [ "off" "left" "right" ];
140      default = "off";
141      description = ''
142        Enable week numbers in calendar and interactive (ikhal) mode.
143        As those are ISO week numbers, they only work properly if
144        {option}`firstweekday` is set to 0.
145      '';
146    };
147  };
148
149in {
150  options.programs.khal = {
151    enable = mkEnableOption "khal, a CLI calendar application";
152
153    locale = mkOption {
154      type = lib.types.submodule { options = localeOptions; };
155      description = ''
156        khal locale settings. 
157      '';
158      default = { };
159    };
160
161    settings = mkOption {
162      type = iniFormat.type;
163      default = { };
164      example = literalExpression ''
165        {
166          default = {
167            default_calendar = "Calendar";
168            timedelta = "5d";
169          };
170          view = {
171            agenda_event_format =
172              "{calendar-color}{cancelled}{start-end-time-style} {title}{repeat-symbol}{reset}";
173          };
174        }'';
175      description = ''
176        Configuration options to add to the various sections in the configuration file.
177      '';
178    };
179  };
180
181  config = mkIf cfg.enable {
182    home.packages = [ pkgs.khal ];
183
184    xdg.configFile."khal/config".text = concatStringsSep "\n" ([ "[calendars]" ]
185      ++ mapAttrsToList genCalendarStr khalAccounts ++ [
186        (generators.toINI { } (recursiveUpdate cfg.settings {
187          locale = definedAttrs (cfg.locale // { _module = null; });
188
189          default = optionalAttrs (!isNull primaryAccount) {
190            highlight_event_days = true;
191            default_calendar = if isNull primaryAccount.primaryCollection then
192              primaryAccount.name
193            else
194              primaryAccount.primaryCollection;
195          };
196        }))
197      ]);
198  };
199}