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