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). 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..45c7599b1c74 100644 --- a/pkgs/build-support/fetchgit/default.nix +++ b/pkgs/build-support/fetchgit/default.nix @@ -1,6 +1,8 @@ { + config, lib, stdenvNoCC, + writeText, git, git-lfs, cacert, @@ -64,6 +66,8 @@ lib.makeOverridable ( fetchTags ? false, # make this subdirectory the root of the result rootDir ? "", + # GIT_CONFIG_GLOBAL (as a file) + gitConfigFile ? config.gitConfigFile, }: /* @@ -148,6 +152,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..9ccb3ff3058b 100644 --- a/pkgs/build-support/fetchgit/tests.nix +++ b/pkgs/build-support/fetchgit/tests.nix @@ -194,4 +194,19 @@ rev = "9d9dbe6ed05854e03811c361a3380e09183f4f4a"; hash = "sha256-7DszvbCNTjpzGRmpIVAWXk20P0/XTrWZ79KSOGLrUWY="; }; + + 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"; };