Skip to content

Commit

Permalink
lib.evalModules: init transformImport, allow users to preprocess imports
Browse files Browse the repository at this point in the history
  • Loading branch information
hsjobeki committed Sep 27, 2024
1 parent 4a7e44f commit 7698730
Showing 1 changed file with 13 additions and 2 deletions.
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

0 comments on commit 7698730

Please sign in to comment.