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