From 06da97fc3a753e0bbebc03ebd63bb1bde2cdda03 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 13 May 2022 08:59:27 +0200 Subject: [PATCH] lib.types.functionTo: Support type merging --- lib/tests/modules.sh | 1 + .../modules/functionTo/submodule-options.nix | 59 +++++++++++++++++++ lib/types.nix | 2 + 3 files changed, 62 insertions(+) create mode 100644 lib/tests/modules/functionTo/submodule-options.nix 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..a + ({ ... }: { + options = { + fun = lib.mkOption { + type = types.functionTo (types.submodule { + options.a = lib.mkOption { }; + }); + }; + }; + }) + + # fun..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.