nix-filter icon indicating copy to clipboard operation
nix-filter copied to clipboard

Comparison with `cleanSourceWith`

Open asymmetric opened this issue 4 years ago • 3 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?

asymmetric avatar May 05 '21 14:05 asymmetric

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.

ilkecan avatar Sep 07 '21 21:09 ilkecan

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.

zimbatm avatar Jan 17 '22 10:01 zimbatm

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

fzakaria avatar Jul 31 '22 01:07 fzakaria