inventory: make resolve module a clanLib function
Unclutter inventory logic
This commit is contained in:
60
lib/resolve-module/default.nix
Normal file
60
lib/resolve-module/default.nix
Normal file
@@ -0,0 +1,60 @@
|
||||
{ lib, ... }:
|
||||
/**
|
||||
Resolve a module from either the clan-core modules or from a flake input.
|
||||
|
||||
The logic is as follows:
|
||||
- If the moduleSpec.input is null then the module is looked up in the clanCore
|
||||
- If the moduleSpec.input is set to a string then the module is looked up in the
|
||||
flake input with that name.
|
||||
*/
|
||||
{
|
||||
moduleSpec,
|
||||
flakeInputs,
|
||||
clanCoreModules,
|
||||
}:
|
||||
let
|
||||
inputError = throw ''
|
||||
Flake doesn't provide input with name '${moduleSpec.input}'
|
||||
|
||||
Choose one of the following inputs:
|
||||
- ${builtins.concatStringsSep "\n- " (lib.attrNames (flakeInputs))}
|
||||
|
||||
To import any official module from '<clan-core>' remove the 'input' attribute from the module definition
|
||||
Remove the following line from the module definition:
|
||||
|
||||
...
|
||||
- module.input = "${moduleSpec.input}"
|
||||
|
||||
'';
|
||||
resolvedModuleSet =
|
||||
# If the module.name is self then take the modules defined in the flake
|
||||
# Otherwise its an external input which provides the modules via 'clan.modules' attribute
|
||||
let
|
||||
input =
|
||||
if moduleSpec.input == null then
|
||||
{ clan.modules = clanCoreModules; }
|
||||
else
|
||||
flakeInputs.${moduleSpec.input} or inputError;
|
||||
in
|
||||
input.clan.modules
|
||||
or (throw "flake input '${moduleSpec.input}' doesn't export any clan services via the `clan.modules` output attribute");
|
||||
|
||||
resolvedModule =
|
||||
resolvedModuleSet.${moduleSpec.name} or (throw ''
|
||||
${
|
||||
lib.optionalString (
|
||||
moduleSpec.input != null
|
||||
) "flake input '${moduleSpec.input}' doesn't provide clan-module with name '${moduleSpec.name}'."
|
||||
}${
|
||||
lib.optionalString (
|
||||
moduleSpec.input == null
|
||||
) "clan-core doesn't provide clan-module with name '${moduleSpec.name}'."
|
||||
}
|
||||
|
||||
Remove `module.input` (or set module.input = null) - to use a <clan-core> module'
|
||||
|
||||
Set `module.input = "self"` if the module is defined in your own flake.
|
||||
Set `module.input = "<flake-input>" if the module is defined by a flake input called `<flake-input>`.
|
||||
'');
|
||||
in
|
||||
resolvedModule
|
||||
70
lib/resolve-module/test.nix
Normal file
70
lib/resolve-module/test.nix
Normal file
@@ -0,0 +1,70 @@
|
||||
# Run: nix-unit ./test-resolve-module.nix
|
||||
{
|
||||
lib ? import <nixpkgs/lib>,
|
||||
clanLib ? import ../default.nix { inherit lib; },
|
||||
}:
|
||||
let
|
||||
resolveModule = clanLib.resolveModule;
|
||||
|
||||
fromSpec =
|
||||
moduleSpec:
|
||||
resolveModule {
|
||||
inherit moduleSpec;
|
||||
flakeInputs = {
|
||||
self.clan.modules = {
|
||||
foo = {
|
||||
name = "self/foo";
|
||||
};
|
||||
};
|
||||
};
|
||||
clanCoreModules = {
|
||||
foo = {
|
||||
name = "clan/foo";
|
||||
};
|
||||
bar = {
|
||||
name = "clan/bar";
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
in
|
||||
{
|
||||
test_default_clan_core = {
|
||||
expr = fromSpec {
|
||||
name = "foo";
|
||||
input = null;
|
||||
};
|
||||
expected = {
|
||||
name = "clan/foo";
|
||||
};
|
||||
};
|
||||
test_self_module = {
|
||||
expr = fromSpec {
|
||||
name = "foo";
|
||||
input = "self";
|
||||
};
|
||||
expected = {
|
||||
name = "self/foo";
|
||||
};
|
||||
};
|
||||
test_missing_self_module = {
|
||||
expr = fromSpec {
|
||||
name = "bar";
|
||||
input = "self";
|
||||
};
|
||||
expectedError = {
|
||||
type = "ThrownError";
|
||||
msg = "flake input 'self' doesn't provide clan-module with name 'bar'";
|
||||
};
|
||||
};
|
||||
test_missing_core_module = {
|
||||
expr = fromSpec {
|
||||
name = "nana";
|
||||
input = null;
|
||||
};
|
||||
expectedError = {
|
||||
type = "ThrownError";
|
||||
msg = "clan-core doesn't provide clan-module with name 'nana'";
|
||||
};
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user