Merge branch 'master' into core-updates
[jackhill/guix/guix.git] / guix / download.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2013, 2014, 2015 Andreas Enge <andreas@enge.fr>
4 ;;; Copyright © 2015 Federico Beffa <beffa@fbengineering.ch>
5 ;;; Copyright © 2016 Alex Griffin <a@ajgrf.com>
6 ;;; Copyright © 2016 David Craven <david@craven.ch>
7 ;;; Copyright © 2017 Tobias Geerinckx-Rice <me@tobias.gr>
8 ;;;
9 ;;; This file is part of GNU Guix.
10 ;;;
11 ;;; GNU Guix is free software; you can redistribute it and/or modify it
12 ;;; under the terms of the GNU General Public License as published by
13 ;;; the Free Software Foundation; either version 3 of the License, or (at
14 ;;; your option) any later version.
15 ;;;
16 ;;; GNU Guix is distributed in the hope that it will be useful, but
17 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;;; GNU General Public License for more details.
20 ;;;
21 ;;; You should have received a copy of the GNU General Public License
22 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23
24 (define-module (guix download)
25 #:use-module (ice-9 match)
26 #:use-module (guix derivations)
27 #:use-module (guix packages)
28 #:use-module (guix store)
29 #:use-module ((guix build download) #:prefix build:)
30 #:use-module (guix monads)
31 #:use-module (guix gexp)
32 #:use-module (guix utils)
33 #:use-module (web uri)
34 #:use-module (srfi srfi-1)
35 #:use-module (srfi srfi-26)
36 #:export (%mirrors
37 url-fetch
38 url-fetch/tarbomb
39 url-fetch/zipbomb
40 download-to-store))
41
42 ;;; Commentary:
43 ;;;
44 ;;; Produce fixed-output derivations with data fetched over HTTP or FTP.
45 ;;;
46 ;;; Code:
47
48 (define %mirrors
49 ;; Mirror lists used when `mirror://' URLs are passed.
50 (let* ((gnu-mirrors
51 '(;; This one redirects to a (supposedly) nearby and (supposedly)
52 ;; up-to-date mirror.
53 "https://ftpmirror.gnu.org/gnu/"
54
55 "ftp://ftp.cs.tu-berlin.de/pub/gnu/"
56 "ftp://ftp.funet.fi/pub/mirrors/ftp.gnu.org/gnu/"
57
58 ;; This one is the master repository, and thus it's always
59 ;; up-to-date.
60 "http://ftp.gnu.org/pub/gnu/")))
61 `((gnu ,@gnu-mirrors)
62 (gcc
63 "ftp://ftp.nluug.nl/mirror/languages/gcc/"
64 "ftp://ftp.fu-berlin.de/unix/languages/gcc/"
65 "ftp://ftp.irisa.fr/pub/mirrors/gcc.gnu.org/gcc/"
66 "ftp://gcc.gnu.org/pub/gcc/"
67 ,@(map (cut string-append <> "/gcc") gnu-mirrors))
68 (gnupg
69 "http://artfiles.org/gnupg.org"
70 "http://www.crysys.hu/"
71 "https://gnupg.org/ftp/gcrypt/"
72 "ftp://mirrors.dotsrc.org/gcrypt/"
73 "ftp://mirror.cict.fr/gnupg/"
74 "ftp://ftp.franken.de/pub/crypt/mirror/ftp.gnupg.org/gcrypt/"
75 "ftp://ftp.freenet.de/pub/ftp.gnupg.org/gcrypt/"
76 "ftp://ftp.hi.is/pub/mirrors/gnupg/"
77 "ftp://ftp.heanet.ie/mirrors/ftp.gnupg.org/gcrypt/"
78 "ftp://ftp.bit.nl/mirror/gnupg/"
79 "ftp://ftp.surfnet.nl/pub/security/gnupg/"
80 "ftp://ftp.iasi.roedu.net/pub/mirrors/ftp.gnupg.org/"
81 "ftp://ftp.sunet.se/pub/security/gnupg/"
82 "ftp://mirror.switch.ch/mirror/gnupg/"
83 "ftp://mirror.tje.me.uk/pub/mirrors/ftp.gnupg.org/"
84 "ftp://ftp.mirrorservice.org/sites/ftp.gnupg.org/gcrypt/"
85 "ftp://ftp.ring.gr.jp/pub/net/gnupg/"
86 "ftp://ftp.gnupg.org/gcrypt/")
87 (gnome
88 "http://ftp.belnet.be/ftp.gnome.org/"
89 "http://ftp.linux.org.uk/mirrors/ftp.gnome.org/"
90 "http://ftp.gnome.org/pub/GNOME/"
91 "https://download.gnome.org/"
92 "http://mirror.yandex.ru/mirrors/ftp.gnome.org/")
93 (hackage
94 "http://hackage.haskell.org/")
95 (savannah
96 "http://download.savannah.gnu.org/releases/"
97 "http://ftp.cc.uoc.gr/mirrors/nongnu.org/"
98 "http://ftp.twaren.net/Unix/NonGNU/"
99 "http://mirror.csclub.uwaterloo.ca/nongnu/"
100 "http://nongnu.askapache.com/"
101 "http://savannah.c3sl.ufpr.br/"
102 "http://download.savannah.gnu.org/releases-noredirect/"
103 "http://download-mirror.savannah.gnu.org/releases/"
104 "ftp://ftp.twaren.net/Unix/NonGNU/"
105 "ftp://mirror.csclub.uwaterloo.ca/nongnu/"
106 "ftp://mirror.publicns.net/pub/nongnu/"
107 "ftp://savannah.c3sl.ufpr.br/")
108 (sourceforge ; https://sourceforge.net/p/forge/documentation/Mirrors/
109 "http://downloads.sourceforge.net/project/"
110 "http://ufpr.dl.sourceforge.net/project/"
111 "http://heanet.dl.sourceforge.net/project/"
112 "http://freefr.dl.sourceforge.net/project/"
113 "http://internode.dl.sourceforge.net/project/"
114 "http://jaist.dl.sourceforge.net/project/"
115 "http://kent.dl.sourceforge.net/project/"
116 "http://liquidtelecom.dl.sourceforge.net/project/"
117 ;; "http://nbtelecom.dl.sourceforge.net/project/" ;never returns 404s
118 "http://nchc.dl.sourceforge.net/project/"
119 "http://ncu.dl.sourceforge.net/project/"
120 "http://netcologne.dl.sourceforge.net/project/"
121 "http://netix.dl.sourceforge.net/project/"
122 "http://pilotfiber.dl.sourceforge.net/project/"
123 "http://superb-sea2.dl.sourceforge.net/project/"
124 "http://tenet.dl.sourceforge.net/project/"
125 "http://vorboss.dl.sourceforge.net/project/"
126 "http://netassist.dl.sourceforge.net/project/")
127 (netfilter.org ; https://www.netfilter.org/mirrors.html
128 "http://ftp.netfilter.org/pub/"
129 "ftp://ftp.es.netfilter.org/mirrors/netfilter/"
130 "ftp://ftp.hu.netfilter.org/"
131 "ftp://www.lt.netfilter.org/pub/")
132 (kernel.org
133 "http://ramses.wh2.tu-dresden.de/pub/mirrors/kernel.org/"
134 "http://linux-kernel.uio.no/pub/"
135 "http://kernel.osuosl.org/pub/"
136 "http://ftp.be.debian.org/pub/"
137 "http://mirror.linux.org.au/"
138 "ftp://ftp.funet.fi/pub/mirrors/ftp.kernel.org/pub/")
139 (apache ; from http://www.apache.org/mirrors/dist.html
140 "http://www.eu.apache.org/dist/"
141 "http://www.us.apache.org/dist/"
142 "http://apache.belnet.be/"
143 "http://mirrors.ircam.fr/pub/apache/"
144 "http://apache-mirror.rbc.ru/pub/apache/"
145
146 ;; As a last resort, try the archive.
147 "http://archive.apache.org/dist/")
148 (xorg ; from http://www.x.org/wiki/Releases/Download
149 "http://www.x.org/releases/" ; main mirrors
150 "http://mirror.csclub.uwaterloo.ca/x.org/" ; North America
151 "http://xorg.mirrors.pair.com/"
152 "http://mirror.us.leaseweb.net/xorg/"
153 "ftp://mirror.csclub.uwaterloo.ca/x.org/"
154 "ftp://xorg.mirrors.pair.com/"
155 "ftp://artfiles.org/x.org/" ; Europe
156 "ftp://ftp.chg.ru/pub/X11/x.org/"
157 "ftp://ftp.fu-berlin.de/unix/X11/FTP.X.ORG/"
158 "ftp://ftp.gwdg.de/pub/x11/x.org/"
159 "ftp://ftp.mirrorservice.org/sites/ftp.x.org/"
160 "ftp://ftp.ntua.gr/pub/X11/"
161 "ftp://ftp.piotrkosoft.net/pub/mirrors/ftp.x.org/"
162 "ftp://ftp.portal-to-web.de/pub/mirrors/x.org/"
163 "ftp://ftp.solnet.ch/mirror/x.org/"
164 "ftp://mi.mirror.garr.it/mirrors/x.org/"
165 "ftp://mirror.cict.fr/x.org/"
166 "ftp://mirror.switch.ch/mirror/X11/"
167 "ftp://mirrors.ircam.fr/pub/x.org/"
168 "ftp://x.mirrors.skynet.be/pub/ftp.x.org/"
169 "http://x.cs.pu.edu.tw/" ; East Asia
170 "ftp://ftp.cs.cuhk.edu.hk/pub/X11"
171 "ftp://ftp.u-aizu.ac.jp/pub/x11/x.org/"
172 "ftp://ftp.yz.yamagata-u.ac.jp/pub/X11/x.org/"
173 "ftp://ftp.kaist.ac.kr/x.org/"
174 "ftp://mirrors.go-part.com/xorg/"
175 "ftp://ftp.is.co.za/pub/x.org") ; South Africa
176 (cpan
177 "http://www.cpan.org/"
178 "http://cpan.metacpan.org/"
179 ;; A selection of HTTP mirrors from http://www.cpan.org/SITES.html.
180 ;; Europe.
181 "http://ftp.belnet.be/mirror/ftp.cpan.org/"
182 "http://mirrors.nic.cz/CPAN/"
183 "http://mirror.ibcp.fr/pub/CPAN/"
184 "http://ftp.ntua.gr/pub/lang/perl/"
185 "http://kvin.lv/pub/CPAN/"
186 "http://mirror.as43289.net/pub/CPAN/"
187 "http://cpan.cs.uu.nl/"
188 "http://cpan.uib.no/"
189 "http://cpan-mirror.rbc.ru/pub/CPAN/"
190 "http://mirror.sbb.rs/CPAN/"
191 "http://cpan.lnx.sk/"
192 "http://ftp.rediris.es/mirror/CPAN/"
193 "http://mirror.ox.ac.uk/sites/www.cpan.org/"
194 ;; Africa.
195 "http://mirror.liquidtelecom.com/CPAN/"
196 "http://cpan.mirror.ac.za/"
197 "http://mirror.is.co.za/pub/cpan/"
198 "http://cpan.saix.net/"
199 "http://mirror.ucu.ac.ug/cpan/"
200 ;; North America.
201 "http://mirrors.gossamer-threads.com/CPAN/"
202 "http://mirror.csclub.uwaterloo.ca/CPAN/"
203 "http://mirrors.ucr.ac.cr/CPAN/"
204 "http://www.msg.com.mx/CPAN/"
205 "http://mirrors.namecheap.com/CPAN/"
206 "http://mirror.uic.edu/CPAN/"
207 "http://mirror.datapipe.net/CPAN/"
208 "http://mirror.cc.columbia.edu/pub/software/cpan/"
209 "http://mirror.uta.edu/CPAN/"
210 ;; South America.
211 "http://cpan.mmgdesigns.com.ar/"
212 "http://mirror.nbtelecom.com.br/CPAN/"
213 "http://linorg.usp.br/CPAN/"
214 "http://cpan.dcc.uchile.cl/"
215 "http://mirror.cedia.org.ec/CPAN/"
216 ;; Oceania.
217 "http://cpan.mirror.serversaustralia.com.au/"
218 "http://mirror.waia.asn.au/pub/cpan/"
219 "http://mirror.as24220.net/pub/cpan/"
220 "http://cpan.lagoon.nc/pub/CPAN/"
221 "http://cpan.inspire.net.nz/"
222 ;; Asia.
223 "http://mirror.dhakacom.com/CPAN/"
224 "http://mirrors.ustc.edu.cn/CPAN/"
225 "http://ftp.cuhk.edu.hk/pub/packages/perl/CPAN/"
226 "http://kambing.ui.ac.id/cpan/"
227 "http://cpan.hostiran.ir/"
228 "http://ftp.nara.wide.ad.jp/pub/CPAN/"
229 "http://mirror.neolabs.kz/CPAN/"
230 "http://cpan.nctu.edu.tw/"
231 "http://cpan.ulak.net.tr/"
232 "http://mirrors.vinahost.vn/CPAN/")
233 (cran
234 ;; Arbitrary mirrors from http://cran.r-project.org/mirrors.html
235 ;; This one automatically redirects to servers worldwide
236 "http://cran.r-project.org/"
237 "http://cran.rstudio.com/"
238 "http://cran.univ-lyon1.fr/"
239 "http://cran.ism.ac.jp/"
240 "http://cran.stat.auckland.ac.nz/"
241 "http://cran.mirror.ac.za/"
242 "http://cran.csie.ntu.edu.tw/")
243 (imagemagick
244 ;; from http://www.imagemagick.org/script/download.php
245 ;; (without mirrors that are unavailable or not up to date)
246 ;; mirrors keeping old versions at the top level
247 "https://sunsite.icm.edu.pl/packages/ImageMagick/"
248 ;; mirrors moving old versions to "legacy"
249 "http://mirrors-usa.go-parts.com/mirrors/ImageMagick/"
250 "http://mirror.checkdomain.de/imagemagick/"
251 "http://ftp.surfnet.nl/pub/ImageMagick/"
252 "http://mirror.searchdaimon.com/ImageMagick"
253 "http://mirror.is.co.za/pub/imagemagick/"
254 "http://www.imagemagick.org/download/"
255 "ftp://mirror.aarnet.edu.au/pub/imagemagick/"
256 "ftp://ftp.kddlabs.co.jp/graphics/ImageMagick/"
257 "ftp://ftp.u-aizu.ac.jp/pub/graphics/image/ImageMagick/imagemagick.org/"
258 "ftp://ftp.nluug.nl/pub/ImageMagick/"
259 "ftp://ftp.tpnet.pl/pub/graphics/ImageMagick/"
260 "ftp://ftp.fifi.org/pub/ImageMagick/"
261 ;; one legacy location as a last resort
262 "http://www.imagemagick.org/download/legacy/")
263 (debian
264 "http://ftp.de.debian.org/debian/"
265 "http://ftp.fr.debian.org/debian/"
266 "http://ftp.debian.org/debian/"
267 "http://archive.debian.org/debian/")
268 (kde
269 "http://download.kde.org"
270 "http://download.kde.org/Attic" ; for when it gets archived.
271 ;; Mirrors from http://files.kde.org/extra/mirrors.html
272 ;; Europe
273 "http://mirror.easyname.at/kde"
274 "http://mirror.karneval.cz/pub/kde"
275 "http://ftp.fi.muni.cz/pub/kde/"
276 "http://mirror.oss.maxcdn.com/kde/"
277 "http://ftp5.gwdg.de/pub/linux/kde/"
278 "http://ftp-stud.fht-esslingen.de/Mirrors/ftp.kde.org/pub/kde/"
279 "http://mirror.klaus-uwe.me/kde/ftp/"
280 "http://kde.beta.mirror.ga/"
281 "http://kde.alpha.mirror.ga/"
282 "http://mirror.netcologne.de/kde"
283 "http://vesta.informatik.rwth-aachen.de/ftp/pub/mirror/kde/"
284 "http://ftp.rz.uni-wuerzburg.de/pub/unix/kde/"
285 "http://mirrors.dotsrc.org/kde/"
286 "http://ftp.funet.fi/pub/mirrors/ftp.kde.org/pub/kde/"
287 "http://kde-mirror.freenux.org/"
288 "http://mirrors.ircam.fr/pub/KDE/"
289 "http://www-ftp.lip6.fr/pub/X11/kde/"
290 "http://fr2.rpmfind.net/linux/KDE/"
291 "http://kde.mirror.anlx.net/"
292 "http://www.mirrorservice.org/sites/ftp.kde.org/pub/kde/"
293 "http://ftp.heanet.ie/mirrors/ftp.kde.org/"
294 "http://ftp.nluug.nl/pub/windowing/kde/"
295 "http://ftp.surfnet.nl/windowing/kde/"
296 "http://ftp.icm.edu.pl/pub/unix/kde/"
297 "http://ftp.pbone.net/pub/kde/"
298 "http://piotrkosoft.net/pub/mirrors/ftp.kde.org/"
299 "http://mirrors.fe.up.pt/pub/kde/"
300 "http://ftp.iasi.roedu.net/pub/mirrors/ftp.kde.org/"
301 "http://ftp.acc.umu.se/mirror/kde.org/ftp/"
302 "http://kde.ip-connect.vn.ua/"
303 ;; North America
304 "http://mirror.its.dal.ca/kde/"
305 "http://mirror.csclub.uwaterloo.ca/kde/"
306 "http://mirror.cc.columbia.edu/pub/software/kde/"
307 "http://mirrors-usa.go-parts.com/kde"
308 "http://kde.mirrors.hoobly.com/"
309 "http://ftp.ussg.iu.edu/kde/"
310 "http://mirrors.mit.edu/kde/"
311 "http://kde.mirrors.tds.net/pub/kde/"
312 ;; Oceania
313 "http://ftp.kddlabs.co.jp/pub/X11/kde/"
314 "http://kde.mirror.uber.com.au/")
315 (openbsd
316 "https://ftp.openbsd.org/pub/OpenBSD/"
317 ;; Anycast CDN redirecting to your friendly local mirror.
318 "https://mirrors.evowise.com/pub/OpenBSD/"
319 ;; Other HTTPS mirrors from https://www.openbsd.org/ftp.html
320 "https://mirror.aarnet.edu.au/pub/OpenBSD/"
321 "https://ftp2.eu.openbsd.org/pub/OpenBSD/"
322 "https://openbsd.c3sl.ufpr.br/pub/OpenBSD/"
323 "https://openbsd.ipacct.com/pub/OpenBSD/"
324 "https://ftp.OpenBSD.org/pub/OpenBSD/"
325 "https://openbsd.cs.toronto.edu/pub/OpenBSD/"
326 "https://openbsd.delfic.org/pub/OpenBSD/"
327 "https://openbsd.mirror.netelligent.ca/pub/OpenBSD/"
328 "https://mirrors.ucr.ac.cr/pub/OpenBSD/"
329 "https://mirrors.dotsrc.org/pub/OpenBSD/"
330 "https://mirror.one.com/pub/OpenBSD/"
331 "https://ftp.fr.openbsd.org/pub/OpenBSD/"
332 "https://ftp2.fr.openbsd.org/pub/OpenBSD/"
333 "https://mirrors.ircam.fr/pub/OpenBSD/"
334 "https://ftp.spline.de/pub/OpenBSD/"
335 "https://mirror.hs-esslingen.de/pub/OpenBSD/"
336 "https://ftp.halifax.rwth-aachen.de/openbsd/"
337 "https://ftp.hostserver.de/pub/OpenBSD/"
338 "https://ftp.fau.de/pub/OpenBSD/"
339 "https://ftp.cc.uoc.gr/pub/OpenBSD/"
340 "https://openbsd.hk/pub/OpenBSD/"
341 "https://ftp.heanet.ie/pub/OpenBSD/"
342 "https://openbsd.mirror.garr.it/pub/OpenBSD/"
343 "https://mirror.litnet.lt/pub/OpenBSD/"
344 "https://mirror.meerval.net/pub/OpenBSD/"
345 "https://ftp.nluug.nl/pub/OpenBSD/"
346 "https://ftp.bit.nl/pub/OpenBSD/"
347 "https://mirrors.dalenys.com/pub/OpenBSD/"
348 "https://ftp.icm.edu.pl/pub/OpenBSD/"
349 "https://ftp.rnl.tecnico.ulisboa.pt/pub/OpenBSD/"
350 "https://mirrors.pidginhost.com/pub/OpenBSD/"
351 "https://mirror.yandex.ru/pub/OpenBSD/"
352 "https://ftp.eu.openbsd.org/pub/OpenBSD/"
353 "https://ftp.yzu.edu.tw/pub/OpenBSD/"
354 "https://www.mirrorservice.org/pub/OpenBSD/"
355 "https://anorien.csc.warwick.ac.uk/pub/OpenBSD/"
356 "https://mirror.bytemark.co.uk/pub/OpenBSD/"
357 "https://mirrors.sonic.net/pub/OpenBSD/"
358 "https://ftp3.usa.openbsd.org/pub/OpenBSD/"
359 "https://mirrors.syringanetworks.net/pub/OpenBSD/"
360 "https://openbsd.mirror.constant.com/pub/OpenBSD/"
361 "https://ftp4.usa.openbsd.org/pub/OpenBSD/"
362 "https://ftp5.usa.openbsd.org/pub/OpenBSD/"
363 "https://mirror.esc7.net/pub/OpenBSD/"))))
364
365 (define %mirror-file
366 ;; Copy of the list of mirrors to a file. This allows us to keep a single
367 ;; copy in the store, and computing it here avoids repeated calls to
368 ;; 'object->string'.
369 (plain-file "mirrors" (object->string %mirrors)))
370
371 (define %content-addressed-mirrors
372 ;; List of content-addressed mirrors. Each mirror is represented as a
373 ;; procedure that takes a file name, an algorithm (symbol) and a hash
374 ;; (bytevector), and returns a URL or #f.
375 ;; Note: Avoid 'https' to mitigate <http://bugs.gnu.org/22774>.
376 ;; TODO: Add more.
377 '(list (lambda (file algo hash)
378 ;; Files served by 'guix publish' are accessible under a single
379 ;; hash algorithm.
380 (string-append "http://mirror.hydra.gnu.org/file/"
381 file "/" (symbol->string algo) "/"
382 (bytevector->nix-base32-string hash)))
383 (lambda (file algo hash)
384 ;; 'tarballs.nixos.org' supports several algorithms.
385 (string-append "http://tarballs.nixos.org/"
386 (symbol->string algo) "/"
387 (bytevector->nix-base32-string hash)))))
388
389 (define %content-addressed-mirror-file
390 ;; Content-addressed mirrors stored in a file.
391 (plain-file "content-addressed-mirrors"
392 (object->string %content-addressed-mirrors)))
393
394 (define built-in-builders*
395 (let ((cache (make-weak-key-hash-table)))
396 (lambda ()
397 "Return, as a monadic value, the list of built-in builders supported by
398 the daemon."
399 (lambda (store)
400 ;; Memoize the result to avoid repeated RPCs.
401 (values (or (hashq-ref cache store)
402 (let ((result (built-in-builders store)))
403 (hashq-set! cache store result)
404 result))
405 store)))))
406
407 (define* (built-in-download file-name url
408 #:key system hash-algo hash
409 mirrors content-addressed-mirrors
410 (guile 'unused))
411 "Download FILE-NAME from URL using the built-in 'download' builder.
412
413 This is an \"out-of-band\" download in that the returned derivation does not
414 explicitly depend on Guile, GnuTLS, etc. Instead, the daemon performs the
415 download by itself using its own dependencies."
416 (mlet %store-monad ((mirrors (lower-object mirrors))
417 (content-addressed-mirrors
418 (lower-object content-addressed-mirrors)))
419 (raw-derivation file-name "builtin:download" '()
420 #:system system
421 #:hash-algo hash-algo
422 #:hash hash
423 #:inputs `((,mirrors)
424 (,content-addressed-mirrors))
425
426 ;; Honor the user's proxy and locale settings.
427 #:leaked-env-vars '("http_proxy" "https_proxy"
428 "LC_ALL" "LC_MESSAGES" "LANG"
429 "COLUMNS")
430
431 #:env-vars `(("url" . ,(object->string url))
432 ("mirrors" . ,mirrors)
433 ("content-addressed-mirrors"
434 . ,content-addressed-mirrors))
435
436 ;; Do not offload this derivation because we cannot be
437 ;; sure that the remote daemon supports the 'download'
438 ;; built-in. We may remove this limitation when support
439 ;; for that built-in is widespread.
440 #:local-build? #t)))
441
442 (define* (url-fetch url hash-algo hash
443 #:optional name
444 #:key (system (%current-system))
445 (guile (default-guile)))
446 "Return a fixed-output derivation that fetches URL (a string, or a list of
447 strings denoting alternate URLs), which is expected to have hash HASH of type
448 HASH-ALGO (a symbol). By default, the file name is the base name of URL;
449 optionally, NAME can specify a different file name.
450
451 When one of the URL starts with mirror://, then its host part is
452 interpreted as the name of a mirror scheme, taken from %MIRROR-FILE.
453
454 Alternately, when URL starts with file://, return the corresponding file name
455 in the store."
456 (define file-name
457 (match url
458 ((head _ ...)
459 (basename head))
460 (_
461 (basename url))))
462
463 (let ((uri (and (string? url) (string->uri url))))
464 (if (or (and (string? url) (not uri))
465 (and uri (memq (uri-scheme uri) '(#f file))))
466 (interned-file (if uri (uri-path uri) url)
467 (or name file-name))
468 (mlet %store-monad ((builtins (built-in-builders*)))
469 ;; The "download" built-in builder was added in guix-daemon in
470 ;; Nov. 2016 and made it in the 0.12.0 release of Dec. 2016. We now
471 ;; require it.
472 (unless (member "download" builtins)
473 (error "'guix-daemon' is too old, please upgrade" builtins))
474
475 (built-in-download (or name file-name) url
476 #:guile guile
477 #:system system
478 #:hash-algo hash-algo
479 #:hash hash
480 #:mirrors %mirror-file
481 #:content-addressed-mirrors
482 %content-addressed-mirror-file)))))
483
484 (define* (url-fetch/tarbomb url hash-algo hash
485 #:optional name
486 #:key (system (%current-system))
487 (guile (default-guile)))
488 "Similar to 'url-fetch' but unpack the file from URL in a directory of its
489 own. This helper makes it easier to deal with \"tar bombs\"."
490 (define file-name
491 (match url
492 ((head _ ...)
493 (basename head))
494 (_
495 (basename url))))
496 (define gzip
497 (module-ref (resolve-interface '(gnu packages compression)) 'gzip))
498 (define tar
499 (module-ref (resolve-interface '(gnu packages base)) 'tar))
500
501 (mlet %store-monad ((drv (url-fetch url hash-algo hash
502 (string-append "tarbomb-"
503 (or name file-name))
504 #:system system
505 #:guile guile)))
506 ;; Take the tar bomb, and simply unpack it as a directory.
507 ;; Use ungrafted tar/gzip so that the resulting tarball doesn't depend on
508 ;; whether grafts are enabled.
509 (gexp->derivation (or name file-name)
510 (with-imported-modules '((guix build utils))
511 #~(begin
512 (use-modules (guix build utils))
513 (mkdir #$output)
514 (setenv "PATH" (string-append #$gzip "/bin"))
515 (chdir #$output)
516 (invoke (string-append #$tar "/bin/tar")
517 "xf" #$drv)))
518 #:graft? #f
519 #:local-build? #t)))
520
521 (define* (url-fetch/zipbomb url hash-algo hash
522 #:optional name
523 #:key (system (%current-system))
524 (guile (default-guile)))
525 "Similar to 'url-fetch' but unpack the zip file at URL in a directory of its
526 own. This helper makes it easier to deal with \"zip bombs\"."
527 (define file-name
528 (match url
529 ((head _ ...)
530 (basename head))
531 (_
532 (basename url))))
533 (define unzip
534 (module-ref (resolve-interface '(gnu packages compression)) 'unzip))
535
536 (mlet %store-monad ((drv (url-fetch url hash-algo hash
537 (string-append "zipbomb-"
538 (or name file-name))
539 #:system system
540 #:guile guile)))
541 ;; Take the zip bomb, and simply unpack it as a directory.
542 ;; Use ungrafted unzip so that the resulting tarball doesn't depend on
543 ;; whether grafts are enabled.
544 (gexp->derivation (or name file-name)
545 (with-imported-modules '((guix build utils))
546 #~(begin
547 (use-modules (guix build utils))
548 (mkdir #$output)
549 (chdir #$output)
550 (invoke (string-append #$unzip "/bin/unzip")
551 #$drv)))
552 #:graft? #f
553 #:local-build? #t)))
554
555 (define* (download-to-store store url #:optional (name (basename url))
556 #:key (log (current-error-port)) recursive?
557 (verify-certificate? #t))
558 "Download from URL to STORE, either under NAME or URL's basename if
559 omitted. Write progress reports to LOG. RECURSIVE? has the same effect as
560 the same-named parameter of 'add-to-store'. VERIFY-CERTIFICATE? determines
561 whether or not to validate HTTPS server certificates."
562 (define uri
563 (string->uri url))
564
565 (if (or (not uri) (memq (uri-scheme uri) '(file #f)))
566 (add-to-store store name recursive? "sha256"
567 (if uri (uri-path uri) url))
568 (call-with-temporary-output-file
569 (lambda (temp port)
570 (let ((result
571 (parameterize ((current-output-port log))
572 (build:url-fetch url temp
573 #:mirrors %mirrors
574 #:verify-certificate?
575 verify-certificate?))))
576 (close port)
577 (and result
578 (add-to-store store name recursive? "sha256" temp)))))))
579
580 ;;; download.scm ends here