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