nixfiles/modules/nixos/rclone-sync.nix

78 lines
1.8 KiB
Nix
Raw Normal View History

{
config,
lib,
pkgs,
...
}:
with lib; let
2022-10-28 13:56:51 +01:00
cfg = config.services.rclone-sync;
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
User =
if cfg.user != null
then "${cfg.user}"
else "root";
2022-10-28 13:56:51 +01: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 = {
source = mkOption {type = types.str;};
dest = mkOption {type = types.str;};
2022-10-28 13:56:51 +01:00
timerConfig = mkOption {type = types.attrs;};
serviceConfig = mkOption {type = types.attrs;};
2022-10-28 13:56:51 +01:00
};
});
default = [];
2022-10-28 13:56:51 +01:00
};
};
};
config = mkMerge [
(mkIf (cfg.enable && cfg.sync_jobs != []) {
2022-10-28 13:56:51 +01:00
systemd.services = listToAttrs (map (job: {
name = "rclone-sync-${makeNameSafe job.source}-${makeNameSafe job.dest}";
value = daemonService job;
})
cfg.sync_jobs);
2022-11-03 06:44:02 +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 = {
wantedBy = ["timers.target"];
partOf = ["${name}.service"];
2022-11-03 06:44:02 +00:00
timerConfig = job.timerConfig;
};
})
cfg.sync_jobs);
2022-10-28 13:56:51 +01:00
})
];
}