Slow nix eval for Unit Tests after adding a Rust program

Hi folks,

For some time I Unit Test my custom functions. I simply use the default runTests that I execute by nix eval.

Everything works as expected including testing some semi-pure functions like:

{
  yamlToJson =
    inp:
    builtins.fromJSON (
      builtins.readFile (
        runCommandLocal "my-data"
          {
            inherit inp;
            passAsFile = [ "inp" ];
          }
          ''
            ${pkgs.remarshall}/bin/remarshal \
                -if yaml \
                -of json \
                -i "''${inpPath}" \
                -o "''${out}"
          ''
      )
    );
}

Today I added a flake input path:/a/b/c which is a flake to build my Rust application. I also added a function like this:

{
  getSomeCustomData =
    inp:
    builtins.fromJSON (
      builtins.readFile (
        runCommandLocal "my-data"
          {
            inherit inp;
            passAsFile = [ "inp" ];
          }
          ''
            ${pkgs.myrustapp}/bin/myrustapp \
                abc \
                --xyz "''${inpPath}" \
                > "''${out}"
          ''
      )
    );
}

And I wrote a unit test for it. And it works OK - I can run nix eval.

The only problem is that the nix eval takes more than 2 minutes to execute.

Of course for the first time this should take a few minutes to build the Rust app. But then the binary should be in /nix/store and the flake should be locked in Flake.lock.

But no, every execution of nix eval takes a few minutes.

While I’m waiting I get:

copying '«path:/a/b/c?lastModified=1761904727&narHash=sha256-O50S9JSPG7RUByZJE%2B60EJtTLSx%2BmU90IdtxlUVM5Rs%3D»/' to the store

(this is the flake input - the rust app)

and then

copying '/nix/store/ybksbvmjmac5jq2mhginzwldifjqwgw7-source'

(this is a copy of the flake input - the Rust app source)

I don’t understand why it is doing all the work (all the copying) and why it is taking so long. The app is already compiled and the binary is in /nix/store and execution of the app takes less than a second. Do you have any idea what’s wrong?

Thank you.

A part of the problem is likely that you’re reading an entire derivation that isn’t built. This is basically IDF which isn’t a good thing.

1 Like

Your flake is not pointing to GitHub, and therefore the whole src, including directories in .gitignore is copied into Nix store. The Rust target directory often has tens of thousands of files, and it is several GiB large.

1 Like

Thank you. Is it possible to tell Nix not to copy the target dir?

You might symlink the target and keep it outside of the repo.

Or you could create b repo, point the flake to the b and mirror a into it.

git init b
git -C a push --mirror "${PWD}/b"
1 Like