about summary refs log tree commit diff
path: root/lib/tests/modules
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2024-02-01 20:15:26 +0100
committerRobert Hensing <robert@roberthensing.nl>2024-02-02 07:31:16 +0100
commit29c7665003cdcfe3319c969a9846f594dfeae550 (patch)
treed446477b76ac310569b4cc2720d240ac0f14f49f /lib/tests/modules
parentbc916a4dffcd6b1801b64f44461d0a277e03cdfd (diff)
downloadnixlib-29c7665003cdcfe3319c969a9846f594dfeae550.tar
nixlib-29c7665003cdcfe3319c969a9846f594dfeae550.tar.gz
nixlib-29c7665003cdcfe3319c969a9846f594dfeae550.tar.bz2
nixlib-29c7665003cdcfe3319c969a9846f594dfeae550.tar.lz
nixlib-29c7665003cdcfe3319c969a9846f594dfeae550.tar.xz
nixlib-29c7665003cdcfe3319c969a9846f594dfeae550.tar.zst
nixlib-29c7665003cdcfe3319c969a9846f594dfeae550.zip
lib.modules.doRename: Add condition parameter
This is to support single-to-multi service migrations, so that the
`to` (e.g. `foos.""`) isn't defined unconditionally. See test cases.
Diffstat (limited to 'lib/tests/modules')
-rw-r--r--lib/tests/modules/doRename-condition-enable.nix10
-rw-r--r--lib/tests/modules/doRename-condition-migrated.nix10
-rw-r--r--lib/tests/modules/doRename-condition-no-enable.nix9
-rw-r--r--lib/tests/modules/doRename-condition.nix42
4 files changed, 71 insertions, 0 deletions
diff --git a/lib/tests/modules/doRename-condition-enable.nix b/lib/tests/modules/doRename-condition-enable.nix
new file mode 100644
index 000000000000..e6eabfa6f89a
--- /dev/null
+++ b/lib/tests/modules/doRename-condition-enable.nix
@@ -0,0 +1,10 @@
+{ config, lib, ... }:
+{
+  config = {
+    services.foo.enable = true;
+    services.foo.bar = "baz";
+    result =
+      assert config.services.foos == { "" = { bar = "baz"; }; };
+      true;
+  };
+}
diff --git a/lib/tests/modules/doRename-condition-migrated.nix b/lib/tests/modules/doRename-condition-migrated.nix
new file mode 100644
index 000000000000..8d21610e8ec6
--- /dev/null
+++ b/lib/tests/modules/doRename-condition-migrated.nix
@@ -0,0 +1,10 @@
+{ config, lib, ... }:
+{
+  config = {
+    services.foos."".bar = "baz";
+    result =
+      assert config.services.foos == { "" = { bar = "baz"; }; };
+      assert config.services.foo.bar == "baz";
+      true;
+  };
+}
diff --git a/lib/tests/modules/doRename-condition-no-enable.nix b/lib/tests/modules/doRename-condition-no-enable.nix
new file mode 100644
index 000000000000..66ec004d3147
--- /dev/null
+++ b/lib/tests/modules/doRename-condition-no-enable.nix
@@ -0,0 +1,9 @@
+{ config, lib, options, ... }:
+{
+  config = {
+    result =
+      assert config.services.foos == { };
+      assert ! options.services.foo.bar.isDefined;
+      true;
+  };
+}
diff --git a/lib/tests/modules/doRename-condition.nix b/lib/tests/modules/doRename-condition.nix
new file mode 100644
index 000000000000..c08b3035be6f
--- /dev/null
+++ b/lib/tests/modules/doRename-condition.nix
@@ -0,0 +1,42 @@
+/*
+  Simulate a migration from a single-instance `services.foo` to a multi instance
+  `services.foos.<name>` module, where `name = ""` serves as the legacy /
+  compatibility instance.
+
+  - No instances must exist, unless one is defined in the multi-instance module,
+  or if the legacy enable option is set to true.
+  - The legacy instance options must be renamed to the new instance, if it exists.
+
+  The relevant scenarios are tested in separate files:
+  - ./doRename-condition-enable.nix
+  - ./doRename-condition-no-enable.nix
+ */
+{ config, lib, ... }:
+let
+  inherit (lib) mkOption mkEnableOption types doRename;
+in
+{
+  options = {
+    services.foo.enable = mkEnableOption "foo";
+    services.foos = mkOption {
+      type = types.attrsOf (types.submodule {
+        options = {
+          bar = mkOption { type = types.str; };
+        };
+      });
+      default = { };
+    };
+    result = mkOption {};
+  };
+  imports = [
+    (doRename {
+      from = [ "services" "foo" "bar" ];
+      to = [ "services" "foos" "" "bar" ];
+      visible = true;
+      warn = false;
+      use = x: x;
+      withPriority = true;
+      condition = config.services.foo.enable;
+    })
+  ];
+}