Nix-shell not rebuilding when github rev changes


On my nix-shell I pull src from github using the revision from a local file (Cargo.toml):

  src = super.fetchFromGitHub {
    rev = (builtins.fromTOML (builtins.readFile ../Cargo.toml)).patch.crates-io.rpc.rev;
    sha256 = "xxxxx";

However, when I change the revision on the toml and exit&reenter the nix-shell that does not cause a rebuild of the src, I have to nix-collect-garbage.
Is there a way to get this to happen automatically, otherwise this might cause some issues on my CI system. I’d like to avoid “collecting garbage” there as most of the time the rev won’t change and I’d prefer to to rebuild src every run.

Thank you

do you also update the sha256 when you update the rev?

nix looks at the hash from the fetcher, if it can fetch that hash from a local/remote cache then it won’t re-fetch anything.

There are add on tools that does the job of recomputing this hash for you, saving you the effort of manually doing it every time you bump a version or revision.

As a trick that would force a rebuild, set the name to the same value as the ref. This will ensure recompilation even with same sha256, as any entry is identified by its sha256 and its name, and most, if not all, fetchers use name = "source" as the default.

Thanks both, if I change the rev from 1 to 2 and the sha from 1 to 2 it does indeed “notice” the change and pulls the rev2.

However, if I then change it back to the previous rev1 and sha1 it does not “notice” this, and still seems to use the rev2!
EDIT: nevermind, I must have done something wrong to hit this like not exiting another shell or something…

1 Like

If you change neither name nor sha256, nix will use whatever it knows under that sha-name combination.

That is why I suggest to use the “moving” parts in the name as well:

super.fetchFromGitHub (let rev = (builtins.fromTOML (builtins.readFile ../Cargo.toml)).patch.crates-io.rpc.rev; in {
  name = "${pname}-${rev}-source";
  inherit rev;
  sha256 = "xxxxx";
1 Like

Ah got it, thanks @NobbZ this is perfect :+1: