gnu: gmsh: Add missing inputs.
[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
cd436bf0
LC
213(define %content-addressed-mirrors
214 ;; List of content-addressed mirrors. Each mirror is represented as a
215 ;; procedure that takes an algorithm (symbol) and a hash (bytevector), and
216 ;; returns a URL or #f.
217 ;; TODO: Add more.
218 '(list (lambda (algo hash)
219 ;; 'tarballs.nixos.org' supports several algorithms.
220 (string-append "http://tarballs.nixos.org/"
221 (symbol->string algo) "/"
222 (bytevector->nix-base32-string hash)))))
223
224(define %content-addressed-mirror-file
225 ;; Content-addressed mirrors stored in a file.
226 (plain-file "content-addressed-mirrors"
227 (object->string %content-addressed-mirrors)))
228
6f8f8ccb 229(define (gnutls-package)
6119ebf1 230 "Return the default GnuTLS package."
9884d7ec 231 (let ((module (resolve-interface '(gnu packages tls))))
6f8f8ccb 232 (module-ref module 'gnutls)))
94d222ad 233
f220a838 234(define* (url-fetch url hash-algo hash
62cab99c 235 #:optional name
f220a838 236 #:key (system (%current-system))
53216419 237 (guile (default-guile)))
f220a838
LC
238 "Return a fixed-output derivation that fetches URL (a string, or a list of
239strings denoting alternate URLs), which is expected to have hash HASH of type
240HASH-ALGO (a symbol). By default, the file name is the base name of URL;
241optionally, NAME can specify a different file name.
94d222ad
LC
242
243When one of the URL starts with mirror://, then its host part is
53216419 244interpreted as the name of a mirror scheme, taken from %MIRROR-FILE.
62cab99c 245
f220a838
LC
246Alternately, when URL starts with file://, return the corresponding file name
247in the store."
62cab99c
LC
248 (define file-name
249 (match url
250 ((head _ ...)
251 (basename head))
252 (_
253 (basename url))))
254
483f1158
LC
255 (define need-gnutls?
256 ;; True if any of the URLs need TLS support.
257 (let ((https? (cut string-prefix? "https://" <>)))
258 (match url
259 ((? string?)
260 (https? url))
261 ((url ...)
262 (any https? url)))))
263
6f8f8ccb
LC
264 (define builder
265 #~(begin
1590e8a1 266 #+(if need-gnutls?
6f8f8ccb
LC
267
268 ;; Add GnuTLS to the inputs and to the load path.
269 #~(eval-when (load expand eval)
270 (set! %load-path
1590e8a1 271 (cons (string-append #+(gnutls-package)
e0ea3f8a
LC
272 "/share/guile/site/"
273 (effective-version))
6f8f8ccb
LC
274 %load-path)))
275 #~#t)
276
cd436bf0
LC
277 (use-modules (guix build download)
278 (guix base32))
279
280 (let ((value-from-environment (lambda (variable)
281 (call-with-input-string
282 (getenv variable)
283 read))))
284 (url-fetch (value-from-environment "guix download url")
285 #$output
286 #:mirrors (call-with-input-file #$%mirror-file read)
ced20032 287
cd436bf0
LC
288 ;; Content-addressed mirrors.
289 #:hashes (value-from-environment "guix download hashes")
290 #:content-addressed-mirrors
291 (primitive-load #$%content-addressed-mirror-file)))))
6f8f8ccb 292
882383a9
LC
293 (let ((uri (and (string? url) (string->uri url))))
294 (if (or (and (string? url) (not uri))
295 (and uri (memq (uri-scheme uri) '(#f file))))
f220a838
LC
296 (interned-file (if uri (uri-path uri) url)
297 (or name file-name))
298 (mlet %store-monad ((guile (package->derivation guile system)))
882383a9 299 (gexp->derivation (or name file-name) builder
f220a838 300 #:guile-for-build guile
882383a9
LC
301 #:system system
302 #:hash-algo hash-algo
303 #:hash hash
304 #:modules '((guix build download)
305 (guix build utils)
cd436bf0
LC
306 (guix ftp-client)
307 (guix base32))
56c72822 308
ced20032
LC
309 ;; Use environment variables and a fixed script
310 ;; name so there's only one script in store for
311 ;; all the downloads.
312 #:script-name "download"
313 #:env-vars
cd436bf0
LC
314 `(("guix download url" . ,(object->string url))
315 ("guix download hashes"
316 . ,(object->string `((,hash-algo . ,hash)))))
ced20032 317
0d883137
LC
318 ;; Honor the user's proxy settings.
319 #:leaked-env-vars '("http_proxy" "https_proxy")
320
6b44a097
LC
321 ;; In general, offloading downloads is not a good
322 ;; idea. Daemons before 0.8.3 would also
323 ;; interpret this as "do not substitute" (see
324 ;; <https://bugs.gnu.org/18747>.)
325 #:local-build? #t)))))
62cab99c 326
95001d4b
LC
327(define* (url-fetch/tarbomb url hash-algo hash
328 #:optional name
329 #:key (system (%current-system))
330 (guile (default-guile)))
331 "Similar to 'url-fetch' but unpack the file from URL in a directory of its
332own. This helper makes it easier to deal with \"tar bombs\"."
333 (define gzip
334 (module-ref (resolve-interface '(gnu packages compression)) 'gzip))
335 (define tar
336 (module-ref (resolve-interface '(gnu packages base)) 'tar))
337
338 (mlet %store-monad ((drv (url-fetch url hash-algo hash
339 (string-append "tarbomb-" name)
340 #:system system
341 #:guile guile)))
342 ;; Take the tar bomb, and simply unpack it as a directory.
343 (gexp->derivation name
344 #~(begin
345 (mkdir #$output)
346 (setenv "PATH" (string-append #$gzip "/bin"))
347 (chdir #$output)
348 (zero? (system* (string-append #$tar "/bin/tar")
349 "xf" #$drv)))
350 #:local-build? #t)))
351
861693f3 352(define* (download-to-store store url #:optional (name (basename url))
a43b55f1 353 #:key (log (current-error-port)) recursive?)
861693f3 354 "Download from URL to STORE, either under NAME or URL's basename if
a43b55f1
LC
355omitted. Write progress reports to LOG. RECURSIVE? has the same effect as
356the same-named parameter of 'add-to-store'."
d8907ac4
LC
357 (define uri
358 (string->uri url))
359
d91a8791 360 (if (or (not uri) (memq (uri-scheme uri) '(file #f)))
a43b55f1 361 (add-to-store store name recursive? "sha256"
d91a8791 362 (if uri (uri-path uri) url))
d8907ac4
LC
363 (call-with-temporary-output-file
364 (lambda (temp port)
365 (let ((result
366 (parameterize ((current-output-port log))
367 (build:url-fetch url temp #:mirrors %mirrors))))
368 (close port)
369 (and result
a43b55f1 370 (add-to-store store name recursive? "sha256" temp)))))))
861693f3 371
62cab99c 372;;; download.scm ends here