Filtering src using an external program

Hello all,

I’ve created a directory tree filtering tool, which I’d like to integrate with nix. The idea is to use it to prune down the src directory, since when nix-ifying existing repos they often contain a lot of cruft that causes unnecessary re-builds.

My original thinking was to provide a prune nix function, which takes the original (messy) src, runs the filtering tool on it, and builds a derivation that outputs the filtered src. Real-life derivations then call this prune function, like
mkDerivation { src = prune "endsWith '.cpp' (basename file)" ./.; ... }
where "endsWith '.cpp' (basename file)" is a filter expression that is read by the filter tool (which is an executable binary).

This only one-quarter-works, because the filtered src still depends upon the original, messy, src. So when the messy src changes, so does the hash of the filtered src, and so so does the hash of the end derivation. So this does not reduce superfluous rebuilds.

Is there a way to have the filtered src not change its hash when files in the original src (that are not included by the filter expression) change? This seems to be difficult by conventional nix logic, so I’m considering two somewhat hacky approaches:

  1. Use __noChroot = true, as detailed here: Nix packaging, the heretic way
  2. Have the filter tool generate a file containing a nix expression, where that nix expression is the list of files to be included in the src. This file can then be imported into the derivation, something like mkDerivation { srcs = (import ./generated_srcs.nix); ... }

Is anyone able to advise which of these two approaches seems most viable? Or if there’s a simpler method that I’ve missed?

Thanks!

The new fileset library should work pretty well for this: Working with local files — nix.dev documentation