NixOSConfig/lib/modules/rclone.nix

186 lines
4.7 KiB
Nix
Raw Normal View History

2024-05-14 11:18:38 +00:00
{ config, lib, pkgs, ... }:
let
cfg = config.programs.rclone;
targetName = "rclone";
2024-05-15 08:01:13 +00:00
mkSystemdDependencyOption =
{ default, ... }: lib.mkOption {
type = lib.types.attrsOf (lib.types.listOf lib.types.str);
description = "The systemd services this sync depends on.";
example = {
secrets = [
"sops-nix.service"
];
};
inherit default;
};
mkProvider =
{
2024-05-15 13:54:03 +00:00
displayName ? "Custom",
2024-05-15 09:48:22 +00:00
secretsScript ? "",
environment ? { },
config ? { },
2024-05-15 15:24:59 +00:00
options ? { }
2024-05-15 09:48:22 +00:00
}:
{ ... }: {
2024-05-15 13:54:03 +00:00
inherit
displayName
2024-05-15 09:48:22 +00:00
secretsScript
environment
config
options
;
};
2024-05-14 12:23:30 +00:00
syncProviders = {
2024-05-15 09:48:22 +00:00
nextcloud = mkProvider {};
proton = mkProvider {};
2024-05-14 13:28:47 +00:00
manual = mkProvider {};
2024-05-14 12:23:30 +00:00
};
2024-05-15 13:54:03 +00:00
mkSyncType = provider: lib.types.submodule (
2024-05-14 12:23:30 +00:00
{ config, name, ... }: {
2024-05-14 11:18:38 +00:00
options = {
autoStart = lib.mkOption {
type = lib.types.bool;
description = "Whether to start this sync automatically.";
default = true;
};
providerOptions = lib.mkOption {
description = "The options of the sync";
2024-05-15 13:54:03 +00:00
type = lib.types.submodule { inherit (provider config) options; };
2024-05-15 15:22:34 +00:00
default = { };
};
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 = [];
};
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-15 15:22:34 +00:00
config = {
inherit (provider (lib.debug.traceVal config))
environment
secretsScript
config
;
};
2024-05-14 11:18:38 +00:00
});
in {
options = {
programs.rclone = {
enable = lib.mkEnableOption "rclone";
2024-05-14 11:18:38 +00:00
systemdDependencies = mkSystemdDependencyOption {
default = {};
};
2024-05-15 13:54:03 +00:00
configs = (builtins.mapAttrs
(name: provider: lib.mkOption {
type = lib.types.attrsOf (mkSyncType provider);
description = "The ${(provider config).displayName} synchronizations to set up.";
default = { };
2024-05-15 13:54:03 +00:00
})
syncProviders);
};
};
config = {
home.packages = lib.optional cfg.enable pkgs.rclone;
systemd.user = lib.optionalAttrs cfg.enable {
enable = true;
services = {
rclone = {
Unit = {
Description = "rclone Starter";
Documentation = "man:rclone(1)";
};
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);
};
};
} // (
lib.attrsets.concatMapAttrs
2024-05-15 13:54:03 +00:00
(providerName: configs:
lib.attrsets.concatMapAttrs
(name: sync:
let
serviceName = "rclone-${providerName}-sync-${name}";
in {
${serviceName} = {
Unit = {
Description = "rclone sync service for ${name} at using ${providerName}";
};
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);
};
};
2024-05-15 13:54:03 +00:00
})
configs)
cfg.configs);
targets.${targetName} = {
Unit = {
Description = "rclone Mounts";
Documentation = "man:rclone(1)";
};
};
};
};
}