diff --git a/lib/modules/rclone.nix b/lib/modules/rclone.nix index 201531c..1fe9a1e 100644 --- a/lib/modules/rclone.nix +++ b/lib/modules/rclone.nix @@ -1,5 +1,8 @@ { config, lib, pkgs, ... }: let + cfg = config.programs.rclone; + targetName = "rclone"; + mkProvider = { secretLoader ? ({ ... }: ""), envLoader ? ({ ... }: { }), options ? {}, ... }: { inherit @@ -9,6 +12,16 @@ let ; }; + mkSystemdDependencyOption = + { default, ... }: lib.mkOption { + type = lib.types.attrsOf lib.types.str; + description = "The systemd services this sync depends on."; + example = { + secrets = "sops-nix.service"; + }; + inherit default; + }; + syncProviders = { nextcloud = mkProvider {}; proton = mkProvider {}; @@ -30,12 +43,8 @@ let 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"; - }; + systemdDependencies = mkSystemdDependencyOption { + default = cfg.systemdDependencies; }; environment = lib.mkOption { @@ -61,6 +70,10 @@ in { programs.rclone = { enable = lib.mkEnableOption "rclone"; + systemdDependencies = mkSystemdDependencyOption { + default = {}; + }; + configs = lib.mkOption { type = lib.types.attrsOf syncType; description = "The synchronizations to set up."; @@ -69,77 +82,79 @@ in { }; }; - config = - let - cfg = config.programs.rclone; - in { - home.packages = lib.optional cfg.enable pkgs.rclone; + config = { + home.packages = lib.optional cfg.enable pkgs.rclone; - systemd.user = lib.optionalAttrs cfg.enable { - enable = true; + 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 - (name: sync: - let - serviceName = "rclone-sync-${name}"; - in { - ${serviceName} = { - Unit = { - Description = "${sync.type}"; - }; - - 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); - }; - }; - }) - cfg.configs); - - targets.rclone = { + services = { + rclone = { Unit = { - Description = "rclone Mounts"; + 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 + (name: sync: + let + serviceName = "rclone-sync-${name}"; + in { + ${serviceName} = { + Unit = { + Description = "${sync.type}"; + }; + + 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)"; }; }; }; + }; }