7.1 Source filtering with lib.fileset
Lesson 47 of 68 · 17:29
Up next in 5s…
About this lesson
Fixing a typo in a README should never cost you hours of recompilation, yet with src = ./. that is exactly what happens. This lesson covers source filtering in Nix with lib.fileset, lib.cleanSource, and cleanSourceWith, so only the files that actually matter reach the build sandbox. The payoff: more cache hits, fewer rebuilds, and a smaller Nix store.
In this video
- Six ways an unfiltered
src = ./.hurts you, including one almost nobody expects: the name of your checkout folder lib.cleanSourceas the quick fix, andcleanSourceWithwith a custom predicate that drops every.nixfile- Allowlist filtering with
lib.fileset.unions, and why it is the safest and most readable style - Composing filters like Lego bricks: filtering by file extension,
difference, andgitTracked, each a one-liner from a real project - Tracing a filter that drops the wrong files, and the warning sign that your filtering has grown too complicated
After this lesson
You can point any derivation at exactly the files it needs, and you know the one restructuring trick that makes source filtering unnecessary in the first place.
Links:
- https://nixos.org/manual/nixpkgs/stable/#sec-functions-library-fileset
- Example C++ project used in the source filtering demo: https://github.com/tfc/pprintpp