guix build: Catch 'getaddrinfo-error' for '--log-file'.
[jackhill/guix/guix.git] / guix / download.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
95001d4b 2;;; Copyright © 2012, 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
9884d7ec 3;;; Copyright © 2013, 2014, 2015 Andreas Enge <andreas@enge.fr>
95001d4b 4;;; Copyright © 2015 Federico Beffa <beffa@fbengineering.ch>
62cab99c 5;;;
233e7676 6;;; This file is part of GNU Guix.
62cab99c 7;;;
233e7676 8;;; GNU Guix is free software; you can redistribute it and/or modify it
62cab99c
LC
9;;; under the terms of the GNU General Public License as published by
10;;; the Free Software Foundation; either version 3 of the License, or (at
11;;; your option) any later version.
12;;;
233e7676 13;;; GNU Guix is distributed in the hope that it will be useful, but
62cab99c
LC
14;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;;; GNU General Public License for more details.
17;;;
18;;; You should have received a copy of the GNU General Public License
233e7676 19;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
62cab99c
LC
20
21(define-module (guix download)
22 #:use-module (ice-9 match)
23 #:use-module (guix derivations)
24 #:use-module (guix packages)
e87f0591 25 #:use-module (guix store)
b5b73a82 26 #:use-module ((guix build download) #:prefix build:)
6f8f8ccb
LC
27 #:use-module (guix monads)
28 #:use-module (guix gexp)
62cab99c 29 #:use-module (guix utils)
d8907ac4 30 #:use-module (web uri)
483f1158 31 #:use-module (srfi srfi-1)
94d222ad 32 #:use-module (srfi srfi-26)
ec4d308a 33 #:export (%mirrors
861693f3 34 url-fetch
95001d4b 35 url-fetch/tarbomb
861693f3 36 download-to-store))
62cab99c
LC
37
38;;; Commentary:
39;;;
40;;; Produce fixed-output derivations with data fetched over HTTP or FTP.
41;;;
42;;; Code:
43
94d222ad
LC
44(define %mirrors
45 ;; Mirror lists used when `mirror://' URLs are passed.
46 (let* ((gnu-mirrors
47 '(;; This one redirects to a (supposedly) nearby and (supposedly)
48 ;; up-to-date mirror.
49 "http://ftpmirror.gnu.org/"
50
51 "ftp://ftp.cs.tu-berlin.de/pub/gnu/"
94d222ad
LC
52 "ftp://ftp.funet.fi/pub/mirrors/ftp.gnu.org/gnu/"
53
54 ;; This one is the master repository, and thus it's always
55 ;; up-to-date.
56 "http://ftp.gnu.org/pub/gnu/")))
57 `((gnu ,@gnu-mirrors)
58 (gcc
59 "ftp://ftp.nluug.nl/mirror/languages/gcc/"
60 "ftp://ftp.fu-berlin.de/unix/languages/gcc/"
61 "ftp://ftp.irisa.fr/pub/mirrors/gcc.gnu.org/gcc/"
62 "ftp://gcc.gnu.org/pub/gcc/"
63 ,@(map (cut string-append <> "/gcc") gnu-mirrors))
64 (gnupg
65 "ftp://gd.tuwien.ac.at/privacy/gnupg/"
d57b88be 66 "ftp://mirrors.dotsrc.org/gcrypt/"
94d222ad 67 "ftp://mirror.cict.fr/gnupg/"
d57b88be
AE
68 "http://artfiles.org/gnupg.org"
69 "ftp://ftp.franken.de/pub/crypt/mirror/ftp.gnupg.org/gcrypt/"
70 "ftp://ftp.freenet.de/pub/ftp.gnupg.org/gcrypt/"
71 "http://www.crysys.hu/"
72 "ftp://ftp.hi.is/pub/mirrors/gnupg/"
73 "ftp://ftp.heanet.ie/mirrors/ftp.gnupg.org/gcrypt/"
74 "ftp://ftp.bit.nl/mirror/gnupg/"
75 "ftp://ftp.surfnet.nl/pub/security/gnupg/"
76 "ftp://ftp.iasi.roedu.net/pub/mirrors/ftp.gnupg.org/"
77 "ftp://ftp.sunet.se/pub/security/gnupg/"
78 "ftp://mirror.switch.ch/mirror/gnupg/"
79 "ftp://mirror.tje.me.uk/pub/mirrors/ftp.gnupg.org/"
80 "ftp://ftp.mirrorservice.org/sites/ftp.gnupg.org/gcrypt/"
81 "ftp://ftp.ring.gr.jp/pub/net/gnupg/"
82 "ftp://ftp.gnupg.org/gcrypt/")
71eb5c10
LC
83 (gnome
84 "http://ftp.belnet.be/ftp.gnome.org/"
85 "http://ftp.linux.org.uk/mirrors/ftp.gnome.org/"
86 "http://ftp.gnome.org/pub/GNOME/"
87 "http://mirror.yandex.ru/mirrors/ftp.gnome.org/")
94d222ad 88 (savannah
a4eabecd 89 "http://download.savannah.gnu.org/releases/"
94d222ad
LC
90 "ftp://ftp.twaren.net/Unix/NonGNU/"
91 "ftp://mirror.csclub.uwaterloo.ca/nongnu/"
92 "ftp://mirror.publicns.net/pub/nongnu/"
93 "ftp://savannah.c3sl.ufpr.br/"
94 "http://ftp.cc.uoc.gr/mirrors/nongnu.org/"
95 "http://ftp.twaren.net/Unix/NonGNU/"
96 "http://mirror.csclub.uwaterloo.ca/nongnu/"
97 "http://nongnu.askapache.com/"
98 "http://savannah.c3sl.ufpr.br/"
3bba7b0a
LC
99 "http://www.centervenus.com/mirrors/nongnu/"
100 "http://download.savannah.gnu.org/releases-noredirect/")
94d222ad
LC
101 (sourceforge
102 "http://prdownloads.sourceforge.net/"
103 "http://heanet.dl.sourceforge.net/sourceforge/"
104 "http://surfnet.dl.sourceforge.net/sourceforge/"
105 "http://dfn.dl.sourceforge.net/sourceforge/"
106 "http://mesh.dl.sourceforge.net/sourceforge/"
107 "http://ovh.dl.sourceforge.net/sourceforge/"
c43e0061 108 "http://osdn.dl.sourceforge.net/sourceforge/")
b40b259f
LC
109 (kernel.org
110 "http://www.all.kernel.org/pub/"
111 "http://ramses.wh2.tu-dresden.de/pub/mirrors/kernel.org/"
112 "http://linux-kernel.uio.no/pub/"
113 "http://kernel.osuosl.org/pub/"
5d9cd707
LC
114 "ftp://ftp.funet.fi/pub/mirrors/ftp.kernel.org/pub/"
115 "http://ftp.be.debian.org/pub/"
116 "http://mirror.linux.org.au/")
47f9db41
LC
117 (apache ; from http://www.apache.org/mirrors/dist.html
118 "http://www.eu.apache.org/dist/"
119 "http://www.us.apache.org/dist/"
120 "ftp://gd.tuwien.ac.at/pub/infosys/servers/http/apache/dist/"
121 "http://apache.belnet.be/"
122 "http://mirrors.ircam.fr/pub/apache/"
f06afd4d
LC
123 "http://apache-mirror.rbc.ru/pub/apache/"
124
125 ;; As a last resort, try the archive.
126 "http://archive.apache.org/dist/")
149acc29 127 (xorg ; from http://www.x.org/wiki/Releases/Download
0820a58b 128 "http://www.x.org/releases/" ; main mirrors
149acc29
AE
129 "ftp://mirror.csclub.uwaterloo.ca/x.org/" ; North America
130 "ftp://xorg.mirrors.pair.com/"
131 "http://mirror.csclub.uwaterloo.ca/x.org/"
132 "http://xorg.mirrors.pair.com/"
133 "http://mirror.us.leaseweb.net/xorg/"
134 "ftp://artfiles.org/x.org/" ; Europe
135 "ftp://ftp.chg.ru/pub/X11/x.org/"
136 "ftp://ftp.fu-berlin.de/unix/X11/FTP.X.ORG/"
137 "ftp://ftp.gwdg.de/pub/x11/x.org/"
138 "ftp://ftp.mirrorservice.org/sites/ftp.x.org/"
139 "ftp://ftp.ntua.gr/pub/X11/"
140 "ftp://ftp.piotrkosoft.net/pub/mirrors/ftp.x.org/"
141 "ftp://ftp.portal-to-web.de/pub/mirrors/x.org/"
142 "ftp://ftp.solnet.ch/mirror/x.org/"
149acc29
AE
143 "ftp://gd.tuwien.ac.at/X11/"
144 "ftp://mi.mirror.garr.it/mirrors/x.org/"
145 "ftp://mirror.cict.fr/x.org/"
146 "ftp://mirror.switch.ch/mirror/X11/"
147 "ftp://mirrors.ircam.fr/pub/x.org/"
148 "ftp://x.mirrors.skynet.be/pub/ftp.x.org/"
149 "ftp://ftp.cs.cuhk.edu.hk/pub/X11" ; East Asia
150 "ftp://ftp.u-aizu.ac.jp/pub/x11/x.org/"
151 "ftp://ftp.yz.yamagata-u.ac.jp/pub/X11/x.org/"
152 "ftp://ftp.kaist.ac.kr/x.org/"
153 "ftp://mirrors.go-part.com/xorg/"
154 "http://x.cs.pu.edu.tw/"
6af31019
LC
155 "ftp://ftp.is.co.za/pub/x.org") ; South Africa
156 (cpan ; from http://www.cpan.org/SITES.html
157 "http://cpan.enstimac.fr/"
158 "ftp://ftp.ciril.fr/pub/cpan/"
159 "ftp://artfiles.org/cpan.org/"
160 "http://www.cpan.org/"
161 "ftp://cpan.rinet.ru/pub/mirror/CPAN/"
162 "http://cpan.cu.be/"
163 "ftp://cpan.inode.at/"
164 "ftp://cpan.iht.co.il/"
165 "ftp://ftp.osuosl.org/pub/CPAN/"
166 "ftp://ftp.nara.wide.ad.jp/pub/CPAN/"
167 "http://mirrors.163.com/cpan/"
7fa37abc 168 "ftp://cpan.mirror.ac.za/")
cb7e4867
RW
169 (cran
170 ;; Arbitrary mirrors from http://cran.r-project.org/mirrors.html
171 ;; This one automatically redirects to servers worldwide
c009bb5a 172 "http://cran.r-project.org/"
cb7e4867
RW
173 "http://cran.rstudio.com/"
174 "http://cran.univ-lyon1.fr/"
cb7e4867
RW
175 "http://cran.ism.ac.jp/"
176 "http://cran.stat.auckland.ac.nz/"
177 "http://cran.mirror.ac.za/"
178 "http://cran.csie.ntu.edu.tw/")
6d763bdd
AE
179 (imagemagick
180 ;; from http://www.imagemagick.org/script/download.php
181 ;; (without mirrors that are unavailable or not up to date)
182 ;; mirrors keeping old versions at the top level
6d763bdd
AE
183 "ftp://sunsite.icm.edu.pl/packages/ImageMagick/"
184 ;; mirrors moving old versions to "legacy"
185 "http://mirrors-au.go-parts.com/mirrors/ImageMagick/"
186 "ftp://mirror.aarnet.edu.au/pub/imagemagick/"
7fa37abc 187 "http://mirror.checkdomain.de/imagemagick/"
6d763bdd
AE
188 "ftp://ftp.kddlabs.co.jp/graphics/ImageMagick/"
189 "ftp://ftp.u-aizu.ac.jp/pub/graphics/image/ImageMagick/imagemagick.org/"
190 "ftp://ftp.nluug.nl/pub/ImageMagick/"
191 "http://ftp.surfnet.nl/pub/ImageMagick/"
192 "http://mirror.searchdaimon.com/ImageMagick"
193 "ftp://ftp.tpnet.pl/pub/graphics/ImageMagick/"
194 "http://mirrors-ru.go-parts.com/mirrors/ImageMagick/"
7fa37abc 195 "http://mirror.is.co.za/pub/imagemagick/"
6d763bdd
AE
196 "http://mirrors-uk.go-parts.com/mirrors/ImageMagick/"
197 "http://mirrors-usa.go-parts.com/mirrors/ImageMagick/"
198 "ftp://ftp.fifi.org/pub/ImageMagick/"
199 "http://www.imagemagick.org/download/"
200 ;; one legacy location as a last resort
201 "http://www.imagemagick.org/download/legacy/")
e0029b74
LC
202 (debian
203 "http://ftp.de.debian.org/debian/"
204 "http://ftp.fr.debian.org/debian/"
205 "http://ftp.debian.org/debian/"))))
94d222ad 206
53216419
LC
207(define %mirror-file
208 ;; Copy of the list of mirrors to a file. This allows us to keep a single
209 ;; copy in the store, and computing it here avoids repeated calls to
210 ;; 'object->string'.
211 (plain-file "mirrors" (object->string %mirrors)))
212
6f8f8ccb 213(define (gnutls-package)
6119ebf1 214 "Return the default GnuTLS package."
9884d7ec 215 (let ((module (resolve-interface '(gnu packages tls))))
6f8f8ccb 216 (module-ref module 'gnutls)))
94d222ad 217
f220a838 218(define* (url-fetch url hash-algo hash
62cab99c 219 #:optional name
f220a838 220 #:key (system (%current-system))
53216419 221 (guile (default-guile)))
f220a838
LC
222 "Return a fixed-output derivation that fetches URL (a string, or a list of
223strings denoting alternate URLs), which is expected to have hash HASH of type
224HASH-ALGO (a symbol). By default, the file name is the base name of URL;
225optionally, NAME can specify a different file name.
94d222ad
LC
226
227When one of the URL starts with mirror://, then its host part is
53216419 228interpreted as the name of a mirror scheme, taken from %MIRROR-FILE.
62cab99c 229
f220a838
LC
230Alternately, when URL starts with file://, return the corresponding file name
231in the store."
62cab99c
LC
232 (define file-name
233 (match url
234 ((head _ ...)
235 (basename head))
236 (_
237 (basename url))))
238
483f1158
LC
239 (define need-gnutls?
240 ;; True if any of the URLs need TLS support.
241 (let ((https? (cut string-prefix? "https://" <>)))
242 (match url
243 ((? string?)
244 (https? url))
245 ((url ...)
246 (any https? url)))))
247
6f8f8ccb
LC
248 (define builder
249 #~(begin
1590e8a1 250 #+(if need-gnutls?
6f8f8ccb
LC
251
252 ;; Add GnuTLS to the inputs and to the load path.
253 #~(eval-when (load expand eval)
254 (set! %load-path
1590e8a1 255 (cons (string-append #+(gnutls-package)
e0ea3f8a
LC
256 "/share/guile/site/"
257 (effective-version))
6f8f8ccb
LC
258 %load-path)))
259 #~#t)
260
261 (use-modules (guix build download))
ced20032
LC
262
263 (url-fetch (call-with-input-string (getenv "guix download url")
264 read)
265 #$output
53216419 266 #:mirrors (call-with-input-file #$%mirror-file read))))
6f8f8ccb 267
882383a9
LC
268 (let ((uri (and (string? url) (string->uri url))))
269 (if (or (and (string? url) (not uri))
270 (and uri (memq (uri-scheme uri) '(#f file))))
f220a838
LC
271 (interned-file (if uri (uri-path uri) url)
272 (or name file-name))
273 (mlet %store-monad ((guile (package->derivation guile system)))
882383a9 274 (gexp->derivation (or name file-name) builder
f220a838 275 #:guile-for-build guile
882383a9
LC
276 #:system system
277 #:hash-algo hash-algo
278 #:hash hash
279 #:modules '((guix build download)
280 (guix build utils)
281 (guix ftp-client))
56c72822 282
ced20032
LC
283 ;; Use environment variables and a fixed script
284 ;; name so there's only one script in store for
285 ;; all the downloads.
286 #:script-name "download"
287 #:env-vars
288 `(("guix download url" . ,(object->string url)))
289
0d883137
LC
290 ;; Honor the user's proxy settings.
291 #:leaked-env-vars '("http_proxy" "https_proxy")
292
6b44a097
LC
293 ;; In general, offloading downloads is not a good
294 ;; idea. Daemons before 0.8.3 would also
295 ;; interpret this as "do not substitute" (see
296 ;; <https://bugs.gnu.org/18747>.)
297 #:local-build? #t)))))
62cab99c 298
95001d4b
LC
299(define* (url-fetch/tarbomb url hash-algo hash
300 #:optional name
301 #:key (system (%current-system))
302 (guile (default-guile)))
303 "Similar to 'url-fetch' but unpack the file from URL in a directory of its
304own. This helper makes it easier to deal with \"tar bombs\"."
305 (define gzip
306 (module-ref (resolve-interface '(gnu packages compression)) 'gzip))
307 (define tar
308 (module-ref (resolve-interface '(gnu packages base)) 'tar))
309
310 (mlet %store-monad ((drv (url-fetch url hash-algo hash
311 (string-append "tarbomb-" name)
312 #:system system
313 #:guile guile)))
314 ;; Take the tar bomb, and simply unpack it as a directory.
315 (gexp->derivation name
316 #~(begin
317 (mkdir #$output)
318 (setenv "PATH" (string-append #$gzip "/bin"))
319 (chdir #$output)
320 (zero? (system* (string-append #$tar "/bin/tar")
321 "xf" #$drv)))
322 #:local-build? #t)))
323
861693f3 324(define* (download-to-store store url #:optional (name (basename url))
a43b55f1 325 #:key (log (current-error-port)) recursive?)
861693f3 326 "Download from URL to STORE, either under NAME or URL's basename if
a43b55f1
LC
327omitted. Write progress reports to LOG. RECURSIVE? has the same effect as
328the same-named parameter of 'add-to-store'."
d8907ac4
LC
329 (define uri
330 (string->uri url))
331
d91a8791 332 (if (or (not uri) (memq (uri-scheme uri) '(file #f)))
a43b55f1 333 (add-to-store store name recursive? "sha256"
d91a8791 334 (if uri (uri-path uri) url))
d8907ac4
LC
335 (call-with-temporary-output-file
336 (lambda (temp port)
337 (let ((result
338 (parameterize ((current-output-port log))
339 (build:url-fetch url temp #:mirrors %mirrors))))
340 (close port)
341 (and result
a43b55f1 342 (add-to-store store name recursive? "sha256" temp)))))))
861693f3 343
62cab99c 344;;; download.scm ends here