nix-prefetch-git: add --no-add-path argument

Adds a new argument to `nix-prefetch-git`: `--no-add-path`. It disables
adding the path to the store. It is useful when working with a read-only
store.
This commit is contained in:
Alexander Bantyev
2025-09-24 22:18:32 +04:00
parent 5660d90bfc
commit f12f3dfb21
3 changed files with 72 additions and 2 deletions
+2
View File
@@ -229,6 +229,8 @@
- `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
View File
@@ -59,6 +59,7 @@ Options:
--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.
"
@@ -91,6 +92,7 @@ for arg; do
--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;;
*)
@@ -519,7 +521,14 @@ else
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
+60 -1
View File
@@ -1,4 +1,13 @@
{ testers, fetchgit, ... }:
{
runCommand,
testers,
fetchgit,
nix-prefetch-git,
jq,
cacert,
nix,
...
}:
{
simple = testers.invalidateFetcherByDrvHash fetchgit {
name = "simple-nix-source";
@@ -105,4 +114,54 @@
rootDir = "misc/systemd";
sha256 = "sha256-UhxHk4SrXYq7ZDMtXLig5SigpbITrVgkpFTmryuvpcM=";
};
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=";
};
}