1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016 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 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 build utils)
28 #:select ((package-name->name+version
29 . hyphen-separated-name->name+version)))
30 #:use-module (ice-9 ftw)
31 #:use-module (ice-9 vlist)
32 #:use-module (ice-9 match)
33 #:use-module (srfi srfi-1)
34 #:use-module (srfi srfi-11)
35 #:use-module (srfi srfi-26)
36 #:use-module (srfi srfi-34)
37 #:use-module (srfi srfi-35)
38 #:use-module (srfi srfi-39)
39 #:export (search-patch
40 search-bootstrap-binary
42 %bootstrap-binaries-path
48 find-best-packages-by-name
49 find-newest-available-packages
51 specification->package
52 specification->package+output))
56 ;;; General utilities for the software distribution---i.e., the modules under
57 ;;; (gnu packages ...).
61 ;; By default, we store patches and bootstrap binaries alongside Guile
62 ;; modules. This is so that these extra files can be found without
63 ;; requiring a special setup, such as a specific installation directory
64 ;; and an extra environment variable. One advantage of this setup is
65 ;; that everything just works in an auto-compilation setting.
67 (define %bootstrap-binaries-path
69 (map (cut string-append <> "/gnu/packages/bootstrap")
72 (define (search-patch file-name)
73 "Search the patch FILE-NAME. Raise an error if not found."
74 (or (search-path (%patch-path) file-name)
76 (&message (message (format #f (_ "~a: patch not found")
79 (define (search-bootstrap-binary file-name system)
80 "Search the bootstrap binary FILE-NAME for SYSTEM. Raise an error if not
82 (or (search-path (%bootstrap-binaries-path)
83 (string-append system "/" file-name))
87 (format #f (_ "could not find bootstrap binary '~a' \
89 file-name system)))))))
91 (define %distro-root-directory
92 ;; Absolute file name of the module hierarchy.
93 (dirname (search-path %load-path "guix.scm")))
95 (define %package-module-path
96 ;; Search path for package modules. Each item must be either a directory
97 ;; name or a pair whose car is a directory and whose cdr is a sub-directory
98 ;; to narrow the search.
99 (let* ((not-colon (char-set-complement (char-set #\:)))
100 (environment (string-tokenize (or (getenv "GUIX_PACKAGE_PATH") "")
102 ;; Automatically add items from $GUIX_PACKAGE_PATH to Guile's search path.
103 (for-each (lambda (directory)
104 (set! %load-path (cons directory %load-path))
105 (set! %load-compiled-path
106 (cons directory %load-compiled-path)))
110 (append environment `((,%distro-root-directory . "gnu/packages"))))))
113 ;; Define it after '%package-module-path' so that '%load-path' contains user
114 ;; directories, allowing patches in $GUIX_PACKAGE_PATH to be found.
116 (map (lambda (directory)
117 (if (string=? directory %distro-root-directory)
118 (string-append directory "/gnu/packages/patches")
122 (define* (scheme-files directory)
123 "Return the list of Scheme files found under DIRECTORY, recursively. The
124 returned list is sorted in alphabetical order."
126 ;; Sort entries so that 'fold-packages' works in a deterministic fashion
127 ;; regardless of details of the underlying file system.
128 (sort (file-system-fold (const #t) ; enter?
129 (lambda (path stat result) ; leaf
130 (if (string-suffix? ".scm" path)
133 (lambda (path stat result) ; down
135 (lambda (path stat result) ; up
138 (lambda (path stat errno result)
139 (warning (_ "cannot access `~a': ~a~%")
140 path (strerror errno))
147 (define file-name->module-name
148 (let ((not-slash (char-set-complement (char-set #\/))))
150 "Return the module name (a list of symbols) corresponding to FILE."
152 (string-tokenize (string-drop-right file 4) not-slash)))))
154 (define* (package-modules directory #:optional sub-directory)
155 "Return the list of modules that provide packages for the distribution.
156 Optionally, narrow the search to SUB-DIRECTORY."
158 (string-length directory))
160 (filter-map (lambda (file)
161 (let* ((file (substring file prefix-len))
162 (module (file-name->module-name file)))
165 (resolve-interface module))
167 ;; Report the error, but keep going.
168 (warn-about-load-error module args)
170 (scheme-files (if sub-directory
171 (string-append directory "/" sub-directory)
174 (define* (all-package-modules #:optional (path (%package-module-path)))
175 "Return the list of package modules found in PATH, a list of directories to
177 (fold-right (lambda (spec result)
179 ((? string? directory)
180 (append (package-modules directory) result))
181 ((directory . sub-directory)
182 (append (package-modules directory sub-directory)
187 (define (fold-packages proc init)
188 "Call (PROC PACKAGE RESULT) for each available package, using INIT as
189 the initial value of RESULT. It is guaranteed to never traverse the
191 (identity ; discard second return value
192 (fold2 (lambda (module result seen)
193 (fold2 (lambda (var result seen)
194 (if (and (package? var)
195 (not (vhash-assq var seen)))
196 (values (proc var result)
197 (vhash-consq var #t seen))
198 (values result seen)))
201 (module-map (lambda (sym var)
202 (false-if-exception (variable-ref var)))
206 (all-package-modules))))
208 (define find-packages-by-name
209 (let ((packages (delay
210 (fold-packages (lambda (p r)
211 (vhash-cons (package-name p) p r))
213 (version>? (lambda (p1 p2)
214 (version>? (package-version p1) (package-version p2)))))
215 (lambda* (name #:optional version)
216 "Return the list of packages with the given NAME. If VERSION is not #f,
217 then only return packages whose version is prefixed by VERSION, sorted in
218 decreasing version order."
219 (let ((matching (sort (vhash-fold* cons '() name (force packages))
222 (filter (lambda (package)
223 (string-prefix? version (package-version package)))
227 (define find-newest-available-packages
230 "Return a vhash keyed by package names, and with
231 associated values of the form
233 (newest-version newest-package ...)
235 where the preferred package is listed first."
237 ;; FIXME: Currently, the preferred package is whichever one
238 ;; was found last by 'fold-packages'. Find a better solution.
239 (fold-packages (lambda (p r)
240 (let ((name (package-name p))
241 (version (package-version p)))
242 (match (vhash-assoc name r)
243 ((_ newest-so-far . pkgs)
244 (case (version-compare version newest-so-far)
245 ((>) (vhash-cons name `(,version ,p) r))
246 ((=) (vhash-cons name `(,version ,p ,@pkgs) r))
248 (#f (vhash-cons name `(,version ,p) r)))))
251 (define (find-best-packages-by-name name version)
252 "If version is #f, return the list of packages named NAME with the highest
253 version numbers; otherwise, return the list of packages named NAME and at
256 (find-packages-by-name name version)
257 (match (vhash-assoc name (find-newest-available-packages))
258 ((_ version pkgs ...) pkgs)
262 (define %sigint-prompt
263 ;; The prompt to jump to upon SIGINT.
264 (make-prompt-tag "interruptible"))
266 (define (call-with-sigint-handler thunk handler)
267 "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
268 number in the context of the continuation of the call to this function, and
269 return its return value."
270 (call-with-prompt %sigint-prompt
274 (sigaction SIGINT SIG_DFL)
275 (abort-to-prompt %sigint-prompt signum)))
279 (cut sigaction SIGINT SIG_DFL)))
285 ;;; Package specification.
288 (define* (%find-package spec name version #:key fallback?)
289 (match (find-best-packages-by-name name version)
292 (warning (_ "ambiguous package specification `~a'~%") spec)
293 (warning (_ "choosing ~a from ~a~%")
294 (package-full-name pkg)
295 (location->string (package-location pkg))))
297 (warning (_ "deprecated NAME-VERSION syntax; \
298 use NAME@VERSION instead~%")))
302 (leave (_ "~A: package not found for version ~a~%") name version)
304 ;; XXX: Fallback to the older specification style with an hyphen
305 ;; between NAME and VERSION, for backward compatibility.
308 (hyphen-separated-name->name+version name))
309 (cut %find-package spec <> <> #:fallback? #t))
311 ;; The fallback case didn't find anything either, so bail out.
312 (leave (_ "~A: unknown package~%") name))))))
314 (define (specification->package spec)
315 "Return a package matching SPEC. SPEC may be a package name, or a package
316 name followed by an at-sign and a version number. If the version number is not
317 present, return the preferred newest version."
318 (let-values (((name version) (package-name->name+version spec)))
319 (%find-package spec name version)))
321 (define* (specification->package+output spec #:optional (output "out"))
322 "Return the package and output specified by SPEC, or #f and #f; SPEC may
323 optionally contain a version number and an output name, as in these examples:
330 If SPEC does not specify a version number, return the preferred newest
331 version; if SPEC does not specify an output, return OUTPUT."
332 (let-values (((name version sub-drv)
333 (package-specification->name+version+output spec output)))
334 (match (%find-package spec name version)
338 (if (member sub-drv (package-outputs package))
339 (values package sub-drv)
340 (leave (_ "package `~a' lacks output `~a'~%")
341 (package-full-name package)