fetchFromGitHub doesn't fetch new files when I update the rev

stdenv.mkDerivation rec {
  pname = "pjsip";
  version = "2.11.1";

  src = fetchFromGitHub {
    owner = pname;
    repo = "pjproject";
    rev = version;
    sha256 = "1sh7841rzqc99rm24x0mjy3j72r7w820mzac8vn0yf3qh7jx4520";
  };
...

I’m trying to update pjsip. The code is shown as above. I change around the version and run nix-build. I looked into the drv files and just found that the src attribute is always the same ( /nix/store/gc4rpb68l8l4ljjqk6kfynnlp3vl1ciz-source which is the source of 2.10 ). Even if I change the sha256 it doesn’t raise errors. How can I make the version really updated?

I tried your snippet in nix-repl

nix-repl> :b fetchFromGitHub {
    owner = pname;
    repo = "pjproject";
    rev = version;
    sha256 = "1sh7841rzqc99rm24x0mjy3j72r7w820mzac8vn0yf3qh7jx4520";
  }

error: hash mismatch in fixed-output derivation '/nix/store/3g7d62fb1v04w1pbx0mjlx20lwk9a13p-source.drv':
         specified: sha256-QBTS5YF4OA/sRkz9CgTiJ4sjh5cVdCJqTonhnwNBB+o=
            got:    sha256-mqtlxQDIFee93wpdn8oNWmMPDyjYTCmVqF6IJvJbRBM=

Changing the hash downloads the correct version as expected.

nix-repl> :b fetchFromGitHub {
    owner = pname;
    repo = "pjproject";
    rev = version;
    sha256 = "sha256-mqtlxQDIFee93wpdn8oNWmMPDyjYTCmVqF6IJvJbRBM=";
  }

If you do not change the hash, Nix is being told that the contents of the repo have not changed, and thus won’t do any download and will re-use what you have in your store already.

1 Like

Thanks for your help! I expected that the nix will download the file again since the version is changed and tell me the correct hash. It seems I have to changed the hash to trigger a download.

Nix calculates the outPath of a derivation, checks if it already exists and then builds it if not. outPath of a fixed output derivation depends on two things: Its name and its outputHash.

For fetchFromGitHub, name defaults to source, and the output hash is obtained from sha256 in this example, so it won’t be rebuilt if none of these things change. One way to get the property desired by you would be to make name depend on rev:

fetchFromGitHub rec {
  name = "source-${rev}";
  rev = "…";
  sha256 = "…";
  …
}

Now, it will get rebuilt if you either change rev or sha256 (or name of course). However this has a disadvantage: If something with a different name, but the same content exists (say your revision hash becomes a tag), they still become different store paths and can’t be deduplicated automatically on cache.nixos.org. This is why name defaults to "source" by default.

Thanks for your detailed answer!