at 24.11-pre 173 lines 4.4 kB view raw
1if [ -e "$NIX_ATTRS_SH_FILE" ]; then . "$NIX_ATTRS_SH_FILE"; elif [ -f .attrs.sh ]; then . .attrs.sh; fi 2source $stdenv/setup 3 4source $mirrorsFile 5 6curlVersion=$(curl -V | head -1 | cut -d' ' -f2) 7 8# Curl flags to handle redirects, not use EPSV, handle cookies for 9# servers to need them during redirects, and work on SSL without a 10# certificate (this isn't a security problem because we check the 11# cryptographic hash of the output anyway). 12curl=( 13 curl 14 --location 15 --max-redirs 20 16 --retry 3 17 --disable-epsv 18 --cookie-jar cookies 19 --user-agent "curl/$curlVersion Nixpkgs/$nixpkgsVersion" 20) 21 22if ! [ -f "$SSL_CERT_FILE" ]; then 23 curl+=(--insecure) 24fi 25 26eval "curl+=($curlOptsList)" 27 28curl+=( 29 $curlOpts 30 $NIX_CURL_FLAGS 31) 32 33downloadedFile="$out" 34if [ -n "$downloadToTemp" ]; then downloadedFile="$TMPDIR/file"; fi 35 36 37tryDownload() { 38 local url="$1" 39 echo 40 echo "trying $url" 41 local curlexit=18; 42 43 success= 44 45 # if we get error code 18, resume partial download 46 while [ $curlexit -eq 18 ]; do 47 # keep this inside an if statement, since on failure it doesn't abort the script 48 if "${curl[@]}" -C - --fail "$url" --output "$downloadedFile"; then 49 success=1 50 break 51 else 52 curlexit=$?; 53 fi 54 done 55} 56 57 58finish() { 59 local skipPostFetch="$1" 60 61 set +o noglob 62 63 if [[ $executable == "1" ]]; then 64 chmod +x $downloadedFile 65 fi 66 67 if [ -z "$skipPostFetch" ]; then 68 runHook postFetch 69 fi 70 71 exit 0 72} 73 74 75tryHashedMirrors() { 76 if test -n "$NIX_HASHED_MIRRORS"; then 77 hashedMirrors="$NIX_HASHED_MIRRORS" 78 fi 79 80 for mirror in $hashedMirrors; do 81 url="$mirror/$outputHashAlgo/$outputHash" 82 if "${curl[@]}" --retry 0 --connect-timeout "${NIX_CONNECT_TIMEOUT:-15}" \ 83 --fail --silent --show-error --head "$url" \ 84 --write-out "%{http_code}" --output /dev/null > code 2> log; then 85 tryDownload "$url" 86 87 # We skip postFetch here, because hashed-mirrors are 88 # already content addressed. So if $outputHash is in the 89 # hashed-mirror, changes from ‘postFetch’ would already be 90 # made. So, running postFetch will end up applying the 91 # change /again/, which we don’t want. 92 if test -n "$success"; then finish skipPostFetch; fi 93 else 94 # Be quiet about 404 errors, which we interpret as the file 95 # not being present on this particular mirror. 96 if test "$(cat code)" != 404; then 97 echo "error checking the existence of $url:" 98 cat log 99 fi 100 fi 101 done 102} 103 104 105# URL list may contain ?. No glob expansion for that, please 106set -o noglob 107 108urls2= 109for url in $urls; do 110 if test "${url:0:9}" != "mirror://"; then 111 urls2="$urls2 $url" 112 else 113 url2="${url:9}"; echo "${url2/\// }" > split; read site fileName < split 114 #varName="mirror_$site" 115 varName="$site" # !!! danger of name clash, fix this 116 if test -z "${!varName}"; then 117 echo "warning: unknown mirror:// site \`$site'" 118 else 119 mirrors=${!varName} 120 121 # Allow command-line override by setting NIX_MIRRORS_$site. 122 varName="NIX_MIRRORS_$site" 123 if test -n "${!varName}"; then mirrors="${!varName}"; fi 124 125 for url3 in $mirrors; do 126 urls2="$urls2 $url3$fileName"; 127 done 128 fi 129 fi 130done 131urls="$urls2" 132 133# Restore globbing settings 134set +o noglob 135 136if test -n "$showURLs"; then 137 echo "$urls" > $out 138 exit 0 139fi 140 141if test -n "$preferHashedMirrors"; then 142 tryHashedMirrors 143fi 144 145# URL list may contain ?. No glob expansion for that, please 146set -o noglob 147 148success= 149for url in $urls; do 150 if [ -z "$postFetch" ]; then 151 case "$url" in 152 https://github.com/*/archive/*) 153 echo "warning: archives from GitHub revisions should use fetchFromGitHub" 154 ;; 155 https://gitlab.com/*/-/archive/*) 156 echo "warning: archives from GitLab revisions should use fetchFromGitLab" 157 ;; 158 esac 159 fi 160 tryDownload "$url" 161 if test -n "$success"; then finish; fi 162done 163 164# Restore globbing settings 165set +o noglob 166 167if test -z "$preferHashedMirrors"; then 168 tryHashedMirrors 169fi 170 171 172echo "error: cannot download $name from any mirror" 173exit 1