2024-05-14 11:18:38 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
2024-05-14 13:39:41 +00:00
|
|
|
cfg = config.programs.rclone;
|
|
|
|
targetName = "rclone";
|
|
|
|
|
2024-05-14 13:24:17 +00:00
|
|
|
mkProvider =
|
2024-05-15 07:48:02 +00:00
|
|
|
{
|
|
|
|
secretLoader ? ({ ... }: ""),
|
|
|
|
envLoader ? ({ ... }: { }),
|
|
|
|
configLoader ? ({ ... }: { }),
|
|
|
|
options ? {},
|
|
|
|
...
|
|
|
|
}: {
|
2024-05-14 13:24:17 +00:00
|
|
|
inherit
|
|
|
|
secretLoader
|
|
|
|
envLoader
|
2024-05-15 07:48:02 +00:00
|
|
|
configLoader
|
2024-05-14 13:24:17 +00:00
|
|
|
options
|
|
|
|
;
|
|
|
|
};
|
|
|
|
|
2024-05-14 13:39:41 +00:00
|
|
|
mkSystemdDependencyOption =
|
|
|
|
{ default, ... }: lib.mkOption {
|
2024-05-15 07:09:56 +00:00
|
|
|
type = lib.types.attrsOf (lib.types.listOf lib.types.str);
|
2024-05-14 13:39:41 +00:00
|
|
|
description = "The systemd services this sync depends on.";
|
|
|
|
example = {
|
2024-05-15 07:09:56 +00:00
|
|
|
secrets = [
|
|
|
|
"sops-nix.service"
|
|
|
|
];
|
2024-05-14 13:39:41 +00:00
|
|
|
};
|
|
|
|
inherit default;
|
|
|
|
};
|
|
|
|
|
2024-05-15 07:48:02 +00:00
|
|
|
mkWebdavOptions = { vendorName }: {
|
|
|
|
username = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "The name of the user to log in to ${vendorName}.";
|
|
|
|
};
|
|
|
|
|
|
|
|
password = lib.mkOption {
|
|
|
|
type = lib.types.nullOr lib.types.str;
|
|
|
|
description = "The password for logging in to ${vendorName}.";
|
2024-05-15 07:49:37 +00:00
|
|
|
default = null;
|
2024-05-15 07:48:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
passwordFile = lib.mkOption {
|
|
|
|
type = lib.types.nullOr (lib.types.either lib.types.path lib.types.str);
|
|
|
|
description = "A path to a file to read the password from for logging in to ${vendorName}.";
|
2024-05-15 07:49:37 +00:00
|
|
|
default = null;
|
2024-05-15 07:48:02 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
mkOwncloudOptions =
|
|
|
|
{ vendorName ? "Nextcloud" }: (mkWebdavOptions { inherit vendorName; }) // {
|
|
|
|
baseUrl = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "The base URL of the ${vendorName} server.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-05-14 12:23:30 +00:00
|
|
|
syncProviders = {
|
2024-05-15 07:48:02 +00:00
|
|
|
nextcloud = mkProvider {
|
|
|
|
options = lib.types.submodule (
|
|
|
|
(mkOwncloudOptions { }));
|
|
|
|
};
|
|
|
|
|
2024-05-14 13:24:17 +00:00
|
|
|
proton = mkProvider {};
|
2024-05-14 13:28:47 +00:00
|
|
|
manual = mkProvider {};
|
2024-05-14 12:23:30 +00:00
|
|
|
};
|
|
|
|
|
2024-05-14 11:18:38 +00:00
|
|
|
syncType = lib.types.submodule (
|
2024-05-14 12:23:30 +00:00
|
|
|
{ config, name, ... }: {
|
2024-05-14 11:18:38 +00:00
|
|
|
options = {
|
|
|
|
type = lib.mkOption {
|
2024-05-14 12:23:30 +00:00
|
|
|
description = "The type of the sync";
|
|
|
|
type = lib.types.enum (builtins.attrNames syncProviders);
|
2024-05-14 13:28:47 +00:00
|
|
|
default = "manual";
|
2024-05-14 11:18:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
autoStart = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
description = "Whether to start this sync automatically.";
|
|
|
|
default = true;
|
|
|
|
};
|
|
|
|
|
2024-05-15 07:48:02 +00:00
|
|
|
providerOptions = lib.mkOption {
|
|
|
|
description = "The options of the sync";
|
|
|
|
type = lib.types.oneOf [
|
|
|
|
(builtins.map
|
|
|
|
(provider: provider.options)
|
|
|
|
syncProviders)
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2024-05-14 13:39:41 +00:00
|
|
|
systemdDependencies = mkSystemdDependencyOption {
|
|
|
|
default = cfg.systemdDependencies;
|
2024-05-14 11:18:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
environment = lib.mkOption {
|
|
|
|
type = lib.types.attrsOf lib.types.envVar;
|
|
|
|
description = "The environment variables to pass to the service.";
|
|
|
|
default = {};
|
|
|
|
};
|
|
|
|
|
2024-05-14 12:23:30 +00:00
|
|
|
secretsScript = lib.mkOption {
|
2024-05-14 11:18:38 +00:00
|
|
|
type = lib.types.lines;
|
2024-05-14 12:23:30 +00:00
|
|
|
description = "A script for loading secrets before launching the sync.";
|
2024-05-14 11:18:38 +00:00
|
|
|
default = [];
|
|
|
|
};
|
2024-05-15 07:48:02 +00:00
|
|
|
|
|
|
|
config = lib.mkOption {
|
|
|
|
type = lib.types.attrs;
|
|
|
|
description = "The rclone config to use for creating the mount.";
|
|
|
|
visible = false;
|
|
|
|
};
|
2024-05-14 11:18:38 +00:00
|
|
|
};
|
2024-05-14 13:24:17 +00:00
|
|
|
|
|
|
|
config = {
|
|
|
|
environment = syncProviders.${config.type}.envLoader config;
|
|
|
|
secretsScript = syncProviders.${config.type}.secretLoader config;
|
2024-05-15 07:48:02 +00:00
|
|
|
config = syncProviders.${config.type}.configLoader config;
|
2024-05-14 13:24:17 +00:00
|
|
|
};
|
2024-05-14 11:18:38 +00:00
|
|
|
});
|
|
|
|
in {
|
2024-05-12 23:20:01 +00:00
|
|
|
options = {
|
|
|
|
programs.rclone = {
|
|
|
|
enable = lib.mkEnableOption "rclone";
|
2024-05-14 11:18:38 +00:00
|
|
|
|
2024-05-14 13:39:41 +00:00
|
|
|
systemdDependencies = mkSystemdDependencyOption {
|
|
|
|
default = {};
|
|
|
|
};
|
|
|
|
|
2024-05-14 11:18:38 +00:00
|
|
|
configs = lib.mkOption {
|
|
|
|
type = lib.types.attrsOf syncType;
|
|
|
|
description = "The synchronizations to set up.";
|
|
|
|
default = {};
|
|
|
|
};
|
2024-05-12 23:20:01 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-05-14 13:39:41 +00:00
|
|
|
config = {
|
|
|
|
home.packages = lib.optional cfg.enable pkgs.rclone;
|
2024-05-12 23:20:01 +00:00
|
|
|
|
2024-05-14 13:39:41 +00:00
|
|
|
systemd.user = lib.optionalAttrs cfg.enable {
|
|
|
|
enable = true;
|
2024-05-12 23:20:01 +00:00
|
|
|
|
2024-05-14 13:39:41 +00:00
|
|
|
services = {
|
|
|
|
rclone = {
|
|
|
|
Unit = {
|
|
|
|
Description = "rclone Starter";
|
|
|
|
Documentation = "man:rclone(1)";
|
|
|
|
};
|
2024-05-12 23:20:01 +00:00
|
|
|
|
2024-05-14 13:39:41 +00:00
|
|
|
Service = {
|
|
|
|
Type = "simple";
|
|
|
|
|
|
|
|
ExecStartPre =
|
|
|
|
let
|
|
|
|
script = pkgs.writeShellScriptBin "rclone-pre" ''
|
|
|
|
sleep 10
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
(lib.getExe script);
|
|
|
|
|
|
|
|
ExecStart =
|
|
|
|
let
|
|
|
|
script = pkgs.writeShellScriptBin "rclone" ''
|
|
|
|
systemctl --user start rclone.target
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
(lib.getExe script);
|
2024-05-12 23:20:01 +00:00
|
|
|
};
|
2024-05-14 13:39:41 +00:00
|
|
|
};
|
|
|
|
} // (
|
|
|
|
lib.attrsets.concatMapAttrs
|
|
|
|
(name: sync:
|
|
|
|
let
|
|
|
|
serviceName = "rclone-sync-${name}";
|
|
|
|
in {
|
|
|
|
${serviceName} = {
|
|
|
|
Unit = {
|
|
|
|
Description = "${sync.type}";
|
2024-05-14 13:24:17 +00:00
|
|
|
};
|
2024-05-12 23:20:01 +00:00
|
|
|
|
2024-05-14 13:39:41 +00:00
|
|
|
Service = {
|
|
|
|
Environment = lib.mapAttrsToList
|
|
|
|
(key: val: (lib.escapeShellArg "${key}=${val}"))
|
|
|
|
sync.environment;
|
|
|
|
|
|
|
|
ExecStart =
|
|
|
|
let
|
|
|
|
script = pkgs.writeShellScriptBin serviceName ''
|
|
|
|
${sync.secretsScript}
|
|
|
|
bash -c echo hello world
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
(lib.getExe script);
|
|
|
|
};
|
|
|
|
|
|
|
|
Install = {
|
|
|
|
WantedBy = lib.optional sync.autoStart "${targetName}.target";
|
|
|
|
After = builtins.concatLists(builtins.attrValues sync.systemdDependencies);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
})
|
|
|
|
cfg.configs);
|
|
|
|
|
|
|
|
targets.${targetName} = {
|
|
|
|
Unit = {
|
|
|
|
Description = "rclone Mounts";
|
|
|
|
Documentation = "man:rclone(1)";
|
2024-05-12 23:20:01 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2024-05-14 13:39:41 +00:00
|
|
|
};
|
2024-05-12 23:20:01 +00:00
|
|
|
}
|