1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018 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)))
32 #:autoload (guix profiles) (packages->manifest)
33 #:use-module (guix describe)
34 #:use-module (ice-9 vlist)
35 #:use-module (ice-9 match)
36 #:use-module (srfi srfi-1)
37 #:use-module (srfi srfi-11)
38 #:use-module (srfi srfi-26)
39 #:use-module (srfi srfi-34)
40 #:use-module (srfi srfi-35)
41 #:use-module (srfi srfi-39)
42 #:export (search-patch
45 search-bootstrap-binary
48 %bootstrap-binaries-path
50 %default-package-module-path
55 find-best-packages-by-name
56 find-newest-available-packages
58 specification->package
59 specification->package+output
60 specifications->manifest))
64 ;;; General utilities for the software distribution---i.e., the modules under
65 ;;; (gnu packages ...).
69 ;; By default, we store patches, auxiliary files and bootstrap binaries
70 ;; alongside Guile modules. This is so that these extra files can be
71 ;; found without requiring a special setup, such as a specific
72 ;; installation directory and an extra environment variable. One
73 ;; advantage of this setup is that everything just works in an
74 ;; auto-compilation setting.
76 (define %bootstrap-binaries-path
78 (map (cut string-append <> "/gnu/packages/bootstrap")
81 (define %auxiliary-files-path
83 (map (cut string-append <> "/gnu/packages/aux-files")
86 (define (search-auxiliary-file file-name)
87 "Search the auxiliary FILE-NAME. Return #f if not found."
88 (search-path (%auxiliary-files-path) file-name))
90 (define (search-patch file-name)
91 "Search the patch FILE-NAME. Raise an error if not found."
92 (or (search-path (%patch-path) file-name)
94 (&message (message (format #f (G_ "~a: patch not found")
97 (define-syntax-rule (search-patches file-name ...)
98 "Return the list of absolute file names corresponding to each
99 FILE-NAME found in %PATCH-PATH."
100 (list (search-patch file-name) ...))
102 (define (search-bootstrap-binary file-name system)
103 "Search the bootstrap binary FILE-NAME for SYSTEM. Raise an error if not
105 (or (search-path (%bootstrap-binaries-path)
106 (string-append system "/" file-name))
110 (format #f (G_ "could not find bootstrap binary '~a' \
112 file-name system)))))))
114 (define %distro-root-directory
115 ;; Absolute file name of the module hierarchy. Since (gnu packages …) might
116 ;; live in a directory different from (guix), try to get the best match.
117 (letrec-syntax ((dirname* (syntax-rules ()
120 ((_ file head tail ...)
121 (dirname (dirname* file tail ...)))))
122 (try (syntax-rules ()
123 ((_ (file things ...) rest ...)
124 (match (search-path %load-path file)
128 (dirname* absolute things ...))))
131 (try ("gnu/packages/base.scm" gnu/ packages/)
132 ("gnu/packages.scm" gnu/)
135 (define %default-package-module-path
136 ;; Default search path for package modules.
137 `((,%distro-root-directory . "gnu/packages")))
139 (define %package-module-path
140 ;; Search path for package modules. Each item must be either a directory
141 ;; name or a pair whose car is a directory and whose cdr is a sub-directory
142 ;; to narrow the search.
143 (let* ((not-colon (char-set-complement (char-set #\:)))
144 (environment (string-tokenize (or (getenv "GUIX_PACKAGE_PATH") "")
146 (channels (package-path-entries)))
147 ;; Automatically add channels and items from $GUIX_PACKAGE_PATH to Guile's
148 ;; search path. For historical reasons, $GUIX_PACKAGE_PATH goes to the
149 ;; front; channels go to the back so that they don't override Guix' own
152 (append environment %load-path channels))
153 (set! %load-compiled-path
154 (append environment %load-compiled-path channels))
158 %default-package-module-path
162 ;; Define it after '%package-module-path' so that '%load-path' contains user
163 ;; directories, allowing patches in $GUIX_PACKAGE_PATH to be found.
165 (map (lambda (directory)
166 (if (string=? directory %distro-root-directory)
167 (string-append directory "/gnu/packages/patches")
171 (define* (fold-packages proc init
173 (modules (all-modules (%package-module-path)
175 warn-about-load-error))
176 #:key (select? (negate hidden-package?)))
177 "Call (PROC PACKAGE RESULT) for each available package defined in one of
178 MODULES that matches SELECT?, using INIT as the initial value of RESULT. It
179 is guaranteed to never traverse the same package twice."
180 (fold-module-public-variables (lambda (object result)
181 (if (and (package? object) (select? object))
187 (define find-packages-by-name
188 (let ((packages (delay
189 (fold-packages (lambda (p r)
190 (vhash-cons (package-name p) p r))
192 (version>? (lambda (p1 p2)
193 (version>? (package-version p1) (package-version p2)))))
194 (lambda* (name #:optional version)
195 "Return the list of packages with the given NAME. If VERSION is not #f,
196 then only return packages whose version is prefixed by VERSION, sorted in
197 decreasing version order."
198 (let ((matching (sort (vhash-fold* cons '() name (force packages))
201 (filter (lambda (package)
202 (version-prefix? version (package-version package)))
206 (define find-newest-available-packages
208 "Return a vhash keyed by package names, and with
209 associated values of the form
211 (newest-version newest-package ...)
213 where the preferred package is listed first."
215 ;; FIXME: Currently, the preferred package is whichever one
216 ;; was found last by 'fold-packages'. Find a better solution.
217 (fold-packages (lambda (p r)
218 (let ((name (package-name p))
219 (version (package-version p)))
220 (match (vhash-assoc name r)
221 ((_ newest-so-far . pkgs)
222 (case (version-compare version newest-so-far)
223 ((>) (vhash-cons name `(,version ,p) r))
224 ((=) (vhash-cons name `(,version ,p ,@pkgs) r))
226 (#f (vhash-cons name `(,version ,p) r)))))
229 (define (find-best-packages-by-name name version)
230 "If version is #f, return the list of packages named NAME with the highest
231 version numbers; otherwise, return the list of packages named NAME and at
234 (find-packages-by-name name version)
235 (match (vhash-assoc name (find-newest-available-packages))
236 ((_ version pkgs ...) pkgs)
240 (define %sigint-prompt
241 ;; The prompt to jump to upon SIGINT.
242 (make-prompt-tag "interruptible"))
244 (define (call-with-sigint-handler thunk handler)
245 "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
246 number in the context of the continuation of the call to this function, and
247 return its return value."
248 (call-with-prompt %sigint-prompt
252 (sigaction SIGINT SIG_DFL)
253 (abort-to-prompt %sigint-prompt signum)))
257 (cut sigaction SIGINT SIG_DFL)))
263 ;;; Package specification.
266 (define* (%find-package spec name version)
267 (match (find-best-packages-by-name name version)
270 (warning (G_ "ambiguous package specification `~a'~%") spec)
271 (warning (G_ "choosing ~a@~a from ~a~%")
272 (package-name pkg) (package-version pkg)
273 (location->string (package-location pkg))))
274 (match (package-superseded pkg)
276 (info (G_ "package '~a' has been superseded by '~a'~%")
277 (package-name pkg) (package-name new))
283 (leave (G_ "~A: package not found for version ~a~%") name version)
284 (leave (G_ "~A: unknown package~%") name)))))
286 (define (specification->package spec)
287 "Return a package matching SPEC. SPEC may be a package name, or a package
288 name followed by an at-sign and a version number. If the version number is not
289 present, return the preferred newest version."
290 (let-values (((name version) (package-name->name+version spec)))
291 (%find-package spec name version)))
293 (define* (specification->package+output spec #:optional (output "out"))
294 "Return the package and output specified by SPEC, or #f and #f; SPEC may
295 optionally contain a version number and an output name, as in these examples:
302 If SPEC does not specify a version number, return the preferred newest
303 version; if SPEC does not specify an output, return OUTPUT."
304 (let-values (((name version sub-drv)
305 (package-specification->name+version+output spec output)))
306 (match (%find-package spec name version)
310 (if (member sub-drv (package-outputs package))
311 (values package sub-drv)
312 (leave (G_ "package `~a' lacks output `~a'~%")
313 (package-full-name package)
316 (define (specifications->manifest specs)
317 "Given SPECS, a list of specifications such as \"emacs@25.2\" or
318 \"guile:debug\", return a profile manifest."
319 ;; This procedure exists mostly so users of 'guix package -m' don't have to
320 ;; fiddle with multiple-value returns.
322 (map (compose list specification->package+output) specs)))