environment: Improve '--help' output.
[jackhill/guix/guix.git] / guix / download.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
f06afd4d 2;;; Copyright © 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org>
6d763bdd 3;;; Copyright © 2013, 2014 Andreas Enge <andreas@enge.fr>
62cab99c 4;;;
233e7676 5;;; This file is part of GNU Guix.
62cab99c 6;;;
233e7676 7;;; GNU Guix is free software; you can redistribute it and/or modify it
62cab99c
LC
8;;; under the terms of the GNU General Public License as published by
9;;; the Free Software Foundation; either version 3 of the License, or (at
10;;; your option) any later version.
11;;;
233e7676 12;;; GNU Guix is distributed in the hope that it will be useful, but
62cab99c
LC
13;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;;; GNU General Public License for more details.
16;;;
17;;; You should have received a copy of the GNU General Public License
233e7676 18;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
62cab99c
LC
19
20(define-module (guix download)
21 #:use-module (ice-9 match)
22 #:use-module (guix derivations)
23 #:use-module (guix packages)
861693f3 24 #:use-module ((guix store) #:select (derivation-path? add-to-store))
b5b73a82 25 #:use-module ((guix build download) #:prefix build:)
6f8f8ccb
LC
26 #:use-module (guix monads)
27 #:use-module (guix gexp)
62cab99c 28 #:use-module (guix utils)
d8907ac4 29 #:use-module (web uri)
483f1158 30 #:use-module (srfi srfi-1)
94d222ad 31 #:use-module (srfi srfi-26)
ec4d308a 32 #:export (%mirrors
861693f3
LC
33 url-fetch
34 download-to-store))
62cab99c
LC
35
36;;; Commentary:
37;;;
38;;; Produce fixed-output derivations with data fetched over HTTP or FTP.
39;;;
40;;; Code:
41
94d222ad
LC
42(define %mirrors
43 ;; Mirror lists used when `mirror://' URLs are passed.
44 (let* ((gnu-mirrors
45 '(;; This one redirects to a (supposedly) nearby and (supposedly)
46 ;; up-to-date mirror.
47 "http://ftpmirror.gnu.org/"
48
49 "ftp://ftp.cs.tu-berlin.de/pub/gnu/"
94d222ad
LC
50 "ftp://ftp.funet.fi/pub/mirrors/ftp.gnu.org/gnu/"
51
52 ;; This one is the master repository, and thus it's always
53 ;; up-to-date.
54 "http://ftp.gnu.org/pub/gnu/")))
55 `((gnu ,@gnu-mirrors)
56 (gcc
57 "ftp://ftp.nluug.nl/mirror/languages/gcc/"
58 "ftp://ftp.fu-berlin.de/unix/languages/gcc/"
59 "ftp://ftp.irisa.fr/pub/mirrors/gcc.gnu.org/gcc/"
60 "ftp://gcc.gnu.org/pub/gcc/"
61 ,@(map (cut string-append <> "/gcc") gnu-mirrors))
62 (gnupg
63 "ftp://gd.tuwien.ac.at/privacy/gnupg/"
64 "ftp://gnupg.x-zone.org/pub/gnupg/"
65 "ftp://ftp.gnupg.cz/pub/gcrypt/"
66 "ftp://sunsite.dk/pub/security/gcrypt/"
67 "http://gnupg.wildyou.net/"
68 "http://ftp.gnupg.zone-h.org/"
69 "ftp://ftp.jyu.fi/pub/crypt/gcrypt/"
70 "ftp://trumpetti.atm.tut.fi/gcrypt/"
71 "ftp://mirror.cict.fr/gnupg/"
72 "ftp://ftp.strasbourg.linuxfr.org/pub/gnupg/")
71eb5c10
LC
73 (gnome
74 "http://ftp.belnet.be/ftp.gnome.org/"
75 "http://ftp.linux.org.uk/mirrors/ftp.gnome.org/"
76 "http://ftp.gnome.org/pub/GNOME/"
77 "http://mirror.yandex.ru/mirrors/ftp.gnome.org/")
94d222ad 78 (savannah
a4eabecd 79 "http://download.savannah.gnu.org/releases/"
94d222ad
LC
80 "ftp://ftp.twaren.net/Unix/NonGNU/"
81 "ftp://mirror.csclub.uwaterloo.ca/nongnu/"
82 "ftp://mirror.publicns.net/pub/nongnu/"
83 "ftp://savannah.c3sl.ufpr.br/"
84 "http://ftp.cc.uoc.gr/mirrors/nongnu.org/"
85 "http://ftp.twaren.net/Unix/NonGNU/"
86 "http://mirror.csclub.uwaterloo.ca/nongnu/"
87 "http://nongnu.askapache.com/"
88 "http://savannah.c3sl.ufpr.br/"
3bba7b0a
LC
89 "http://www.centervenus.com/mirrors/nongnu/"
90 "http://download.savannah.gnu.org/releases-noredirect/")
94d222ad
LC
91 (sourceforge
92 "http://prdownloads.sourceforge.net/"
93 "http://heanet.dl.sourceforge.net/sourceforge/"
94 "http://surfnet.dl.sourceforge.net/sourceforge/"
95 "http://dfn.dl.sourceforge.net/sourceforge/"
96 "http://mesh.dl.sourceforge.net/sourceforge/"
97 "http://ovh.dl.sourceforge.net/sourceforge/"
c43e0061 98 "http://osdn.dl.sourceforge.net/sourceforge/")
b40b259f
LC
99 (kernel.org
100 "http://www.all.kernel.org/pub/"
101 "http://ramses.wh2.tu-dresden.de/pub/mirrors/kernel.org/"
102 "http://linux-kernel.uio.no/pub/"
103 "http://kernel.osuosl.org/pub/"
5d9cd707
LC
104 "ftp://ftp.funet.fi/pub/mirrors/ftp.kernel.org/pub/"
105 "http://ftp.be.debian.org/pub/"
106 "http://mirror.linux.org.au/")
47f9db41
LC
107 (apache ; from http://www.apache.org/mirrors/dist.html
108 "http://www.eu.apache.org/dist/"
109 "http://www.us.apache.org/dist/"
110 "ftp://gd.tuwien.ac.at/pub/infosys/servers/http/apache/dist/"
111 "http://apache.belnet.be/"
112 "http://mirrors.ircam.fr/pub/apache/"
f06afd4d
LC
113 "http://apache-mirror.rbc.ru/pub/apache/"
114
115 ;; As a last resort, try the archive.
116 "http://archive.apache.org/dist/")
149acc29 117 (xorg ; from http://www.x.org/wiki/Releases/Download
0820a58b 118 "http://www.x.org/releases/" ; main mirrors
149acc29
AE
119 "ftp://mirror.csclub.uwaterloo.ca/x.org/" ; North America
120 "ftp://xorg.mirrors.pair.com/"
121 "http://mirror.csclub.uwaterloo.ca/x.org/"
122 "http://xorg.mirrors.pair.com/"
123 "http://mirror.us.leaseweb.net/xorg/"
124 "ftp://artfiles.org/x.org/" ; Europe
125 "ftp://ftp.chg.ru/pub/X11/x.org/"
126 "ftp://ftp.fu-berlin.de/unix/X11/FTP.X.ORG/"
127 "ftp://ftp.gwdg.de/pub/x11/x.org/"
128 "ftp://ftp.mirrorservice.org/sites/ftp.x.org/"
129 "ftp://ftp.ntua.gr/pub/X11/"
130 "ftp://ftp.piotrkosoft.net/pub/mirrors/ftp.x.org/"
131 "ftp://ftp.portal-to-web.de/pub/mirrors/x.org/"
132 "ftp://ftp.solnet.ch/mirror/x.org/"
149acc29
AE
133 "ftp://gd.tuwien.ac.at/X11/"
134 "ftp://mi.mirror.garr.it/mirrors/x.org/"
135 "ftp://mirror.cict.fr/x.org/"
136 "ftp://mirror.switch.ch/mirror/X11/"
137 "ftp://mirrors.ircam.fr/pub/x.org/"
138 "ftp://x.mirrors.skynet.be/pub/ftp.x.org/"
139 "ftp://ftp.cs.cuhk.edu.hk/pub/X11" ; East Asia
140 "ftp://ftp.u-aizu.ac.jp/pub/x11/x.org/"
141 "ftp://ftp.yz.yamagata-u.ac.jp/pub/X11/x.org/"
142 "ftp://ftp.kaist.ac.kr/x.org/"
143 "ftp://mirrors.go-part.com/xorg/"
144 "http://x.cs.pu.edu.tw/"
6af31019
LC
145 "ftp://ftp.is.co.za/pub/x.org") ; South Africa
146 (cpan ; from http://www.cpan.org/SITES.html
147 "http://cpan.enstimac.fr/"
148 "ftp://ftp.ciril.fr/pub/cpan/"
149 "ftp://artfiles.org/cpan.org/"
150 "http://www.cpan.org/"
151 "ftp://cpan.rinet.ru/pub/mirror/CPAN/"
152 "http://cpan.cu.be/"
153 "ftp://cpan.inode.at/"
154 "ftp://cpan.iht.co.il/"
155 "ftp://ftp.osuosl.org/pub/CPAN/"
156 "ftp://ftp.nara.wide.ad.jp/pub/CPAN/"
157 "http://mirrors.163.com/cpan/"
7fa37abc 158 "ftp://cpan.mirror.ac.za/")
6d763bdd
AE
159 (imagemagick
160 ;; from http://www.imagemagick.org/script/download.php
161 ;; (without mirrors that are unavailable or not up to date)
162 ;; mirrors keeping old versions at the top level
6d763bdd
AE
163 "ftp://sunsite.icm.edu.pl/packages/ImageMagick/"
164 ;; mirrors moving old versions to "legacy"
165 "http://mirrors-au.go-parts.com/mirrors/ImageMagick/"
166 "ftp://mirror.aarnet.edu.au/pub/imagemagick/"
7fa37abc 167 "http://mirror.checkdomain.de/imagemagick/"
6d763bdd
AE
168 "ftp://ftp.kddlabs.co.jp/graphics/ImageMagick/"
169 "ftp://ftp.u-aizu.ac.jp/pub/graphics/image/ImageMagick/imagemagick.org/"
170 "ftp://ftp.nluug.nl/pub/ImageMagick/"
171 "http://ftp.surfnet.nl/pub/ImageMagick/"
172 "http://mirror.searchdaimon.com/ImageMagick"
173 "ftp://ftp.tpnet.pl/pub/graphics/ImageMagick/"
174 "http://mirrors-ru.go-parts.com/mirrors/ImageMagick/"
7fa37abc 175 "http://mirror.is.co.za/pub/imagemagick/"
6d763bdd
AE
176 "http://mirrors-uk.go-parts.com/mirrors/ImageMagick/"
177 "http://mirrors-usa.go-parts.com/mirrors/ImageMagick/"
178 "ftp://ftp.fifi.org/pub/ImageMagick/"
179 "http://www.imagemagick.org/download/"
180 ;; one legacy location as a last resort
181 "http://www.imagemagick.org/download/legacy/")
e0029b74
LC
182 (debian
183 "http://ftp.de.debian.org/debian/"
184 "http://ftp.fr.debian.org/debian/"
185 "http://ftp.debian.org/debian/"))))
94d222ad 186
6f8f8ccb 187(define (gnutls-package)
6119ebf1 188 "Return the default GnuTLS package."
6f8f8ccb
LC
189 (let ((module (resolve-interface '(gnu packages gnutls))))
190 (module-ref module 'gnutls)))
94d222ad 191
62cab99c
LC
192(define* (url-fetch store url hash-algo hash
193 #:optional name
94d222ad
LC
194 #:key (system (%current-system)) guile
195 (mirrors %mirrors))
62cab99c
LC
196 "Return the path of a fixed-output derivation in STORE that fetches
197URL (a string, or a list of strings denoting alternate URLs), which is
198expected to have hash HASH of type HASH-ALGO (a symbol). By default,
199the file name is the base name of URL; optionally, NAME can specify a
94d222ad
LC
200different file name.
201
202When one of the URL starts with mirror://, then its host part is
203interpreted as the name of a mirror scheme, taken from MIRRORS; MIRRORS
204must be a list of symbol/URL-list pairs."
62cab99c 205 (define guile-for-build
6f8f8ccb
LC
206 (package-derivation store
207 (or guile
bdb36958
LC
208 (let ((distro (resolve-interface
209 '(gnu packages commencement))))
6f8f8ccb
LC
210 (module-ref distro 'guile-final)))
211 system))
62cab99c
LC
212
213 (define file-name
214 (match url
215 ((head _ ...)
216 (basename head))
217 (_
218 (basename url))))
219
483f1158
LC
220 (define need-gnutls?
221 ;; True if any of the URLs need TLS support.
222 (let ((https? (cut string-prefix? "https://" <>)))
223 (match url
224 ((? string?)
225 (https? url))
226 ((url ...)
227 (any https? url)))))
228
6f8f8ccb
LC
229 (define builder
230 #~(begin
231 #$(if need-gnutls?
232
233 ;; Add GnuTLS to the inputs and to the load path.
234 #~(eval-when (load expand eval)
235 (set! %load-path
236 (cons (string-append #$(gnutls-package)
237 "/share/guile/site")
238 %load-path)))
239 #~#t)
240
241 (use-modules (guix build download))
242 (url-fetch '#$url #$output
243 #:mirrors '#$mirrors)))
244
882383a9
LC
245 (let ((uri (and (string? url) (string->uri url))))
246 (if (or (and (string? url) (not uri))
247 (and uri (memq (uri-scheme uri) '(#f file))))
248 (add-to-store store (or name file-name)
249 #f "sha256" (if uri (uri-path uri) url))
250 (run-with-store store
251 (gexp->derivation (or name file-name) builder
252 #:system system
253 #:hash-algo hash-algo
254 #:hash hash
255 #:modules '((guix build download)
256 (guix build utils)
257 (guix ftp-client))
258 #:guile-for-build guile-for-build
56c72822 259
882383a9 260 ;; In general, offloading downloads is not a good idea.
347e17b4
LC
261 ;;#:local-build? #t
262 ;; FIXME: The above would also disable use of
263 ;; substitutes, so comment it out; see
264 ;; <https://bugs.gnu.org/18747>.
265 )
882383a9
LC
266 #:guile-for-build guile-for-build
267 #:system system))))
62cab99c 268
861693f3
LC
269(define* (download-to-store store url #:optional (name (basename url))
270 #:key (log (current-error-port)))
271 "Download from URL to STORE, either under NAME or URL's basename if
272omitted. Write progress reports to LOG."
d8907ac4
LC
273 (define uri
274 (string->uri url))
275
d91a8791
LC
276 (if (or (not uri) (memq (uri-scheme uri) '(file #f)))
277 (add-to-store store name #f "sha256"
278 (if uri (uri-path uri) url))
d8907ac4
LC
279 (call-with-temporary-output-file
280 (lambda (temp port)
281 (let ((result
282 (parameterize ((current-output-port log))
283 (build:url-fetch url temp #:mirrors %mirrors))))
284 (close port)
285 (and result
286 (add-to-store store name #f "sha256" temp)))))))
861693f3 287
62cab99c 288;;; download.scm ends here