Handling Git Submodules in Flakes from Nix 2.18 to 2.22 - NAR Hash Mismatch Issues

I’ve encountered a problem with git submodules in flakes when upgrading from Nix 2.18 to Nix 2.22. Previously, the following flake input configuration worked without issues:

inputs.foo = { type = "git"; url = "https://github.com/user/foo.git"; submodules = true; flake = false; };

However, after updating to Nix 2.22, I’m experiencing a NAR hash mismatch error during nix develop :

error: NAR hash mismatch in input 'git+https://github.com/user/foo.git?ref=refs/heads/master&rev=a53859f2167c31ab5225b6c09f30cf05527b94f4&submodules=1', expected 'sha256-SglyTmXOPv8jJOjwAjJrj/WhAkItQfUbvKfUqrynwRg=' but got 'sha256-CR9GxzXpwYSVTHcpJ8AjtvrVlW4xE8JyLg3KrUdcPx8='

This seems to be related to how Nix now handles submodules and calculates NAR hashes for them, which might have changed between these versions. I have read through several discussions and issues, such as:

[1] nix flakes: add support for git submodules · Issue #4423 · NixOS/nix · GitHub
[2] pkgs.fetchgit not deterministic when fetching submodules · Issue #100498 · NixOS/nixpkgs · GitHub
[3] feat: support managing submodules via inputs.self by tomberek · Pull Request #7862 · NixOS/nix · GitHub

Despite these resources, I haven’t found a clear example or explanation on how to adjust my flake configuration to resolve this hash mismatch issue.

Could someone provide some insight or examples of how to properly handle git submodules in flakes with the recent changes in Nix? Any help or guidance would be greatly appreciated!

Thank you!

3 Likes

Can’t tell which it is, but these changes may have caused the hash to change:

It’s possible that the original narHash was computed by an older version of Nix.
Could you clear your fetcher cache and try with the backport?

rm ~/.cache/nix/fetcher-cache*`
# nix build something with the backport applied
nix run nix/2.22-maintenance -- build

Hello, and thank you for your insights. I followed your suggestion to clear the fetcher cache and tried running the backport as you advised, focusing on the development environment since that’s relevant to my workflow (using nix develop instead of nix build ). Here are the results with different versions of Nix:

nix 2.18: Everything works fine.
nix 2.22: After running the following commands:

rm ~/.cache/nix/fetcher-cache*
nix run nix/2.22-maintenance -- develop

I encountered this error:

error: Server does not allow request for unadvertised object 9aa7a5957c07bb6e862fc1a6d3153d109c7407e4
fatal: Fetched in submodule path 'src/jtag/drivers/libjaylink', but it did not contain 9aa7a5957c07bb6e862fc1a6d3153d109

I have not made any changes to my flake inputs or touched the flake.lock file, so it’s possible I’m still using the old NAR hash. Would it be advisable to run a nix flake update for the specific input to potentially resolve this issue?

Does this error also occur when making a fresh clone with, say, git clone --recurse-submodules?
If not, the above fix may be insufficient for remote repos.
Could you provide a reproducer? E.g. a fetchGit or fetchTree call, or a flakeref?

The issue seems to have been resolved in fetchTree: shallow git fetching by default by DavHau · Pull Request #10028 · NixOS/nix · GitHub, as I am unable to reproduce the errors in nix 2.23.0.