Skip to content

Transforming Paths

.map takes a function that transforms each discovered path. Transformations are applied after filtering.

import-tree.map (path: { imports = [ path ]; }) ./modules
import-tree.map lib.traceVal ./modules

This prints each discovered path during evaluation — useful for debugging.

Multiple .map calls compose left-to-right (the first map runs first):

lib.pipe import-tree [
(i: i.map import) # import each .nix file
(i: i.map builtins.stringLength) # get the length of each result
(i: i.withLib lib)
(i: i.leafs ./dir)
]

When used with .leafs or .pipeTo, .map transforms paths into arbitrary values — not just modules:

# Read all .md files under a directory
lib.pipe import-tree [
(i: i.initFilter (lib.hasSuffix ".md"))
(i: i.map builtins.readFile)
(i: i.withLib lib)
(i: i.leafs ./docs)
]
# => [ "# Title\n..." "# Other\n..." ]
Contribute Community Sponsor