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.evalModules: init transformImport, allow users to preprocess imports #344874

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions lib/modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ let
args ? {}
, # This would be remove in the future, Prefer _module.check option instead.
check ? true
, # This function is used to transform imports.
# It is mapped over all imports recursively and may transform the imported value.
# Defaults to the identity function, which is a no-op.
# Imports can also be filtered by returning `{}` Which is the no-op value.
transformImport ? x: x
}:
let
withWarnings = x:
Expand Down Expand Up @@ -239,6 +244,11 @@ let

merged =
let collected = collectModules
# global Options
{
inherit transformImport;
}
# regular Arguments
class
(specialArgs.modulesPath or "")
(regularModules ++ [ internalModule ])
Expand Down Expand Up @@ -342,7 +352,7 @@ let
#
# Collects all modules recursively through `import` statements, filtering out
# all modules in disabledModules.
collectModules = class: let
collectModules = {transformImport}: class: let

# Like unifyModuleSyntax, but also imports paths and calls functions if necessary
loadModule = args: fallbackFile: fallbackKey: m:
Expand Down Expand Up @@ -409,8 +419,9 @@ let
};
in parentFile: parentKey: initialModules: args: collectResults (imap1 (n: x:
let
finalImports = map transformImport module.imports;
module = checkModule (loadModule args parentFile "${parentKey}:anon-${toString n}" x);
collectedImports = collectStructuredModules module._file module.key module.imports args;
collectedImports = collectStructuredModules module._file module.key finalImports args;
in {
key = module.key;
module = module;
Expand Down