Skip to content

fetchgit with leaveDotGit = true is still not completely deterministic #8567

Open

Description

@wizeman

The hash of the cargo git repository returned by fetchgit has changed twice already, even though the git revision didn't change (see de322b4 and now #8566).

See also: #4752 and #4767

cc @bjornfor and @madjar (due to #4767).

Activity

  1. bjornfor commented on Jun 30, 2015

    @bjornfor
    Contributor

    How sad. So it may change even with the same git version?

    It'd be interesting to know exactly what in .git/ that changes. But if there is non-determinism for single-threaded git repack of the repo, then I don't know what to do.

    Btw, here is the script I used to "prove" that git clones could be deterministic (if anyone wants to play around with it):

    https://gist.github.com/bjornfor/bb96b09d4bd1a488cd01

    It has a copy of the "make_deterministic_repo()" function from nixpkgs and it creates three repos in current dir with the same basename as the script itself.

    UPDATE: I mean "make_deterministic_repo" (not ...clone())

  2. madjar commented on Jun 30, 2015

    @madjar
    Member

    I feared the day would happen.

    As @bjornfor, it would be great if you could get your hands on two versions of the same derivation with different hashes. This might be hard, because sometimes it depends on the upstream (I first had problems when new commits where pushed to the master branch). Maybe a new branch is added, or something like that?

  3. wizeman commented on Jun 30, 2015

    @wizeman
    MemberAuthor

    Ok, so I tarred up my local copy of the cargo src tree in my /nix/store (which is supposed to have the old sha256 hash), and then used nix-prefetch-git with --leave-dotGit and --fetch-submodules to clone a new tree to see how they differ. I have verified that nix-prefetch-git returned the same new sha256 hash as mentioned in issue #8566.

    This is the resulting diff:
    https://gist.github.com/wizeman/291be066b61e591051af

    You can find the tarballs with full contents here:
    https://dl.dropboxusercontent.com/u/731705/cargo-old.tar.bz2
    https://dl.dropboxusercontent.com/u/731705/cargo-new.tar.bz2

  4. wizeman commented on Jun 30, 2015

    @wizeman
    MemberAuthor

    The most obvious differences are inside the fetched submodule in src/rust-installer, specifically:

    1. In the old clone, there is a .git/shallow file (which contains a hash). This file doesn't exist in the new clone.
    2. In the new clone, there is a new .git/refs/remotes directory, which itself contains an empty origin subdirectory. The old clone doesn't have these directories.

    It seems that there are also differences in the pack files, but I haven't examined these.

  5. wizeman commented on Jun 30, 2015

    @wizeman
    MemberAuthor

    Regarding point 1, it seems that the previous fetchgit did a shallow clone of the src/rust-installer submodule, because git log only shows one commit (the last one). However, now it seems to be doing a non-shallow clone of that submodule, because I can see the commit history (but only up to the same commit as in the previous fetchgit).

  6. madjar commented on Jun 30, 2015

    @madjar
    Member

    Do you have a way to get the git versions used for both clones? If they differ, a change in the behavior might explain the difference.

  7. jagajaga commented on Jun 30, 2015

    @jagajaga
    Member

    Do you use --deepClone?

  8. wizeman commented on Jul 1, 2015

    @wizeman
    MemberAuthor

    Neither the cargo derivation nor my nix-prefetch-git command used the deepClone option, they only used leaveDotGit and the option to fetch submodules.

    Regarding git, I was using version 2.4.1 when I ran the nix-prefetch-git command.
    However, I don't know with which git version my pre-existing cargo source was downloaded with. Apparently, the build logs of fetchgit derivations don't mention the git version...

  9. bjornfor commented on Jul 1, 2015

    @bjornfor
    Contributor

    I grabbed this diff from two repositories at work, cloned at different times (and probably with different git versions):

    https://gist.github.com/bjornfor/5e5ad0a03a52cbc6f70b

  10. andrewrk commented on Jul 16, 2015

    @andrewrk
    Member

    what's the use case for leaveDotGit = true? It seems one could clone the repository with a normal git clone, then rm -rf the .git folder, and viola, you have a deterministic fetch. What am I missing?

  11. aflatter commented on Jul 17, 2015

    @aflatter
    Contributor

    @andrewrk e.g. some ruby gems use git ls-files in their gemspec to define the list of all files in the package.

  12. nbp commented on Jul 17, 2015

    @nbp
    Member

    What is causing such non-determinism? Is that we might be downloading separated commit, then later, as the repository is optimized, we might download packed files?

    I think this is doable if we do not clone all the branches/tag, but only the one that we care about, and run

    git repack -dnA
    git gc --prune=now
    

    Otherwise, we would have to white-list the tags / branches that we are interested in.

  13. nbp commented on Jul 17, 2015

    @nbp
    Member

    Also the solution above was suggested by @bjornfor before.

    Regarding point 1, it seems that the previous fetchgit did a shallow clone of the src/rust-installer submodule, because git log only shows one commit (the last one). However, now it seems to be doing a non-shallow clone of that submodule, because I can see the commit history (but only up to the same commit as in the previous fetchgit).

    Yes, this is an optimization I made a while ago, because otherwise we were pulling full repositories all the time, which is extremely inefficient. I think we can get rid of this limitation when we are downloading a copy of the history, by creating a branch, and making a local non-shared --depth=1 clone of the submodule and replacing the .git, by the one of the cloned version.

  14. wizeman commented on Jul 21, 2015

    @wizeman
    MemberAuthor

    It seems that leaveDotGit still continues to cause hash mismatches for the cargo package, making it very hard to maintain.

    See: de322b4, #8566 and #8862.

  15. 84 remaining items

  16. nixos-discourse commented on Jul 27, 2024

    @nixos-discourse
  17. Scrumplex commented on Sep 4, 2024

    @Scrumplex
    Member

    I think fetchgit should throw an eval warning referencing this issue when leaveDotGit is enabled.

    Additionally, this issue doesn't seem to be documented in https://github.com/NixOS/nixpkgs/blob/43b37c5e92802000e2a988cc6652037b49c06a0e/doc/build-helpers/fetchers.chapter.md#fetchgit-fetchgit

  18. nixos-discourse commented on Oct 3, 2024

    @nixos-discourse
  19. Atemu commented on Oct 8, 2024

    @Atemu
    Member

    There are valid use-cases for leaveDotGit: Extracting information from .git deterministically (i.e. current commit hash) and then manually removing .git before FOD hashing is still deterministic.

    These use-cases could be retained more safely by introducing an argument for commands to be run before .git removal though.

    Deprecating leaveDotGit in favor of that new argument could perhaps happen as a fetchgit2 as there are other issues with the current incantation too:

    #347212

  20. added a commit that references this issue on Jan 3, 2025
  21. added
    2.status: stale
    on Jul 3, 2025
  22. removed
    2.status: stale
    on Jul 16, 2025
  23. me-and commented on Jul 16, 2025

    @me-and
    Member

    Fun!

    I'm inclined to:

    • Make the trick I'm pulling with NIX_PREFETCH_GIT_CHECKOUT_HOOK in my Nix/Git CI tests into a hook that's more readily exposed and usable.

      That would effectively provide a space to run arbitrary code after checking out the repository, but before removing the .git directory or computing the FOD hash. That would cover all the use cases I'm aware of for leaveDotGit.

    • Add a deprecation warning to any calls to fetchgit that set leaveDotGit = true. Hopefully anyone who relies on leaveDotGit for a step that happens after the FOD calculation will see that and be able to rewrite their derivations to use the new hook.

    • Assuming nobody comes up with a use case for leaveDotGit that can't be handled with the new hook, replace the deprecation warning with an error after a release or two.

    The obvious alternative would be to find a way to make the .git directory deterministic, but that's clearly a difficult problem. Even if we were to solve it now, FOD hashes would still need updating when the contents of the .git directory change (e.g. as happened in #372784 with Git v2.48). I could also believe we'd need to do more significant work to restore determinism when there are bigger changes, e.g. some of those currently planned for Git v3.0.

    I'll add this to my to-do list, but that definitely shouldn't stop anyone else who wants to work on this problem from doing so; my to-do list is long and my time is sadly finite...

  24. tobiasBora commented on Jul 17, 2025

    @tobiasBora
    Contributor

    Make the trick I'm pulling with NIX_PREFETCH_GIT_CHECKOUT_HOOK in my Nix/Git CI tests into a hook that's more readily exposed and usable.

    That would effectively provide a space to run arbitrary code after checking out the repository, but before removing the .git directory or computing the FOD hash. That would cover all the use cases I'm aware of for leaveDotGit.

    That's a cool and simple enough idea. I guess to have a working & practical solution we would need to have a way to fake git commands in, e.g., the build phase of the derivation, otherwise it means that we need to manually patch the code which may be quite unpractical as it requires more maintenance and may be impossible to do when git commands are executed by pre-build binaries. This fake git command could read its argument from a file created in your NIX_PREFETCH_GIT_CHECKOUT_HOOK hook. The user could provide to this hook a list of commands to cache, like git rev-parse HEAD, then we would save the stdout/stderr/return status of the command inside a file (e.g. binary json…), and in the build phase, we would create a fake git binary that checks if its inputs are contained in the binary json file, if so it returns the stdout/stderr/status code, if not it forwards the argument to the original git command.

    I'm also thinking that this may even be helpful to deal with derivations that try to download stuff during the build phase (e.g. some npm packages…), so a user could say "cache curl https://foo.com/mybinary" the same way we would cache git rev-parse HEAD. To support commands that download new files like wget, we can improve the binary json format to also include creation of files.

  25. ShamrockLee commented on Nov 27, 2025

    @ShamrockLee
    Contributor

    Hi there,

    I opened PR #465497 to facilitate the NIX_PREFETCH_GIT_CHECKOUT_HOOK with a new fetchgit argument postCheckout. Please take a look!

  26. added
    2.status: stale
    on May 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

2.status: stale6.topic: fetch6.topic: reproducible builds

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions