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