Merge branch 'staging' into core-updates
[jackhill/guix/guix.git] / gnu / packages.scm
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 Mark H Weaver <mhw@netris.org>
4 ;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
5 ;;; Copyright © 2016, 2017 Alex Kost <alezost@gmail.com>
6 ;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
7 ;;;
8 ;;; This file is part of GNU Guix.
9 ;;;
10 ;;; GNU Guix is free software; you can redistribute it and/or modify it
11 ;;; under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or (at
13 ;;; your option) any later version.
14 ;;;
15 ;;; GNU Guix is distributed in the hope that it will be useful, but
16 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
19 ;;;
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22
23 (define-module (gnu packages)
24 #:use-module (guix packages)
25 #:use-module (guix ui)
26 #:use-module (guix utils)
27 #:use-module (guix discovery)
28 #:use-module (guix memoization)
29 #:use-module ((guix build utils)
30 #:select ((package-name->name+version
31 . hyphen-separated-name->name+version)
32 mkdir-p))
33 #:autoload (guix profiles) (packages->manifest)
34 #:use-module (guix describe)
35 #:use-module (ice-9 vlist)
36 #:use-module (ice-9 match)
37 #:autoload (ice-9 binary-ports) (put-bytevector)
38 #:autoload (system base compile) (compile)
39 #:use-module (srfi srfi-1)
40 #:use-module (srfi srfi-11)
41 #:use-module (srfi srfi-26)
42 #:use-module (srfi srfi-34)
43 #:use-module (srfi srfi-35)
44 #:use-module (srfi srfi-39)
45 #:export (search-patch
46 search-patches
47 search-auxiliary-file
48 search-bootstrap-binary
49 %patch-path
50 %auxiliary-files-path
51 %bootstrap-binaries-path
52 %package-module-path
53 %default-package-module-path
54
55 fold-packages
56 fold-available-packages
57
58 find-packages-by-name
59 find-package-locations
60 find-best-packages-by-name
61
62 specification->package
63 specification->package+output
64 specification->location
65 specifications->manifest
66
67 generate-package-cache))
68
69 ;;; Commentary:
70 ;;;
71 ;;; General utilities for the software distribution---i.e., the modules under
72 ;;; (gnu packages ...).
73 ;;;
74 ;;; Code:
75
76 ;; By default, we store patches, auxiliary files and bootstrap binaries
77 ;; alongside Guile modules. This is so that these extra files can be
78 ;; found without requiring a special setup, such as a specific
79 ;; installation directory and an extra environment variable. One
80 ;; advantage of this setup is that everything just works in an
81 ;; auto-compilation setting.
82
83 (define %bootstrap-binaries-path
84 (make-parameter
85 (map (cut string-append <> "/gnu/packages/bootstrap")
86 %load-path)))
87
88 (define %auxiliary-files-path
89 (make-parameter
90 (map (cut string-append <> "/gnu/packages/aux-files")
91 %load-path)))
92
93 (define (search-auxiliary-file file-name)
94 "Search the auxiliary FILE-NAME. Return #f if not found."
95 (search-path (%auxiliary-files-path) file-name))
96
97 (define (search-patch file-name)
98 "Search the patch FILE-NAME. Raise an error if not found."
99 (or (search-path (%patch-path) file-name)
100 (raise (condition
101 (&message (message (format #f (G_ "~a: patch not found")
102 file-name)))))))
103
104 (define-syntax-rule (search-patches file-name ...)
105 "Return the list of absolute file names corresponding to each
106 FILE-NAME found in %PATCH-PATH."
107 (list (search-patch file-name) ...))
108
109 (define (search-bootstrap-binary file-name system)
110 "Search the bootstrap binary FILE-NAME for SYSTEM. Raise an error if not
111 found."
112 ;; On x86_64 always use the i686 binaries.
113 (let ((system (match system
114 ("x86_64-linux" "i686-linux")
115 (_ system))))
116 (or (search-path (%bootstrap-binaries-path)
117 (string-append system "/" file-name))
118 (raise (condition
119 (&message
120 (message
121 (format #f (G_ "could not find bootstrap binary '~a' \
122 for system '~a'")
123 file-name system))))))))
124
125 (define %distro-root-directory
126 ;; Absolute file name of the module hierarchy. Since (gnu packages …) might
127 ;; live in a directory different from (guix), try to get the best match.
128 (letrec-syntax ((dirname* (syntax-rules ()
129 ((_ file)
130 (dirname file))
131 ((_ file head tail ...)
132 (dirname (dirname* file tail ...)))))
133 (try (syntax-rules ()
134 ((_ (file things ...) rest ...)
135 (match (search-path %load-path file)
136 (#f
137 (try rest ...))
138 (absolute
139 (dirname* absolute things ...))))
140 ((_)
141 #f))))
142 (try ("gnu/packages/base.scm" gnu/ packages/)
143 ("gnu/packages.scm" gnu/)
144 ("guix.scm"))))
145
146 (define %default-package-module-path
147 ;; Default search path for package modules.
148 `((,%distro-root-directory . "gnu/packages")))
149
150 (define (cache-is-authoritative?)
151 "Return true if the pre-computed package cache is authoritative. It is not
152 authoritative when entries have been added via GUIX_PACKAGE_PATH or '-L'
153 flags."
154 (equal? (%package-module-path)
155 (append %default-package-module-path
156 (package-path-entries))))
157
158 (define %package-module-path
159 ;; Search path for package modules. Each item must be either a directory
160 ;; name or a pair whose car is a directory and whose cdr is a sub-directory
161 ;; to narrow the search.
162 (let* ((not-colon (char-set-complement (char-set #\:)))
163 (environment (string-tokenize (or (getenv "GUIX_PACKAGE_PATH") "")
164 not-colon))
165 (channels (package-path-entries)))
166 ;; Automatically add channels and items from $GUIX_PACKAGE_PATH to Guile's
167 ;; search path. For historical reasons, $GUIX_PACKAGE_PATH goes to the
168 ;; front; channels go to the back so that they don't override Guix' own
169 ;; modules.
170 (set! %load-path
171 (append environment %load-path channels))
172 (set! %load-compiled-path
173 (append environment %load-compiled-path channels))
174
175 (make-parameter
176 (append environment
177 %default-package-module-path
178 channels))))
179
180 (define %patch-path
181 ;; Define it after '%package-module-path' so that '%load-path' contains user
182 ;; directories, allowing patches in $GUIX_PACKAGE_PATH to be found.
183 (make-parameter
184 (map (lambda (directory)
185 (if (string=? directory %distro-root-directory)
186 (string-append directory "/gnu/packages/patches")
187 directory))
188 %load-path)))
189
190 (define (fold-available-packages proc init)
191 "Fold PROC over the list of available packages. For each available package,
192 PROC is called along these lines:
193
194 (PROC NAME VERSION RESULT
195 #:outputs OUTPUTS
196 #:location LOCATION
197 …)
198
199 PROC can use #:allow-other-keys to ignore the bits it's not interested in.
200 When a package cache is available, this procedure does not actually load any
201 package module."
202 (define cache
203 (load-package-cache (current-profile)))
204
205 (if (and cache (cache-is-authoritative?))
206 (vhash-fold (lambda (name vector result)
207 (match vector
208 (#(name version module symbol outputs
209 supported? deprecated?
210 file line column)
211 (proc name version result
212 #:outputs outputs
213 #:location (and file
214 (location file line column))
215 #:supported? supported?
216 #:deprecated? deprecated?))))
217 init
218 cache)
219 (fold-packages (lambda (package result)
220 (proc (package-name package)
221 (package-version package)
222 result
223 #:outputs (package-outputs package)
224 #:location (package-location package)
225 #:supported?
226 (->bool
227 (member (%current-system)
228 (package-supported-systems package)))
229 #:deprecated?
230 (->bool
231 (package-superseded package))))
232 init)))
233
234 (define* (fold-packages proc init
235 #:optional
236 (modules (all-modules (%package-module-path)
237 #:warn
238 warn-about-load-error))
239 #:key (select? (negate hidden-package?)))
240 "Call (PROC PACKAGE RESULT) for each available package defined in one of
241 MODULES that matches SELECT?, using INIT as the initial value of RESULT. It
242 is guaranteed to never traverse the same package twice."
243 (fold-module-public-variables (lambda (object result)
244 (if (and (package? object) (select? object))
245 (proc object result)
246 result))
247 init
248 modules))
249
250 (define %package-cache-file
251 ;; Location of the package cache.
252 "/lib/guix/package.cache")
253
254 (define load-package-cache
255 (mlambda (profile)
256 "Attempt to load the package cache. On success return a vhash keyed by
257 package names. Return #f on failure."
258 (match profile
259 (#f #f)
260 (profile
261 (catch 'system-error
262 (lambda ()
263 (define lst
264 (load-compiled (string-append profile %package-cache-file)))
265 (fold (lambda (item vhash)
266 (match item
267 (#(name version module symbol outputs
268 supported? deprecated?
269 file line column)
270 (vhash-cons name item vhash))))
271 vlist-null
272 lst))
273 (lambda args
274 (if (= ENOENT (system-error-errno args))
275 #f
276 (apply throw args))))))))
277
278 (define find-packages-by-name/direct ;bypass the cache
279 (let ((packages (delay
280 (fold-packages (lambda (p r)
281 (vhash-cons (package-name p) p r))
282 vlist-null)))
283 (version>? (lambda (p1 p2)
284 (version>? (package-version p1) (package-version p2)))))
285 (lambda* (name #:optional version)
286 "Return the list of packages with the given NAME. If VERSION is not #f,
287 then only return packages whose version is prefixed by VERSION, sorted in
288 decreasing version order."
289 (let ((matching (sort (vhash-fold* cons '() name (force packages))
290 version>?)))
291 (if version
292 (filter (lambda (package)
293 (version-prefix? version (package-version package)))
294 matching)
295 matching)))))
296
297 (define (cache-lookup cache name)
298 "Lookup package NAME in CACHE. Return a list sorted in increasing version
299 order."
300 (define (package-version<? v1 v2)
301 (version>? (vector-ref v2 1) (vector-ref v1 1)))
302
303 (sort (vhash-fold* cons '() name cache)
304 package-version<?))
305
306 (define* (find-packages-by-name name #:optional version)
307 "Return the list of packages with the given NAME. If VERSION is not #f,
308 then only return packages whose version is prefixed by VERSION, sorted in
309 decreasing version order."
310 (define cache
311 (load-package-cache (current-profile)))
312
313 (if (and (cache-is-authoritative?) cache)
314 (match (cache-lookup cache name)
315 (#f #f)
316 ((#(_ versions modules symbols _ _ _ _ _ _) ...)
317 (fold (lambda (version* module symbol result)
318 (if (or (not version)
319 (version-prefix? version version*))
320 (cons (module-ref (resolve-interface module)
321 symbol)
322 result)
323 result))
324 '()
325 versions modules symbols)))
326 (find-packages-by-name/direct name version)))
327
328 (define* (find-package-locations name #:optional version)
329 "Return a list of version/location pairs corresponding to each package
330 matching NAME and VERSION."
331 (define cache
332 (load-package-cache (current-profile)))
333
334 (if (and cache (cache-is-authoritative?))
335 (match (cache-lookup cache name)
336 (#f '())
337 ((#(name versions modules symbols outputs
338 supported? deprecated?
339 files lines columns) ...)
340 (fold (lambda (version* file line column result)
341 (if (and file
342 (or (not version)
343 (version-prefix? version version*)))
344 (alist-cons version* (location file line column)
345 result)
346 result))
347 '()
348 versions files lines columns)))
349 (map (lambda (package)
350 (cons (package-version package) (package-location package)))
351 (find-packages-by-name/direct name version))))
352
353 (define (find-best-packages-by-name name version)
354 "If version is #f, return the list of packages named NAME with the highest
355 version numbers; otherwise, return the list of packages named NAME and at
356 VERSION."
357 (if version
358 (find-packages-by-name name version)
359 (match (find-packages-by-name name)
360 (()
361 '())
362 ((matches ...)
363 ;; Return the subset of MATCHES with the higher version number.
364 (let ((highest (package-version (first matches))))
365 (take-while (lambda (p)
366 (string=? (package-version p) highest))
367 matches))))))
368
369 (define (generate-package-cache directory)
370 "Generate under DIRECTORY a cache of all the available packages.
371
372 The primary purpose of the cache is to speed up package lookup by name such
373 that we don't have to traverse and load all the package modules, thereby also
374 reducing the memory footprint."
375 (define cache-file
376 (string-append directory %package-cache-file))
377
378 (define (expand-cache module symbol variable result+seen)
379 (match (false-if-exception (variable-ref variable))
380 ((? package? package)
381 (match result+seen
382 ((result . seen)
383 (if (or (vhash-assq package seen)
384 (hidden-package? package))
385 (cons result seen)
386 (cons (cons `#(,(package-name package)
387 ,(package-version package)
388 ,(module-name module)
389 ,symbol
390 ,(package-outputs package)
391 ,(->bool
392 (member (%current-system)
393 (package-supported-systems package)))
394 ,(->bool (package-superseded package))
395 ,@(let ((loc (package-location package)))
396 (if loc
397 `(,(location-file loc)
398 ,(location-line loc)
399 ,(location-column loc))
400 '(#f #f #f))))
401 result)
402 (vhash-consq package #t seen))))))
403 (_
404 result+seen)))
405
406 (define exp
407 (first
408 (fold-module-public-variables* expand-cache
409 (cons '() vlist-null)
410 (all-modules (%package-module-path)
411 #:warn
412 warn-about-load-error))))
413
414 (mkdir-p (dirname cache-file))
415 (call-with-output-file cache-file
416 (lambda (port)
417 ;; Store the cache as a '.go' file. This makes loading fast and reduces
418 ;; heap usage since some of the static data is directly mmapped.
419 (put-bytevector port
420 (compile `'(,@exp)
421 #:to 'bytecode
422 #:opts '(#:to-file? #t)))))
423 cache-file)
424
425 \f
426 (define %sigint-prompt
427 ;; The prompt to jump to upon SIGINT.
428 (make-prompt-tag "interruptible"))
429
430 (define (call-with-sigint-handler thunk handler)
431 "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
432 number in the context of the continuation of the call to this function, and
433 return its return value."
434 (call-with-prompt %sigint-prompt
435 (lambda ()
436 (sigaction SIGINT
437 (lambda (signum)
438 (sigaction SIGINT SIG_DFL)
439 (abort-to-prompt %sigint-prompt signum)))
440 (dynamic-wind
441 (const #t)
442 thunk
443 (cut sigaction SIGINT SIG_DFL)))
444 (lambda (k signum)
445 (handler signum))))
446
447 \f
448 ;;;
449 ;;; Package specification.
450 ;;;
451
452 (define* (%find-package spec name version)
453 (match (find-best-packages-by-name name version)
454 ((pkg . pkg*)
455 (unless (null? pkg*)
456 (warning (G_ "ambiguous package specification `~a'~%") spec)
457 (warning (G_ "choosing ~a@~a from ~a~%")
458 (package-name pkg) (package-version pkg)
459 (location->string (package-location pkg))))
460 (match (package-superseded pkg)
461 ((? package? new)
462 (info (G_ "package '~a' has been superseded by '~a'~%")
463 (package-name pkg) (package-name new))
464 new)
465 (#f
466 pkg)))
467 (x
468 (if version
469 (leave (G_ "~A: package not found for version ~a~%") name version)
470 (leave (G_ "~A: unknown package~%") name)))))
471
472 (define (specification->package spec)
473 "Return a package matching SPEC. SPEC may be a package name, or a package
474 name followed by an at-sign and a version number. If the version number is not
475 present, return the preferred newest version."
476 (let-values (((name version) (package-name->name+version spec)))
477 (%find-package spec name version)))
478
479 (define (specification->location spec)
480 "Return the location of the highest-numbered package matching SPEC, a
481 specification such as \"guile@2\" or \"emacs\"."
482 (let-values (((name version) (package-name->name+version spec)))
483 (match (find-package-locations name version)
484 (()
485 (if version
486 (leave (G_ "~A: package not found for version ~a~%") name version)
487 (leave (G_ "~A: unknown package~%") name)))
488 (lst
489 (let* ((highest (match lst (((version . _) _ ...) version)))
490 (locations (take-while (match-lambda
491 ((version . location)
492 (string=? version highest)))
493 lst)))
494 (match locations
495 (((version . location) . rest)
496 (unless (null? rest)
497 (warning (G_ "ambiguous package specification `~a'~%") spec)
498 (warning (G_ "choosing ~a@~a from ~a~%")
499 name version
500 (location->string location)))
501 location)))))))
502
503 (define* (specification->package+output spec #:optional (output "out"))
504 "Return the package and output specified by SPEC, or #f and #f; SPEC may
505 optionally contain a version number and an output name, as in these examples:
506
507 guile
508 guile@2.0.9
509 guile:debug
510 guile@2.0.9:debug
511
512 If SPEC does not specify a version number, return the preferred newest
513 version; if SPEC does not specify an output, return OUTPUT."
514 (let-values (((name version sub-drv)
515 (package-specification->name+version+output spec output)))
516 (match (%find-package spec name version)
517 (#f
518 (values #f #f))
519 (package
520 (if (member sub-drv (package-outputs package))
521 (values package sub-drv)
522 (leave (G_ "package `~a' lacks output `~a'~%")
523 (package-full-name package)
524 sub-drv))))))
525
526 (define (specifications->manifest specs)
527 "Given SPECS, a list of specifications such as \"emacs@25.2\" or
528 \"guile:debug\", return a profile manifest."
529 ;; This procedure exists mostly so users of 'guix package -m' don't have to
530 ;; fiddle with multiple-value returns.
531 (packages->manifest
532 (map (compose list specification->package+output) specs)))