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 local target="$2"
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 "$target"; 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 # Directly download to $out, because postFetch doesn't need to run,
86 # since hashed mirrors provide pre-built derivation outputs.
87 tryDownload "$url" "$out"
88
89 # We skip postFetch here, because hashed-mirrors are
90 # already content addressed. So if $outputHash is in the
91 # hashed-mirror, changes from ‘postFetch’ would already be
92 # made. So, running postFetch will end up applying the
93 # change /again/, which we don’t want.
94 if test -n "$success"; then finish skipPostFetch; fi
95 else
96 # Be quiet about 404 errors, which we interpret as the file
97 # not being present on this particular mirror.
98 if test "$(cat code)" != 404; then
99 echo "error checking the existence of $url:"
100 cat log
101 fi
102 fi
103 done
104}
105
106
107# URL list may contain ?. No glob expansion for that, please
108set -o noglob
109
110urls2=
111for url in $urls; do
112 if test "${url:0:9}" != "mirror://"; then
113 urls2="$urls2 $url"
114 else
115 url2="${url:9}"; echo "${url2/\// }" > split; read site fileName < split
116 #varName="mirror_$site"
117 varName="$site" # !!! danger of name clash, fix this
118 if test -z "${!varName}"; then
119 echo "warning: unknown mirror:// site \`$site'"
120 else
121 mirrors=${!varName}
122
123 # Allow command-line override by setting NIX_MIRRORS_$site.
124 varName="NIX_MIRRORS_$site"
125 if test -n "${!varName}"; then mirrors="${!varName}"; fi
126
127 for url3 in $mirrors; do
128 urls2="$urls2 $url3$fileName";
129 done
130 fi
131 fi
132done
133urls="$urls2"
134
135# Restore globbing settings
136set +o noglob
137
138if test -n "$showURLs"; then
139 echo "$urls" > $out
140 exit 0
141fi
142
143if test -n "$preferHashedMirrors"; then
144 tryHashedMirrors
145fi
146
147# URL list may contain ?. No glob expansion for that, please
148set -o noglob
149
150success=
151for url in $urls; do
152 if [ -z "$postFetch" ]; then
153 case "$url" in
154 https://github.com/*/archive/*)
155 echo "warning: archives from GitHub revisions should use fetchFromGitHub"
156 ;;
157 https://gitlab.com/*/-/archive/*)
158 echo "warning: archives from GitLab revisions should use fetchFromGitLab"
159 ;;
160 esac
161 fi
162 tryDownload "$url" "$downloadedFile"
163 if test -n "$success"; then finish; fi
164done
165
166# Restore globbing settings
167set +o noglob
168
169if test -z "$preferHashedMirrors"; then
170 tryHashedMirrors
171fi
172
173
174echo "error: cannot download $name from any mirror"
175exit 1