From 06da97fc3a753e0bbebc03ebd63bb1bde2cdda03 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 13 May 2022 08:59:27 +0200 Subject: [PATCH 1/4] 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. From 062bc5e74a8905d5f1b8573155c558a4d56e55d4 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 13 May 2022 09:00:06 +0200 Subject: [PATCH 2/4] lib.types.functionTo: Add pseudo-attr to generated docs --- lib/types.nix | 2 +- nixos/lib/make-options-doc/options-to-docbook.xsl | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/types.nix b/lib/types.nix index 70d22d91b6b..0be9db30851 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -526,7 +526,7 @@ rec { check = isFunction; merge = loc: defs: fnArgs: (mergeDefinitions (loc ++ [ "[function body]" ]) elemType (map (fn: { inherit (fn) file; value = fn.value fnArgs; }) defs)).mergedValue; - getSubOptions = elemType.getSubOptions; + getSubOptions = prefix: elemType.getSubOptions (prefix ++ [ "[function body]" ]); getSubModules = elemType.getSubModules; substSubModules = m: functionTo (elemType.substSubModules m); functor = (defaultFunctor "functionTo") // { wrapped = elemType; }; diff --git a/nixos/lib/make-options-doc/options-to-docbook.xsl b/nixos/lib/make-options-doc/options-to-docbook.xsl index b286f7b5e2c..49b189ee303 100644 --- a/nixos/lib/make-options-doc/options-to-docbook.xsl +++ b/nixos/lib/make-options-doc/options-to-docbook.xsl @@ -20,7 +20,19 @@ Configuration Options - + From 81a0a8be297a88f338ee0fd90c330cd94b4a55bd Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 13 May 2022 09:09:16 +0200 Subject: [PATCH 3/4] lib/tests/modules: Test functionTo submodule merging too --- lib/tests/modules.sh | 3 ++- .../modules/functionTo/submodule-options.nix | 16 +++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh index 1a11f4ccfc0..c7b8bbbf8c7 100755 --- a/lib/tests/modules.sh +++ b/lib/tests/modules.sh @@ -290,7 +290,8 @@ 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 +checkConfigOutput '^"a bee"$' config.result ./functionTo/submodule-options.nix +checkConfigOutput '^"fun.\[function body\].a fun.\[function body\].b"$' config.optionsResult ./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 index 7bf70823150..b884892efd6 100644 --- a/lib/tests/modules/functionTo/submodule-options.nix +++ b/lib/tests/modules/functionTo/submodule-options.nix @@ -10,7 +10,7 @@ in options = { fun = lib.mkOption { type = types.functionTo (types.submodule { - options.a = lib.mkOption { }; + options.a = lib.mkOption { default = "a"; }; }); }; }; @@ -21,7 +21,7 @@ in options = { fun = lib.mkOption { type = types.functionTo (types.submodule { - options.b = lib.mkOption { }; + options.b = lib.mkOption { default = "b"; }; }); }; }; @@ -30,6 +30,12 @@ in options = { result = lib.mkOption + { + type = types.str; + default = lib.concatStringsSep " " (lib.attrValues (config.fun (throw "shouldn't use input param"))); + }; + + optionsResult = lib.mkOption { type = types.str; default = lib.concatStringsSep " " @@ -50,10 +56,6 @@ in config.fun = lib.mkMerge [ - (input: { inherit (input) a; }) - (input: { inherit (input) b; }) - (input: { - b = lib.mkForce input.c; - }) + (input: { b = "bee"; }) ]; } From 0b02135d3bdf49f9154f60e7a4c9d54d5e0ee70d Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 16 May 2022 22:53:23 +0200 Subject: [PATCH 4/4] nixosOptionsDoc: refactor Thanks to Infinisil for pointing this out. --- nixos/lib/make-options-doc/options-to-docbook.xsl | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/nixos/lib/make-options-doc/options-to-docbook.xsl b/nixos/lib/make-options-doc/options-to-docbook.xsl index 49b189ee303..607db4bb21b 100644 --- a/nixos/lib/make-options-doc/options-to-docbook.xsl +++ b/nixos/lib/make-options-doc/options-to-docbook.xsl @@ -22,17 +22,11 @@ + '*< >[]:', + '_______' + ))" />