download.scm 29 KB

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