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