Import all nix files in a directory tree. Discussions: https://oeiuwq.zulipchat.com/join/nqp26cd4kngon6mo3ncgnuap/ dendrix.oeiuwq.com/Dendritic.html
dendritic inputs
3
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: Importable import-tree objects. (#6)

* feat: Importable import-tree objects.

* fix typo. Update README.md

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

authored by oeiuwq.com

Copilot and committed by
GitHub
139d1a75 c5fd656c

+67 -8
+26 -4
README.md
··· 27 27 28 28 ## API usage 29 29 30 - The following goes recursively through the provided `./modules` path and imports the files whose names end with `.nix`. 30 + The following goes recursively through `./modules` and imports all `.nix` files. 31 31 32 32 ```nix 33 33 {config, ...} { 34 34 imports = [ (import-tree ./modules) ]; 35 35 } 36 36 ``` 37 + 38 + For more advanced usage, `import-tree` can be configured via its builder API. 39 + This means that the result of calling a function on an `import-tree` object 40 + is itself another `import-tree` object. 37 41 38 42 </summary> 39 43 ··· 89 93 } 90 94 ``` 91 95 96 + As an special case, when the single argument given to an `import-tree` object is an 97 + attribute-set *meaning it is _NOT_ a path or list of paths*, the `import-tree` object 98 + assumes it is being imported as a module. This way, a pre-configured `import-tree` can 99 + also be used directly in a list of module imports. 100 + 101 + This is useful for authors exposing pre-configured `import-tree`s that users can directly 102 + add to their import list or continue configuring themselves using the API. 103 + 104 + ```nix 105 + let 106 + # imagine this configured tree is actually provided by some flake or library. 107 + # users can directly import it or continue using API methods on it. 108 + configured-tree = import-tree.addPath [./a [./b]]; # paths are configured by library author. 109 + in { 110 + imports = [ configured-tree ]; # but then imported or further configured by the library user. 111 + } 112 + ``` 113 + 92 114 ## Configurable behavior 93 115 94 - `import-tree` functions with custom behavior can be obtained using a builder pattern. 116 + `import-tree` objects with custom behavior can be obtained using a builder pattern. 95 117 For example: 96 118 97 119 ```nix 98 120 lib.pipe import-tree [ 99 - (i: i.mapWith lib.traceVal) # trace all paths 121 + (i: i.mapWith lib.traceVal) # trace all paths. useful for debugging what is being imported. 100 122 (i: i.filtered (lib.hasInfix ".mod.")) # filter nix files by some predicate 101 - (i: i ./modules) # finally, call the configured callable with a path 123 + (i: i ./modules) # finally, call the configured import-tree with a path 102 124 ] 103 125 ``` 104 126
+18
checkmate.nix
··· 121 121 oneElement inner.imports; 122 122 expected = ./tree/x/y.nix; 123 123 }; 124 + 125 + import-tree."test evaluates returned module as part of module-eval" = { 126 + expr = 127 + let 128 + res = lib.modules.evalModules { modules = [ (it ./tree/modules) ]; }; 129 + in 130 + res.config.hello; 131 + expected = "world"; 132 + }; 133 + 134 + import-tree."test can itself be used as a module" = { 135 + expr = 136 + let 137 + res = lib.modules.evalModules { modules = [ (it.addPath ./tree/modules) ]; }; 138 + in 139 + res.config.hello; 140 + expected = "world"; 141 + }; 124 142 }; 125 143 126 144 }
+13 -4
default.nix
··· 58 58 attrs: k: f: 59 59 attrs // { ${k} = f attrs.${k}; }; 60 60 61 - functor = self: perform self.config; 61 + functor = 62 + self: args: 63 + let 64 + imported-as-module = builtins.isAttrs args; 65 + module = { 66 + imports = [ (perform self.__config [ ]) ]; 67 + }; 68 + result = perform self.__config args; 69 + in 70 + if imported-as-module then module else result; 62 71 63 72 callable = 64 73 let 65 - config = { 74 + __config = { 66 75 # Accumulated configuration 67 76 mapf = (i: i); 68 77 filterf = _: true; 69 78 paths = [ ]; 70 79 71 80 __functor = self: f: { 72 - config = (f self); 81 + __config = (f self); 73 82 __functor = functor; 74 83 75 84 # Configuration updates (accumulating) ··· 88 97 }; 89 98 }; 90 99 in 91 - config (c: c); 100 + __config (c: c); 92 101 93 102 in 94 103 callable
+7
tree/modules/hello-option/mod.nix
··· 1 + { lib, ... }: 2 + { 3 + options.hello = lib.mkOption { 4 + type = lib.types.str; 5 + default = "goodbye"; 6 + }; 7 + }
+3
tree/modules/hello-world/mod.nix
··· 1 + { 2 + hello = "world"; 3 + }