nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix
at devShellTools-shell 76 lines 2.0 kB view raw
1#! /bin/sh -e 2 3url=$1 4rev=$2 5expHash=$3 6 7hashType=$NIX_HASH_ALGO 8if test -z "$hashType"; then 9 hashType=sha256 10fi 11if test -z "$hashFormat"; then 12 hashFormat=--sri 13fi 14 15if test -z "$url"; then 16 echo "syntax: nix-prefetch-bzr URL [REVISION [EXPECTED-HASH]]" >&2 17 exit 1 18fi 19 20revarg="-r $rev" 21test -n "$rev" || revarg="" 22 23repoName=$(echo $url | sed ' 24 s,.*/\([^/]\+\)/trunk/*$,\1,;t 25 s,.*/\([^/]\+\)/branches/\([^/]\+\)/*$,\1-\2,;t 26 s,.*/\([^/]\+\)/tags/\([^/]\+\)/*$,\1-\2,;t 27 s,.*/\([^/]\+\)/*$,\1,;t 28') 29dstFile="bzr-export" 30 31# If the hash was given, a file with that hash may already be in the 32# store. 33if test -n "$expHash"; then 34 finalPath=$(nix-store --print-fixed-path --recursive "$hashType" "$expHash" $dstFile) 35 if ! nix-store --check-validity "$finalPath" 2> /dev/null; then 36 finalPath= 37 fi 38 hash=$expHash 39fi 40 41 42# If we don't know the hash or a path with that hash doesn't exist, 43# download the file and add it to the store. 44if test -z "$finalPath"; then 45 # nix>=2.20 rejects adding symlinked paths to the store, so use realpath 46 # to resolve to a physical path. https://github.com/NixOS/nix/issues/11941 47 tmpPath="$(realpath "$(mktemp -d --tmpdir bzr-checkout-tmp-XXXXXXXX)")" 48 trap "rm -rf \"$tmpPath\"" EXIT 49 50 tmpFile="$tmpPath/$dstFile" 51 52 # Perform the checkout. 53 bzr -Ossl.cert_reqs=none export $revarg --format=dir "$tmpFile" "$url" 54 55 echo "bzr revision is $(bzr revno $revarg "$url")" 56 57 # Compute the hash. 58 hash=$(nix-hash --type $hashType $hashFormat $tmpFile) 59 if ! test -n "$QUIET"; then echo "hash is $hash" >&2; fi 60 61 # Add the downloaded file to the Nix store. 62 finalPath=$(nix-store --add-fixed --recursive "$hashType" $tmpFile) 63 64 if test -n "$expHash" -a "$expHash" != "$hash"; then 65 echo "hash mismatch for URL \`$url'" 66 exit 1 67 fi 68fi 69 70if ! test -n "$QUIET"; then echo "path is $finalPath" >&2; fi 71 72echo $hash 73 74if test -n "$PRINT_PATH"; then 75 echo $finalPath 76fi