numtide / nix-filter

a small self-contained source filtering lib

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Comparison with `cleanSourceWith`

asymmetric opened this issue · comments

I'm currently using cleanSourceWith like:

      let srcFilter = path: type:
        let
          p = baseNameOf path;
        in
          !(
            # ignore CI directories
            (type == "directory" && (p == ".github" || p == "ci")) ||
            # ignore CI files
            p == ".travis.yml" || p == "cloudbuild.yaml" ||
            # ignore flake.(nix|lock)
            p == "flake.nix" || p == "flake.lock" ||
            # ignore docker files
            p == ".dockerignore" || p == "docker-compose.yml" ||
            # ignore misc
            p == "rustfmt.toml"
          );

         # ...

          src = pkgs.lib.cleanSourceWith {
            src = ./.;
            filter = srcFilter;
            name = "foo-source";
          };

What is the advantage of using nix-filter?

Compare yours to below

src = nix-filter {
  root = ./.;
  exclude = [
    .github
    ci
    .travis.yml
    ...
  ];
};

It is shorter and doesn't contain any visual clutter. You are only doing blacklisting on your example; once you start to do whitelisting baseNameOf path won't cut it anymore for the directories. You will need to also define something like relativePath = removePrefix (toString src) name inside the let expression and then check for hasPrefix dir relativePath.

It also provides some other utilities like matchExt that are documented in the README.

Like every library, it tries to create an reusable useful abstraction so that you don't have to reinvent or copy the same thing every time.

These filters need to be super precise in order to minimize rebuilds. The best way to do this is to make them easy to maintain and debug.

I put cleanSourceWith on my ./. src root and it made the derivation take way longer interestingly