Skip to content

Commit 6aab7a2

Browse files
authored
feat: Allow import-trees to be used in argument as if they were paths. (#12)
1 parent 5d25947 commit 6aab7a2

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ The following is valid usage:
101101
}
102102
```
103103

104+
Other import-tree objects can also be given as arguments (or in lists) as if they were paths.
105+
104106
As an special case, when the single argument given to an `import-tree` object is an
105-
attribute-set *-it is _NOT_ a path or list of paths-*, the `import-tree` object
107+
attribute-set containing an `options` attribute, the `import-tree` object
106108
assumes it is being evaluated as a module. This way, a pre-configured `import-tree` can
107109
also be used directly in a list of module imports.
108110

checkmate.nix

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,17 @@ in
166166
res.config.hello;
167167
expected = "world";
168168
};
169+
170+
import-tree."test can take other import-trees as if they were paths" = {
171+
expr = (lit.filter (lib.hasInfix "mod")).leafs [
172+
(it.addPath ./tree/modules/hello-option)
173+
./tree/modules/hello-world
174+
];
175+
expected = [
176+
./tree/modules/hello-option/mod.nix
177+
./tree/modules/hello-world/mod.nix
178+
];
179+
};
169180
};
170181

171182
}

default.nix

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ let
2929
lib: root:
3030
let
3131
initialFilter = andNot (lib.hasInfix "/_") (lib.hasSuffix ".nix");
32+
listFilesRecursive = x: if isImportTree x then treeFiles x else lib.filesystem.listFilesRecursive x;
33+
treeFiles = t: (t.withLib lib).leafs.result;
3234
in
3335
lib.pipe
3436
[ paths root ]
3537
[
3638
(lib.lists.flatten)
37-
(map lib.filesystem.listFilesRecursive)
39+
(map listFilesRecursive)
3840
(lib.lists.flatten)
3941
(builtins.filter (compose (and filterf initialFilter) toString))
4042
(map mapf)
@@ -60,13 +62,11 @@ let
6062
attrs: k: f:
6163
attrs // { ${k} = f attrs.${k}; };
6264

63-
functor =
64-
self: path:
65-
let
66-
imported-as-module = builtins.isAttrs path;
67-
arg = if imported-as-module then [ ] else path;
68-
in
69-
perform self.__config arg;
65+
isImportTree = and (x: x ? __config.__functor) builtins.isAttrs;
66+
67+
inModuleEval = and (x: x ? options) builtins.isAttrs;
68+
69+
functor = self: arg: perform self.__config (if inModuleEval arg then [ ] else arg);
7070

7171
callable =
7272
let

0 commit comments

Comments
 (0)