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 ]; namespaces ++ [ namespace ];
in in
builtins.foldl' filter [ ] normalized; 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 in
builtins.seq throwOnConflict namespacedModules; builtins.seq throwOnConflict normalized;
in in
if lib.modules.validate.keys module then if lib.modules.validate.keys module then
{ {
@ -587,19 +568,36 @@ lib: {
collect = collect =
let let
load = load =
args: file: key: module: args: file: key: module':
let let
namespace = module'.namespace or null;
module = module'.module or module';
moduleFromValue = lib.modules.normalize file key (lib.modules.resolve key module args); moduleFromValue = lib.modules.normalize file key (lib.modules.resolve key module args);
moduleFromPath = lib.modules.normalize (builtins.toString module) (builtins.toString module) ( moduleFromPath = lib.modules.normalize (builtins.toString module) (builtins.toString module) (
lib.modules.resolve (builtins.toString module) (import module) args lib.modules.resolve (builtins.toString module) (import module) args
); );
in in
if builtins.isAttrs module || builtins.isFunction module then if namespace == null then
moduleFromValue if builtins.isAttrs module || builtins.isFunction module then
else if builtins.isString module || builtins.isPath module then moduleFromValue
moduleFromPath 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}"
# We don't resolve the module yet if we're under a namespace
else else
builtins.throw "The provided module must be either an attribute set, function, or path but got ${builtins.typeOf module}"; 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 = normalize =
parentFile: parentKey: modules: args: parentFile: parentKey: modules: args:

View file

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