download.scm 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 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 © 2016, 2017, 2020, 2022 Efraim Flashner <efraim@flashner.co.il>
  8. ;;; Copyright © 2017 Tobias Geerinckx-Rice <me@tobias.gr>
  9. ;;; Copyright © 2019 Guy Fleury Iteriteka <hoonandon@gmail.com>
  10. ;;;
  11. ;;; This file is part of GNU Guix.
  12. ;;;
  13. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  14. ;;; under the terms of the GNU General Public License as published by
  15. ;;; the Free Software Foundation; either version 3 of the License, or (at
  16. ;;; your option) any later version.
  17. ;;;
  18. ;;; GNU Guix is distributed in the hope that it will be useful, but
  19. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;;; GNU General Public License for more details.
  22. ;;;
  23. ;;; You should have received a copy of the GNU General Public License
  24. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  25. (define-module (guix download)
  26. #:use-module (ice-9 match)
  27. #:use-module (guix derivations)
  28. #:use-module (guix packages)
  29. #:use-module (guix store)
  30. #:autoload (guix build download) (url-fetch)
  31. #:use-module (guix monads)
  32. #:use-module (guix gexp)
  33. #:use-module (guix utils)
  34. #:use-module (web uri)
  35. #:use-module (srfi srfi-1)
  36. #:use-module (srfi srfi-26)
  37. #:export (%mirrors
  38. %disarchive-mirrors
  39. %download-fallback-test
  40. (url-fetch* . url-fetch)
  41. url-fetch/executable
  42. url-fetch/tarbomb
  43. url-fetch/zipbomb
  44. download-to-store))
  45. ;;; Commentary:
  46. ;;;
  47. ;;; Produce fixed-output derivations with data fetched over HTTP or FTP.
  48. ;;;
  49. ;;; Code:
  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.
  55. "https://ftpmirror.gnu.org/gnu/"
  56. "ftp://ftp.cs.tu-berlin.de/pub/gnu/"
  57. "ftp://ftp.funet.fi/pub/mirrors/ftp.gnu.org/gnu/"
  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. "https://download.gnome.org/"
  89. "http://ftp.gnome.org/pub/GNOME/"
  90. "http://mirror.yandex.ru/mirrors/ftp.gnome.org/")
  91. (hackage
  92. "http://hackage.haskell.org/")
  93. (savannah ; http://download0.savannah.gnu.org/mirmon/savannah/
  94. "https://download.savannah.gnu.org/releases/"
  95. "https://de.freedif.org/savannah/"
  96. "https://ftp.cc.uoc.gr/mirrors/nongnu.org/"
  97. "http://ftp.twaren.net/Unix/NonGNU/" ; https appears unsupported
  98. "https://mirror.csclub.uwaterloo.ca/nongnu/"
  99. "https://nongnu.askapache.com/"
  100. "https://savannah.c3sl.ufpr.br/"
  101. "http://download.savannah.gnu.org/releases-noredirect/"
  102. "https://download-mirror.savannah.gnu.org/releases/"
  103. "ftp://ftp.twaren.net/Unix/NonGNU/"
  104. "ftp://mirror.csclub.uwaterloo.ca/nongnu/"
  105. "ftp://mirror.publicns.net/pub/nongnu/"
  106. "ftp://savannah.c3sl.ufpr.br/")
  107. (sourceforge ; https://sourceforge.net/p/forge/documentation/Mirrors/
  108. "http://downloads.sourceforge.net/project/"
  109. "http://ufpr.dl.sourceforge.net/project/"
  110. "http://freefr.dl.sourceforge.net/project/"
  111. "http://internode.dl.sourceforge.net/project/"
  112. "http://jaist.dl.sourceforge.net/project/"
  113. "http://liquidtelecom.dl.sourceforge.net/project/"
  114. ;; "http://nbtelecom.dl.sourceforge.net/project/" ;never returns 404s
  115. "http://nchc.dl.sourceforge.net/project/"
  116. "http://netcologne.dl.sourceforge.net/project/"
  117. "http://netix.dl.sourceforge.net/project/"
  118. "http://pilotfiber.dl.sourceforge.net/project/"
  119. "http://tenet.dl.sourceforge.net/project/")
  120. (netfilter.org ; https://www.netfilter.org/mirrors.html
  121. "http://ftp.netfilter.org/pub/"
  122. "ftp://ftp.es.netfilter.org/mirrors/netfilter/"
  123. "ftp://ftp.hu.netfilter.org/"
  124. "ftp://www.lt.netfilter.org/pub/")
  125. (kernel.org
  126. "http://linux-kernel.uio.no/pub/"
  127. "http://kernel.osuosl.org/pub/"
  128. "http://ftp.be.debian.org/pub/"
  129. "http://mirror.linux.org.au/"
  130. "https://mirrors.edge.kernel.org/pub/"
  131. "ftp://ftp.funet.fi/pub/mirrors/ftp.kernel.org/pub/")
  132. (apache ; from http://www.apache.org/mirrors/dist.html
  133. "http://www.eu.apache.org/dist/"
  134. "http://www.us.apache.org/dist/"
  135. "https://ftp.nluug.nl/internet/apache/"
  136. "http://apache.mirror.iweb.ca/"
  137. "http://mirrors.ircam.fr/pub/apache/"
  138. "http://apache.mirrors.ovh.net/ftp.apache.org/dist/"
  139. "http://apache-mirror.rbc.ru/pub/apache/"
  140. "ftp://ftp.osuosl.org/pub/apache/"
  141. "http://mirrors.ibiblio.org/apache/"
  142. ;; As a last resort, try the archive.
  143. "http://archive.apache.org/dist/")
  144. (xorg ; from http://www.x.org/wiki/Releases/Download
  145. "http://www.x.org/releases/" ; main mirrors
  146. "http://mirror.csclub.uwaterloo.ca/x.org/" ; North America
  147. "http://xorg.mirrors.pair.com/"
  148. "http://mirror.us.leaseweb.net/xorg/"
  149. "ftp://mirror.csclub.uwaterloo.ca/x.org/"
  150. "ftp://xorg.mirrors.pair.com/"
  151. "ftp://artfiles.org/x.org/" ; Europe
  152. "ftp://ftp.chg.ru/pub/X11/x.org/"
  153. "ftp://ftp.fu-berlin.de/unix/X11/FTP.X.ORG/"
  154. "ftp://ftp.gwdg.de/pub/x11/x.org/"
  155. "ftp://ftp.mirrorservice.org/sites/ftp.x.org/"
  156. "ftp://ftp.ntua.gr/pub/X11/"
  157. "ftp://ftp.piotrkosoft.net/pub/mirrors/ftp.x.org/"
  158. "ftp://ftp.portal-to-web.de/pub/mirrors/x.org/"
  159. "ftp://ftp.solnet.ch/mirror/x.org/"
  160. "ftp://mi.mirror.garr.it/mirrors/x.org/"
  161. "ftp://mirror.cict.fr/x.org/"
  162. "ftp://mirror.switch.ch/mirror/X11/"
  163. "ftp://mirrors.ircam.fr/pub/x.org/"
  164. "ftp://x.mirrors.skynet.be/pub/ftp.x.org/"
  165. "http://x.cs.pu.edu.tw/" ; East Asia
  166. "ftp://ftp.cs.cuhk.edu.hk/pub/X11"
  167. "ftp://ftp.u-aizu.ac.jp/pub/x11/x.org/"
  168. "ftp://ftp.yz.yamagata-u.ac.jp/pub/X11/x.org/"
  169. "ftp://ftp.kaist.ac.kr/x.org/"
  170. "ftp://mirrors.go-part.com/xorg/"
  171. "ftp://ftp.is.co.za/pub/x.org") ; South Africa
  172. (cpan
  173. "http://www.cpan.org/"
  174. "http://cpan.metacpan.org/"
  175. ;; A selection of HTTP mirrors from http://www.cpan.org/SITES.html.
  176. ;; Europe.
  177. "http://ftp.belnet.be/mirror/ftp.cpan.org/"
  178. "http://mirrors.nic.cz/CPAN/"
  179. "http://mirror.ibcp.fr/pub/CPAN/"
  180. "http://ftp.ntua.gr/pub/lang/perl/"
  181. "http://mirror.as43289.net/pub/CPAN/"
  182. "http://cpan.cs.uu.nl/"
  183. "http://cpan.uib.no/"
  184. "http://cpan-mirror.rbc.ru/pub/CPAN/"
  185. "http://mirror.sbb.rs/CPAN/"
  186. "http://cpan.lnx.sk/"
  187. "http://ftp.rediris.es/mirror/CPAN/"
  188. "http://mirror.ox.ac.uk/sites/www.cpan.org/"
  189. ;; Africa.
  190. "http://mirror.liquidtelecom.com/CPAN/"
  191. "http://cpan.mirror.ac.za/"
  192. "http://mirror.is.co.za/pub/cpan/"
  193. "http://cpan.saix.net/"
  194. "http://mirror.ucu.ac.ug/cpan/"
  195. ;; North America.
  196. "http://mirrors.gossamer-threads.com/CPAN/"
  197. "http://mirror.csclub.uwaterloo.ca/CPAN/"
  198. "http://mirrors.ucr.ac.cr/CPAN/"
  199. "http://www.msg.com.mx/CPAN/"
  200. "http://mirrors.namecheap.com/CPAN/"
  201. "http://mirror.uic.edu/CPAN/"
  202. "http://mirror.datapipe.net/CPAN/"
  203. "http://mirror.cc.columbia.edu/pub/software/cpan/"
  204. "http://mirror.uta.edu/CPAN/"
  205. ;; South America.
  206. "http://cpan.mmgdesigns.com.ar/"
  207. "http://mirror.nbtelecom.com.br/CPAN/"
  208. "http://linorg.usp.br/CPAN/"
  209. "http://cpan.dcc.uchile.cl/"
  210. "http://mirror.cedia.org.ec/CPAN/"
  211. ;; Oceania.
  212. "http://cpan.mirror.serversaustralia.com.au/"
  213. "http://mirror.waia.asn.au/pub/cpan/"
  214. "http://mirror.as24220.net/pub/cpan/"
  215. "http://cpan.lagoon.nc/pub/CPAN/"
  216. "http://cpan.inspire.net.nz/"
  217. ;; Asia.
  218. "http://mirror.dhakacom.com/CPAN/"
  219. "http://mirrors.ustc.edu.cn/CPAN/"
  220. "http://ftp.cuhk.edu.hk/pub/packages/perl/CPAN/"
  221. "http://kambing.ui.ac.id/cpan/"
  222. "http://cpan.hostiran.ir/"
  223. "http://ftp.nara.wide.ad.jp/pub/CPAN/"
  224. "http://mirror.neolabs.kz/CPAN/"
  225. "http://cpan.nctu.edu.tw/"
  226. "http://cpan.ulak.net.tr/"
  227. "http://mirrors.vinahost.vn/CPAN/")
  228. (cran
  229. ;; Arbitrary mirrors from http://cran.r-project.org/mirrors.html
  230. ;; This one automatically redirects to servers worldwide
  231. "http://cran.r-project.org/"
  232. "http://cran.rstudio.com/"
  233. "http://cran.univ-lyon1.fr/"
  234. "http://cran.ism.ac.jp/"
  235. "http://cran.stat.auckland.ac.nz/"
  236. "http://cran.mirror.ac.za/"
  237. "http://cran.csie.ntu.edu.tw/")
  238. (ctan
  239. ;; This is the CTAN mirror multiplexor service, which automatically
  240. ;; redirect to a mirror in or close to the country of the requester
  241. ;; (see: https://ctan.org/mirrors/).
  242. "https://mirror.ctan.org/")
  243. (imagemagick
  244. ;; from http://www.imagemagick.org/script/download.php
  245. ;; (without mirrors that are unavailable or not up to date)
  246. "https://sunsite.icm.edu.pl/packages/ImageMagick/releases"
  247. "http://mirror.checkdomain.de/imagemagick/releases"
  248. "ftp://ftp.u-aizu.ac.jp/pub/graphics/image/ImageMagick/imagemagick.org/releases"
  249. "ftp://ftp.nluug.nl/pub/ImageMagick/"
  250. "http://www.imagemagick.org/download/releases/"
  251. ;; Try this if all else fails (normally contains just the latest version).
  252. "http://www.imagemagick.org/download/")
  253. (debian
  254. "http://ftp.de.debian.org/debian/"
  255. "http://ftp.fr.debian.org/debian/"
  256. "http://ftp.debian.org/debian/"
  257. "http://archive.debian.org/debian/")
  258. (kde
  259. "https://download.kde.org/"
  260. "https://download.kde.org/Attic/" ; for when it gets archived.
  261. ;; I could not find the classic static mirror list anymore. Instead,
  262. ;; add ‘.mirrorlist’ to the end of a recent download.kde.org tarball URL.
  263. ;; Europe
  264. "https://mirrors.xtom.de/kde/"
  265. "https://mirror.lyrahosting.com/pub/kde/"
  266. "https://mirrors.xtom.nl/kde/"
  267. "https://mirror.hs-esslingen.de/Mirrors/ftp.kde.org/pub/kde/"
  268. "https://mirror.kumi.systems/kde/ftp/"
  269. "https://mirrors.ircam.fr/pub/KDE/"
  270. "https://ftp.gwdg.de/pub/linux/kde/"
  271. "https://mirrors.gethosted.online/kde/pub/kde/"
  272. "https://fr2.rpmfind.net/linux/KDE/"
  273. "https://mirror.faigner.de/kde/ftp/"
  274. "https://www.mirrorservice.org/sites/download.kde.org/"
  275. "https://mirrors.ukfast.co.uk/sites/kde.org/ftp/"
  276. "https://mirrors.dotsrc.org/kde/"
  277. "http://kde.mirror.anlx.net/"
  278. "https://mirror.karneval.cz/pub/kde/"
  279. "https://ftp.fi.muni.cz/pub/kde/"
  280. "https://www-ftp.lip6.fr/pub/X11/kde/"
  281. "https://ftp.icm.edu.pl/pub/unix/kde/"
  282. "https://kde.mirror.garr.it/kde/ftp/"
  283. "https://ftp.acc.umu.se/mirror/kde.org/ftp/"
  284. "https://mirrors.up.pt/pub/kde/"
  285. "https://mirrors.nav.ro/kde/"
  286. "https://mirrors.xtom.ee/kde/"
  287. "https://ftp.funet.fi/pub/mirrors/ftp.kde.org/pub/kde/"
  288. "https://kde.ip-connect.vn.ua/"
  289. "https://mirrors.netix.net/kde/"
  290. "https://ftp.cc.uoc.gr/mirrors/kde/"
  291. ;; North America
  292. "https://mirror.its.dal.ca/kde/"
  293. "https://nnenix.mm.fcix.net/kdeftp/"
  294. "https://mirrors.mit.edu/kde/"
  295. "https://mirror.csclub.uwaterloo.ca/kde/"
  296. "https://mirror.fcix.net/kdeftp/"
  297. "https://mirrors.ocf.berkeley.edu/kde/"
  298. "https://mirrors.xtom.com/kde/"
  299. ;; South America
  300. "https://kde.c3sl.ufpr.br/"
  301. ;; Asia
  302. "https://mirrors.bfsu.edu.cn/kde/"
  303. "https://ftp-srv2.kddi-research.jp/pub/X11/kde/"
  304. "https://mirrors.xtom.jp/kde/"
  305. "https://mirrors.xtom.hk/kde/"
  306. ;; Africa
  307. "http://mirror.retentionrange.co.bw/kde/"
  308. ;; Oceania
  309. "https://mirrors.xtom.au/kde/")
  310. (openbsd
  311. "https://ftp.openbsd.org/pub/OpenBSD/"
  312. ;; Anycast CDN redirecting to your friendly local mirror.
  313. "https://mirrors.evowise.com/pub/OpenBSD/"
  314. ;; Other HTTPS mirrors from https://www.openbsd.org/ftp.html
  315. "https://mirror.aarnet.edu.au/pub/OpenBSD/"
  316. "https://ftp2.eu.openbsd.org/pub/OpenBSD/"
  317. "https://openbsd.c3sl.ufpr.br/pub/OpenBSD/"
  318. "https://openbsd.ipacct.com/pub/OpenBSD/"
  319. "https://ftp.OpenBSD.org/pub/OpenBSD/"
  320. "https://openbsd.cs.toronto.edu/pub/OpenBSD/"
  321. "https://openbsd.delfic.org/pub/OpenBSD/"
  322. "https://openbsd.mirror.netelligent.ca/pub/OpenBSD/"
  323. "https://mirrors.ucr.ac.cr/pub/OpenBSD/"
  324. "https://mirrors.dotsrc.org/pub/OpenBSD/"
  325. "https://mirror.one.com/pub/OpenBSD/"
  326. "https://ftp.fr.openbsd.org/pub/OpenBSD/"
  327. "https://ftp2.fr.openbsd.org/pub/OpenBSD/"
  328. "https://mirrors.ircam.fr/pub/OpenBSD/"
  329. "https://ftp.spline.de/pub/OpenBSD/"
  330. "https://mirror.hs-esslingen.de/pub/OpenBSD/"
  331. "https://ftp.halifax.rwth-aachen.de/openbsd/"
  332. "https://ftp.hostserver.de/pub/OpenBSD/"
  333. "https://ftp.fau.de/pub/OpenBSD/"
  334. "https://ftp.cc.uoc.gr/pub/OpenBSD/"
  335. "https://openbsd.hk/pub/OpenBSD/"
  336. "https://ftp.heanet.ie/pub/OpenBSD/"
  337. "https://openbsd.mirror.garr.it/pub/OpenBSD/"
  338. "https://mirror.litnet.lt/pub/OpenBSD/"
  339. "https://mirror.meerval.net/pub/OpenBSD/"
  340. "https://ftp.nluug.nl/pub/OpenBSD/"
  341. "https://ftp.bit.nl/pub/OpenBSD/"
  342. "https://mirrors.dalenys.com/pub/OpenBSD/"
  343. "https://ftp.icm.edu.pl/pub/OpenBSD/"
  344. "https://ftp.rnl.tecnico.ulisboa.pt/pub/OpenBSD/"
  345. "https://mirrors.pidginhost.com/pub/OpenBSD/"
  346. "https://mirror.yandex.ru/pub/OpenBSD/"
  347. "https://ftp.eu.openbsd.org/pub/OpenBSD/"
  348. "https://ftp.yzu.edu.tw/pub/OpenBSD/"
  349. "https://www.mirrorservice.org/pub/OpenBSD/"
  350. "https://anorien.csc.warwick.ac.uk/pub/OpenBSD/"
  351. "https://mirror.bytemark.co.uk/pub/OpenBSD/"
  352. "https://mirrors.sonic.net/pub/OpenBSD/"
  353. "https://ftp3.usa.openbsd.org/pub/OpenBSD/"
  354. "https://mirrors.syringanetworks.net/pub/OpenBSD/"
  355. "https://openbsd.mirror.constant.com/pub/OpenBSD/"
  356. "https://ftp4.usa.openbsd.org/pub/OpenBSD/"
  357. "https://ftp5.usa.openbsd.org/pub/OpenBSD/"
  358. "https://mirror.esc7.net/pub/OpenBSD/")
  359. (mate
  360. "https://pub.mate-desktop.org/releases/"
  361. "http://pub.mate-desktop.org/releases/"))))
  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. (define %content-addressed-mirrors
  368. ;; List of content-addressed mirrors. Each mirror is represented as a
  369. ;; procedure that takes a file name, an algorithm (symbol) and a hash
  370. ;; (bytevector), and returns a URL or #f.
  371. '(begin
  372. (use-modules (guix base16) (guix base32))
  373. (define (guix-publish host)
  374. (lambda (file algo hash)
  375. ;; Files served by 'guix publish' are accessible under a single
  376. ;; hash algorithm.
  377. (string-append "https://" host "/file/"
  378. file "/" (symbol->string algo) "/"
  379. (bytevector->nix-base32-string hash))))
  380. (list (guix-publish
  381. ;; bordeaux.guix.gnu.org uses the nar-herder rather than guix
  382. ;; publish, but it supports the same style of requests
  383. "bordeaux.guix.gnu.org")
  384. (guix-publish "ci.guix.gnu.org")
  385. (lambda (file algo hash)
  386. ;; 'tarballs.nixos.org' supports several algorithms.
  387. (string-append "https://tarballs.nixos.org/"
  388. (symbol->string algo) "/"
  389. (bytevector->nix-base32-string hash)))
  390. (lambda (file algo hash)
  391. ;; Software Heritage usually archives VCS history rather than
  392. ;; tarballs, but tarballs are sometimes available (and can be
  393. ;; explicitly stored there.) For example, see
  394. ;; <https://archive.softwareheritage.org/api/1/content/sha256:92d0fa1c311cacefa89853bdb53c62f4110cdfda3820346b59cbd098f40f955e/>.
  395. (string-append "https://archive.softwareheritage.org/api/1/content/"
  396. (symbol->string algo) ":"
  397. (bytevector->base16-string hash) "/raw/")))))
  398. (define %content-addressed-mirror-file
  399. ;; Content-addressed mirrors stored in a file.
  400. (plain-file "content-addressed-mirrors"
  401. (object->string %content-addressed-mirrors)))
  402. (define %no-mirrors-file
  403. ;; File specifying an empty list of mirrors, for fallback tests.
  404. (plain-file "no-content-addressed-mirrors" (object->string ''())))
  405. (define %disarchive-mirrors
  406. ;; TODO: Eventually turn into a procedure that takes a hash algorithm
  407. ;; (symbol) and hash (bytevector).
  408. '("https://disarchive.guix.gnu.org/"
  409. "https://disarchive.ngyro.com/"))
  410. (define %disarchive-mirror-file
  411. (plain-file "disarchive-mirrors" (object->string %disarchive-mirrors)))
  412. (define %no-disarchive-mirrors-file
  413. ;; File specifying an empty list of Disarchive mirrors, for fallback tests.
  414. (plain-file "no-disarchive-mirrors" (object->string '())))
  415. (define built-in-builders*
  416. (store-lift built-in-builders))
  417. (define* (built-in-download file-name url
  418. #:key system hash-algo hash
  419. mirrors content-addressed-mirrors
  420. disarchive-mirrors
  421. executable?
  422. (guile 'unused))
  423. "Download FILE-NAME from URL using the built-in 'download' builder. When
  424. EXECUTABLE? is true, make the downloaded file executable.
  425. This is an \"out-of-band\" download in that the returned derivation does not
  426. explicitly depend on Guile, GnuTLS, etc. Instead, the daemon performs the
  427. download by itself using its own dependencies."
  428. (mlet %store-monad ((mirrors (lower-object mirrors))
  429. (content-addressed-mirrors
  430. (lower-object content-addressed-mirrors))
  431. (disarchive-mirrors (lower-object disarchive-mirrors)))
  432. (raw-derivation file-name "builtin:download" '()
  433. #:system system
  434. #:hash-algo hash-algo
  435. #:hash hash
  436. #:recursive? executable?
  437. #:sources (list mirrors
  438. content-addressed-mirrors
  439. disarchive-mirrors)
  440. ;; Honor the user's proxy and locale settings.
  441. #:leaked-env-vars '("http_proxy" "https_proxy"
  442. "LC_ALL" "LC_MESSAGES" "LANG"
  443. "COLUMNS")
  444. #:env-vars `(("url" . ,(object->string url))
  445. ("mirrors" . ,mirrors)
  446. ("content-addressed-mirrors"
  447. . ,content-addressed-mirrors)
  448. ("disarchive-mirrors" . ,disarchive-mirrors)
  449. ,@(if executable?
  450. '(("executable" . "1"))
  451. '()))
  452. ;; Do not offload this derivation because we cannot be
  453. ;; sure that the remote daemon supports the 'download'
  454. ;; built-in. We may remove this limitation when support
  455. ;; for that built-in is widespread.
  456. #:local-build? #t)))
  457. (define %download-fallback-test
  458. ;; Define whether to test one of the download fallback mechanism. Possible
  459. ;; values are:
  460. ;;
  461. ;; - #f, to use the normal download methods, not trying to exercise the
  462. ;; fallback mechanism;
  463. ;;
  464. ;; - 'none, to disable all the fallback mechanisms;
  465. ;;
  466. ;; - 'content-addressed-mirrors, to purposefully attempt to download from
  467. ;; a content-addressed mirror;
  468. ;;
  469. ;; - 'disarchive-mirrors, to download from Disarchive + Software Heritage.
  470. ;;
  471. ;; This is meant to be used for testing purposes.
  472. (make-parameter (and=> (getenv "GUIX_DOWNLOAD_FALLBACK_TEST")
  473. string->symbol)))
  474. (define* (url-fetch* url hash-algo hash
  475. #:optional name
  476. #:key (system (%current-system))
  477. (guile (default-guile))
  478. executable?)
  479. "Return a fixed-output derivation that fetches data from URL (a string, or a
  480. list of strings denoting alternate URLs), which is expected to have hash HASH
  481. of type HASH-ALGO (a symbol). By default, the file name is the base name of
  482. URL; optionally, NAME can specify a different file name. When EXECUTABLE? is
  483. true, make the downloaded file executable.
  484. When one of the URL starts with mirror://, then its host part is
  485. interpreted as the name of a mirror scheme, taken from %MIRROR-FILE.
  486. Alternatively, when URL starts with file://, return the corresponding file
  487. name in the store."
  488. (define file-name
  489. (match url
  490. ((head _ ...)
  491. (basename head))
  492. (_
  493. (basename url))))
  494. (let ((uri (and (string? url) (string->uri url))))
  495. (if (or (and (string? url) (not uri))
  496. (and uri (memq (uri-scheme uri) '(#f file))))
  497. (interned-file (if uri (uri-path uri) url)
  498. (or name file-name))
  499. (mlet %store-monad ((builtins (built-in-builders*)))
  500. ;; The "download" built-in builder was added in guix-daemon in
  501. ;; Nov. 2016 and made it in the 0.12.0 release of Dec. 2016. We now
  502. ;; require it.
  503. (unless (member "download" builtins)
  504. (error "'guix-daemon' is too old, please upgrade" builtins))
  505. (built-in-download (or name file-name)
  506. (match (%download-fallback-test)
  507. ((or #f 'none) url)
  508. (_ "https://example.org/does-not-exist"))
  509. #:guile guile
  510. #:system system
  511. #:hash-algo hash-algo
  512. #:hash hash
  513. #:executable? executable?
  514. #:mirrors %mirror-file
  515. #:content-addressed-mirrors
  516. (match (%download-fallback-test)
  517. ((or #f 'content-addressed-mirrors)
  518. %content-addressed-mirror-file)
  519. (_ %no-mirrors-file))
  520. #:disarchive-mirrors
  521. (match (%download-fallback-test)
  522. ((or #f 'disarchive-mirrors)
  523. %disarchive-mirror-file)
  524. (_ %no-disarchive-mirrors-file)))))))
  525. (define* (url-fetch/executable url hash-algo hash
  526. #:optional name
  527. #:key (system (%current-system))
  528. (guile (default-guile)))
  529. "Like 'url-fetch', but make the downloaded file executable."
  530. (url-fetch* url hash-algo hash name
  531. #:system system
  532. #:guile guile
  533. #:executable? #t))
  534. (define* (url-fetch/tarbomb url hash-algo hash
  535. #:optional name
  536. #:key (system (%current-system))
  537. (guile (default-guile)))
  538. "Similar to 'url-fetch' but unpack the file from URL in a directory of its
  539. own. This helper makes it easier to deal with \"tar bombs\"."
  540. (define file-name
  541. (match url
  542. ((head _ ...)
  543. (basename head))
  544. (_
  545. (basename url))))
  546. (define gzip
  547. (module-ref (resolve-interface '(gnu packages compression)) 'gzip))
  548. (define tar
  549. (module-ref (resolve-interface '(gnu packages base)) 'tar))
  550. (mlet %store-monad ((drv (url-fetch* url hash-algo hash
  551. (string-append "tarbomb-"
  552. (or name file-name))
  553. #:system system
  554. #:guile guile))
  555. (guile (package->derivation guile system)))
  556. ;; Take the tar bomb, and simply unpack it as a directory.
  557. ;; Use ungrafted tar/gzip so that the resulting tarball doesn't depend on
  558. ;; whether grafts are enabled.
  559. (gexp->derivation (or name file-name)
  560. (with-imported-modules '((guix build utils))
  561. #~(begin
  562. (use-modules (guix build utils))
  563. (mkdir #$output)
  564. (setenv "PATH" (string-append #+gzip "/bin"))
  565. (chdir #$output)
  566. (invoke (string-append #+tar "/bin/tar")
  567. "xf" #$drv)))
  568. #:system system
  569. #:guile-for-build guile
  570. #:graft? #f
  571. #:local-build? #t)))
  572. (define* (url-fetch/zipbomb url hash-algo hash
  573. #:optional name
  574. #:key (system (%current-system))
  575. (guile (default-guile)))
  576. "Similar to 'url-fetch' but unpack the zip file at URL in a directory of its
  577. own. This helper makes it easier to deal with \"zip bombs\"."
  578. (define file-name
  579. (match url
  580. ((head _ ...)
  581. (basename head))
  582. (_
  583. (basename url))))
  584. (define unzip
  585. (module-ref (resolve-interface '(gnu packages compression)) 'unzip))
  586. (mlet %store-monad ((drv (url-fetch* url hash-algo hash
  587. (string-append "zipbomb-"
  588. (or name file-name))
  589. #:system system
  590. #:guile guile))
  591. (guile (package->derivation guile system)))
  592. ;; Take the zip bomb, and simply unpack it as a directory.
  593. ;; Use ungrafted unzip so that the resulting tarball doesn't depend on
  594. ;; whether grafts are enabled.
  595. (gexp->derivation (or name file-name)
  596. (with-imported-modules '((guix build utils))
  597. #~(begin
  598. (use-modules (guix build utils))
  599. (mkdir #$output)
  600. (chdir #$output)
  601. (invoke (string-append #+unzip "/bin/unzip")
  602. #$drv)))
  603. #:system system
  604. #:guile-for-build guile
  605. #:graft? #f
  606. #:local-build? #t)))
  607. (define* (download-to-store store url #:optional (name (basename url))
  608. #:key (log (current-error-port)) recursive?
  609. (verify-certificate? #t))
  610. "Download from URL to STORE, either under NAME or URL's basename if
  611. omitted. Write progress reports to LOG. RECURSIVE? has the same effect as
  612. the same-named parameter of 'add-to-store'. VERIFY-CERTIFICATE? determines
  613. whether or not to validate HTTPS server certificates."
  614. (define uri
  615. (string->uri url))
  616. (if (or (not uri) (memq (uri-scheme uri) '(file #f)))
  617. (add-to-store store name recursive? "sha256"
  618. (if uri (uri-path uri) url))
  619. (call-with-temporary-output-file
  620. (lambda (temp port)
  621. (let ((result
  622. (parameterize ((current-output-port log))
  623. (url-fetch url temp
  624. #:mirrors %mirrors
  625. #:verify-certificate? verify-certificate?))))
  626. (close port)
  627. (and result
  628. (add-to-store store name recursive? "sha256" temp)))))))
  629. ;;; download.scm ends here