From 296399ec68fcc1e9f3120b07d1cdeebec9614ab8 Mon Sep 17 00:00:00 2001 From: Alexander Bantyev Date: Wed, 17 Sep 2025 19:55:06 +0400 Subject: [PATCH] fetchgit: add a `gitConfigFile` argument Passing a `gitConfigFile` argument as a string allows setting a custom git config (as `$GIT_CONFIG_GLOBAL`). The most obvious use is to override URLs with custom mirrors, but others are possible too. --- pkgs/build-support/fetchgit/builder.sh | 5 +++++ pkgs/build-support/fetchgit/default.nix | 4 ++++ pkgs/build-support/fetchgit/tests.nix | 11 +++++++++++ 3 files changed, 20 insertions(+) diff --git a/pkgs/build-support/fetchgit/builder.sh b/pkgs/build-support/fetchgit/builder.sh index 393d0a57d163..8f72881d3b91 100644 --- a/pkgs/build-support/fetchgit/builder.sh +++ b/pkgs/build-support/fetchgit/builder.sh @@ -8,6 +8,11 @@ echo "exporting $url (rev $rev) into $out" runHook preFetch +if [ -n "$gitConfigFile" ]; then + echo "using GIT_CONFIG_GLOBAL=$gitConfigFile" + export GIT_CONFIG_GLOBAL="$gitConfigFile" +fi + $SHELL $fetcher --builder --url "$url" --out "$out" --rev "$rev" --name "$name" \ ${leaveDotGit:+--leave-dotGit} \ ${fetchLFS:+--fetch-lfs} \ diff --git a/pkgs/build-support/fetchgit/default.nix b/pkgs/build-support/fetchgit/default.nix index b2f5f15a309d..37a8c50eb928 100644 --- a/pkgs/build-support/fetchgit/default.nix +++ b/pkgs/build-support/fetchgit/default.nix @@ -1,6 +1,7 @@ { lib, stdenvNoCC, + writeText, git, git-lfs, cacert, @@ -64,6 +65,8 @@ lib.makeOverridable ( fetchTags ? false, # make this subdirectory the root of the result rootDir ? "", + # GIT_CONFIG_GLOBAL (as a file) + gitConfigFile ? null, }: /* @@ -148,6 +151,7 @@ lib.makeOverridable ( postFetch fetchTags rootDir + gitConfigFile ; rev = revWithTag; diff --git a/pkgs/build-support/fetchgit/tests.nix b/pkgs/build-support/fetchgit/tests.nix index 08eac5d59553..a39b8ad1bd93 100644 --- a/pkgs/build-support/fetchgit/tests.nix +++ b/pkgs/build-support/fetchgit/tests.nix @@ -7,6 +7,7 @@ cacert, nix, closureInfo, + lib, ... }: { @@ -194,4 +195,14 @@ rev = "9d9dbe6ed05854e03811c361a3380e09183f4f4a"; hash = "sha256-7DszvbCNTjpzGRmpIVAWXk20P0/XTrWZ79KSOGLrUWY="; }; + + withGitConfig = testers.invalidateFetcherByDrvHash fetchgit { + name = "fetchgit-with-config"; + url = "https://doesntexist.forsure/NixOS/nix"; + rev = "9d9dbe6ed05854e03811c361a3380e09183f4f4a"; + sha256 = "sha256-7DszvbCNTjpzGRmpIVAWXk20P0/XTrWZ79KSOGLrUWY="; + gitConfigFile = builtins.toFile "gitconfig" (lib.generators.toGitINI { + url."https://github.com".insteadOf = "https://doesntexist.forsure"; + }); + }; }