Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/modules: prepareImport, nixos: import the flake default module #197215

Closed
wants to merge 2 commits into from
Closed
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
13 changes: 10 additions & 3 deletions lib/modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ rec {
# when resolving module structure (like in imports). For everything else,
# there's _module.args. If specialArgs.modulesPath is defined it will be
# used as the base path for disabledModules.
#
# specialArgs.prepareImport: A function that preprocess the raw `imports`.
# This can be used to pick out the default module in flake.
# The argument can be of any type. The return value must
# be a valid imports item. It should not ignore any imports,
# so that imports preparation is a convenience, not a liability.
specialArgs ? {}
, # This would be remove in the future, Prefer _module.args option instead.
args ? {}
Expand Down Expand Up @@ -251,6 +257,7 @@ rec {

merged =
let collected = collectModules
(specialArgs.prepareImport or (x: x))
(specialArgs.modulesPath or "")
(regularModules ++ [ internalModule ])
({ inherit lib options config specialArgs; } // specialArgs);
Expand Down Expand Up @@ -337,7 +344,7 @@ rec {
#
# Collects all modules recursively through `import` statements, filtering out
# all modules in disabledModules.
collectModules = let
collectModules = prepareImport: let

# Like unifyModuleSyntax, but also imports paths and calls functions if necessary
loadModule = args: fallbackFile: fallbackKey: m:
Expand Down Expand Up @@ -382,7 +389,7 @@ rec {
in parentFile: parentKey: initialModules: args: collectResults (imap1 (n: x:
let
module = loadModule args parentFile "${parentKey}:anon-${toString n}" x;
collectedImports = collectStructuredModules module._file module.key module.imports args;
collectedImports = collectStructuredModules module._file module.key (map prepareImport module.imports) args;
in {
key = module.key;
module = module;
Expand All @@ -407,7 +414,7 @@ rec {
});

in modulesPath: initialModules: args:
filterModules modulesPath (collectStructuredModules unknownModule "" initialModules args);
filterModules modulesPath (collectStructuredModules unknownModule "" (map prepareImport initialModules) args);

/* Wrap a module with a default location for reporting errors. */
setDefaultModuleLocation = file: m:
Expand Down
4 changes: 4 additions & 0 deletions lib/tests/modules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,10 @@ 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

# specialArgs.prepareImport
checkConfigOutput 'ok' config.result ./prepareImport.nix


cat <<EOF
====== module tests ======
$pass Pass
Expand Down
24 changes: 24 additions & 0 deletions lib/tests/modules/prepareImport.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{ config, lib, ... }: {
options.result = lib.mkOption { };
config.result = (lib.evalModules {
specialArgs.prepareImport = x: x.testModules.default or x;
modules = [
{
testModules.default = {
config = {
v = "ok";
};
};
}
{
imports = [
{
testModules.default = {
options.v = lib.mkOption { };
};
}
];
}
];
}).config.v;
}
6 changes: 6 additions & 0 deletions nixos/lib/eval-config-minimal.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ let
inherit prefix modules;
specialArgs = {
modulesPath = builtins.toString ../modules;
prepareImport = x:
# TODO https://github.com/NixOS/nix/issues/7186
# x._type or null == "flake"
if x?sourceInfo && x?inputs && x?nixosModules.default
then x.nixosModules.default
else x;
} // specialArgs;
};

Expand Down