nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix
at devShellTools-shell 84 lines 2.1 kB view raw
1#! /usr/bin/env bash 2set -e 3 4url=$1 5rev=$2 6expHash=$3 7 8hashType="${NIX_HASH_ALGO:-sha256}" 9hashFormat=${hashFormat:-"--base32"} 10rev="${rev:-tip}" 11 12LOG() { 13 echo "$@" >&2 14} 15 16die() { 17 LOG "$@" 18 exit 1 19} 20 21if [[ -z "$url" || "$url" == "--help" ]]; then 22 die "Usage: nix-prefetch-hg URL [rev [EXPECTED-HASH]]" 23fi 24 25if [[ "${fetchSubrepos:-0}" == 1 ]]; then 26 subrepoClause=S 27else 28 subrepoClause= 29fi 30 31# If the hash was given, a file with that hash may already be in the 32# store. 33if [[ -n "$expHash" ]]; then 34 finalPath=$(nix-store --print-fixed-path --recursive "$hashType" "$expHash" hg-archive) 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 [[ -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 hg-checkout-tmp-XXXXXXXX)")" 48 cleanup() { x=$?; rm -rf "$tmpPath"; exit $x; }; trap cleanup EXIT 49 50 tmpArchive="$tmpPath/hg-archive" 51 52 # Perform the checkout. 53 if [[ "$url" != /* ]]; then 54 tmpClone="$tmpPath/hg-clone" 55 hg clone -q -y -U "$url" "$tmpClone" >&2 56 else 57 tmpClone=$url 58 fi 59 hg archive -q$subrepoClause -y -r "$rev" --cwd "$tmpClone" "$tmpArchive" 60 rm -f "$tmpArchive/.hg_archival.txt" 61 62 LOG "hg revision is $(cd "$tmpClone"; hg id -r "$rev" -i)" 63 64 # Compute the hash. 65 hash=$(nix-hash --type "$hashType" "$hashFormat" "$tmpArchive") 66 if [[ -z "$QUIET" ]]; then LOG "hash is $hash"; fi 67 68 # Add the downloaded file to the Nix store. 69 finalPath=$(nix-store --add-fixed --recursive "$hashType" "$tmpArchive") 70 71 if [[ -n "$expHash" && "$expHash" != "$hash" ]]; then 72 die "ERROR: hash mismatch for URL \`$url'" 73 fi 74 75 76fi 77 78if [[ -z "$QUIET" ]]; then LOG "path is $finalPath"; fi 79 80echo "$hash" 81 82if [[ -n "$PRINT_PATH" ]]; then 83 echo "$finalPath" 84fi