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>
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 discovery)
28 #:use-module (guix memoization)
29 #:use-module ((guix build utils)
30 #:select ((package-name->name+version
31 . hyphen-separated-name->name+version)
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
48 search-bootstrap-binary
51 %bootstrap-binaries-path
53 %default-package-module-path
56 fold-available-packages
59 find-package-locations
60 find-best-packages-by-name
62 specification->package
63 specification->package+output
64 specification->location
65 specifications->manifest
67 generate-package-cache))
71 ;;; General utilities for the software distribution---i.e., the modules under
72 ;;; (gnu packages ...).
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.
83 (define %bootstrap-binaries-path
85 (map (cut string-append <> "/gnu/packages/bootstrap")
88 (define %auxiliary-files-path
90 (map (cut string-append <> "/gnu/packages/aux-files")
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))
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)
101 (&message (message (format #f (G_ "~a: patch not found")
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) ...))
109 (define (search-bootstrap-binary file-name system)
110 "Search the bootstrap binary FILE-NAME for SYSTEM. Raise an error if not
112 (or (search-path (%bootstrap-binaries-path)
113 (string-append system "/" file-name))
117 (format #f (G_ "could not find bootstrap binary '~a' \
119 file-name system)))))))
121 (define %distro-root-directory
122 ;; Absolute file name of the module hierarchy. Since (gnu packages …) might
123 ;; live in a directory different from (guix), try to get the best match.
124 (letrec-syntax ((dirname* (syntax-rules ()
127 ((_ file head tail ...)
128 (dirname (dirname* file tail ...)))))
129 (try (syntax-rules ()
130 ((_ (file things ...) rest ...)
131 (match (search-path %load-path file)
135 (dirname* absolute things ...))))
138 (try ("gnu/packages/base.scm" gnu/ packages/)
139 ("gnu/packages.scm" gnu/)
142 (define %default-package-module-path
143 ;; Default search path for package modules.
144 `((,%distro-root-directory . "gnu/packages")))
146 (define (cache-is-authoritative?)
147 "Return true if the pre-computed package cache is authoritative. It is not
148 authoritative when entries have been added via GUIX_PACKAGE_PATH or '-L'
150 (equal? (%package-module-path)
151 (append %default-package-module-path
152 (package-path-entries))))
154 (define %package-module-path
155 ;; Search path for package modules. Each item must be either a directory
156 ;; name or a pair whose car is a directory and whose cdr is a sub-directory
157 ;; to narrow the search.
158 (let* ((not-colon (char-set-complement (char-set #\:)))
159 (environment (string-tokenize (or (getenv "GUIX_PACKAGE_PATH") "")
161 (channels (package-path-entries)))
162 ;; Automatically add channels and items from $GUIX_PACKAGE_PATH to Guile's
163 ;; search path. For historical reasons, $GUIX_PACKAGE_PATH goes to the
164 ;; front; channels go to the back so that they don't override Guix' own
167 (append environment %load-path channels))
168 (set! %load-compiled-path
169 (append environment %load-compiled-path channels))
173 %default-package-module-path
177 ;; Define it after '%package-module-path' so that '%load-path' contains user
178 ;; directories, allowing patches in $GUIX_PACKAGE_PATH to be found.
180 (map (lambda (directory)
181 (if (string=? directory %distro-root-directory)
182 (string-append directory "/gnu/packages/patches")
186 (define (fold-available-packages proc init)
187 "Fold PROC over the list of available packages. For each available package,
188 PROC is called along these lines:
190 (PROC NAME VERSION RESULT
195 PROC can use #:allow-other-keys to ignore the bits it's not interested in.
196 When a package cache is available, this procedure does not actually load any
199 (load-package-cache (current-profile)))
201 (if (and cache (cache-is-authoritative?))
202 (vhash-fold (lambda (name vector result)
204 (#(name version module symbol outputs
205 supported? deprecated?
207 (proc name version result
210 (location file line column))
211 #:supported? supported?
212 #:deprecated? deprecated?))))
215 (fold-packages (lambda (package result)
216 (proc (package-name package)
217 (package-version package)
219 #:outputs (package-outputs package)
220 #:location (package-location package)
223 (member (%current-system)
224 (package-supported-systems package)))
227 (package-superseded package))))
230 (define* (fold-packages proc init
232 (modules (all-modules (%package-module-path)
234 warn-about-load-error))
235 #:key (select? (negate hidden-package?)))
236 "Call (PROC PACKAGE RESULT) for each available package defined in one of
237 MODULES that matches SELECT?, using INIT as the initial value of RESULT. It
238 is guaranteed to never traverse the same package twice."
239 (fold-module-public-variables (lambda (object result)
240 (if (and (package? object) (select? object))
246 (define %package-cache-file
247 ;; Location of the package cache.
248 "/lib/guix/package.cache")
250 (define load-package-cache
252 "Attempt to load the package cache. On success return a vhash keyed by
253 package names. Return #f on failure."
260 (load-compiled (string-append profile %package-cache-file)))
261 (fold (lambda (item vhash)
263 (#(name version module symbol outputs
264 supported? deprecated?
266 (vhash-cons name item vhash))))
270 (if (= ENOENT (system-error-errno args))
272 (apply throw args))))))))
274 (define find-packages-by-name/direct ;bypass the cache
275 (let ((packages (delay
276 (fold-packages (lambda (p r)
277 (vhash-cons (package-name p) p r))
279 (version>? (lambda (p1 p2)
280 (version>? (package-version p1) (package-version p2)))))
281 (lambda* (name #:optional version)
282 "Return the list of packages with the given NAME. If VERSION is not #f,
283 then only return packages whose version is prefixed by VERSION, sorted in
284 decreasing version order."
285 (let ((matching (sort (vhash-fold* cons '() name (force packages))
288 (filter (lambda (package)
289 (version-prefix? version (package-version package)))
293 (define (cache-lookup cache name)
294 "Lookup package NAME in CACHE. Return a list sorted in increasing version
296 (define (package-version<? v1 v2)
297 (version>? (vector-ref v2 1) (vector-ref v1 1)))
299 (sort (vhash-fold* cons '() name cache)
302 (define* (find-packages-by-name name #:optional version)
303 "Return the list of packages with the given NAME. If VERSION is not #f,
304 then only return packages whose version is prefixed by VERSION, sorted in
305 decreasing version order."
307 (load-package-cache (current-profile)))
309 (if (and (cache-is-authoritative?) cache)
310 (match (cache-lookup cache name)
312 ((#(_ versions modules symbols _ _ _ _ _ _) ...)
313 (fold (lambda (version* module symbol result)
314 (if (or (not version)
315 (version-prefix? version version*))
316 (cons (module-ref (resolve-interface module)
321 versions modules symbols)))
322 (find-packages-by-name/direct name version)))
324 (define* (find-package-locations name #:optional version)
325 "Return a list of version/location pairs corresponding to each package
326 matching NAME and VERSION."
328 (load-package-cache (current-profile)))
330 (if (and cache (cache-is-authoritative?))
331 (match (cache-lookup cache name)
333 ((#(name versions modules symbols outputs
334 supported? deprecated?
335 files lines columns) ...)
336 (fold (lambda (version* file line column result)
339 (version-prefix? version version*)))
340 (alist-cons version* (location file line column)
344 versions files lines columns)))
345 (map (lambda (package)
346 (cons (package-version package) (package-location package)))
347 (find-packages-by-name/direct name version))))
349 (define (find-best-packages-by-name name version)
350 "If version is #f, return the list of packages named NAME with the highest
351 version numbers; otherwise, return the list of packages named NAME and at
354 (find-packages-by-name name version)
355 (match (find-packages-by-name name)
359 ;; Return the subset of MATCHES with the higher version number.
360 (let ((highest (package-version (first matches))))
361 (take-while (lambda (p)
362 (string=? (package-version p) highest))
365 (define (generate-package-cache directory)
366 "Generate under DIRECTORY a cache of all the available packages.
368 The primary purpose of the cache is to speed up package lookup by name such
369 that we don't have to traverse and load all the package modules, thereby also
370 reducing the memory footprint."
372 (string-append directory %package-cache-file))
374 (define (expand-cache module symbol variable result)
375 (match (false-if-exception (variable-ref variable))
376 ((? package? package)
377 (if (hidden-package? package)
379 (cons `#(,(package-name package)
380 ,(package-version package)
381 ,(module-name module)
383 ,(package-outputs package)
384 ,(->bool (member (%current-system)
385 (package-supported-systems package)))
386 ,(->bool (package-superseded package))
387 ,@(let ((loc (package-location package)))
389 `(,(location-file loc)
391 ,(location-column loc))
398 (fold-module-public-variables* expand-cache '()
399 (all-modules (%package-module-path)
401 warn-about-load-error)))
403 (mkdir-p (dirname cache-file))
404 (call-with-output-file cache-file
406 ;; Store the cache as a '.go' file. This makes loading fast and reduces
407 ;; heap usage since some of the static data is directly mmapped.
411 #:opts '(#:to-file? #t)))))
415 (define %sigint-prompt
416 ;; The prompt to jump to upon SIGINT.
417 (make-prompt-tag "interruptible"))
419 (define (call-with-sigint-handler thunk handler)
420 "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
421 number in the context of the continuation of the call to this function, and
422 return its return value."
423 (call-with-prompt %sigint-prompt
427 (sigaction SIGINT SIG_DFL)
428 (abort-to-prompt %sigint-prompt signum)))
432 (cut sigaction SIGINT SIG_DFL)))
438 ;;; Package specification.
441 (define* (%find-package spec name version)
442 (match (find-best-packages-by-name name version)
445 (warning (G_ "ambiguous package specification `~a'~%") spec)
446 (warning (G_ "choosing ~a@~a from ~a~%")
447 (package-name pkg) (package-version pkg)
448 (location->string (package-location pkg))))
449 (match (package-superseded pkg)
451 (info (G_ "package '~a' has been superseded by '~a'~%")
452 (package-name pkg) (package-name new))
458 (leave (G_ "~A: package not found for version ~a~%") name version)
459 (leave (G_ "~A: unknown package~%") name)))))
461 (define (specification->package spec)
462 "Return a package matching SPEC. SPEC may be a package name, or a package
463 name followed by an at-sign and a version number. If the version number is not
464 present, return the preferred newest version."
465 (let-values (((name version) (package-name->name+version spec)))
466 (%find-package spec name version)))
468 (define (specification->location spec)
469 "Return the location of the highest-numbered package matching SPEC, a
470 specification such as \"guile@2\" or \"emacs\"."
471 (let-values (((name version) (package-name->name+version spec)))
472 (match (find-package-locations name version)
475 (leave (G_ "~A: package not found for version ~a~%") name version)
476 (leave (G_ "~A: unknown package~%") name)))
478 (let* ((highest (match lst (((version . _) _ ...) version)))
479 (locations (take-while (match-lambda
480 ((version . location)
481 (string=? version highest)))
484 (((version . location) . rest)
486 (warning (G_ "ambiguous package specification `~a'~%") spec)
487 (warning (G_ "choosing ~a@~a from ~a~%")
489 (location->string location)))
492 (define* (specification->package+output spec #:optional (output "out"))
493 "Return the package and output specified by SPEC, or #f and #f; SPEC may
494 optionally contain a version number and an output name, as in these examples:
501 If SPEC does not specify a version number, return the preferred newest
502 version; if SPEC does not specify an output, return OUTPUT."
503 (let-values (((name version sub-drv)
504 (package-specification->name+version+output spec output)))
505 (match (%find-package spec name version)
509 (if (member sub-drv (package-outputs package))
510 (values package sub-drv)
511 (leave (G_ "package `~a' lacks output `~a'~%")
512 (package-full-name package)
515 (define (specifications->manifest specs)
516 "Given SPECS, a list of specifications such as \"emacs@25.2\" or
517 \"guile:debug\", return a profile manifest."
518 ;; This procedure exists mostly so users of 'guix package -m' don't have to
519 ;; fiddle with multiple-value returns.
521 (map (compose list specification->package+output) specs)))