Feat(clanLib): init types {uniqueDeferredSerializableModule}
This commit is contained in:
@@ -43,6 +43,8 @@ lib.fix (clanLib: {
|
|||||||
inventory = clanLib.callLib ./inventory { };
|
inventory = clanLib.callLib ./inventory { };
|
||||||
modules = clanLib.callLib ./inventory/frontmatter { };
|
modules = clanLib.callLib ./inventory/frontmatter { };
|
||||||
test = clanLib.callLib ./test { };
|
test = clanLib.callLib ./test { };
|
||||||
|
# Custom types
|
||||||
|
types = clanLib.callLib ./types { };
|
||||||
|
|
||||||
# Plain imports.
|
# Plain imports.
|
||||||
introspection = import ./introspection { inherit lib; };
|
introspection = import ./introspection { inherit lib; };
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ rec {
|
|||||||
./introspection/flake-module.nix
|
./introspection/flake-module.nix
|
||||||
./inventory/flake-module.nix
|
./inventory/flake-module.nix
|
||||||
./jsonschema/flake-module.nix
|
./jsonschema/flake-module.nix
|
||||||
|
./types/flake-module.nix
|
||||||
];
|
];
|
||||||
flake.clanLib = import ./default.nix {
|
flake.clanLib = import ./default.nix {
|
||||||
inherit lib inputs self;
|
inherit lib inputs self;
|
||||||
|
|||||||
28
lib/types/default.nix
Normal file
28
lib/types/default.nix
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
{ lib, ... }:
|
||||||
|
{
|
||||||
|
uniqueDeferredSerializableModule = lib.fix (
|
||||||
|
self:
|
||||||
|
# Essentially the "raw" type, but with a custom name and check
|
||||||
|
lib.mkOptionType {
|
||||||
|
name = "deferredModule";
|
||||||
|
description = "deferred module that has custom check and merge behavior";
|
||||||
|
descriptionClass = "noun";
|
||||||
|
# Unfortunately, tryEval doesn't catch JSON errors
|
||||||
|
check = value: lib.seq (builtins.toJSON value) true;
|
||||||
|
merge = lib.options.mergeUniqueOption {
|
||||||
|
message = "------";
|
||||||
|
merge = loc: defs: {
|
||||||
|
imports = map (
|
||||||
|
def: lib.setDefaultModuleLocation "${def.file}, via option ${lib.showOption loc}" def.value
|
||||||
|
) defs;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
functor = {
|
||||||
|
inherit (self) name;
|
||||||
|
type = self;
|
||||||
|
# Non mergable type
|
||||||
|
binOp = _a: _b: null;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
25
lib/types/flake-module.nix
Normal file
25
lib/types/flake-module.nix
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
{ self, inputs, ... }:
|
||||||
|
{
|
||||||
|
perSystem =
|
||||||
|
{ ... }:
|
||||||
|
let
|
||||||
|
# Module that contains the tests
|
||||||
|
# This module adds:
|
||||||
|
# - legacyPackages.<system>.eval-tests-hello-world
|
||||||
|
# - checks.<system>.eval-tests-hello-world
|
||||||
|
test-types-module = (
|
||||||
|
self.clanLib.test.flakeModules.makeEvalChecks {
|
||||||
|
module = throw "";
|
||||||
|
inherit self inputs;
|
||||||
|
testName = "types";
|
||||||
|
tests = ./tests.nix;
|
||||||
|
# Optional arguments passed to the test
|
||||||
|
testArgs = { };
|
||||||
|
}
|
||||||
|
);
|
||||||
|
in
|
||||||
|
{
|
||||||
|
imports = [ test-types-module ];
|
||||||
|
legacyPackages.xxx = { };
|
||||||
|
};
|
||||||
|
}
|
||||||
41
lib/types/tests.nix
Normal file
41
lib/types/tests.nix
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
{ lib, clanLib, ... }:
|
||||||
|
let
|
||||||
|
evalSettingsModule =
|
||||||
|
m:
|
||||||
|
lib.evalModules {
|
||||||
|
modules = [
|
||||||
|
{
|
||||||
|
options.foo = lib.mkOption {
|
||||||
|
type = clanLib.types.uniqueDeferredSerializableModule;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
m
|
||||||
|
];
|
||||||
|
};
|
||||||
|
in
|
||||||
|
{
|
||||||
|
test_1 =
|
||||||
|
let
|
||||||
|
eval = evalSettingsModule {
|
||||||
|
foo = { };
|
||||||
|
};
|
||||||
|
in
|
||||||
|
{
|
||||||
|
inherit eval;
|
||||||
|
expr = eval.config.foo;
|
||||||
|
expected = {
|
||||||
|
# Foo has imports
|
||||||
|
# This can only ever be one module due to the type of foo
|
||||||
|
imports = [
|
||||||
|
{
|
||||||
|
# This is the result of 'setDefaultModuleLocation'
|
||||||
|
# Which also returns exactly one module
|
||||||
|
_file = "<unknown-file>, via option foo";
|
||||||
|
imports = [
|
||||||
|
{ }
|
||||||
|
];
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user