download.scm 29 KB

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