Make sync providers distinguishable

This commit is contained in:
Manuel Thalmann 2024-05-15 15:54:03 +02:00
parent b781eb7e08
commit c4926df9f6

View file

@ -17,6 +17,7 @@ let
mkProvider = mkProvider =
{ {
displayName ? "Custom",
secretsScript ? "", secretsScript ? "",
environment ? { }, environment ? { },
config ? { }, config ? { },
@ -24,7 +25,8 @@ let
defaultOptions ? { } defaultOptions ? { }
}: }:
{ ... }: { { ... }: {
inherit inherit
displayName
secretsScript secretsScript
environment environment
config config
@ -38,7 +40,7 @@ let
manual = mkProvider {}; manual = mkProvider {};
}; };
syncType = lib.types.submodule ( mkSyncType = provider: lib.types.submodule (
{ config, name, ... }: { { config, name, ... }: {
options = { options = {
type = lib.mkOption { type = lib.mkOption {
@ -55,11 +57,8 @@ let
providerOptions = lib.mkOption { providerOptions = lib.mkOption {
description = "The options of the sync"; description = "The options of the sync";
type = lib.types.oneOf [ type = lib.types.submodule { inherit (provider config) options; };
(builtins.map default = {};
(provider: provider.options)
syncProviders)
];
}; };
systemdDependencies = mkSystemdDependencyOption { systemdDependencies = mkSystemdDependencyOption {
@ -105,11 +104,12 @@ in {
default = {}; default = {};
}; };
configs = lib.mkOption { configs = (builtins.mapAttrs
type = lib.types.attrsOf syncType; (name: provider: lib.mkOption {
description = "The synchronizations to set up."; type = lib.types.attrsOf (mkSyncType provider);
default = {}; description = "The ${(provider config).displayName} synchronizations to set up.";
}; })
syncProviders);
}; };
}; };
@ -148,36 +148,39 @@ in {
}; };
} // ( } // (
lib.attrsets.concatMapAttrs lib.attrsets.concatMapAttrs
(name: sync: (providerName: configs:
let lib.attrsets.concatMapAttrs
serviceName = "rclone-sync-${name}"; (name: sync:
in { let
${serviceName} = { serviceName = "rclone-${providerName}-sync-${name}";
Unit = { in {
Description = "${sync.type}"; ${serviceName} = {
}; Unit = {
Description = "rclone sync service for ${name} at using ${providerName}";
};
Service = { Service = {
Environment = lib.mapAttrsToList Environment = lib.mapAttrsToList
(key: val: (lib.escapeShellArg "${key}=${val}")) (key: val: (lib.escapeShellArg "${key}=${val}"))
sync.environment; sync.environment;
ExecStart = ExecStart =
let let
script = pkgs.writeShellScriptBin serviceName '' script = pkgs.writeShellScriptBin serviceName ''
${sync.secretsScript} ${sync.secretsScript}
bash -c echo hello world bash -c echo hello world
''; '';
in in
(lib.getExe script); (lib.getExe script);
}; };
Install = { Install = {
WantedBy = lib.optional sync.autoStart "${targetName}.target"; WantedBy = lib.optional sync.autoStart "${targetName}.target";
After = builtins.concatLists(builtins.attrValues sync.systemdDependencies); After = builtins.concatLists(builtins.attrValues sync.systemdDependencies);
};
}; };
}; })
}) configs)
cfg.configs); cfg.configs);
targets.${targetName} = { targets.${targetName} = {