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>
8 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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)
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
53 %default-package-module-path
54 cache-is-authoritative?
57 fold-available-packages
59 find-newest-available-packages
61 find-package-locations
62 find-best-packages-by-name
64 specification->package
65 specification->package+output
66 specification->location
67 specifications->manifest
69 package-unique-version-prefix
71 generate-package-cache))
75 ;;; General utilities for the software distribution---i.e., the modules under
76 ;;; (gnu packages ...).
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.
87 (define %auxiliary-files-path
89 (map (cut string-append <> "/gnu/packages/aux-files")
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))
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")
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) ...))
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 ()
113 ((_ file head tail ...)
114 (dirname (dirname* file tail ...)))))
115 (try (syntax-rules ()
116 ((_ (file things ...) rest ...)
117 (match (search-path %load-path file)
121 (dirname* absolute things ...))))
124 (try ("gnu/packages/base.scm" gnu/ packages/)
125 ("gnu/packages.scm" gnu/)
128 (define %default-package-module-path
129 ;; Default search path for package modules.
130 `((,%distro-root-directory . "gnu/packages")))
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'
136 (equal? (%package-module-path)
137 (append %default-package-module-path
138 (package-path-entries))))
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 #\:)))
147 (string-tokenize (or (getenv "GUIX_PACKAGE_PATH") "")
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
156 (append environment %load-path channels-scm))
157 (set! %load-compiled-path
158 (append environment %load-compiled-path channels-go))
162 %default-package-module-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.
169 (map (lambda (directory)
170 (if (string=? directory %distro-root-directory)
171 (string-append directory "/gnu/packages/patches")
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
180 "Return a vhash keyed by package names, and with
181 associated values of the form
183 (newest-version newest-package ...)
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))
195 (#f (vhash-cons name `(,version ,p) r)))))
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:
202 (PROC NAME VERSION RESULT
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
211 (load-package-cache (current-profile)))
213 (if (and cache (cache-is-authoritative?))
214 (vhash-fold (lambda (name vector result)
216 (#(name version module symbol outputs
217 supported? deprecated?
219 (proc name version result
222 (location file line column))
223 #:supported? supported?
224 #:deprecated? deprecated?))))
227 (fold-packages (lambda (package result)
228 (proc (package-name package)
229 (package-version package)
231 #:outputs (package-outputs package)
232 #:location (package-location package)
234 (->bool (supported-package? package))
237 (package-superseded package))))
240 (define* (fold-packages proc init
242 (modules (all-modules (%package-module-path)
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))
256 (define %package-cache-file
257 ;; Location of the package cache.
258 "/lib/guix/package.cache")
260 (define load-package-cache
262 "Attempt to load the package cache. On success return a vhash keyed by
263 package names. Return #f on failure."
270 (load-compiled (string-append profile %package-cache-file)))
271 (fold (lambda (item vhash)
273 (#(name version module symbol outputs
274 supported? deprecated?
276 (vhash-cons name item vhash))))
280 (if (= ENOENT (system-error-errno args))
282 (apply throw args))))))))
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))
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))
298 (filter (lambda (package)
299 (version-prefix? version (package-version package)))
303 (define (cache-lookup cache name)
304 "Lookup package NAME in CACHE. Return a list sorted in increasing version
306 (define (package-version<? v1 v2)
307 (version>? (vector-ref v2 1) (vector-ref v1 1)))
309 (sort (vhash-fold* cons '() name cache)
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."
317 (load-package-cache (current-profile)))
319 (if (and (cache-is-authoritative?) cache)
320 (match (cache-lookup cache name)
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)
331 versions modules symbols)))
332 (find-packages-by-name/direct name version)))
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."
338 (load-package-cache (current-profile)))
340 (if (and cache (cache-is-authoritative?))
341 (match (cache-lookup cache name)
343 ((#(name versions modules symbols outputs
344 supported? deprecated?
345 files lines columns) ...)
346 (fold (lambda (version* file line column result)
349 (version-prefix? version version*)))
350 (alist-cons version* (location file line column)
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))))
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
364 (find-packages-by-name name version)
365 (match (find-packages-by-name name)
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))
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)
378 (define (generate-package-cache directory)
379 "Generate under DIRECTORY a cache of all the available packages.
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."
385 (string-append directory %package-cache-file))
389 (((module symbol variable) (result . seen))
390 (let ((package (variable-ref variable)))
391 (if (or (vhash-assq package seen)
392 (hidden-package? package))
394 (cons (cons `#(,(package-name package)
395 ,(package-version package)
396 ,(module-name module)
398 ,(package-outputs package)
399 ,(->bool (supported-package? package))
400 ,(->bool (package-superseded package))
401 ,@(let ((loc (package-location package)))
403 `(,(location-file loc)
405 ,(location-column loc))
408 (vhash-consq package #t seen)))))))
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))))))
418 (define (entry<? a b)
419 (string<? (entry-key a) (entry-key b)))
422 ;; First sort variables so that 'expand-cache' later dismisses
423 ;; already-seen package objects in a deterministic fashion.
425 (fold-module-public-variables* (lambda (module symbol variable lst)
426 (let ((value (false-if-exception
427 (variable-ref variable))))
429 (cons (list module symbol variable)
433 (all-modules (%package-module-path)
435 warn-about-load-error))
439 (first (fold expand-cache (cons '() vlist-null) variables)))
441 (mkdir-p (dirname cache-file))
442 (call-with-output-file cache-file
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.
449 #:opts '(#:to-file? #t)))))
453 (define %sigint-prompt
454 ;; The prompt to jump to upon SIGINT.
455 (make-prompt-tag "interruptible"))
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
465 (sigaction SIGINT SIG_DFL)
466 (abort-to-prompt %sigint-prompt signum)))
470 (cut sigaction SIGINT SIG_DFL)))
476 ;;; Package specification.
479 (define* (%find-package spec name version)
480 (match (find-best-packages-by-name name version)
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)
489 (info (G_ "package '~a' has been superseded by '~a'~%")
490 (package-name pkg) (package-name new))
496 (leave (G_ "~A: package not found for version ~a~%") name version)
497 (leave (G_ "~A: unknown package~%") name)))))
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)))
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)
513 (leave (G_ "~A: package not found for version ~a~%") name version)
514 (leave (G_ "~A: unknown package~%") name)))
516 (let* ((highest (match lst (((version . _) _ ...) version)))
517 (locations (take-while (match-lambda
518 ((version . location)
519 (string=? version highest)))
522 (((version . location) . rest)
524 (warning (G_ "ambiguous package specification `~a'~%") spec)
525 (warning (G_ "choosing ~a@~a from ~a~%")
527 (location->string location)))
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:
539 If SPEC does not specify a version number, return the preferred newest
540 version; if SPEC does not specify an output, return OUTPUT.
542 When OUTPUT is false and SPEC does not specify any output, return #f as the
544 (let-values (((name version sub-drv)
545 (package-specification->name+version+output spec output)))
546 (match (%find-package spec name version)
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)
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.
563 (map (compose list specification->package+output) specs)))
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))
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.
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
579 (if (every (cut version>? version <>)
580 (delete version versions))
582 (version-unique-prefix version versions)))))