diff --git a/README.md b/README.md index 6bcd9db..7feaca8 100644 --- a/README.md +++ b/README.md @@ -101,8 +101,10 @@ The following is valid usage: } ``` +Other import-tree objects can also be given as arguments (or in lists) as if they were paths. + As an special case, when the single argument given to an `import-tree` object is an -attribute-set *-it is _NOT_ a path or list of paths-*, the `import-tree` object +attribute-set containing an `options` attribute, the `import-tree` object assumes it is being evaluated as a module. This way, a pre-configured `import-tree` can also be used directly in a list of module imports. diff --git a/checkmate.nix b/checkmate.nix index 7ff1c23..9fa7954 100644 --- a/checkmate.nix +++ b/checkmate.nix @@ -166,6 +166,17 @@ in res.config.hello; expected = "world"; }; + + import-tree."test can take other import-trees as if they were paths" = { + expr = (lit.filter (lib.hasInfix "mod")).leafs [ + (it.addPath ./tree/modules/hello-option) + ./tree/modules/hello-world + ]; + expected = [ + ./tree/modules/hello-option/mod.nix + ./tree/modules/hello-world/mod.nix + ]; + }; }; } diff --git a/default.nix b/default.nix index 5f9d48d..fdfc0cc 100644 --- a/default.nix +++ b/default.nix @@ -29,12 +29,14 @@ let lib: root: let initialFilter = andNot (lib.hasInfix "/_") (lib.hasSuffix ".nix"); + listFilesRecursive = x: if isImportTree x then treeFiles x else lib.filesystem.listFilesRecursive x; + treeFiles = t: (t.withLib lib).leafs.result; in lib.pipe [ paths root ] [ (lib.lists.flatten) - (map lib.filesystem.listFilesRecursive) + (map listFilesRecursive) (lib.lists.flatten) (builtins.filter (compose (and filterf initialFilter) toString)) (map mapf) @@ -60,13 +62,11 @@ let attrs: k: f: attrs // { ${k} = f attrs.${k}; }; - functor = - self: path: - let - imported-as-module = builtins.isAttrs path; - arg = if imported-as-module then [ ] else path; - in - perform self.__config arg; + isImportTree = and (x: x ? __config.__functor) builtins.isAttrs; + + inModuleEval = and (x: x ? options) builtins.isAttrs; + + functor = self: arg: perform self.__config (if inModuleEval arg then [ ] else arg); callable = let