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