Expose import-tree addApi Modules as a list or attrset.
Reason
The title might be a little cryptic; English, not being my native language, unfortunately does not help. To be clearer, here's an example of what I'm doing when exposing my flake modules:
let
modules-tree = import-tree (i: i.addPath (self + "/modules")) (
i:
i.addAPI {
core = self: self.filter (lib.hasInfix "/core/");
default = self: self.filter (lib.hasInfix "/core/");
#desktop = self: self.filter (lib.hasInfix "/desktop/");
#server = self: self.filter (lib.hasInfix "/server/");
all = self: self;
}
);
in
builtins.mapAttrs (name: value: (_: {
imports = [ value ];
config._module.args = specialArgs;
}
)) (builtins.removeAttrs modules-tree
[
"__config"
"__functor"
"addAPI"
"addPath"
"addScoped"
"files"
"filter"
"filterNot"
"initFilter"
"leafs"
"map"
"match"
"matchNot"
"new"
"pipeTo"
"result"
"withLib"
])
This is, of course, overkill just to avoid building an attribute set manually, but it's the import-tree "mission" to be a "write once" and "autodetect modules". This approach works but is extremely fragile to any changes in the import-tree API.
Adding the code duplication of core = self: self.filter (lib.hasInfix "/core/") hurts even more when I want to just import eack directory.
Proposed Solutions
To avoid breaking everyone's use of import-tree, yet to allow for getting the modules, I can see two solutions:
- Add a new attribute
namesas a list containing the detected module names (or paths); - Add a
modulesattribute as an attribute set containing the modules directly (like how many lib attributes are present in leaf attribute sets -lib.splitbeing a shortcut tolib.strings.split).
This would save me from listing the unwanted attributes and save me from any change in the API breaking my flake. Maybe there's something else I'm missing thought.
Thanks for taking the time to read me, and keep up the good work!
Hum. I realized I could have done :
modulesNames = [
"core"
"desktop"
];
modules-tree = import-tree (i: i.addPath (self + "/modules")) (
i:
i.addAPI (
{
all = self: self;
}
// (listToAttrs (x: {
name = x;
value = self: self.filter (lib.hasInfix "/${name}/");
}) modulesNames)
)
);
modules = listToAttrs (
map (x: {
name = x;
value = _: {
imports = [ modules-tree.${x} ];
config._module.args = specialArgs;
};
}) (modulesNames ++ [ "all" ])
);
in
modules
// {
default = modules.core;
}
it doesn't solve the auto-detection of modules folder, but it's clean anyway. Sorry to have bothered you 😓