nixfiles/hosts/storage/modules/rclone-sync.nix

74 lines
1.8 KiB
Nix
Raw Normal View History

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