2022-12-04 13:45:43 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2023-03-10 11:09:11 +00:00
|
|
|
Type = "oneshot";
|
2022-10-28 13:56:51 +01:00
|
|
|
|
2023-03-10 11:09:11 +00: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 {
|
2023-03-10 11:09:11 +00:00
|
|
|
TimeoutSec = 60;
|
|
|
|
Restart = "on-failure";
|
|
|
|
})
|
|
|
|
|
2023-09-18 03:56:58 +01:00
|
|
|
syncConfig.serviceConfig
|
2022-11-03 06:44:02 +00:00
|
|
|
];
|
2023-03-10 11:09:11 +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 = {
|
2022-12-04 13:45:43 +00:00
|
|
|
source = mkOption {type = types.str;};
|
|
|
|
dest = mkOption {type = types.str;};
|
2023-03-19 11:44:27 +00:00
|
|
|
id = mkOption {type = types.str;};
|
|
|
|
|
2023-03-25 09:33:20 +00:00
|
|
|
extraArgs = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
};
|
|
|
|
|
2023-03-10 11:09:11 +00:00
|
|
|
autoRestart = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
};
|
2022-10-28 13:56:51 +01:00
|
|
|
|
2023-03-10 11:09:11 +00:00
|
|
|
timerConfig = mkOption {
|
|
|
|
type = types.attrs;
|
|
|
|
default = {
|
2024-03-09 22:18:26 +00:00
|
|
|
OnStartupSec = "1m";
|
|
|
|
OnUnitActiveSec = "2h";
|
2023-03-10 11:09:11 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
serviceConfig = mkOption {
|
|
|
|
type = types.attrs;
|
|
|
|
default = {};
|
|
|
|
};
|
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 [
|
2023-09-18 03:56:58 +01:00
|
|
|
(mkIf (cfg.enable && cfg.syncJobs != []) {
|
2023-03-19 11:44:27 +00:00
|
|
|
environment.systemPackages =
|
|
|
|
[
|
2023-09-28 14:04:35 +01:00
|
|
|
(pkgs.writeShellScriptBin "rclone-sync-stop-all" (concatStringsSep "\n" (map (
|
|
|
|
job: ''
|
2023-10-02 03:08:24 +01:00
|
|
|
systemctl stop rclone-sync-${job.id}.service
|
2023-09-28 14:04:35 +01:00
|
|
|
''
|
|
|
|
)
|
|
|
|
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
|
|
|
);
|
2023-03-10 11:09:11 +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}";
|
2022-12-04 13:45:43 +00:00
|
|
|
value = daemonService job;
|
|
|
|
})
|
2023-09-18 03:56:58 +01:00
|
|
|
cfg.syncJobs);
|
2022-11-03 06:44:02 +00:00
|
|
|
|
2022-12-04 13:45:43 +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 = {
|
2022-12-04 13:45:43 +00:00
|
|
|
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
|
|
|
};
|
2022-12-04 13:45:43 +00:00
|
|
|
})
|
2023-09-18 03:56:58 +01:00
|
|
|
cfg.syncJobs);
|
2022-10-28 13:56:51 +01:00
|
|
|
})
|
|
|
|
];
|
|
|
|
}
|