1
0
Fork 0
nixos-config/home-manager/roles/vdirsyncer.nix

110 lines
3.2 KiB
Nix
Raw Normal View History

2021-07-14 16:31:53 +00:00
{ pkgs, lib, config, ... }:
let
2021-05-18 14:33:28 +00:00
addressbooks = pkgs.privateValue [ ] "addressbooks";
calendars = pkgs.privateValue [ ] "calendars";
mkConfig = config:
2021-05-18 14:33:28 +00:00
(pkgs.formats.ini { }).generate "vdirsyncer-config" (
2021-02-14 20:19:25 +00:00
lib.mapAttrs
(
name: section:
(lib.mapAttrs (name: option: builtins.toJSON option) section)
2021-05-18 14:33:28 +00:00
)
config
2021-02-14 20:19:25 +00:00
);
2021-04-12 13:41:05 +00:00
mkCalendar = { name, url, username, passwordPath, collections ? [ "from a" "from b" ], readOnly ? false, type ? "caldav" }:
let
pairName = "${name}_calendar";
remoteName = "${pairName}_remote";
localName = "${pairName}_local";
2021-02-14 20:19:25 +00:00
in
2021-05-18 14:33:28 +00:00
{
"pair ${pairName}" = {
a = localName;
b = remoteName;
inherit collections;
conflict_resolution = "b wins";
metadata = [ "color" ];
};
2021-05-18 14:33:28 +00:00
"storage ${localName}" = {
type = "filesystem";
path = "~/.calendars/${name}/";
fileext = ".ics";
};
"storage ${remoteName}" = {
inherit type;
inherit url;
2021-07-05 19:18:23 +00:00
} // (
if (type == "caldav") then {
inherit username;
"password.fetch" = [ "command" "${pkgs.pass}/bin/pass" passwordPath ];
read_only = readOnly;
} else { }
);
2021-05-18 14:33:28 +00:00
};
2021-02-14 20:19:25 +00:00
mkAddressbook = { name, url, username, passwordPath, collections ? [ "from a" "from b" ], readOnly ? false }:
let
pairName = "${name}_contacts";
remoteName = "${pairName}_remote";
localName = "${pairName}_local";
2021-02-14 20:19:25 +00:00
in
2021-05-18 14:33:28 +00:00
{
"pair ${pairName}" = {
a = localName;
b = remoteName;
inherit collections;
conflict_resolution = "b wins";
};
"storage ${localName}" = {
type = "filesystem";
path = "~/.contacts/${name}/";
fileext = ".vcf";
};
2021-05-18 14:33:28 +00:00
"storage ${remoteName}" = {
type = "carddav";
inherit url username;
"password.fetch" = [ "command" "${pkgs.pass}/bin/pass" passwordPath ];
read_only = readOnly;
};
};
2021-02-14 20:19:25 +00:00
in
{
2021-07-05 19:18:23 +00:00
xdg.configFile."vdirsyncer/config".source = mkConfig
(
pkgs.lib.fold (a: b: a // b)
{
general.status_path = "~/.vdirsyncer/status";
}
(map mkCalendar calendars ++ map mkAddressbook addressbooks)
);
home.packages = [ pkgs.vdirsyncer ];
systemd.user = {
2021-07-15 11:48:34 +00:00
services.watch-vdir = {
2021-07-14 16:31:53 +00:00
Unit.Description = "Watch vdir data for changes";
Service = {
ExecStart = toString (
2021-07-15 11:48:34 +00:00
pkgs.writeShellScript "watch-vdir" ''
2021-07-14 16:31:53 +00:00
while sleep 1s; do
2021-07-15 11:48:34 +00:00
${pkgs.vdirsyncer}/bin/vdirsyncer sync
${pkgs.inotify-tools}/bin/inotifywait -e move,create,delete,modify -r ${config.home.homeDirectory}/.contacts ${config.home.homeDirectory}/.calendars
2021-07-14 16:31:53 +00:00
done
''
);
};
Install.WantedBy = [ "default.target" ];
};
services.vdirsyncer = {
Unit.Description = "vdirsyncer sync";
Service = {
Type = "oneshot";
ExecStart = "${pkgs.vdirsyncer}/bin/vdirsyncer sync";
};
};
timers.vdirsyncer = {
Unit.Description = "vdirsync sync timer";
Timer.OnCalendar = "*:0/15";
Install.WantedBy = [ "timers.target" ];
};
};
}