NixOSConfig/lib/modules/rclone.nix

103 lines
2.5 KiB
Nix
Raw Normal View History

2024-05-14 11:18:38 +00:00
{ config, lib, pkgs, ... }:
let
2024-05-14 12:23:30 +00:00
syncProviders = {
nextcloud = {};
proton = {};
};
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 11:18:38 +00:00
};
autoStart = lib.mkOption {
type = lib.types.bool;
description = "Whether to start this sync automatically.";
default = true;
};
systemdDependencies = lib.mkOption {
type = lib.types.attrsOf lib.types.str;
description = "The systemd services this sync depends on.";
example = {
secrets = "sops-nix.service";
};
};
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 = [];
};
};
});
in {
options = {
programs.rclone = {
enable = lib.mkEnableOption "rclone";
2024-05-14 11:18:38 +00:00
configs = lib.mkOption {
type = lib.types.attrsOf syncType;
description = "The synchronizations to set up.";
default = {};
};
};
};
config =
let
cfg = config.programs.rclone;
in {
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);
};
};
};
targets.rclone = {
Unit = {
Description = "rclone Mounts";
Documentation = "man:rclone(1)";
};
};
};
};
}