Why `runCommandLocal` for `check-flake-file` ?
Hello from NixCon,
It has been the last 2 days that I am trying to optimise Gitlab CI Nix builds and cache all the things, with the help of @gabyx, @layus and @mic92. I had an issue because I could cache most of the things in the Gitlab CI cache, but totally unable to cache treefmt-check and check-flake-file derivations and their dependencies. At each build, most of the things were downloaded from the cache, except the things related to denful/flake-file and treefmt where everything was downloaded again and again from cache.nixos.org. How frustrating !!!
After exposing the issue to multiple persons at NixCon, @mattsturgeon asked me to check whether those derivations were using runCommandLocal or not... and here I am.
I am going to test a fork of this project locally and replace runCommandLocal with runCommand and see how it behaves, then report back.
https://github.com/denful/flake-file/blob/eccac77d2c2567efb6f368330930f3d55af8668a/modules/write-flake.nix#L179
I keep you posted.
Note: treefmt-check is also using runCommandLocal: https://github.com/NixOS/nixpkgs/blame/601cdefea293a16b71e52bd8f36f2c366f5a3d02/pkgs/by-name/tr/treefmt/check-wrapper.nix#L14, introduced in https://github.com/NixOS/nixpkgs/pull/529783
I just tested locally and replacing runCommandLocal with runCommand doesn't seem to alter the behavior:
- In the caller's project, I run
nix flake check-> it does not fail. - I fork
denful/flake-filelocally - In the caller project, in
flake.nix's inputs, I replacedenful/flake-filewith/home/pol/Code/drupol/flake-file - I edit the
write-flake.nixfile and replacerunCommandLocalwithrunCommand - I run
nix flake update flake-filein the caller project, the input has been updated - I modify manually the caller's
flake.nixfile. - I run
nix flake check-> it is failing
Thanks for testing this, it narrowed things down nicely. Your result is right: runCommandLocal isn't the cause, and here's what we found.
check-flake-file read ${self}/flake.nix, which makes the whole flake source an input of the derivation. So its hash changes on every commit, even one that only touches a README. We confirmed this in dev/: after a README-only commit, check-flake-file, treefmt-check and nix-unit-check all had new .drv hashes.
No cache can have a copy of a derivation it has never seen, so CI has to build it, and building needs its build inputs. Those inputs don't end up in any cache that is filled from outputs, because the check's output is an empty $out with no references:
| check | output closure | build inputs needed |
|---|---|---|
check-flake-file |
1 path | 75 paths (bash, stdenv, diffutils, flake-formatted, nix-auto-follow, …) |
treefmt-check |
0 paths | 179 paths (treefmt, nixfmt, deadnix, prettier, nodejs, git, perl, …) |
Each run rebuilds the checks and fetches those inputs from cache.nixos.org again. Swapping runCommandLocal for runCommand can't help while the hash changes every commit, which is why your test made no difference.
#138 fixes this for check-flake-file. The check now sees only flake.nix and flake.lock (configurable through the new flake-file.check-files), so its hash only changes when those files do. It also uses runCommand with preferLocalBuild = true, so that stable output can be substituted from a cache.
treefmt-check formats the whole tree, so it has to depend on every file and can't be fixed the same way. To stop re-downloading its tools, have your GitLab cache keep the check's build-time closure as well as its outputs, for example nix-store -qR --include-outputs $(nix-store -qd <check-out-path>), or keep-outputs = true + keep-derivations = true on the machine whose store you cache.
Thats a very nice answer and indeed enlightening.