Skip to content

Commit

Permalink
lib/modules: Add attrArgName
Browse files Browse the repository at this point in the history
  • Loading branch information
roberth committed Jun 14, 2022
1 parent 9ef09e0 commit d06f5fa
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ rec {
args ? {}
, # This would be remove in the future, Prefer _module.check option instead.
check ? true
, attrArgName ? "name"
}:
let
withWarnings = x:
Expand Down Expand Up @@ -341,15 +342,17 @@ rec {
modules ? [],
specialArgs ? {},
prefix ? [],
attrArgName ? "name",
}:
evalModules (evalModulesArgs // {
modules = regularModules ++ modules;
specialArgs = evalModulesArgs.specialArgs or {} // specialArgs;
prefix = extendArgs.prefix or evalModulesArgs.prefix or [];
inherit attrArgName;
});

type = lib.types.submoduleWith {
inherit modules specialArgs;
inherit modules specialArgs attrArgName;
};

result = withWarnings {
Expand Down
2 changes: 2 additions & 0 deletions lib/tests/modules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ checkConfigOutput 'ok' config.freeformItems.foo.bar ./adhoc-freeformType-survive
# because of an `extendModules` bug, issue 168767.
checkConfigOutput '^1$' config.sub.specialisation.value ./extendModules-168767-imports.nix

checkConfigOutput '^"foo=qux,bar=xyz"$' config.result ./nested-extendModules-with-name.nix

cat <<EOF
====== module tests ======
$pass Pass
Expand Down
33 changes: 33 additions & 0 deletions lib/tests/modules/nested-extendModules-with-name.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{ config, extendModules, lib, ... }:
let
inherit (lib) mkOption types;
inherit (types) attrsOf submodule;
in
{
options = {
withKey = mkOption {
type = attrsOf (
submodule ({ name, ... }: {
options.value = mkOption {
type = attrsOf (
(extendModules {
attrArgName = name;
}).type
);
};
})
);
};
res = mkOption {
type = types.str;
};
result = mkOption {
default = config.withKey.foo.value.qux.withKey.bar.value.xyz.res;
};
};
config = {
withKey.foo.value.qux.withKey.bar.value.xyz = { foo, bar, lib, config, ... }: {
res = "foo=" + foo + ",bar=" + bar;
};
};
}
5 changes: 3 additions & 2 deletions lib/types.nix
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ rec {
{ modules
, specialArgs ? {}
, shorthandOnlyDefinesConfig ? false
, attrArgName ? "name"
, description ? null
}@attrs:
let
Expand Down Expand Up @@ -599,7 +600,7 @@ rec {
# &gt; and &lt; wouldn't be encoded correctly so the encoded values
# would be used, and use of `<` and `>` would break the XML document.
# It shouldn't cause an issue since this is cosmetic for the manual.
_module.args.name = lib.mkOptionDefault "‹name›";
_module.args.${attrArgName} = lib.mkOptionDefault "‹name›";
}] ++ modules;
};

Expand All @@ -616,7 +617,7 @@ rec {
check = x: isAttrs x || isFunction x || path.check x;
merge = loc: defs:
(base.extendModules {
modules = [ { _module.args.name = last loc; } ] ++ allModules defs;
modules = [ { _module.args.${attrArgName} = last loc; } ] ++ allModules defs;
prefix = loc;
}).config;
emptyValue = { value = {}; };
Expand Down

0 comments on commit d06f5fa

Please sign in to comment.