7.1 Source filtering with lib.fileset
Lesson 47 of 68 · 17:29
Video chapters
-
0:00 Why Source Filtering Matters
-
3:10 Using lib.cleanSource and cleanSourceWith
-
7:10 Modern fileset-Based Filtering
-
13:40 Debugging Filters and Best Practices
Transcript
Why Source Filtering Matters
0:00 So at this point we know how to package software, we know how to override things to have multiple variants of it, we know how to create like release Nix files that export or emit multiple packages, we know how to make them depend on each other and so on and so on and we're practically ready for bigger projects now. But before we go there, I want to introduce you to more advanced packaging topics and especially source filtering.
0:33 So one example is what is the problem here? Because this is essentially the kind of derivation that we just wrote until now. I mean, it works, but we have multiple problems here. The thing is that when we say something like this here, we practically copy everything. Of course, this file must essentially be in the same folder as what we're importing here. So we are practically importing at least this Nix expression, but potentially all of them.
1:08 Imagine you fix a typo in a readme file and it leads to multiple hours of recompilation. That's surely unnecessary. Build artifacts from local work is also an interesting problem because imagine you do like incremental development in your project and you have all those .o files or a target folder from your cargo or whatever. And that will when you run nix build on your own checkout to just test if it still works from fresh state, right?
The full transcript comes with the lesson.
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