From 4e37d03ba265234fe56a7da1e07251d3ec2867d5 Mon Sep 17 00:00:00 2001 From: Yueh-Shun Li Date: Sat, 22 Nov 2025 06:14:29 +0800 Subject: [PATCH] fetchgit: reference hash from finalAttrs.hash --- pkgs/build-support/fetchgit/default.nix | 22 ++++++++++-- pkgs/test/overriding.nix | 46 +++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/pkgs/build-support/fetchgit/default.nix b/pkgs/build-support/fetchgit/default.nix index 7a8f689df439..2a175a71ccd2 100644 --- a/pkgs/build-support/fetchgit/default.nix +++ b/pkgs/build-support/fetchgit/default.nix @@ -47,7 +47,7 @@ lib.makeOverridable ( # Hashes, handled by `lib.fetchers.withNormalizedHash` # whose outputs contain outputHash* attributes. - "hash" + # Use `hash` when overriding with `.overrideAttrs`. "sha256" ]; @@ -131,6 +131,11 @@ lib.makeOverridable ( server admins start using the new version? */ + let + finalHashHasColon = lib.hasInfix ":" finalAttrs.hash; + finalHashColonMatch = lib.match "([^:]+)[:](.*)" finalAttrs.hash; + in + derivationArgs // { inherit name; @@ -145,7 +150,20 @@ lib.makeOverridable ( ++ lib.optionals fetchLFS [ git-lfs ] ++ nativeBuildInputs; - inherit outputHash outputHashAlgo; + hash = + if outputHashAlgo == null || outputHash == "" || lib.hasPrefix outputHashAlgo outputHash then + outputHash + else + "${outputHashAlgo}:${outputHash}"; + + outputHash = + if finalAttrs.hash == "" then + lib.fakeHash + else if finalHashHasColon then + lib.elemAt finalHashColonMatch 1 + else + finalAttrs.hash; + outputHashAlgo = if finalHashHasColon then lib.head finalHashColonMatch else null; outputHashMode = "recursive"; sparseCheckout = diff --git a/pkgs/test/overriding.nix b/pkgs/test/overriding.nix index 2ee08e590408..3daa01b6247d 100644 --- a/pkgs/test/overriding.nix +++ b/pkgs/test/overriding.nix @@ -137,6 +137,52 @@ let }; }; + tests-fetchgit = + let + src-with-sha256 = pkgs.fetchgit { + url = "https://example.com/source.git"; + sha256 = lib.fakeSha256; + }; + in + { + test-fetchgit-hash-compat = { + expr = { + inherit (src-with-sha256) + outputHash + outputHashAlgo + ; + }; + expected = { + outputHash = lib.fakeSha256; + outputHashAlgo = "sha256"; + }; + }; + test-fetchgit-overrideAttrs-hash = { + expr = { + inherit (src-with-sha256.overrideAttrs { hash = pkgs.nix.src.hash; }) + outputHash + outputHashAlgo + ; + }; + expected = { + outputHash = pkgs.nix.src.hash; + outputHashAlgo = null; + }; + }; + test-fetchurl-overrideAttrs-hash-empty = { + expr = { + inherit (src-with-sha256.overrideAttrs { hash = ""; }) + outputHash + outputHashAlgo + ; + }; + expected = { + outputHash = lib.fakeHash; + outputHashAlgo = null; + }; + }; + }; + tests-fetchurl = let src-with-sha256 = pkgs.fetchurl {