From 296399ec68fcc1e9f3120b07d1cdeebec9614ab8 Mon Sep 17 00:00:00 2001 From: Alexander Bantyev Date: Wed, 17 Sep 2025 19:55:06 +0400 Subject: [PATCH 1/3] 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"; + }); + }; } From 80d9975bd4496412085208ecc7adf9e7d4f023d3 Mon Sep 17 00:00:00 2001 From: Alexander Bantyev Date: Wed, 17 Sep 2025 19:56:24 +0400 Subject: [PATCH 2/3] config: add a `gitConfig`/`gitConfigFile` option Adds a `gitConfig` option (and `gitConfigFile`), to set a default `gitConfigFile` argument for `fetchgit`. --- pkgs/build-support/fetchgit/default.nix | 3 ++- pkgs/build-support/fetchgit/tests.nix | 24 +++++++++-------- pkgs/top-level/config.nix | 34 +++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/pkgs/build-support/fetchgit/default.nix b/pkgs/build-support/fetchgit/default.nix index 37a8c50eb928..45c7599b1c74 100644 --- a/pkgs/build-support/fetchgit/default.nix +++ b/pkgs/build-support/fetchgit/default.nix @@ -1,4 +1,5 @@ { + config, lib, stdenvNoCC, writeText, @@ -66,7 +67,7 @@ lib.makeOverridable ( # make this subdirectory the root of the result rootDir ? "", # GIT_CONFIG_GLOBAL (as a file) - gitConfigFile ? null, + gitConfigFile ? config.gitConfigFile, }: /* diff --git a/pkgs/build-support/fetchgit/tests.nix b/pkgs/build-support/fetchgit/tests.nix index a39b8ad1bd93..9ccb3ff3058b 100644 --- a/pkgs/build-support/fetchgit/tests.nix +++ b/pkgs/build-support/fetchgit/tests.nix @@ -7,7 +7,6 @@ cacert, nix, closureInfo, - lib, ... }: { @@ -196,13 +195,18 @@ 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"; - }); - }; + withGitConfig = + let + pkgs = import ../../.. { + config.gitConfig = { + url."https://github.com".insteadOf = "https://doesntexist.forsure"; + }; + }; + in + pkgs.testers.invalidateFetcherByDrvHash pkgs.fetchgit { + name = "fetchgit-with-config"; + url = "https://doesntexist.forsure/NixOS/nix"; + rev = "9d9dbe6ed05854e03811c361a3380e09183f4f4a"; + sha256 = "sha256-7DszvbCNTjpzGRmpIVAWXk20P0/XTrWZ79KSOGLrUWY="; + }; } diff --git a/pkgs/top-level/config.nix b/pkgs/top-level/config.nix index c3c78e9d3e80..e969e1be3a36 100644 --- a/pkgs/top-level/config.nix +++ b/pkgs/top-level/config.nix @@ -105,6 +105,40 @@ let ''; }; + gitConfig = mkOption { + type = types.attrsOf (types.attrsOf types.anything); + description = '' + The default [git configuration](https://git-scm.com/docs/git-config#_variables) for all [`pkgs.fetchgit`](#fetchgit) calls. + + Among many other potential uses, this can be used to override URLs to point to local mirrors. + + Changing this will not cause any rebuilds because `pkgs.fetchgit` produces a [fixed-output derivation](https://nix.dev/manual/nix/stable/glossary.html?highlight=fixed-output%20derivation#gloss-fixed-output-derivation). + + To set the configuration file directly, use the [`gitConfigFile`](#opt-gitConfigFile) option instead. + + To set the configuration file for individual calls, use `fetchurl { gitConfigFile = "..."; }`. + ''; + default = { }; + example = { + url."https://my-github-mirror.local".insteadOf = [ "https://github.com" ]; + }; + }; + + # A rendered version of gitConfig that can be reused by all pkgs.fetchgit calls + gitConfigFile = mkOption { + type = types.nullOr types.path; + description = '' + A path to a [git configuration](https://git-scm.com/docs/git-config#_variables) file, to be used for all [`pkgs.fetchgit`](#fetchgit) calls. + + This overrides the [`gitConfig`](#opt-gitConfig) option, see its documentation for more details. + ''; + default = + if config.gitConfig != { } then + builtins.toFile "gitconfig" (lib.generators.toGitINI config.gitConfig) + else + null; + }; + doCheckByDefault = mkMassRebuild { feature = "run `checkPhase` by default"; }; From fde6a0b7c35530f99da9e200c4872a7408bcfc11 Mon Sep 17 00:00:00 2001 From: Alexander Bantyev Date: Wed, 17 Sep 2025 20:01:45 +0400 Subject: [PATCH 3/3] Add release notes for fetchgit's gitConfig --- doc/release-notes/rl-2511.section.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/release-notes/rl-2511.section.md b/doc/release-notes/rl-2511.section.md index 7d47a81abc29..653d562c05aa 100644 --- a/doc/release-notes/rl-2511.section.md +++ b/doc/release-notes/rl-2511.section.md @@ -208,6 +208,8 @@ - Added `rewriteURL` attribute to the nixpkgs `config`, to allow for rewriting the URLs downloaded by `fetchurl`. +- Added `gitConfig` and `gitConfigFile` option to the nixpkgs `config`, to allow for setting a default `gitConfigFile` for all `fetchgit` invocations. + - The `dockerTools.streamLayeredImage` builder now uses a better algorithm for generating layered docker images, such that much more sharing is possible when the number of store paths exceeds the layer limit. It gives each of the largest store paths its own layer and adds dependencies to those layers when they aren't used elsewhere. - The systemd initrd will now respect `x-systemd.wants` and `x-systemd.requires` for reliably unlocking multi-disk bcachefs volumes. @@ -244,6 +246,8 @@ * `$debug/lib/debug/.build-id/48/3bd7f7229bdb06462222e1e353e4f37e15c293.sourceoverlay` is a symlink to a directory with the same structure as the expanded `$sourceRoot` but containing only a copy of files which were patched during the build * `$debug/lib/debug/.build-id/48/3bd7f7229bdb06462222e1e353e4f37e15c293.debug` is the file containing debug symbols (like before). +- `fetchgit`: Add `gitConfigFile` argument to set a git config (via `$GIT_CONFIG_GLOBAL`) for the fetcher. + - `fetchgit`: Add `rootDir` argument to limit the resulting source to one subdirectory of the whole Git repository. Corresponding `--root-dir` option added to `nix-prefetch-git`. - `nix-prefetch-git`: Added a `--no-add-path` argument to disable adding the path to the store; this is useful when working with a [read-only store](https://nix.dev/manual/nix/2.28/command-ref/new-cli/nix3-help-stores#store-experimental-local-overlay-store-read-only).