Apply namespace in load rather than lib.modules.normalize

This commit is contained in:
Austreelis 2024-06-22 20:09:37 +02:00
parent 4b9b9c3506
commit 8288e21555
Failed to generate hash of commit
2 changed files with 55 additions and 56 deletions

View file

@ -237,27 +237,8 @@ lib: {
namespaces ++ [ namespace ];
in
builtins.foldl' filter [ ] normalized;
createNamespacedModule =
{ namespace, module, ... }:
if namespace == null then
module
else
{
__key__ = "${__key__}:include-${namespace}";
options.${namespace} = lib.options.create {
description = "options and configuration included from ${namespace}";
default.value = { };
type = lib.types.submodules.of {
modules = [ module ];
description = "include ${namespace}";
};
};
};
namespacedModules = builtins.map createNamespacedModule normalized;
in
builtins.seq throwOnConflict namespacedModules;
builtins.seq throwOnConflict normalized;
in
if lib.modules.validate.keys module then
{
@ -587,19 +568,36 @@ lib: {
collect =
let
load =
args: file: key: module:
args: file: key: module':
let
namespace = module'.namespace or null;
module = module'.module or module';
moduleFromValue = lib.modules.normalize file key (lib.modules.resolve key module args);
moduleFromPath = lib.modules.normalize (builtins.toString module) (builtins.toString module) (
lib.modules.resolve (builtins.toString module) (import module) args
);
in
if namespace == null then
if builtins.isAttrs module || builtins.isFunction module then
moduleFromValue
else if builtins.isString module || builtins.isPath module then
moduleFromPath
else
builtins.throw "The provided module must be either an attribute set, function, or path but got ${builtins.typeOf module}";
builtins.throw "The provided module must be either an attribute set, function, or path but got ${builtins.typeOf module}"
# We don't resolve the module yet if we're under a namespace
else
lib.modules.normalize file key {
__key__ = "${key}:include-${namespace}";
options.${namespace} = lib.options.create {
description = "options and configuration included from ${namespace}";
default.value = { };
type = lib.types.submodules.of {
modules = [ module ];
description = "include ${namespace}";
};
};
};
normalize =
parentFile: parentKey: modules: args:

View file

@ -305,26 +305,21 @@ in
"handles a mixed list" =
let
expected = [
null
{ a = null; }
{
module = { };
namespace = null;
}
{
module = { };
namespace = "a";
}
];
actual =
# Because includes leverage submodules, we can't match the actual
# included namespaced submodule under "a". So we just assert the
# namespace was gotten right and do not evaluate the included value.
builtins.map
(
include:
if builtins.isAttrs include then
builtins.mapAttrs (_: _: null) include.options or include
else
include
)
(lib.modules.normalize "/aux/example.nix" "example" {
includes = [
null
{ }
{
module = null;
module = { };
namespace = "a";
}
];
@ -337,11 +332,11 @@ in
normalized = lib.modules.normalize "/aux/example.nix" "example" {
includes = [
{
module = null;
module = { };
namespace = "a";
}
{
module = null;
module = { };
namespace = "a";
}
];
@ -353,8 +348,8 @@ in
let
normalized = lib.modules.normalize "/aux/example.nix" "example" {
includes = [
null
null
{ }
{ }
];
};
in
@ -363,14 +358,20 @@ in
"handles multiple without namespace" =
let
expected = [
null
null
{
namespace = null;
module = { };
}
{
namespace = null;
module = { };
}
];
actual =
(lib.modules.normalize "/aux/example.nix" "example" {
includes = [
null
null
{ }
{ }
];
}).includes;
in