1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 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 memoization)
28 #:use-module (guix combinators)
29 #:use-module ((guix build utils)
30 #:select ((package-name->name+version
31 . hyphen-separated-name->name+version)))
32 #:use-module (ice-9 ftw)
33 #:use-module (ice-9 vlist)
34 #:use-module (ice-9 match)
35 #:use-module (srfi srfi-1)
36 #:use-module (srfi srfi-11)
37 #:use-module (srfi srfi-26)
38 #:use-module (srfi srfi-34)
39 #:use-module (srfi srfi-35)
40 #:use-module (srfi srfi-39)
41 #:export (search-patch
44 search-bootstrap-binary
47 %bootstrap-binaries-path
51 scheme-modules ;XXX: for lack of a better place
54 find-best-packages-by-name
55 find-newest-available-packages
57 specification->package
58 specification->package+output))
62 ;;; General utilities for the software distribution---i.e., the modules under
63 ;;; (gnu packages ...).
67 ;; By default, we store patches, auxiliary files and bootstrap binaries
68 ;; alongside Guile modules. This is so that these extra files can be
69 ;; found without requiring a special setup, such as a specific
70 ;; installation directory and an extra environment variable. One
71 ;; advantage of this setup is that everything just works in an
72 ;; auto-compilation setting.
74 (define %bootstrap-binaries-path
76 (map (cut string-append <> "/gnu/packages/bootstrap")
79 (define %auxiliary-files-path
81 (map (cut string-append <> "/gnu/packages/aux-files")
84 (define (search-auxiliary-file file-name)
85 "Search the auxiliary FILE-NAME. Return #f if not found."
86 (search-path (%auxiliary-files-path) file-name))
88 (define (search-patch file-name)
89 "Search the patch FILE-NAME. Raise an error if not found."
90 (or (search-path (%patch-path) file-name)
92 (&message (message (format #f (_ "~a: patch not found")
95 (define-syntax-rule (search-patches file-name ...)
96 "Return the list of absolute file names corresponding to each
97 FILE-NAME found in %PATCH-PATH."
98 (list (search-patch file-name) ...))
100 (define (search-bootstrap-binary file-name system)
101 "Search the bootstrap binary FILE-NAME for SYSTEM. Raise an error if not
103 (or (search-path (%bootstrap-binaries-path)
104 (string-append system "/" file-name))
108 (format #f (_ "could not find bootstrap binary '~a' \
110 file-name system)))))))
112 (define %distro-root-directory
113 ;; Absolute file name of the module hierarchy.
114 (dirname (search-path %load-path "guix.scm")))
116 (define %package-module-path
117 ;; Search path for package modules. Each item must be either a directory
118 ;; name or a pair whose car is a directory and whose cdr is a sub-directory
119 ;; to narrow the search.
120 (let* ((not-colon (char-set-complement (char-set #\:)))
121 (environment (string-tokenize (or (getenv "GUIX_PACKAGE_PATH") "")
123 ;; Automatically add items from $GUIX_PACKAGE_PATH to Guile's search path.
124 (for-each (lambda (directory)
125 (set! %load-path (cons directory %load-path))
126 (set! %load-compiled-path
127 (cons directory %load-compiled-path)))
131 (append environment `((,%distro-root-directory . "gnu/packages"))))))
134 ;; Define it after '%package-module-path' so that '%load-path' contains user
135 ;; directories, allowing patches in $GUIX_PACKAGE_PATH to be found.
137 (map (lambda (directory)
138 (if (string=? directory %distro-root-directory)
139 (string-append directory "/gnu/packages/patches")
143 (define* (scheme-files directory)
144 "Return the list of Scheme files found under DIRECTORY, recursively. The
145 returned list is sorted in alphabetical order."
147 ;; Sort entries so that 'fold-packages' works in a deterministic fashion
148 ;; regardless of details of the underlying file system.
149 (sort (file-system-fold (const #t) ; enter?
150 (lambda (path stat result) ; leaf
151 (if (string-suffix? ".scm" path)
154 (lambda (path stat result) ; down
156 (lambda (path stat result) ; up
159 (lambda (path stat errno result)
160 (warning (_ "cannot access `~a': ~a~%")
161 path (strerror errno))
168 (define file-name->module-name
169 (let ((not-slash (char-set-complement (char-set #\/))))
171 "Return the module name (a list of symbols) corresponding to FILE."
173 (string-tokenize (string-drop-right file 4) not-slash)))))
175 (define* (scheme-modules directory #:optional sub-directory)
176 "Return the list of Scheme modules available under DIRECTORY.
177 Optionally, narrow the search to SUB-DIRECTORY."
179 (string-length directory))
181 (filter-map (lambda (file)
182 (let* ((file (substring file prefix-len))
183 (module (file-name->module-name file)))
186 (resolve-interface module))
188 ;; Report the error, but keep going.
189 (warn-about-load-error module args)
191 (scheme-files (if sub-directory
192 (string-append directory "/" sub-directory)
195 (define* (all-package-modules #:optional (path (%package-module-path)))
196 "Return the list of package modules found in PATH, a list of directories to
198 (fold-right (lambda (spec result)
200 ((? string? directory)
201 (append (scheme-modules directory) result))
202 ((directory . sub-directory)
203 (append (scheme-modules directory sub-directory)
208 (define (fold-packages proc init)
209 "Call (PROC PACKAGE RESULT) for each available package, using INIT as
210 the initial value of RESULT. It is guaranteed to never traverse the
212 (identity ; discard second return value
213 (fold2 (lambda (module result seen)
214 (fold2 (lambda (var result seen)
215 (if (and (package? var)
216 (not (vhash-assq var seen))
217 (not (hidden-package? var)))
218 (values (proc var result)
219 (vhash-consq var #t seen))
220 (values result seen)))
223 (module-map (lambda (sym var)
224 (false-if-exception (variable-ref var)))
228 (all-package-modules))))
230 (define find-packages-by-name
231 (let ((packages (delay
232 (fold-packages (lambda (p r)
233 (vhash-cons (package-name p) p r))
235 (version>? (lambda (p1 p2)
236 (version>? (package-version p1) (package-version p2)))))
237 (lambda* (name #:optional version)
238 "Return the list of packages with the given NAME. If VERSION is not #f,
239 then only return packages whose version is prefixed by VERSION, sorted in
240 decreasing version order."
241 (let ((matching (sort (vhash-fold* cons '() name (force packages))
244 (filter (lambda (package)
245 (string-prefix? version (package-version package)))
249 (define find-newest-available-packages
251 "Return a vhash keyed by package names, and with
252 associated values of the form
254 (newest-version newest-package ...)
256 where the preferred package is listed first."
258 ;; FIXME: Currently, the preferred package is whichever one
259 ;; was found last by 'fold-packages'. Find a better solution.
260 (fold-packages (lambda (p r)
261 (let ((name (package-name p))
262 (version (package-version p)))
263 (match (vhash-assoc name r)
264 ((_ newest-so-far . pkgs)
265 (case (version-compare version newest-so-far)
266 ((>) (vhash-cons name `(,version ,p) r))
267 ((=) (vhash-cons name `(,version ,p ,@pkgs) r))
269 (#f (vhash-cons name `(,version ,p) r)))))
272 (define (find-best-packages-by-name name version)
273 "If version is #f, return the list of packages named NAME with the highest
274 version numbers; otherwise, return the list of packages named NAME and at
277 (find-packages-by-name name version)
278 (match (vhash-assoc name (find-newest-available-packages))
279 ((_ version pkgs ...) pkgs)
283 (define %sigint-prompt
284 ;; The prompt to jump to upon SIGINT.
285 (make-prompt-tag "interruptible"))
287 (define (call-with-sigint-handler thunk handler)
288 "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
289 number in the context of the continuation of the call to this function, and
290 return its return value."
291 (call-with-prompt %sigint-prompt
295 (sigaction SIGINT SIG_DFL)
296 (abort-to-prompt %sigint-prompt signum)))
300 (cut sigaction SIGINT SIG_DFL)))
306 ;;; Package specification.
309 (define* (%find-package spec name version)
310 (match (find-best-packages-by-name name version)
313 (warning (_ "ambiguous package specification `~a'~%") spec)
314 (warning (_ "choosing ~a@~a from ~a~%")
315 (package-name pkg) (package-version pkg)
316 (location->string (package-location pkg))))
317 (match (package-superseded pkg)
319 (info (_ "package '~a' has been superseded by '~a'~%")
320 (package-name pkg) (package-name new))
326 (leave (_ "~A: package not found for version ~a~%") name version)
327 (leave (_ "~A: unknown package~%") name)))))
329 (define (specification->package spec)
330 "Return a package matching SPEC. SPEC may be a package name, or a package
331 name followed by an at-sign and a version number. If the version number is not
332 present, return the preferred newest version."
333 (let-values (((name version) (package-name->name+version spec)))
334 (%find-package spec name version)))
336 (define* (specification->package+output spec #:optional (output "out"))
337 "Return the package and output specified by SPEC, or #f and #f; SPEC may
338 optionally contain a version number and an output name, as in these examples:
345 If SPEC does not specify a version number, return the preferred newest
346 version; if SPEC does not specify an output, return OUTPUT."
347 (let-values (((name version sub-drv)
348 (package-specification->name+version+output spec output)))
349 (match (%find-package spec name version)
353 (if (member sub-drv (package-outputs package))
354 (values package sub-drv)
355 (leave (_ "package `~a' lacks output `~a'~%")
356 (package-full-name package)