download.scm 29 KB

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