summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2022-05-13 08:59:27 +0200
committerRobert Hensing <robert@roberthensing.nl>2022-05-13 09:01:05 +0200
commit06da97fc3a753e0bbebc03ebd63bb1bde2cdda03 (patch)
tree7983cda12b7a622357d2964d54b7ac37c091bf57 /lib
parentf771d397500b5138329bd206af2634086d51d108 (diff)
downloadnixpkgs-06da97fc3a753e0bbebc03ebd63bb1bde2cdda03.tar
nixpkgs-06da97fc3a753e0bbebc03ebd63bb1bde2cdda03.tar.gz
nixpkgs-06da97fc3a753e0bbebc03ebd63bb1bde2cdda03.tar.bz2
nixpkgs-06da97fc3a753e0bbebc03ebd63bb1bde2cdda03.tar.lz
nixpkgs-06da97fc3a753e0bbebc03ebd63bb1bde2cdda03.tar.xz
nixpkgs-06da97fc3a753e0bbebc03ebd63bb1bde2cdda03.tar.zst
nixpkgs-06da97fc3a753e0bbebc03ebd63bb1bde2cdda03.zip
lib.types.functionTo: Support type merging
Diffstat (limited to 'lib')
-rwxr-xr-xlib/tests/modules.sh1
-rw-r--r--lib/tests/modules/functionTo/submodule-options.nix59
-rw-r--r--lib/types.nix2
3 files changed, 62 insertions, 0 deletions
diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh
index cc13a8d38e3..1a11f4ccfc0 100755
--- a/lib/tests/modules.sh
+++ b/lib/tests/modules.sh
@@ -290,6 +290,7 @@ checkConfigOutput '^"a b"$' config.result ./functionTo/merging-list.nix
 checkConfigError 'A definition for option .fun.\[function body\]. is not of type .string.. Definition values:\n\s*- In .*wrong-type.nix' config.result ./functionTo/wrong-type.nix
 checkConfigOutput '^"b a"$' config.result ./functionTo/list-order.nix
 checkConfigOutput '^"a c"$' config.result ./functionTo/merging-attrs.nix
+checkConfigOutput '^"fun.\[function body\].a fun.\[function body\].b"$' config.result ./functionTo/submodule-options.nix
 
 # moduleType
 checkConfigOutput '^"a b"$' config.resultFoo ./declare-variants.nix ./define-variant.nix
diff --git a/lib/tests/modules/functionTo/submodule-options.nix b/lib/tests/modules/functionTo/submodule-options.nix
new file mode 100644
index 00000000000..7bf70823150
--- /dev/null
+++ b/lib/tests/modules/functionTo/submodule-options.nix
@@ -0,0 +1,59 @@
+{ lib, config, options, ... }:
+let
+  inherit (lib) types;
+in
+{
+  imports = [
+
+    # fun.<function-body>.a
+    ({ ... }: {
+      options = {
+        fun = lib.mkOption {
+          type = types.functionTo (types.submodule {
+            options.a = lib.mkOption { };
+          });
+        };
+      };
+    })
+
+    # fun.<function-body>.b
+    ({ ... }: {
+      options = {
+        fun = lib.mkOption {
+          type = types.functionTo (types.submodule {
+            options.b = lib.mkOption { };
+          });
+        };
+      };
+    })
+  ];
+
+  options = {
+    result = lib.mkOption
+      {
+        type = types.str;
+        default = lib.concatStringsSep " "
+          (lib.concatLists
+            (lib.mapAttrsToList
+              (k: v:
+                if k == "_module"
+                then [ ]
+                else [ (lib.showOption v.loc) ]
+              )
+              (
+                (options.fun.type.getSubOptions [ "fun" ])
+              )
+            )
+          );
+      };
+  };
+
+  config.fun = lib.mkMerge
+    [
+      (input: { inherit (input) a; })
+      (input: { inherit (input) b; })
+      (input: {
+        b = lib.mkForce input.c;
+      })
+    ];
+}
diff --git a/lib/types.nix b/lib/types.nix
index 91b040d2455..70d22d91b6b 100644
--- a/lib/types.nix
+++ b/lib/types.nix
@@ -529,6 +529,8 @@ rec {
       getSubOptions = elemType.getSubOptions;
       getSubModules = elemType.getSubModules;
       substSubModules = m: functionTo (elemType.substSubModules m);
+      functor = (defaultFunctor "functionTo") // { wrapped = elemType; };
+      nestedTypes.elemType = elemType;
     };
 
     # A submodule (like typed attribute set). See NixOS manual.