download.scm 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 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. #:use-module ((guix build download) #:prefix build:)
  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. url-fetch
  38. url-fetch/executable
  39. url-fetch/tarbomb
  40. url-fetch/zipbomb
  41. download-to-store))
  42. ;;; Commentary:
  43. ;;;
  44. ;;; Produce fixed-output derivations with data fetched over HTTP or FTP.
  45. ;;;
  46. ;;; Code:
  47. (define %mirrors
  48. ;; Mirror lists used when `mirror://' URLs are passed.
  49. (let* ((gnu-mirrors
  50. '(;; This one redirects to a (supposedly) nearby and (supposedly)
  51. ;; up-to-date mirror.
  52. "https://ftpmirror.gnu.org/gnu/"
  53. "ftp://ftp.cs.tu-berlin.de/pub/gnu/"
  54. "ftp://ftp.funet.fi/pub/mirrors/ftp.gnu.org/gnu/"
  55. ;; This one is the master repository, and thus it's always
  56. ;; up-to-date.
  57. "http://ftp.gnu.org/pub/gnu/")))
  58. `((gnu ,@gnu-mirrors)
  59. (gcc
  60. "ftp://ftp.nluug.nl/mirror/languages/gcc/"
  61. "ftp://ftp.fu-berlin.de/unix/languages/gcc/"
  62. "ftp://ftp.irisa.fr/pub/mirrors/gcc.gnu.org/gcc/"
  63. "ftp://gcc.gnu.org/pub/gcc/"
  64. ,@(map (cut string-append <> "/gcc") gnu-mirrors))
  65. (gnupg
  66. "http://artfiles.org/gnupg.org"
  67. "http://www.crysys.hu/"
  68. "https://gnupg.org/ftp/gcrypt/"
  69. "ftp://mirrors.dotsrc.org/gcrypt/"
  70. "ftp://mirror.cict.fr/gnupg/"
  71. "ftp://ftp.franken.de/pub/crypt/mirror/ftp.gnupg.org/gcrypt/"
  72. "ftp://ftp.freenet.de/pub/ftp.gnupg.org/gcrypt/"
  73. "ftp://ftp.hi.is/pub/mirrors/gnupg/"
  74. "ftp://ftp.heanet.ie/mirrors/ftp.gnupg.org/gcrypt/"
  75. "ftp://ftp.bit.nl/mirror/gnupg/"
  76. "ftp://ftp.surfnet.nl/pub/security/gnupg/"
  77. "ftp://ftp.iasi.roedu.net/pub/mirrors/ftp.gnupg.org/"
  78. "ftp://ftp.sunet.se/pub/security/gnupg/"
  79. "ftp://mirror.switch.ch/mirror/gnupg/"
  80. "ftp://mirror.tje.me.uk/pub/mirrors/ftp.gnupg.org/"
  81. "ftp://ftp.mirrorservice.org/sites/ftp.gnupg.org/gcrypt/"
  82. "ftp://ftp.ring.gr.jp/pub/net/gnupg/"
  83. "ftp://ftp.gnupg.org/gcrypt/")
  84. (gnome
  85. "http://ftp.belnet.be/ftp.gnome.org/"
  86. "http://ftp.linux.org.uk/mirrors/ftp.gnome.org/"
  87. "http://ftp.gnome.org/pub/GNOME/"
  88. "https://download.gnome.org/"
  89. "http://mirror.yandex.ru/mirrors/ftp.gnome.org/")
  90. (hackage
  91. "http://hackage.haskell.org/")
  92. (savannah
  93. "http://download.savannah.gnu.org/releases/"
  94. "http://ftp.cc.uoc.gr/mirrors/nongnu.org/"
  95. "http://ftp.twaren.net/Unix/NonGNU/"
  96. "http://mirror.csclub.uwaterloo.ca/nongnu/"
  97. "http://nongnu.askapache.com/"
  98. "http://savannah.c3sl.ufpr.br/"
  99. "http://download.savannah.gnu.org/releases-noredirect/"
  100. "http://download-mirror.savannah.gnu.org/releases/"
  101. "ftp://ftp.twaren.net/Unix/NonGNU/"
  102. "ftp://mirror.csclub.uwaterloo.ca/nongnu/"
  103. "ftp://mirror.publicns.net/pub/nongnu/"
  104. "ftp://savannah.c3sl.ufpr.br/")
  105. (sourceforge ; https://sourceforge.net/p/forge/documentation/Mirrors/
  106. "http://downloads.sourceforge.net/project/"
  107. "http://ufpr.dl.sourceforge.net/project/"
  108. "http://heanet.dl.sourceforge.net/project/"
  109. "http://freefr.dl.sourceforge.net/project/"
  110. "http://internode.dl.sourceforge.net/project/"
  111. "http://jaist.dl.sourceforge.net/project/"
  112. "http://kent.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://ncu.dl.sourceforge.net/project/"
  117. "http://netcologne.dl.sourceforge.net/project/"
  118. "http://netix.dl.sourceforge.net/project/"
  119. "http://pilotfiber.dl.sourceforge.net/project/"
  120. "http://superb-sea2.dl.sourceforge.net/project/"
  121. "http://tenet.dl.sourceforge.net/project/"
  122. "http://vorboss.dl.sourceforge.net/project/"
  123. "http://netassist.dl.sourceforge.net/project/")
  124. (netfilter.org ; https://www.netfilter.org/mirrors.html
  125. "http://ftp.netfilter.org/pub/"
  126. "ftp://ftp.es.netfilter.org/mirrors/netfilter/"
  127. "ftp://ftp.hu.netfilter.org/"
  128. "ftp://www.lt.netfilter.org/pub/")
  129. (kernel.org
  130. "http://linux-kernel.uio.no/pub/"
  131. "http://kernel.osuosl.org/pub/"
  132. "http://ftp.be.debian.org/pub/"
  133. "http://mirror.linux.org.au/"
  134. "ftp://ftp.funet.fi/pub/mirrors/ftp.kernel.org/pub/")
  135. (apache ; from http://www.apache.org/mirrors/dist.html
  136. "http://www.eu.apache.org/dist/"
  137. "http://www.us.apache.org/dist/"
  138. "http://apache.belnet.be/"
  139. "http://mirrors.ircam.fr/pub/apache/"
  140. "http://apache-mirror.rbc.ru/pub/apache/"
  141. ;; As a last resort, try the archive.
  142. "http://archive.apache.org/dist/")
  143. (xorg ; from http://www.x.org/wiki/Releases/Download
  144. "http://www.x.org/releases/" ; main mirrors
  145. "http://mirror.csclub.uwaterloo.ca/x.org/" ; North America
  146. "http://xorg.mirrors.pair.com/"
  147. "http://mirror.us.leaseweb.net/xorg/"
  148. "ftp://mirror.csclub.uwaterloo.ca/x.org/"
  149. "ftp://xorg.mirrors.pair.com/"
  150. "ftp://artfiles.org/x.org/" ; Europe
  151. "ftp://ftp.chg.ru/pub/X11/x.org/"
  152. "ftp://ftp.fu-berlin.de/unix/X11/FTP.X.ORG/"
  153. "ftp://ftp.gwdg.de/pub/x11/x.org/"
  154. "ftp://ftp.mirrorservice.org/sites/ftp.x.org/"
  155. "ftp://ftp.ntua.gr/pub/X11/"
  156. "ftp://ftp.piotrkosoft.net/pub/mirrors/ftp.x.org/"
  157. "ftp://ftp.portal-to-web.de/pub/mirrors/x.org/"
  158. "ftp://ftp.solnet.ch/mirror/x.org/"
  159. "ftp://mi.mirror.garr.it/mirrors/x.org/"
  160. "ftp://mirror.cict.fr/x.org/"
  161. "ftp://mirror.switch.ch/mirror/X11/"
  162. "ftp://mirrors.ircam.fr/pub/x.org/"
  163. "ftp://x.mirrors.skynet.be/pub/ftp.x.org/"
  164. "http://x.cs.pu.edu.tw/" ; East Asia
  165. "ftp://ftp.cs.cuhk.edu.hk/pub/X11"
  166. "ftp://ftp.u-aizu.ac.jp/pub/x11/x.org/"
  167. "ftp://ftp.yz.yamagata-u.ac.jp/pub/X11/x.org/"
  168. "ftp://ftp.kaist.ac.kr/x.org/"
  169. "ftp://mirrors.go-part.com/xorg/"
  170. "ftp://ftp.is.co.za/pub/x.org") ; South Africa
  171. (cpan
  172. "http://www.cpan.org/"
  173. "http://cpan.metacpan.org/"
  174. ;; A selection of HTTP mirrors from http://www.cpan.org/SITES.html.
  175. ;; Europe.
  176. "http://ftp.belnet.be/mirror/ftp.cpan.org/"
  177. "http://mirrors.nic.cz/CPAN/"
  178. "http://mirror.ibcp.fr/pub/CPAN/"
  179. "http://ftp.ntua.gr/pub/lang/perl/"
  180. "http://kvin.lv/pub/CPAN/"
  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. (imagemagick
  239. ;; from http://www.imagemagick.org/script/download.php
  240. ;; (without mirrors that are unavailable or not up to date)
  241. ;; mirrors keeping old versions at the top level
  242. "https://sunsite.icm.edu.pl/packages/ImageMagick/"
  243. ;; mirrors moving old versions to "legacy"
  244. "http://mirrors-usa.go-parts.com/mirrors/ImageMagick/"
  245. "http://mirror.checkdomain.de/imagemagick/"
  246. "http://ftp.surfnet.nl/pub/ImageMagick/"
  247. "http://mirror.searchdaimon.com/ImageMagick"
  248. "http://mirror.is.co.za/pub/imagemagick/"
  249. "http://www.imagemagick.org/download/"
  250. "ftp://mirror.aarnet.edu.au/pub/imagemagick/"
  251. "ftp://ftp.kddlabs.co.jp/graphics/ImageMagick/"
  252. "ftp://ftp.u-aizu.ac.jp/pub/graphics/image/ImageMagick/imagemagick.org/"
  253. "ftp://ftp.nluug.nl/pub/ImageMagick/"
  254. "ftp://ftp.tpnet.pl/pub/graphics/ImageMagick/"
  255. "ftp://ftp.fifi.org/pub/ImageMagick/"
  256. ;; one legacy location as a last resort
  257. "http://www.imagemagick.org/download/legacy/")
  258. (debian
  259. "http://ftp.de.debian.org/debian/"
  260. "http://ftp.fr.debian.org/debian/"
  261. "http://ftp.debian.org/debian/"
  262. "http://archive.debian.org/debian/")
  263. (kde
  264. "http://download.kde.org"
  265. "http://download.kde.org/Attic" ; for when it gets archived.
  266. ;; Mirrors from http://files.kde.org/extra/mirrors.html
  267. ;; Europe
  268. "http://mirror.easyname.at/kde"
  269. "http://mirror.karneval.cz/pub/kde"
  270. "http://ftp.fi.muni.cz/pub/kde/"
  271. "http://mirror.oss.maxcdn.com/kde/"
  272. "http://ftp5.gwdg.de/pub/linux/kde/"
  273. "http://ftp-stud.fht-esslingen.de/Mirrors/ftp.kde.org/pub/kde/"
  274. "http://mirror.klaus-uwe.me/kde/ftp/"
  275. "http://kde.beta.mirror.ga/"
  276. "http://kde.alpha.mirror.ga/"
  277. "http://mirror.netcologne.de/kde"
  278. "http://vesta.informatik.rwth-aachen.de/ftp/pub/mirror/kde/"
  279. "http://ftp.rz.uni-wuerzburg.de/pub/unix/kde/"
  280. "http://mirrors.dotsrc.org/kde/"
  281. "http://ftp.funet.fi/pub/mirrors/ftp.kde.org/pub/kde/"
  282. "http://kde-mirror.freenux.org/"
  283. "http://mirrors.ircam.fr/pub/KDE/"
  284. "http://www-ftp.lip6.fr/pub/X11/kde/"
  285. "http://fr2.rpmfind.net/linux/KDE/"
  286. "http://kde.mirror.anlx.net/"
  287. "http://www.mirrorservice.org/sites/ftp.kde.org/pub/kde/"
  288. "http://ftp.heanet.ie/mirrors/ftp.kde.org/"
  289. "http://ftp.nluug.nl/pub/windowing/kde/"
  290. "http://ftp.surfnet.nl/windowing/kde/"
  291. "http://ftp.icm.edu.pl/pub/unix/kde/"
  292. "http://ftp.pbone.net/pub/kde/"
  293. "http://piotrkosoft.net/pub/mirrors/ftp.kde.org/"
  294. "http://mirrors.fe.up.pt/pub/kde/"
  295. "http://ftp.iasi.roedu.net/pub/mirrors/ftp.kde.org/"
  296. "http://ftp.acc.umu.se/mirror/kde.org/ftp/"
  297. "http://kde.ip-connect.vn.ua/"
  298. ;; North America
  299. "http://mirror.its.dal.ca/kde/"
  300. "http://mirror.csclub.uwaterloo.ca/kde/"
  301. "http://mirror.cc.columbia.edu/pub/software/kde/"
  302. "http://mirrors-usa.go-parts.com/kde"
  303. "http://kde.mirrors.hoobly.com/"
  304. "http://ftp.ussg.iu.edu/kde/"
  305. "http://mirrors.mit.edu/kde/"
  306. "http://kde.mirrors.tds.net/pub/kde/"
  307. ;; Oceania
  308. "http://ftp.kddlabs.co.jp/pub/X11/kde/"
  309. "http://kde.mirror.uber.com.au/")
  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 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. ;; XXX: (guix base16) appeared in March 2017 (and thus 0.13.0) so old
  381. ;; installations of the daemon might lack it. Thus, load it lazily to
  382. ;; avoid gratuitous errors. See <https://bugs.gnu.org/33542>.
  383. (module-autoload! (current-module)
  384. '(guix base16) '(bytevector->base16-string))
  385. (list (guix-publish "ci.guix.gnu.org")
  386. (lambda (file algo hash)
  387. ;; 'tarballs.nixos.org' supports several algorithms.
  388. (string-append "https://tarballs.nixos.org/"
  389. (symbol->string algo) "/"
  390. (bytevector->nix-base32-string hash)))
  391. (lambda (file algo hash)
  392. ;; Software Heritage usually archives VCS history rather than
  393. ;; tarballs, but tarballs are sometimes available (and can be
  394. ;; explicitly stored there.) For example, see
  395. ;; <https://archive.softwareheritage.org/api/1/content/sha256:92d0fa1c311cacefa89853bdb53c62f4110cdfda3820346b59cbd098f40f955e/>.
  396. (string-append "https://archive.softwareheritage.org/api/1/content/"
  397. (symbol->string algo) ":"
  398. (bytevector->base16-string hash) "/raw/")))))
  399. (define %content-addressed-mirror-file
  400. ;; Content-addressed mirrors stored in a file.
  401. (plain-file "content-addressed-mirrors"
  402. (object->string %content-addressed-mirrors)))
  403. (define built-in-builders*
  404. (store-lift built-in-builders))
  405. (define* (built-in-download file-name url
  406. #:key system hash-algo hash
  407. mirrors content-addressed-mirrors
  408. executable?
  409. (guile 'unused))
  410. "Download FILE-NAME from URL using the built-in 'download' builder. When
  411. EXECUTABLE? is true, make the downloaded file executable.
  412. This is an \"out-of-band\" download in that the returned derivation does not
  413. explicitly depend on Guile, GnuTLS, etc. Instead, the daemon performs the
  414. download by itself using its own dependencies."
  415. (mlet %store-monad ((mirrors (lower-object mirrors))
  416. (content-addressed-mirrors
  417. (lower-object content-addressed-mirrors)))
  418. (raw-derivation file-name "builtin:download" '()
  419. #:system system
  420. #:hash-algo hash-algo
  421. #:hash hash
  422. #:recursive? executable?
  423. #:sources (list mirrors content-addressed-mirrors)
  424. ;; Honor the user's proxy and locale settings.
  425. #:leaked-env-vars '("http_proxy" "https_proxy"
  426. "LC_ALL" "LC_MESSAGES" "LANG"
  427. "COLUMNS")
  428. #:env-vars `(("url" . ,(object->string url))
  429. ("mirrors" . ,mirrors)
  430. ("content-addressed-mirrors"
  431. . ,content-addressed-mirrors)
  432. ,@(if executable?
  433. '(("executable" . "1"))
  434. '()))
  435. ;; Do not offload this derivation because we cannot be
  436. ;; sure that the remote daemon supports the 'download'
  437. ;; built-in. We may remove this limitation when support
  438. ;; for that built-in is widespread.
  439. #:local-build? #t)))
  440. (define* (url-fetch url hash-algo hash
  441. #:optional name
  442. #:key (system (%current-system))
  443. (guile (default-guile))
  444. executable?)
  445. "Return a fixed-output derivation that fetches URL (a string, or a list of
  446. strings denoting alternate URLs), which is expected to have hash HASH of type
  447. HASH-ALGO (a symbol). By default, the file name is the base name of URL;
  448. optionally, NAME can specify a different file name. When EXECUTABLE? is true,
  449. make the downloaded file executable.
  450. When one of the URL starts with mirror://, then its host part is
  451. interpreted as the name of a mirror scheme, taken from %MIRROR-FILE.
  452. Alternately, when URL starts with file://, return the corresponding file name
  453. in the store."
  454. (define file-name
  455. (match url
  456. ((head _ ...)
  457. (basename head))
  458. (_
  459. (basename url))))
  460. (let ((uri (and (string? url) (string->uri url))))
  461. (if (or (and (string? url) (not uri))
  462. (and uri (memq (uri-scheme uri) '(#f file))))
  463. (interned-file (if uri (uri-path uri) url)
  464. (or name file-name))
  465. (mlet %store-monad ((builtins (built-in-builders*)))
  466. ;; The "download" built-in builder was added in guix-daemon in
  467. ;; Nov. 2016 and made it in the 0.12.0 release of Dec. 2016. We now
  468. ;; require it.
  469. (unless (member "download" builtins)
  470. (error "'guix-daemon' is too old, please upgrade" builtins))
  471. (built-in-download (or name file-name) url
  472. #:guile guile
  473. #:system system
  474. #:hash-algo hash-algo
  475. #:hash hash
  476. #:executable? executable?
  477. #:mirrors %mirror-file
  478. #:content-addressed-mirrors
  479. %content-addressed-mirror-file)))))
  480. (define* (url-fetch/executable url hash-algo hash
  481. #:optional name
  482. #:key (system (%current-system))
  483. (guile (default-guile)))
  484. "Like 'url-fetch', but make the downloaded file executable."
  485. (url-fetch url hash-algo hash name
  486. #:system system
  487. #:guile guile
  488. #:executable? #t))
  489. (define* (url-fetch/tarbomb url hash-algo hash
  490. #:optional name
  491. #:key (system (%current-system))
  492. (guile (default-guile)))
  493. "Similar to 'url-fetch' but unpack the file from URL in a directory of its
  494. own. This helper makes it easier to deal with \"tar bombs\"."
  495. (define file-name
  496. (match url
  497. ((head _ ...)
  498. (basename head))
  499. (_
  500. (basename url))))
  501. (define gzip
  502. (module-ref (resolve-interface '(gnu packages compression)) 'gzip))
  503. (define tar
  504. (module-ref (resolve-interface '(gnu packages base)) 'tar))
  505. (mlet %store-monad ((drv (url-fetch url hash-algo hash
  506. (string-append "tarbomb-"
  507. (or name file-name))
  508. #:system system
  509. #:guile guile)))
  510. ;; Take the tar bomb, and simply unpack it as a directory.
  511. ;; Use ungrafted tar/gzip so that the resulting tarball doesn't depend on
  512. ;; whether grafts are enabled.
  513. (gexp->derivation (or name file-name)
  514. (with-imported-modules '((guix build utils))
  515. #~(begin
  516. (use-modules (guix build utils))
  517. (mkdir #$output)
  518. (setenv "PATH" (string-append #$gzip "/bin"))
  519. (chdir #$output)
  520. (invoke (string-append #$tar "/bin/tar")
  521. "xf" #$drv)))
  522. #:graft? #f
  523. #:local-build? #t)))
  524. (define* (url-fetch/zipbomb url hash-algo hash
  525. #:optional name
  526. #:key (system (%current-system))
  527. (guile (default-guile)))
  528. "Similar to 'url-fetch' but unpack the zip file at URL in a directory of its
  529. own. This helper makes it easier to deal with \"zip bombs\"."
  530. (define file-name
  531. (match url
  532. ((head _ ...)
  533. (basename head))
  534. (_
  535. (basename url))))
  536. (define unzip
  537. (module-ref (resolve-interface '(gnu packages compression)) 'unzip))
  538. (mlet %store-monad ((drv (url-fetch url hash-algo hash
  539. (string-append "zipbomb-"
  540. (or name file-name))
  541. #:system system
  542. #:guile guile)))
  543. ;; Take the zip bomb, and simply unpack it as a directory.
  544. ;; Use ungrafted unzip so that the resulting tarball doesn't depend on
  545. ;; whether grafts are enabled.
  546. (gexp->derivation (or name file-name)
  547. (with-imported-modules '((guix build utils))
  548. #~(begin
  549. (use-modules (guix build utils))
  550. (mkdir #$output)
  551. (chdir #$output)
  552. (invoke (string-append #$unzip "/bin/unzip")
  553. #$drv)))
  554. #:graft? #f
  555. #:local-build? #t)))
  556. (define* (download-to-store store url #:optional (name (basename url))
  557. #:key (log (current-error-port)) recursive?
  558. (verify-certificate? #t))
  559. "Download from URL to STORE, either under NAME or URL's basename if
  560. omitted. Write progress reports to LOG. RECURSIVE? has the same effect as
  561. the same-named parameter of 'add-to-store'. VERIFY-CERTIFICATE? determines
  562. whether or not to validate HTTPS server certificates."
  563. (define uri
  564. (string->uri url))
  565. (if (or (not uri) (memq (uri-scheme uri) '(file #f)))
  566. (add-to-store store name recursive? "sha256"
  567. (if uri (uri-path uri) url))
  568. (call-with-temporary-output-file
  569. (lambda (temp port)
  570. (let ((result
  571. (parameterize ((current-output-port log))
  572. (build:url-fetch url temp
  573. #:mirrors %mirrors
  574. #:verify-certificate?
  575. verify-certificate?))))
  576. (close port)
  577. (and result
  578. (add-to-store store name recursive? "sha256" temp)))))))
  579. ;;; download.scm ends here