Skip to content

Commit

Permalink
Merge pull request NixOS#172813 from hercules-ci/functionTo-properly
Browse files Browse the repository at this point in the history
`lib.types.functionTo` type merging and docs
  • Loading branch information
roberth authored May 17, 2022
2 parents 2a093da + 0b02135 commit 2d1a34b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/tests/modules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +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 '^"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
Expand Down
61 changes: 61 additions & 0 deletions lib/tests/modules/functionTo/submodule-options.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{ 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 { default = "a"; };
});
};
};
})

# fun.<function-body>.b
({ ... }: {
options = {
fun = lib.mkOption {
type = types.functionTo (types.submodule {
options.b = lib.mkOption { default = "b"; };
});
};
};
})
];

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 " "
(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: { b = "bee"; })
];
}
4 changes: 3 additions & 1 deletion lib/types.nix
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,11 @@ 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; };
nestedTypes.elemType = elemType;
};

# A submodule (like typed attribute set). See NixOS manual.
Expand Down
8 changes: 7 additions & 1 deletion nixos/lib/make-options-doc/options-to-docbook.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@
<title>Configuration Options</title>
<variablelist xml:id="configuration-variable-list">
<xsl:for-each select="attrs">
<xsl:variable name="id" select="concat('opt-', str:replace(str:replace(str:replace(str:replace(attr[@name = 'name']/string/@value, '*', '_'), '&lt;', '_'), '>', '_'), ':', '_'))" />
<xsl:variable name="id" select="
concat('opt-',
translate(
attr[@name = 'name']/string/@value,
'*&lt; >[]:',
'_______'
))" />
<varlistentry>
<term xlink:href="#{$id}">
<xsl:attribute name="xml:id"><xsl:value-of select="$id"/></xsl:attribute>
Expand Down

0 comments on commit 2d1a34b

Please sign in to comment.