WIP: feat: Namespaced includes #3
|
@ -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 namespace == null then
|
||||||
if builtins.isAttrs module || builtins.isFunction module then
|
if builtins.isAttrs module || builtins.isFunction module then
|
||||||
moduleFromValue
|
moduleFromValue
|
||||||
else if builtins.isString module || builtins.isPath module then
|
else if builtins.isString module || builtins.isPath module then
|
||||||
moduleFromPath
|
moduleFromPath
|
||||||
else
|
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 =
|
normalize =
|
||||||
parentFile: parentKey: modules: args:
|
parentFile: parentKey: modules: args:
|
||||||
|
|
|
@ -305,26 +305,21 @@ 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
|
|
||||||
# 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" {
|
(lib.modules.normalize "/aux/example.nix" "example" {
|
||||||
includes = [
|
includes = [
|
||||||
null
|
{ }
|
||||||
{
|
{
|
||||||
module = null;
|
module = { };
|
||||||
namespace = "a";
|
namespace = "a";
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue