From b0d76f8f46b61a9c0090d3aad770b5fdb84b9c2f Mon Sep 17 00:00:00 2001
From: Manuel Thalmann <m@nuth.ch>
Date: Thu, 16 May 2024 01:16:12 +0200
Subject: [PATCH] Simplify creating login options

---
 lib/modules/rclone.nix | 42 +++++++++++++++++++++++++-----------------
 1 file changed, 25 insertions(+), 17 deletions(-)

diff --git a/lib/modules/rclone.nix b/lib/modules/rclone.nix
index 9e2090f..cdb7d59 100644
--- a/lib/modules/rclone.nix
+++ b/lib/modules/rclone.nix
@@ -8,6 +8,28 @@ let
   nextcloudVendor = "nextcloud";
   mkIfNotNull = value: name: lib.mkIf (value != null) { ${name} = value; };
 
+  mkUsernameOption = { service }: lib.mkOption {
+    type = lib.types.nullOr lib.types.str;
+    description = "The user name for logging in to ${service}.";
+    default = null;
+  };
+
+  mkPasswordOption = { service }: lib.mkOption {
+    type = lib.types.nullOr lib.types.str;
+    description = "The password obscured using the `rclone obscure` command for logging in to ${service}.";
+    default = null;
+  };
+
+  mkPasswordFileOption = { service }: lib.mkOption {
+    type = lib.types.nullOr (lib.types.either lib.types.path lib.types.str);
+    description = "The path to a file containing the password obscured using the `rclone obscure` command for logging in to ${service}.";
+    default = null;
+  };
+
+  mkServerUsernameOption = { service }: mkUsernameOption { service = "the ${service} server."; };
+  mkServerPasswordOption = { service }: mkPasswordOption { service = "the ${service} server."; };
+  mkServerPasswordFileOption = { service }: mkPasswordFileOption { service = "the ${service} server."; };
+
   mkSystemdDependencyOption =
     { default, global ? false, ... }: lib.mkOption {
       type = lib.types.attrsOf (lib.types.listOf lib.types.str);
@@ -106,23 +128,9 @@ let
             default = null;
           };
 
-          username = lib.mkOption {
-            type = lib.types.nullOr lib.types.str;
-            description = "The user name for logging in to the ${displayName} server.";
-            default = null;
-          };
-
-          obscuredPassword = lib.mkOption {
-            type = lib.types.nullOr lib.types.str;
-            description = "The password obscured using the `rclone obscure` command for logging in to the ${displayName} server.";
-            default = null;
-          };
-
-          obscuredPasswordFile = lib.mkOption {
-            type = lib.types.nullOr (lib.types.either lib.types.path lib.types.str);
-            description = "The path to a file containing the password obscured using the `rclone obscure` command for logging in to the ${displayName} server.";
-            default = null;
-          };
+          username = mkServerUsernameOption { service = displayName; };
+          obscuredPassword = mkServerPasswordOption { service = displayName; };
+          obscuredPasswordFile = mkServerPasswordFileOption { service = displayName; };
 
           bearerToken = lib.mkOption {
             type = lib.types.nullOr lib.types.str;