Merge '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 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://gd.tuwien.ac.at/privacy/gnupg/"
70 "http://artfiles.org/gnupg.org"
71 "http://www.crysys.hu/"
72 "https://gnupg.org/ftp/gcrypt/"
73 "ftp://mirrors.dotsrc.org/gcrypt/"
74 "ftp://mirror.cict.fr/gnupg/"
75 "ftp://ftp.franken.de/pub/crypt/mirror/ftp.gnupg.org/gcrypt/"
76 "ftp://ftp.freenet.de/pub/ftp.gnupg.org/gcrypt/"
77 "ftp://ftp.hi.is/pub/mirrors/gnupg/"
78 "ftp://ftp.heanet.ie/mirrors/ftp.gnupg.org/gcrypt/"
79 "ftp://ftp.bit.nl/mirror/gnupg/"
80 "ftp://ftp.surfnet.nl/pub/security/gnupg/"
81 "ftp://ftp.iasi.roedu.net/pub/mirrors/ftp.gnupg.org/"
82 "ftp://ftp.sunet.se/pub/security/gnupg/"
83 "ftp://mirror.switch.ch/mirror/gnupg/"
84 "ftp://mirror.tje.me.uk/pub/mirrors/ftp.gnupg.org/"
85 "ftp://ftp.mirrorservice.org/sites/ftp.gnupg.org/gcrypt/"
86 "ftp://ftp.ring.gr.jp/pub/net/gnupg/"
87 "ftp://ftp.gnupg.org/gcrypt/")
88 (gnome
89 "http://ftp.belnet.be/ftp.gnome.org/"
90 "http://ftp.linux.org.uk/mirrors/ftp.gnome.org/"
91 "http://ftp.gnome.org/pub/GNOME/"
92 "https://download.gnome.org/"
93 "http://mirror.yandex.ru/mirrors/ftp.gnome.org/")
94 (hackage
95 "http://hackage.haskell.org/")
96 (savannah
97 "http://download.savannah.gnu.org/releases/"
98 "ftp://ftp.twaren.net/Unix/NonGNU/"
99 "ftp://mirror.csclub.uwaterloo.ca/nongnu/"
100 "ftp://mirror.publicns.net/pub/nongnu/"
101 "ftp://savannah.c3sl.ufpr.br/"
102 "http://ftp.cc.uoc.gr/mirrors/nongnu.org/"
103 "http://ftp.twaren.net/Unix/NonGNU/"
104 "http://mirror.csclub.uwaterloo.ca/nongnu/"
105 "http://nongnu.askapache.com/"
106 "http://savannah.c3sl.ufpr.br/"
107 "http://download.savannah.gnu.org/releases-noredirect/"
108 "http://download-mirror.savannah.gnu.org/releases/")
109 (sourceforge ; https://sourceforge.net/p/forge/documentation/Mirrors/
110 "http://downloads.sourceforge.net/project/"
111 "http://ufpr.dl.sourceforge.net/project/"
112 "http://heanet.dl.sourceforge.net/project/"
113 "http://freefr.dl.sourceforge.net/project/"
114 "http://internode.dl.sourceforge.net/project/"
115 "http://jaist.dl.sourceforge.net/project/"
116 "http://kent.dl.sourceforge.net/project/"
117 "http://liquidtelecom.dl.sourceforge.net/project/"
118 ;; "http://nbtelecom.dl.sourceforge.net/project/" ;never returns 404s
119 "http://nchc.dl.sourceforge.net/project/"
120 "http://ncu.dl.sourceforge.net/project/"
121 "http://netcologne.dl.sourceforge.net/project/"
122 "http://netix.dl.sourceforge.net/project/"
123 "http://pilotfiber.dl.sourceforge.net/project/"
124 "http://superb-sea2.dl.sourceforge.net/project/"
125 "http://tenet.dl.sourceforge.net/project/"
126 "http://vorboss.dl.sourceforge.net/project/"
127 "http://netassist.dl.sourceforge.net/project/")
128 (netfilter.org ; https://www.netfilter.org/mirrors.html
129 "http://ftp.netfilter.org/pub/"
130 "ftp://ftp.es.netfilter.org/mirrors/netfilter/"
131 "ftp://ftp.hu.netfilter.org/"
132 "ftp://www.lt.netfilter.org/pub/")
133 (kernel.org
134 "http://ramses.wh2.tu-dresden.de/pub/mirrors/kernel.org/"
135 "http://linux-kernel.uio.no/pub/"
136 "http://kernel.osuosl.org/pub/"
137 "ftp://ftp.funet.fi/pub/mirrors/ftp.kernel.org/pub/"
138 "http://ftp.be.debian.org/pub/"
139 "http://mirror.linux.org.au/")
140 (apache ; from http://www.apache.org/mirrors/dist.html
141 "http://www.eu.apache.org/dist/"
142 "http://www.us.apache.org/dist/"
143 "ftp://gd.tuwien.ac.at/pub/infosys/servers/http/apache/dist/"
144 "http://apache.belnet.be/"
145 "http://mirrors.ircam.fr/pub/apache/"
146 "http://apache-mirror.rbc.ru/pub/apache/"
147
148 ;; As a last resort, try the archive.
149 "http://archive.apache.org/dist/")
150 (xorg ; from http://www.x.org/wiki/Releases/Download
151 "http://www.x.org/releases/" ; main mirrors
152 "ftp://mirror.csclub.uwaterloo.ca/x.org/" ; North America
153 "ftp://xorg.mirrors.pair.com/"
154 "http://mirror.csclub.uwaterloo.ca/x.org/"
155 "http://xorg.mirrors.pair.com/"
156 "http://mirror.us.leaseweb.net/xorg/"
157 "ftp://artfiles.org/x.org/" ; Europe
158 "ftp://ftp.chg.ru/pub/X11/x.org/"
159 "ftp://ftp.fu-berlin.de/unix/X11/FTP.X.ORG/"
160 "ftp://ftp.gwdg.de/pub/x11/x.org/"
161 "ftp://ftp.mirrorservice.org/sites/ftp.x.org/"
162 "ftp://ftp.ntua.gr/pub/X11/"
163 "ftp://ftp.piotrkosoft.net/pub/mirrors/ftp.x.org/"
164 "ftp://ftp.portal-to-web.de/pub/mirrors/x.org/"
165 "ftp://ftp.solnet.ch/mirror/x.org/"
166 "ftp://gd.tuwien.ac.at/X11/"
167 "ftp://mi.mirror.garr.it/mirrors/x.org/"
168 "ftp://mirror.cict.fr/x.org/"
169 "ftp://mirror.switch.ch/mirror/X11/"
170 "ftp://mirrors.ircam.fr/pub/x.org/"
171 "ftp://x.mirrors.skynet.be/pub/ftp.x.org/"
172 "ftp://ftp.cs.cuhk.edu.hk/pub/X11" ; East Asia
173 "ftp://ftp.u-aizu.ac.jp/pub/x11/x.org/"
174 "ftp://ftp.yz.yamagata-u.ac.jp/pub/X11/x.org/"
175 "ftp://ftp.kaist.ac.kr/x.org/"
176 "ftp://mirrors.go-part.com/xorg/"
177 "http://x.cs.pu.edu.tw/"
178 "ftp://ftp.is.co.za/pub/x.org") ; South Africa
179 (cpan ; from http://www.cpan.org/SITES.html
180 "http://mirror.ibcp.fr/pub/CPAN/"
181 "ftp://ftp.ciril.fr/pub/cpan/"
182 "ftp://artfiles.org/cpan.org/"
183 "http://www.cpan.org/"
184 "ftp://cpan.rinet.ru/pub/mirror/CPAN/"
185 "ftp://cpan.inode.at/"
186 "ftp://cpan.iht.co.il/"
187 "ftp://ftp.osuosl.org/pub/CPAN/"
188 "ftp://ftp.nara.wide.ad.jp/pub/CPAN/"
189 "http://mirrors.163.com/cpan/"
190 "ftp://cpan.mirror.ac.za/"
191 "http://cpan.mirrors.ionfish.org/"
192 "http://cpan.mirror.dkm.cz/pub/CPAN/"
193 "http://cpan.mirror.iphh.net/"
194 "http://mirrors.teentelecom.net/CPAN/"
195 "http://mirror.teklinks.com/CPAN/"
196 "http://cpan.weepeetelecom.be/"
197 "http://mirrors.xservers.ro/CPAN/"
198 "http://cpan.yimg.com/"
199 "http://mirror.yazd.ac.ir/cpan/"
200 "http://ftp.belnet.be/ftp.cpan.org/")
201 (cran
202 ;; Arbitrary mirrors from http://cran.r-project.org/mirrors.html
203 ;; This one automatically redirects to servers worldwide
204 "http://cran.r-project.org/"
205 "http://cran.rstudio.com/"
206 "http://cran.univ-lyon1.fr/"
207 "http://cran.ism.ac.jp/"
208 "http://cran.stat.auckland.ac.nz/"
209 "http://cran.mirror.ac.za/"
210 "http://cran.csie.ntu.edu.tw/")
211 (imagemagick
212 ;; from http://www.imagemagick.org/script/download.php
213 ;; (without mirrors that are unavailable or not up to date)
214 ;; mirrors keeping old versions at the top level
215 "ftp://sunsite.icm.edu.pl/packages/ImageMagick/"
216 ;; mirrors moving old versions to "legacy"
217 "ftp://mirror.aarnet.edu.au/pub/imagemagick/"
218 "http://mirror.checkdomain.de/imagemagick/"
219 "ftp://ftp.kddlabs.co.jp/graphics/ImageMagick/"
220 "ftp://ftp.u-aizu.ac.jp/pub/graphics/image/ImageMagick/imagemagick.org/"
221 "ftp://ftp.nluug.nl/pub/ImageMagick/"
222 "http://ftp.surfnet.nl/pub/ImageMagick/"
223 "http://mirror.searchdaimon.com/ImageMagick"
224 "ftp://ftp.tpnet.pl/pub/graphics/ImageMagick/"
225 "http://mirror.is.co.za/pub/imagemagick/"
226 "http://mirrors-usa.go-parts.com/mirrors/ImageMagick/"
227 "ftp://ftp.fifi.org/pub/ImageMagick/"
228 "http://www.imagemagick.org/download/"
229 ;; one legacy location as a last resort
230 "http://www.imagemagick.org/download/legacy/")
231 (debian
232 "http://ftp.de.debian.org/debian/"
233 "http://ftp.fr.debian.org/debian/"
234 "http://ftp.debian.org/debian/"
235 "http://archive.debian.org/debian/")
236 (kde
237 "http://download.kde.org"
238 "http://download.kde.org/Attic" ; for when it gets archived.
239 ;; Mirrors from http://files.kde.org/extra/mirrors.html
240 ;; Europe
241 "http://mirror.easyname.at/kde"
242 "http://mirror.karneval.cz/pub/kde"
243 "http://ftp.fi.muni.cz/pub/kde/"
244 "http://mirror.oss.maxcdn.com/kde/"
245 "http://ftp5.gwdg.de/pub/linux/kde/"
246 "http://ftp-stud.fht-esslingen.de/Mirrors/ftp.kde.org/pub/kde/"
247 "http://mirror.klaus-uwe.me/kde/ftp/"
248 "http://kde.beta.mirror.ga/"
249 "http://kde.alpha.mirror.ga/"
250 "http://mirror.netcologne.de/kde"
251 "http://vesta.informatik.rwth-aachen.de/ftp/pub/mirror/kde/"
252 "http://ftp.rz.uni-wuerzburg.de/pub/unix/kde/"
253 "http://mirrors.dotsrc.org/kde/"
254 "http://ftp.funet.fi/pub/mirrors/ftp.kde.org/pub/kde/"
255 "http://kde-mirror.freenux.org/"
256 "http://mirrors.ircam.fr/pub/KDE/"
257 "http://www-ftp.lip6.fr/pub/X11/kde/"
258 "http://fr2.rpmfind.net/linux/KDE/"
259 "http://kde.mirror.anlx.net/"
260 "http://www.mirrorservice.org/sites/ftp.kde.org/pub/kde/"
261 "http://ftp.heanet.ie/mirrors/ftp.kde.org/"
262 "http://ftp.nluug.nl/pub/windowing/kde/"
263 "http://ftp.surfnet.nl/windowing/kde/"
264 "http://ftp.icm.edu.pl/pub/unix/kde/"
265 "http://ftp.pbone.net/pub/kde/"
266 "http://piotrkosoft.net/pub/mirrors/ftp.kde.org/"
267 "http://mirrors.fe.up.pt/pub/kde/"
268 "http://ftp.iasi.roedu.net/pub/mirrors/ftp.kde.org/"
269 "http://ftp.acc.umu.se/mirror/kde.org/ftp/"
270 "http://kde.ip-connect.vn.ua/"
271 ;; North America
272 "http://mirror.its.dal.ca/kde/"
273 "http://mirror.csclub.uwaterloo.ca/kde/"
274 "http://mirror.cc.columbia.edu/pub/software/kde/"
275 "http://mirrors-usa.go-parts.com/kde"
276 "http://kde.mirrors.hoobly.com/"
277 "http://ftp.ussg.iu.edu/kde/"
278 "http://mirrors.mit.edu/kde/"
279 "http://kde.mirrors.tds.net/pub/kde/"
280 ;; Oceania
281 "http://ftp.kddlabs.co.jp/pub/X11/kde/"
282 "http://kde.mirror.uber.com.au/")
283 (openbsd
284 "https://ftp.openbsd.org/pub/OpenBSD/"
285 ;; Anycast CDN redirecting to your friendly local mirror.
286 "https://mirrors.evowise.com/pub/OpenBSD/"
287 ;; Other HTTPS mirrors from https://www.openbsd.org/ftp.html
288 "https://mirror.aarnet.edu.au/pub/OpenBSD/"
289 "https://ftp2.eu.openbsd.org/pub/OpenBSD/"
290 "https://openbsd.c3sl.ufpr.br/pub/OpenBSD/"
291 "https://openbsd.ipacct.com/pub/OpenBSD/"
292 "https://ftp.OpenBSD.org/pub/OpenBSD/"
293 "https://openbsd.cs.toronto.edu/pub/OpenBSD/"
294 "https://openbsd.delfic.org/pub/OpenBSD/"
295 "https://openbsd.mirror.netelligent.ca/pub/OpenBSD/"
296 "https://mirrors.ucr.ac.cr/pub/OpenBSD/"
297 "https://mirrors.dotsrc.org/pub/OpenBSD/"
298 "https://mirror.one.com/pub/OpenBSD/"
299 "https://ftp.fr.openbsd.org/pub/OpenBSD/"
300 "https://ftp2.fr.openbsd.org/pub/OpenBSD/"
301 "https://mirrors.ircam.fr/pub/OpenBSD/"
302 "https://ftp.spline.de/pub/OpenBSD/"
303 "https://mirror.hs-esslingen.de/pub/OpenBSD/"
304 "https://ftp.halifax.rwth-aachen.de/openbsd/"
305 "https://ftp.hostserver.de/pub/OpenBSD/"
306 "https://ftp.fau.de/pub/OpenBSD/"
307 "https://ftp.cc.uoc.gr/pub/OpenBSD/"
308 "https://openbsd.hk/pub/OpenBSD/"
309 "https://ftp.heanet.ie/pub/OpenBSD/"
310 "https://openbsd.mirror.garr.it/pub/OpenBSD/"
311 "https://mirror.litnet.lt/pub/OpenBSD/"
312 "https://mirror.meerval.net/pub/OpenBSD/"
313 "https://ftp.nluug.nl/pub/OpenBSD/"
314 "https://ftp.bit.nl/pub/OpenBSD/"
315 "https://mirrors.dalenys.com/pub/OpenBSD/"
316 "https://ftp.icm.edu.pl/pub/OpenBSD/"
317 "https://ftp.rnl.tecnico.ulisboa.pt/pub/OpenBSD/"
318 "https://mirrors.pidginhost.com/pub/OpenBSD/"
319 "https://mirror.yandex.ru/pub/OpenBSD/"
320 "https://ftp.eu.openbsd.org/pub/OpenBSD/"
321 "https://ftp.yzu.edu.tw/pub/OpenBSD/"
322 "https://www.mirrorservice.org/pub/OpenBSD/"
323 "https://anorien.csc.warwick.ac.uk/pub/OpenBSD/"
324 "https://mirror.bytemark.co.uk/pub/OpenBSD/"
325 "https://mirrors.sonic.net/pub/OpenBSD/"
326 "https://ftp3.usa.openbsd.org/pub/OpenBSD/"
327 "https://mirrors.syringanetworks.net/pub/OpenBSD/"
328 "https://openbsd.mirror.constant.com/pub/OpenBSD/"
329 "https://ftp4.usa.openbsd.org/pub/OpenBSD/"
330 "https://ftp5.usa.openbsd.org/pub/OpenBSD/"
331 "https://mirror.esc7.net/pub/OpenBSD/"))))
332
333 (define %mirror-file
334 ;; Copy of the list of mirrors to a file. This allows us to keep a single
335 ;; copy in the store, and computing it here avoids repeated calls to
336 ;; 'object->string'.
337 (plain-file "mirrors" (object->string %mirrors)))
338
339 (define %content-addressed-mirrors
340 ;; List of content-addressed mirrors. Each mirror is represented as a
341 ;; procedure that takes a file name, an algorithm (symbol) and a hash
342 ;; (bytevector), and returns a URL or #f.
343 ;; Note: Avoid 'https' to mitigate <http://bugs.gnu.org/22774>.
344 ;; TODO: Add more.
345 '(list (lambda (file algo hash)
346 ;; Files served by 'guix publish' are accessible under a single
347 ;; hash algorithm.
348 (string-append "http://mirror.hydra.gnu.org/file/"
349 file "/" (symbol->string algo) "/"
350 (bytevector->nix-base32-string hash)))
351 (lambda (file algo hash)
352 ;; 'tarballs.nixos.org' supports several algorithms.
353 (string-append "http://tarballs.nixos.org/"
354 (symbol->string algo) "/"
355 (bytevector->nix-base32-string hash)))))
356
357 (define %content-addressed-mirror-file
358 ;; Content-addressed mirrors stored in a file.
359 (plain-file "content-addressed-mirrors"
360 (object->string %content-addressed-mirrors)))
361
362 (define (gnutls-package)
363 "Return the default GnuTLS package."
364 (let ((module (resolve-interface '(gnu packages tls))))
365 (module-ref module 'gnutls)))
366
367 (define built-in-builders*
368 (let ((cache (make-weak-key-hash-table)))
369 (lambda ()
370 "Return, as a monadic value, the list of built-in builders supported by
371 the daemon."
372 (lambda (store)
373 ;; Memoize the result to avoid repeated RPCs.
374 (values (or (hashq-ref cache store)
375 (let ((result (built-in-builders store)))
376 (hashq-set! cache store result)
377 result))
378 store)))))
379
380 (define* (built-in-download file-name url
381 #:key system hash-algo hash
382 mirrors content-addressed-mirrors
383 (guile 'unused))
384 "Download FILE-NAME from URL using the built-in 'download' builder.
385
386 This is an \"out-of-band\" download in that the returned derivation does not
387 explicitly depend on Guile, GnuTLS, etc. Instead, the daemon performs the
388 download by itself using its own dependencies."
389 (mlet %store-monad ((mirrors (lower-object mirrors))
390 (content-addressed-mirrors
391 (lower-object content-addressed-mirrors)))
392 (raw-derivation file-name "builtin:download" '()
393 #:system system
394 #:hash-algo hash-algo
395 #:hash hash
396 #:inputs `((,mirrors)
397 (,content-addressed-mirrors))
398
399 ;; Honor the user's proxy and locale settings.
400 #:leaked-env-vars '("http_proxy" "https_proxy"
401 "LC_ALL" "LC_MESSAGES" "LANG"
402 "COLUMNS")
403
404 #:env-vars `(("url" . ,(object->string url))
405 ("mirrors" . ,mirrors)
406 ("content-addressed-mirrors"
407 . ,content-addressed-mirrors))
408
409 ;; Do not offload this derivation because we cannot be
410 ;; sure that the remote daemon supports the 'download'
411 ;; built-in. We may remove this limitation when support
412 ;; for that built-in is widespread.
413 #:local-build? #t)))
414
415 (define* (url-fetch url hash-algo hash
416 #:optional name
417 #:key (system (%current-system))
418 (guile (default-guile)))
419 "Return a fixed-output derivation that fetches URL (a string, or a list of
420 strings denoting alternate URLs), which is expected to have hash HASH of type
421 HASH-ALGO (a symbol). By default, the file name is the base name of URL;
422 optionally, NAME can specify a different file name.
423
424 When one of the URL starts with mirror://, then its host part is
425 interpreted as the name of a mirror scheme, taken from %MIRROR-FILE.
426
427 Alternately, when URL starts with file://, return the corresponding file name
428 in the store."
429 (define file-name
430 (match url
431 ((head _ ...)
432 (basename head))
433 (_
434 (basename url))))
435
436 (let ((uri (and (string? url) (string->uri url))))
437 (if (or (and (string? url) (not uri))
438 (and uri (memq (uri-scheme uri) '(#f file))))
439 (interned-file (if uri (uri-path uri) url)
440 (or name file-name))
441 (mlet %store-monad ((builtins (built-in-builders*)))
442 ;; The "download" built-in builder was added in guix-daemon in
443 ;; Nov. 2016 and made it in the 0.12.0 release of Dec. 2016. We now
444 ;; require it.
445 (unless (member "download" builtins)
446 (error "'guix-daemon' is too old, please upgrade" builtins))
447
448 (built-in-download (or name file-name) url
449 #:guile guile
450 #:system system
451 #:hash-algo hash-algo
452 #:hash hash
453 #:mirrors %mirror-file
454 #:content-addressed-mirrors
455 %content-addressed-mirror-file)))))
456
457 (define* (url-fetch/tarbomb url hash-algo hash
458 #:optional name
459 #:key (system (%current-system))
460 (guile (default-guile)))
461 "Similar to 'url-fetch' but unpack the file from URL in a directory of its
462 own. This helper makes it easier to deal with \"tar bombs\"."
463 (define file-name
464 (match url
465 ((head _ ...)
466 (basename head))
467 (_
468 (basename url))))
469 (define gzip
470 (module-ref (resolve-interface '(gnu packages compression)) 'gzip))
471 (define tar
472 (module-ref (resolve-interface '(gnu packages base)) 'tar))
473
474 (mlet %store-monad ((drv (url-fetch url hash-algo hash
475 (string-append "tarbomb-"
476 (or name file-name))
477 #:system system
478 #:guile guile)))
479 ;; Take the tar bomb, and simply unpack it as a directory.
480 (gexp->derivation (or name file-name)
481 #~(begin
482 (mkdir #$output)
483 (setenv "PATH" (string-append #$gzip "/bin"))
484 (chdir #$output)
485 (zero? (system* (string-append #$tar "/bin/tar")
486 "xf" #$drv)))
487 #:local-build? #t)))
488
489 (define* (url-fetch/zipbomb url hash-algo hash
490 #:optional name
491 #:key (system (%current-system))
492 (guile (default-guile)))
493 "Similar to 'url-fetch' but unpack the zip file at URL in a directory of its
494 own. This helper makes it easier to deal with \"zip bombs\"."
495 (define file-name
496 (match url
497 ((head _ ...)
498 (basename head))
499 (_
500 (basename url))))
501 (define unzip
502 (module-ref (resolve-interface '(gnu packages compression)) 'unzip))
503
504 (mlet %store-monad ((drv (url-fetch url hash-algo hash
505 (string-append "zipbomb-"
506 (or name file-name))
507 #:system system
508 #:guile guile)))
509 ;; Take the zip bomb, and simply unpack it as a directory.
510 (gexp->derivation (or name file-name)
511 #~(begin
512 (mkdir #$output)
513 (chdir #$output)
514 (zero? (system* (string-append #$unzip "/bin/unzip")
515 #$drv)))
516 #:local-build? #t)))
517
518 (define* (download-to-store store url #:optional (name (basename url))
519 #:key (log (current-error-port)) recursive?
520 (verify-certificate? #t))
521 "Download from URL to STORE, either under NAME or URL's basename if
522 omitted. Write progress reports to LOG. RECURSIVE? has the same effect as
523 the same-named parameter of 'add-to-store'. VERIFY-CERTIFICATE? determines
524 whether or not to validate HTTPS server certificates."
525 (define uri
526 (string->uri url))
527
528 (if (or (not uri) (memq (uri-scheme uri) '(file #f)))
529 (add-to-store store name recursive? "sha256"
530 (if uri (uri-path uri) url))
531 (call-with-temporary-output-file
532 (lambda (temp port)
533 (let ((result
534 (parameterize ((current-output-port log))
535 (build:url-fetch url temp
536 #:mirrors %mirrors
537 #:verify-certificate?
538 verify-certificate?))))
539 (close port)
540 (and result
541 (add-to-store store name recursive? "sha256" temp)))))))
542
543 ;;; download.scm ends here