nix icon indicating copy to clipboard operation
nix copied to clipboard

fetchGit is flaky: sometimes cannot find git revision

Open DavHau opened this issue 1 year ago • 10 comments

Describe the bug

The problem only occurs since nix 2.21.x

fetchGit is flaky on an empty ~/.cache/nix/gitv3 and can often crash like that:

error: Cannot find Git revision '43c751b41d74f96cbbd4e07b7aec9675651e2951' in [...]

Steps To Reproduce

  • Evaluate fetchGit { url="https://github.com/sebastianbergmann/environment.git"; rev = "43c751b41d74f96cbbd4e07b7aec9675651e2951";} on an empty ~/.cache/nix/gitv3 several times in a row.
  • notice that sometimes the fetching works and sometimes it crashes.
  • To speed up the evaluation, you can set shallow=true. It does not affect the outcome.

Here an example with 3 evaluations of the same expression, succeeding 2 times and failing one time.

❯ rm -rf ~/.cache/nix/gitv3

~
❯ nix repl
Nix 2.21.2
Type :? for help.
nix-repl> fetchGit { url="https://github.com/sebastianbergmann/environment.git"; rev = "43c751b41d74f96cbbd4e07b7aec9675651e2951"; }
fatal: unable to access 'https://github.com/sebastianbergmann/environment.git/': Could not resolve host: github.com
warning: could not read HEAD ref from repo at 'https://github.com/sebastianbergmann/environment.git', using 'master'
warning: could not update mtime for file '/home/grmpf/.cache/nix/gitv3/0da421j037zz3w3qxl31dc3r7s84xsrgr7hs25yn3ic0h6iqald1/refs/heads/master': No such file or directory
fatal: Refusing to point HEAD outside of refs/
warning: could not update cached head 'master' for 'https://github.com/sebastianbergmann/environment.git'
{
  lastModified = 1681191566;
  lastModifiedDate = "20230411053926";
  narHash = "sha256-Ttez88NVbtHeT/4AglFTEdsGqLgkKTuvqP7uR4tP7BI=";
  outPath = "/nix/store/391g2lyf8f1014riqpfdbn93s9gjv42h-source";
  rev = "43c751b41d74f96cbbd4e07b7aec9675651e2951";
  revCount = 448;
  shortRev = "43c751b";
  submodules = false;
}



~ took 13s
❯ rm -rf ~/.cache/nix/gitv3

~
❯ nix repl
Nix 2.21.2
Type :? for help.
nix-repl> fetchGit { url="https://github.com/sebastianbergmann/environment.git"; rev = "43c751b41d74f96cbbd4e07b7aec9675651e2951"; }
warning: could not update mtime for file '/home/grmpf/.cache/nix/gitv3/0da421j037zz3w3qxl31dc3r7s84xsrgr7hs25yn3ic0h6iqald1/refs/heads/main': No such file or directory
{
  lastModified = 1681191566;
  lastModifiedDate = "20230411053926";
  narHash = "sha256-Ttez88NVbtHeT/4AglFTEdsGqLgkKTuvqP7uR4tP7BI=";
  outPath = "/nix/store/391g2lyf8f1014riqpfdbn93s9gjv42h-source";
  rev = "43c751b41d74f96cbbd4e07b7aec9675651e2951";
  revCount = 448;
  shortRev = "43c751b";
  submodules = false;
}



~ took 49s
❯ rm -rf ~/.cache/nix/gitv3

~
❯ nix repl
Nix 2.21.2
Type :? for help.
nix-repl> fetchGit { url="https://github.com/sebastianbergmann/environment.git"; rev = "43c751b41d74f96cbbd4e07b7aec9675651e2951"; }
fatal: unable to access 'https://github.com/sebastianbergmann/environment.git/': Could not resolve host: github.com
warning: could not update mtime for file '/home/grmpf/.cache/nix/gitv3/0da421j037zz3w3qxl31dc3r7s84xsrgr7hs25yn3ic0h6iqald1/refs/heads/main': No such file or directory
error:
       … while calling the 'fetchGit' builtin
         at «string»:1:1:
            1| fetchGit { url="https://github.com/sebastianbergmann/environment.git"; rev = "43c751b41d74f96cbbd4e07b7aec9675651e2951"; }
             | ^

       … while fetching the input 'git+https://github.com/sebastianbergmann/environment.git?exportIgnore=1&rev=43c751b41d74f96cbbd4e07b7aec9675651e2951'

       error: Cannot find Git revision '43c751b41d74f96cbbd4e07b7aec9675651e2951' in ref 'refs/heads/main' of repository 'https://github.com/sebastianbergmann/environment.git'! Please make sure that the rev exists on the ref you've specified or add allRefs = true; to fetchGit.

What can be observed is that the warnings raised by nix are not reproducible as well. Though, I could not determine any correlation between the warnings and the outcome.

Expected behavior

nix-env --version output

Additional context

Add any other context about the problem here.

Priorities

Add :+1: to issues you find important.

DavHau avatar Apr 08 '24 10:04 DavHau

The problem only occurs since nix 2.21.x

DavHau avatar Apr 08 '24 10:04 DavHau

Small correction. I mistakenly associated the buildbot eval failure of https://github.com/nix-community/dream2nix/pull/921 with the current bug, but in fact the reason for it was a missing allRefs = true which made pre 2.20 nix versions fail.

Nevertheless the current bug still exists and I had to downgrade my nix version to 2.20.x to be able to do my work.

DavHau avatar Apr 09 '24 06:04 DavHau

This problem appears no longer in the latest nix version:

% nix-shell -p nixVersions.nix_2_24

[nix-shell:~]$ nix repl
Nix 2.24.3
Type :? for help.
nix-repl> fetchGit { url="https://github.com/sebastianbergmann/environment.git"; rev = "43c751b41d74f96cbbd4e07b7aec9675651e2951"; }
warning: could not update mtime for file '/home/joerg/.cache/nix/gitv3/0da421j037zz3w3qxl31dc3r7s84xsrgr7hs25yn3ic0h6iqald1/refs/heads/main': error: changing modification time of '"/home/joerg/.cache/nix/gitv3/0da421j037zz3w3qxl31dc3r7s84xsrgr7hs25yn3ic0h6iqald1/refs/heads/main"': No such file or directory
{
  lastModified = 1681191566;
  lastModifiedDate = "20230411053926";
  narHash = "sha256-Ttez88NVbtHeT/4AglFTEdsGqLgkKTuvqP7uR4tP7BI=";
  outPath = "/nix/store/391g2lyf8f1014riqpfdbn93s9gjv42h-source";
  rev = "43c751b41d74f96cbbd4e07b7aec9675651e2951";
  revCount = 448;
  shortRev = "43c751b";
  submodules = false;
}

I don't remember exactly where, but I remember there were some changes done to not using libgit or something.

Mic92 avatar Aug 24 '24 13:08 Mic92

I am also testing this loop now, but so far no failures:

set -eux
for _ in $(seq 1 1000); do
   rm -rf ~/.cache/nix/gitv3
   nix eval --expr 'fetchGit { url="https://github.com/sebastianbergmann/environment.git"; rev = "43c751b41d74f96cbbd4e07b7aec9675651e2951"; }'
done

Mic92 avatar Aug 24 '24 13:08 Mic92

I believe the fix for #11012 might have fixed this as well? However GitHub does support shallow and it was specified shallow did not affect the outcome, so not 100% convinced.

Anyways, good thing it is fixed.

terlar avatar Aug 24 '24 20:08 terlar

I'm encountering this when trying to fetch some old commits on a poor connection such as

error:
       … while calling the 'fetchGit' builtin
         at «string»:1:1:
            1| builtins.fetchGit {url = "https://github.com/KhronosGroup/Vulkan-Hpp.git"; shallow=true; rev = "128261aa4517aaf3b30809a27311dd7e1cb5dbea"; allRefs=true;}
             | ^

       … while fetching the input 'git+https://github.com/KhronosGroup/Vulkan-Hpp.git?exportIgnore=1&rev=128261aa4517aaf3b30809a27311dd7e1cb5dbea&shallow=1'

       error: Cannot find Git revision '128261aa4517aaf3b30809a27311dd7e1cb5dbea' in ref 'refs/heads/main' of repository 'https://github.com/KhronosGroup/Vulkan-Hpp.git'! Please make sure that the rev exists on the ref you've specified or add allRefs = true; to fetchGit.

expenses avatar Oct 04 '24 13:10 expenses

I've been encountering this as well. I host a Git server (Forgejo) where I host my OS/ZSH/NeoVim configs and I encounter this pretty often. Most of the time it works again after nix flake update. So far I haven't figured out when/why this happens but it always appears after a while when I don't check for updates on these inputs.

EDIT: Well today it didn't help. Had to append .git to the URL so that it triggers a rewrite of those inputs in the lock file.

jiriks74 avatar Dec 27 '24 20:12 jiriks74

In my case only root user is facing the issue. Any idea why?

# rm -fr ~/.cache/nix/gitv3/
# nix repl 
Nix 2.24.12
Type :? for help.
nix-repl> builtins.fetchGit { url = "https://github.com/intel/ipu6-drivers.git"; ref = "master"; rev =  "b4ba63df5922150ec14ef7f202b3589896e0301a"; }
error: executing 'git': No such file or directory
error:
       … while calling the 'fetchGit' builtin
         at «string»:1:1:
            1| builtins.fetchGit { url = "https://github.com/intel/ipu6-drivers.git"; ref = "master"; rev =  "b4ba63df5922150ec14ef7f202b3589896e0301a"; }
             | ^

       … while fetching the input 'git+https://github.com/intel/ipu6-drivers.git?exportIgnore=1&ref=master&rev=b4ba63df5922150ec14ef7f202b3589896e0301a'

       error: Cannot find Git revision 'b4ba63df5922150ec14ef7f202b3589896e0301a' in ref 'master' of repository 'https://github.com/intel/ipu6-drivers.git'! Please make sure that the rev exists on the ref you've specified or add allRefs = true; to fetchGit.

$ nix repl 
Nix 2.24.12
Type :? for help.
nix-repl> builtins.fetchGit { url = "https://github.com/intel/ipu6-drivers.git"; ref = "master"; rev =  "b4ba63df5922150ec14ef7f202b3589896e0301a"; }
{
  lastModified = 1739777904;
  lastModifiedDate = "20250217073824";
  narHash = "sha256-CmIRiz+xCBArl6shFW0vzegYsMUdaeons4r9ycNEvPg=";
  outPath = "/nix/store/xjn9dlr45zr4i7vckinp79gh3kz6k138-source";
  rev = "b4ba63df5922150ec14ef7f202b3589896e0301a";
  revCount = 220;
  shortRev = "b4ba63d";
  submodules = false;
}

p-alik avatar Feb 21 '25 08:02 p-alik

Repeatedly hitting this on a low-power device with a relatively slow connection (pi). Same config working fine on other devices. Had hoped that a nix-prefetch-url would resolve it, but alas -- it succeeds but nixos-rebuild fails.

n8henrie avatar Mar 01 '25 19:03 n8henrie

I had a similar issue that led me here — I was trying to build https://github.com/akarbas/configs, and was getting the following error:

%  make
cd darwin && nix build .#darwinConfigurations.UNiCORN.system \
	--extra-experimental-features 'nix-command flakes'
warning: Git tree '/Users/amin/dev/personal/configs' is dirty
evaluation warning: `programs.zsh.initExtra` is deprecated, use `programs.zsh.initContent` instead.

                    Example: programs.zsh.initContent = "your content here";
cd darwin && sudo ./result/sw/bin/darwin-rebuild switch --flake .#UNiCORN
building the system configuration...
warning: Git tree '/Users/amin/dev/personal/configs' is dirty
fatal: unable to access 'https://github.com/flox/httpmock.git/': SSL certificate problem: unable to get local issuer certificate
error:
       … while calling the 'derivationStrict' builtin
         at <nix/derivation-internal.nix>:37:12:
           36|
           37|   strict = derivationStrict drvAttrs;
             |            ^
           38|

       … while evaluating derivation 'darwin-system-25.05.75b99da'
         whose name attribute is located at /nix/store/baqymak7x7mr5y719pyflz7fcm9rmkxb-source/pkgs/stdenv/generic/make-derivation.nix:480:13

       … while evaluating attribute 'activationScript' of derivation 'darwin-system-25.05.75b99da'
         at /nix/store/58l69qyjjrxvacf943jinrdb6c7mb12p-source/modules/system/default.nix:97:7:
           96|
           97|       activationScript = cfg.activationScripts.script.text;
             |       ^
           98|

       … while evaluating the option `system.activationScripts.script.text':

       … while evaluating definitions from `/nix/store/58l69qyjjrxvacf943jinrdb6c7mb12p-source/modules/system/activation-scripts.nix':

       … while evaluating the option `system.activationScripts.applications.text':

       … while evaluating definitions from `/nix/store/58l69qyjjrxvacf943jinrdb6c7mb12p-source/modules/system/applications.nix':

       (stack trace truncated; use '--show-trace' to show the full, detailed trace)

       error: Cannot find Git revision '84956ea0e3ab4cc44919e4a06862644b58c09469' in ref 'refs/heads/flox/remove_recording_struct' of repository 'https://github.com/flox/httpmock.git'! Please make sure that the rev exists on the ref you've specified or add allRefs = true; to fetchGit.
make: *** [Makefile:17: darwin] Error 1

I verified in nix repl that I can fetch that git repo/revision. The solution was running this command once:

cd nix/darwin && sudo -E ./result/sw/bin/darwin-rebuild switch --flake .#UNiCORN --option sandbox false

Specifically, --option sandbox false. Now it works w/o the flag.

HTH!

AKarbas avatar Jun 04 '25 23:06 AKarbas

I was having similar issues and the real problem was that the user didn't have access to the git repo. Granting them access solved the issue. IMHO the message should be clearer. Something like "Cannot find Git repo" would help more than "Cannot find Git revision" in this case.

Commenting here in case it helps someone else.

yajo avatar Jul 03 '25 08:07 yajo

Isn't the problem:

fatal: unable to access 'https://github.com/sebastianbergmann/environment.git/': Could not resolve host: github.com

Wouldn't that mean that the git repo wasn't even fetched? Then of course the commit isn't found in the repo... nothing was fetched.

fzakaria avatar Jul 11 '25 16:07 fzakaria

I think this is poor network but the error at the bottom is not making that clear

# create a network namespace
sudo ip netns add no-dns

# enter network namespace
sudo ip netns add no-dns

nix eval --expr 'fetchGit { url="https://github.com/sebastianbergmann/environment.git"; rev = "43c751b41d74f96cbbd4e07b7aec9675651e2951"; }'
warning: you don't have Internet access; disabling some network-dependent features
fatal: unable to access 'https://github.com/sebastianbergmann/environment.git/': Failed to connect to github.com port 443 after 30 ms: Could not connect to server
warning: could not read HEAD ref from repo at 'https://github.com/sebastianbergmann/environment.git', using 'master'
fatal: unable to access 'https://github.com/sebastianbergmann/environment.git/': Failed to connect to github.com port 443 after 4 ms: Could not connect to server
fatal: Refusing to point HEAD outside of refs/
warning: could not update cached head 'master' for 'https://github.com/sebastianbergmann/environment.git'
error:
       … while calling the 'fetchGit' builtin
         at «string»:1:1:
            1| fetchGit { url="https://github.com/sebastianbergmann/environment.git"; rev = "43c751b41d74f96cbbd4e07b7aec9675651e2951"; }
             | ^

       … while fetching the input 'git+https://github.com/sebastianbergmann/environment.git?exportIgnore=1&rev=43c751b41d74f96cbbd4e07b7aec9675651e2951'

       error: Cannot find Git revision '43c751b41d74f96cbbd4e07b7aec9675651e2951' in ref 'master' of repository 'https://github.com/sebastianbergmann/environment.git'! Please make sure that the rev exists on the ref you've specified or add allRefs = true; to fetchGit.

It looks like it's "flaky" but the repo really was never cloned.

fzakaria avatar Jul 11 '25 16:07 fzakaria

https://github.com/NixOS/nix/pull/13449 should help and make it more clear.

fzakaria avatar Jul 11 '25 16:07 fzakaria