How to `nix-build` again a built store path?

5.1k Views Asked by At

I create my own repository to fetch some git source.

# packages.nix
with (import <nixpkgs> {});

rec {
  rustcSource = fetchgit {
    url = https://github.com/rust-lang/rust;
    rev = "3191fbae9da539442351f883bdabcad0d72efcb6";
    sha256 = "0w1l14kz9kxyj5dw3w9xxk1fzww5xqs3sf8saay0mh7nkmvrdb59";
  };
}

Then I build rustcSource,

sudo nix-env -f package.nix -A rustcSource

It reveals a store path of /nix/store/096fpy9qjbz5r14aadjnq9d2md9ql9cg-rust-3191fba. The problem is, I forgot to download it's submodules, so I change my expression to include submodules,

with (import <nixpkgs> {});

rec {
  rustcSource = fetchgit {
    url = https://github.com/rust-lang/rust;
    rev = "3191fbae9da539442351f883bdabcad0d72efcb6";
    sha256 = "0w1l14kz9kxyj5dw3w9xxk1fzww5xqs3sf8saay0mh7nkmvrdb59";
    leaveDotGit = true;
    fetchSubmodules = true;
  };
}

But, I discovered that nix-build doesn't recalculate the hash and see that the path has been built. So, it ends up not downloading the submodules.

Q: Is it nix bug? How can I "rebuild" or redownloading the repository?

P.S. Actually I create a derivation with fetchgit in it, but it fails because the submodules doesn't being downloaded. So, the above case simplify my problem.

3

There are 3 best solutions below

3
On

Not a bug, this is by design.

Derivations that specify a hash are called fixed-output derivations and they only check if hash matches any existing derivations in store and use that. So your fetchSubmodules change is ignored.

For more discussion see https://github.com/NixOS/nix/issues/969

0
On

Fetching submodules will result in a package with a different hash. The easiest way to fix this is to change the hash to an invalid value and rebuild the package. The error message will include the correct hash. Use that and rebuild.

1
On

To fix the issue, you need to change the hash to some value that isn't already a valid hash of any path in your nix store.

For fixed-output derivations (those are the ones that have an explicit hash specified and only those get network access), if the hash already matches a path in the nix store, then nix will skip the download and just use the existing path. So slightly changing the hash (so that it no longer matches) should be enough to force a rebuild.