Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ rec {
args ? {}
, # This would be remove in the future, Prefer _module.check option instead.
check ? true
# Internal variable to avoid `_key` collisions regardless
# of `extendModules`. Used in `submoduleWith`.
# Test case: lib/tests/modules, "168767"
, extensionOffset ? 0
}:
let
withWarnings = x:
Expand Down Expand Up @@ -338,15 +342,17 @@ rec {
modules ? [],
specialArgs ? {},
prefix ? [],
extensionOffset ? length modules,
}:
evalModules (evalModulesArgs // {
modules = regularModules ++ modules;
specialArgs = evalModulesArgs.specialArgs or {} // specialArgs;
prefix = extendArgs.prefix or evalModulesArgs.prefix;
inherit extensionOffset;
});

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

result = withWarnings {
Expand Down
6 changes: 5 additions & 1 deletion lib/tests/modules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ checkConfigOutput '^"a c"$' config.result ./functionTo/merging-attrs.nix

# moduleType
checkConfigOutput '^"a b"$' config.resultFoo ./declare-variants.nix ./define-variant.nix
checkConfigOutput '^"a y z"$' config.resultFooBar ./declare-variants.nix ./define-variant.nix
checkConfigOutput '^"a b y z"$' config.resultFooBar ./declare-variants.nix ./define-variant.nix
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't recognize the problem at the time.

The assertion looks for attrNames config.variants.foo.variants.bar.attrs.

    attrs.a = 1;
    variants.foo.attrs.b = 1;
    variants.bar.attrs.y = 1;
    variants.foo.variants.bar.attrs.z = 1;
    variants.foo.variants.foo.attrs.c = 3;

b occurs in the foo variant, so it should also occur in the foo+bar variant.

checkConfigOutput '^"a b c"$' config.resultFooFoo ./declare-variants.nix ./define-variant.nix

## emptyValue's
Expand Down Expand Up @@ -327,6 +327,10 @@ checkConfigError 'The option .theOption.nested. in .other.nix. is already declar
# Test that types.optionType leaves types untouched as long as they don't need to be merged
checkConfigOutput 'ok' config.freeformItems.foo.bar ./adhoc-freeformType-survives-type-merge.nix

# Anonymous submodules don't get nixed by import resolution/deduplication
# because of an `extendModules` bug, issue 168767.
checkConfigOutput '^1$' config.sub.specialisation.value ./extendModules-168767-imports.nix

cat <<EOF
====== module tests ======
$pass Pass
Expand Down
41 changes: 41 additions & 0 deletions lib/tests/modules/extendModules-168767-imports.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{ lib
, extendModules
, ...
}:
with lib;
{
imports = [

{
options.sub = mkOption {
default = { };
type = types.submodule (
{ config
, extendModules
, ...
}:
{
options.value = mkOption {
type = types.int;
};

options.specialisation = mkOption {
default = { };
inherit
(extendModules {
modules = [{
specialisation = mkOverride 0 { };
}];
})
type;
};
}
);
};
}

{ config.sub.value = 1; }


];
}
10 changes: 8 additions & 2 deletions lib/types.nix
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,11 @@ rec {
{ modules
, specialArgs ? {}
, shorthandOnlyDefinesConfig ? false

# Internal variable to avoid `_key` collisions regardless
# of `extendModules`. Wired through by `evalModules`.
# Test case: lib/tests/modules, "168767"
, extensionOffset ? 0
}@attrs:
let
inherit (lib.modules) evalModules;
Expand All @@ -579,11 +584,11 @@ rec {
allModules = defs: imap1 (n: { value, file }:
if isFunction value
then setFunctionArgs
(args: lib.modules.unifyModuleSyntax file "${toString file}-${toString n}" (value args))
(args: lib.modules.unifyModuleSyntax file "${toString file}-${toString (n + extensionOffset)}" (value args))
(functionArgs value)
else if isAttrs value
then
lib.modules.unifyModuleSyntax file "${toString file}-${toString n}" (shorthandToModule value)
lib.modules.unifyModuleSyntax file "${toString file}-${toString (n + extensionOffset)}" (shorthandToModule value)
else value
) defs;

Expand Down Expand Up @@ -620,6 +625,7 @@ rec {
(base.extendModules {
modules = [ { _module.args.name = last loc; } ] ++ allModules defs;
prefix = loc;
extensionOffset = extensionOffset + length defs;
}).config;
emptyValue = { value = {}; };
getSubOptions = prefix: (base.extendModules
Expand Down