diff --git a/pkgs/build-support/fetchgit/default.nix b/pkgs/build-support/fetchgit/default.nix index 7a8f689df439..6bd8b632f2f8 100644 --- a/pkgs/build-support/fetchgit/default.nix +++ b/pkgs/build-support/fetchgit/default.nix @@ -83,6 +83,8 @@ lib.makeOverridable ( # run operations between the checkout completing and deleting the .git # directory. preFetch ? "", + # Shell code executed after `git checkout` and before .git directory removal/sanitization. + postCheckout ? "", # Shell code executed after the file has been fetched # successfully. This can do things like check or transform the file. postFetch ? "", @@ -171,6 +173,7 @@ lib.makeOverridable ( deepClone branchName preFetch + postCheckout postFetch fetchTags rootDir @@ -227,6 +230,10 @@ lib.makeOverridable ( inherit preferLocalBuild meta allowedRequisites; + env = { + NIX_PREFETCH_GIT_CHECKOUT_HOOK = finalAttrs.postCheckout; + }; + passthru = { gitRepoUrl = url; } diff --git a/pkgs/build-support/fetchgit/tests.nix b/pkgs/build-support/fetchgit/tests.nix index 9ccb3ff3058b..c836f830545d 100644 --- a/pkgs/build-support/fetchgit/tests.nix +++ b/pkgs/build-support/fetchgit/tests.nix @@ -17,6 +17,16 @@ sha256 = "sha256-7DszvbCNTjpzGRmpIVAWXk20P0/XTrWZ79KSOGLrUWY="; }; + collect-rev = testers.invalidateFetcherByDrvHash fetchgit { + name = "collect-rev-nix-source"; + url = "https://github.com/NixOS/nix"; + rev = "9d9dbe6ed05854e03811c361a3380e09183f4f4a"; + hash = "sha256-AUTX1K7J5+fojvKYJacXYVV5kio3hrWYz5MCekO6h68="; + postCheckout = '' + git -C "$out" rev-parse HEAD | tee "$out/revision.txt" + ''; + }; + sparseCheckout = testers.invalidateFetcherByDrvHash fetchgit { name = "sparse-checkout-nix-source"; url = "https://github.com/NixOS/nix";