nix-prefetch-git: add --no-add-path argument (#442337)

Changed files
+102 -2
doc
release-notes
pkgs
build-support
+2
doc/release-notes/rl-2511.section.md
···
- `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).
+
- `sftpman` has been updated to version 2, a rewrite in Rust which is mostly backward compatible but does include some changes to the CLI.
For more information, [check the project's README](https://github.com/spantaleev/sftpman-rs#is-sftpman-v2-compatible-with-sftpman-v1).
+10 -1
pkgs/build-support/fetchgit/nix-prefetch-git
···
--fetch-submodules Fetch submodules.
--fetch-tags Fetch all tags (useful for git describe).
--builder Clone as fetchgit does, but url, rev, and out option are mandatory.
+
--no-add-path Do not actually add the contents of the git repo to the store.
--root-dir dir Directory in the repository that will be copied to the output instead of the full repository.
--quiet Only print the final json summary.
"
···
--fetch-submodules) fetchSubmodules=true;;
--fetch-tags) fetchTags=true;;
--builder) builder=true;;
+
--no-add-path) noAddPath=true;;
--root-dir) argfun=set_rootDir;;
-h|--help) usage; exit;;
*)
···
hash=$(nix-hash --type $hashType --base32 "$tmpOut")
# Add the downloaded file to the Nix store.
-
finalPath=$(nix-store --add-fixed --recursive "$hashType" "$tmpOut")
+
+
if test -z "$noAddPath"; then
+
finalPath=$(nix-store --add-fixed --recursive "$hashType" "$tmpOut") \
+
|| { printf "maybe try again with \`nix-prefetch-git --no-add-path <...>' ?\n" >&2; exit 1; }
+
else
+
printf "the path for \`%s' has NOT been added to the store\n" "$url" >&2
+
finalPath=$(nix-store --print-fixed-path --recursive "$hashType" "$hash" "$storePathName")
+
fi
if test -n "$expHash" -a "$expHash" != "$hash"; then
echo "hash mismatch for URL \`$url'. Got \`$hash'; expected \`$expHash'." >&2
+90 -1
pkgs/build-support/fetchgit/tests.nix
···
-
{ testers, fetchgit, ... }:
+
{
+
runCommand,
+
testers,
+
fetchgit,
+
nix-prefetch-git,
+
jq,
+
cacert,
+
nix,
+
closureInfo,
+
...
+
}:
{
simple = testers.invalidateFetcherByDrvHash fetchgit {
name = "simple-nix-source";
···
rootDir = "misc/systemd";
sha256 = "sha256-UhxHk4SrXYq7ZDMtXLig5SigpbITrVgkpFTmryuvpcM=";
};
+
+
# Make sure that if an expected hash is given and the corresponding store path exists already, no fetch is done
+
cached-prefetch-avoids-fetch =
+
let
+
name = "cached-prefetch-avoids-fetch";
+
url = "https://github.com/NixOS/nix";
+
rev = "9d9dbe6ed05854e03811c361a3380e09183f4f4a";
+
sha256 = "sha256-7DszvbCNTjpzGRmpIVAWXk20P0/XTrWZ79KSOGLrUWY=";
+
fetched = fetchgit {
+
inherit
+
name
+
url
+
rev
+
sha256
+
;
+
};
+
in
+
runCommand "cached-prefetch-avoids-fetch"
+
{
+
nativeBuildInputs = [
+
nix-prefetch-git
+
nix
+
];
+
}
+
''
+
export NIX_REMOTE=local?root=$(mktemp -d)
+
nix-store --load-db < ${closureInfo { rootPaths = fetched; }}/registration
+
nix-prefetch-git --name "${name}" "${url}" "${rev}" "${sha256}" > $out
+
'';
+
+
prefetch-git-no-add-path =
+
testers.invalidateFetcherByDrvHash
+
(
+
{
+
name,
+
url,
+
rev,
+
hash,
+
...
+
}:
+
runCommand name
+
{
+
buildInputs = [
+
nix-prefetch-git
+
nix
+
cacert
+
jq
+
];
+
outputHashMode = "recursive";
+
outputHashAlgo = null;
+
outputHash = hash;
+
inherit url rev;
+
}
+
''
+
store_root="$(mktemp -d)"
+
prefetch() { NIX_REMOTE="local?root=$store_root" nix-prefetch-git $@ "$url" --rev "$rev" | jq -r .path; }
+
path="$(prefetch --no-add-path)"
+
if test -e "$store_root/$path"; then
+
echo "$path exists in $NIX_REMOTE when it shouldn't" >&2
+
exit 1
+
fi
+
path_added="$(prefetch)"
+
if ! test -e "$store_root/$path"; then
+
echo "$path_added doesn't exist in NIX_REMOTE when it should" >&2
+
exit 1
+
fi
+
if test "$path" != "$path_added"; then
+
echo "Paths are different with and without --no-add-path: $path != $path_added" >&2
+
exit 1
+
fi
+
cp -r "$store_root/$path_added" "$out"
+
''
+
)
+
{
+
name = "nix-prefetch-git-no-add-path";
+
url = "https://github.com/NixOS/nix";
+
rev = "9d9dbe6ed05854e03811c361a3380e09183f4f4a";
+
hash = "sha256-7DszvbCNTjpzGRmpIVAWXk20P0/XTrWZ79KSOGLrUWY=";
+
};
}