nixfiles/modules/nixos/rcloneSync.nix

130 lines
3.2 KiB
Nix
Raw Permalink Normal View History

{
config,
lib,
pkgs,
...
2023-09-18 03:56:58 +01:00
}: let
inherit (lib.modules) mkIf mkMerge;
inherit (lib.options) mkOption;
inherit (lib.strings) concatStringsSep;
inherit (lib) types;
inherit (builtins) listToAttrs;
2022-10-28 13:56:51 +01:00
cfg = config.services.rclone-sync;
2023-09-18 03:56:58 +01:00
daemonService = syncConfig: {
serviceConfig = mkMerge [
2022-11-03 06:44:02 +00:00
{
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
2023-09-18 03:56:58 +01:00
ExecStart = "${pkgs.rclone}/bin/rclone sync ${syncConfig.source} ${syncConfig.dest} ${concatStringsSep " " syncConfig.extraArgs} -P";
2022-11-03 06:44:02 +00:00
}
2023-09-18 03:56:58 +01:00
(mkIf syncConfig.autoRestart {
TimeoutSec = 60;
Restart = "on-failure";
})
2023-09-18 03:56:58 +01:00
syncConfig.serviceConfig
2022-11-03 06:44:02 +00:00
];
};
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;
};
2023-09-18 03:56:58 +01:00
syncJobs = mkOption {
2022-10-28 13:56:51 +01:00
type = types.listOf (types.submodule {
options = {
source = mkOption {type = types.str;};
dest = mkOption {type = types.str;};
2023-03-19 11:44:27 +00:00
id = mkOption {type = types.str;};
extraArgs = mkOption {
type = types.listOf types.str;
default = [];
};
autoRestart = mkOption {
type = types.bool;
default = true;
};
2022-10-28 13:56:51 +01:00
timerConfig = mkOption {
type = types.attrs;
default = {
2024-03-09 22:18:26 +00:00
OnStartupSec = "1m";
OnUnitActiveSec = "2h";
};
};
serviceConfig = mkOption {
type = types.attrs;
default = {};
};
2022-10-28 13:56:51 +01:00
};
});
default = [];
2022-10-28 13:56:51 +01:00
};
};
};
config = mkMerge [
2023-09-18 03:56:58 +01:00
(mkIf (cfg.enable && cfg.syncJobs != []) {
2023-03-19 11:44:27 +00:00
environment.systemPackages =
[
(pkgs.writeShellScriptBin "rclone-sync-stop-all" (concatStringsSep "\n" (map (
job: ''
systemctl stop rclone-sync-${job.id}.service
''
)
cfg.syncJobs)))
2023-09-18 03:56:58 +01:00
(pkgs.writeShellScriptBin "rclone-sync-all" (concatStringsSep "\n" (map (
2023-03-19 11:44:27 +00:00
job: ''
2023-09-18 03:56:58 +01:00
${pkgs.rclone}/bin/rclone sync ${job.source} ${job.dest} ${concatStringsSep " " job.extraArgs} -P $@
2023-03-19 11:44:27 +00:00
''
)
2023-09-18 03:56:58 +01:00
cfg.syncJobs)))
2023-03-19 11:44:27 +00:00
]
++ (
map (
job:
pkgs.writeShellScriptBin "rclone-manual-sync-${job.id}" ''
2023-09-18 03:56:58 +01:00
exec ${pkgs.rclone}/bin/rclone sync ${job.source} ${job.dest} ${concatStringsSep " " job.extraArgs} -P $@
2023-03-19 11:44:27 +00:00
''
)
2023-09-18 03:56:58 +01:00
cfg.syncJobs
2023-03-19 11:44:27 +00:00
);
2022-10-28 13:56:51 +01:00
systemd.services = listToAttrs (map (job: {
2023-03-19 11:44:27 +00:00
name = "rclone-sync-${job.id}";
value = daemonService job;
})
2023-09-18 03:56:58 +01:00
cfg.syncJobs);
2022-11-03 06:44:02 +00:00
systemd.timers = listToAttrs (map (job: let
2023-03-19 11:44:27 +00:00
name = "rclone-sync-${job.id}";
2022-11-03 06:44:02 +00:00
in {
inherit name;
value = {
wantedBy = ["timers.target"];
partOf = ["${name}.service"];
2024-03-10 17:26:18 +00:00
inherit (job) timerConfig;
2022-11-03 06:44:02 +00:00
};
})
2023-09-18 03:56:58 +01:00
cfg.syncJobs);
2022-10-28 13:56:51 +01:00
})
];
}