2022-12-04 13:45:43 +00:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
with lib; let
|
2022-10-28 13:56:51 +01:00
|
|
|
cfg = config.services.rclone-sync;
|
|
|
|
|
2022-12-04 13:45:43 +00:00
|
|
|
makeNameSafe = name: builtins.replaceStrings ["/" ":"] ["-" "-"] name;
|
2022-10-28 13:56:51 +01:00
|
|
|
|
2022-11-03 06:44:02 +00:00
|
|
|
daemonService = sync_config:
|
|
|
|
lib.mkMerge [
|
|
|
|
{
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
2022-10-28 13:56:51 +01:00
|
|
|
|
2022-12-04 13:45:43 +00:00
|
|
|
User =
|
|
|
|
if cfg.user != null
|
|
|
|
then "${cfg.user}"
|
|
|
|
else "root";
|
2022-10-28 13:56:51 +01:00
|
|
|
|
2022-12-04 13:45:43 +00:00
|
|
|
ExecStart = "${pkgs.rclone}/bin/rclone sync ${sync_config.source} ${sync_config.dest} -P";
|
2022-11-03 06:44:02 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
sync_config.serviceConfig
|
|
|
|
];
|
2022-10-28 13:56:51 +01:00
|
|
|
in {
|
|
|
|
options = {
|
|
|
|
services.rclone-sync = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
sync_jobs = mkOption {
|
|
|
|
type = types.listOf (types.submodule {
|
|
|
|
options = {
|
2022-12-04 13:45:43 +00:00
|
|
|
source = mkOption {type = types.str;};
|
|
|
|
dest = mkOption {type = types.str;};
|
2022-10-28 13:56:51 +01:00
|
|
|
|
2022-12-04 13:45:43 +00:00
|
|
|
timerConfig = mkOption {type = types.attrs;};
|
|
|
|
serviceConfig = mkOption {type = types.attrs;};
|
2022-10-28 13:56:51 +01:00
|
|
|
};
|
|
|
|
});
|
2022-12-04 13:45:43 +00:00
|
|
|
default = [];
|
2022-10-28 13:56:51 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkMerge [
|
2022-12-04 13:45:43 +00:00
|
|
|
(mkIf (cfg.enable && cfg.sync_jobs != []) {
|
2022-10-28 13:56:51 +01:00
|
|
|
systemd.services = listToAttrs (map (job: {
|
2022-12-04 13:45:43 +00:00
|
|
|
name = "rclone-sync-${makeNameSafe job.source}-${makeNameSafe job.dest}";
|
|
|
|
value = daemonService job;
|
|
|
|
})
|
|
|
|
cfg.sync_jobs);
|
2022-11-03 06:44:02 +00:00
|
|
|
|
2022-12-04 13:45:43 +00:00
|
|
|
systemd.timers = listToAttrs (map (job: let
|
|
|
|
name = "rclone-sync-${makeNameSafe job.source}-${makeNameSafe job.dest}";
|
2022-11-03 06:44:02 +00:00
|
|
|
in {
|
|
|
|
inherit name;
|
|
|
|
value = {
|
2022-12-04 13:45:43 +00:00
|
|
|
wantedBy = ["timers.target"];
|
|
|
|
partOf = ["${name}.service"];
|
2022-11-03 06:44:02 +00:00
|
|
|
timerConfig = job.timerConfig;
|
|
|
|
};
|
2022-12-04 13:45:43 +00:00
|
|
|
})
|
|
|
|
cfg.sync_jobs);
|
2022-10-28 13:56:51 +01:00
|
|
|
})
|
|
|
|
];
|
|
|
|
}
|