Filtering Files
Default Behavior
Section titled “Default Behavior”By default, import-tree includes files that:
- Have the
.nixsuffix - Do not have
/_in their path (underscore-prefixed directories are ignored)
You can narrow this down with filters, or replace the defaults entirely.
filter / filterNot
Section titled “filter / filterNot”filter takes a predicate function string -> bool applied to each file’s path:
# Only import files containing ".mod." in their nameimport-tree.filter (lib.hasInfix ".mod.") ./modulesfilterNot is the inverse — exclude files matching the predicate:
# Skip any file with "experimental" in the pathimport-tree.filterNot (lib.hasInfix "experimental") ./modulesComposing Filters
Section titled “Composing Filters”Multiple filters combine with logical AND — a file must pass all of them:
lib.pipe import-tree [ (i: i.filter (lib.hasInfix "/desktop/")) (i: i.filter (lib.hasSuffix "bar.nix")) (i: i ./modules)]This selects only files under a desktop/ directory whose name ends in bar.nix.
match / matchNot
Section titled “match / matchNot”match takes a regular expression. The regex is tested against the full path using builtins.match:
# Only files named like "word_word.nix"import-tree.match ".*/[a-z]+_[a-z]+\.nix" ./modulesmatchNot excludes files matching the regex:
# Skip files with numeric namesimport-tree.matchNot ".*/[0-9]+\.nix" ./modulesMixing filter and match
Section titled “Mixing filter and match”All filter types compose together:
(import-tree.match ".*_.*\.nix").filter (lib.hasInfix "/src/") ./treeThis finds files matching the regex and containing /src/ in their path.
initFilter — Replacing Defaults
Section titled “initFilter — Replacing Defaults”initFilter replaces the built-in .nix + no-/_ filter entirely. Use it to discover non-Nix files or change the ignore convention:
# Find .txt files instead of .nix filesimport-tree.initFilter (lib.hasSuffix ".txt") ./dir
# Find .nix files but use /ignored/ instead of /_import-tree.initFilter (p: lib.hasSuffix ".nix" p && !lib.hasInfix "/ignored/" p)