WIP: feat: Namespaced includes #3
|
@ -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 builtins.isAttrs module || builtins.isFunction module then
|
||||
moduleFromValue
|
||||
else if builtins.isString module || builtins.isPath module then
|
||||
moduleFromPath
|
||||
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}"
|
||||
# We don't resolve the module yet if we're under a namespace
|
||||
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 =
|
||||
parentFile: parentKey: modules: args:
|
||||
|
|
|
@ -305,30 +305,25 @@ 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;
|
||||
namespace = "a";
|
||||
}
|
||||
];
|
||||
}).includes;
|
||||
(lib.modules.normalize "/aux/example.nix" "example" {
|
||||
includes = [
|
||||
{ }
|
||||
{
|
||||
module = { };
|
||||
namespace = "a";
|
||||
}
|
||||
];
|
||||
}).includes;
|
||||
in
|
||||
expected == actual;
|
||||
|
||||
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue