gnu: Add fish-guix.
[jackhill/guix/guix.git] / gnu / packages.scm
... / ...
CommitLineData
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>
7;;;
8;;; This file is part of GNU Guix.
9;;;
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.
14;;;
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.
19;;;
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/>.
22
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
42 search-patches
43 search-auxiliary-file
44 search-bootstrap-binary
45 %patch-path
46 %auxiliary-files-path
47 %bootstrap-binaries-path
48 %package-module-path
49
50 fold-packages
51 scheme-modules ;XXX: for lack of a better place
52
53 find-packages-by-name
54 find-best-packages-by-name
55 find-newest-available-packages
56
57 specification->package
58 specification->package+output))
59
60;;; Commentary:
61;;;
62;;; General utilities for the software distribution---i.e., the modules under
63;;; (gnu packages ...).
64;;;
65;;; Code:
66
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.
73
74(define %bootstrap-binaries-path
75 (make-parameter
76 (map (cut string-append <> "/gnu/packages/bootstrap")
77 %load-path)))
78
79(define %auxiliary-files-path
80 (make-parameter
81 (map (cut string-append <> "/gnu/packages/aux-files")
82 %load-path)))
83
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))
87
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)
91 (raise (condition
92 (&message (message (format #f (G_ "~a: patch not found")
93 file-name)))))))
94
95(define-syntax-rule (search-patches file-name ...)
96 "Return the list of absolute file names corresponding to each
97FILE-NAME found in %PATCH-PATH."
98 (list (search-patch file-name) ...))
99
100(define (search-bootstrap-binary file-name system)
101 "Search the bootstrap binary FILE-NAME for SYSTEM. Raise an error if not
102found."
103 (or (search-path (%bootstrap-binaries-path)
104 (string-append system "/" file-name))
105 (raise (condition
106 (&message
107 (message
108 (format #f (G_ "could not find bootstrap binary '~a' \
109for system '~a'")
110 file-name system)))))))
111
112(define %distro-root-directory
113 ;; Absolute file name of the module hierarchy.
114 (dirname (search-path %load-path "guix.scm")))
115
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") "")
122 not-colon)))
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)))
128 environment)
129
130 (make-parameter
131 (append environment `((,%distro-root-directory . "gnu/packages"))))))
132
133(define %patch-path
134 ;; Define it after '%package-module-path' so that '%load-path' contains user
135 ;; directories, allowing patches in $GUIX_PACKAGE_PATH to be found.
136 (make-parameter
137 (map (lambda (directory)
138 (if (string=? directory %distro-root-directory)
139 (string-append directory "/gnu/packages/patches")
140 directory))
141 %load-path)))
142
143(define* (scheme-files directory)
144 "Return the list of Scheme files found under DIRECTORY, recursively. The
145returned list is sorted in alphabetical order."
146
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)
152 (cons path result)
153 result))
154 (lambda (path stat result) ; down
155 result)
156 (lambda (path stat result) ; up
157 result)
158 (const #f) ; skip
159 (lambda (path stat errno result)
160 (warning (G_ "cannot access `~a': ~a~%")
161 path (strerror errno))
162 result)
163 '()
164 directory
165 stat)
166 string<?))
167
168(define file-name->module-name
169 (let ((not-slash (char-set-complement (char-set #\/))))
170 (lambda (file)
171 "Return the module name (a list of symbols) corresponding to FILE."
172 (map string->symbol
173 (string-tokenize (string-drop-right file 4) not-slash)))))
174
175(define* (scheme-modules directory #:optional sub-directory)
176 "Return the list of Scheme modules available under DIRECTORY.
177Optionally, narrow the search to SUB-DIRECTORY."
178 (define prefix-len
179 (string-length directory))
180
181 (filter-map (lambda (file)
182 (let* ((file (substring file prefix-len))
183 (module (file-name->module-name file)))
184 (catch #t
185 (lambda ()
186 (resolve-interface module))
187 (lambda args
188 ;; Report the error, but keep going.
189 (warn-about-load-error module args)
190 #f))))
191 (scheme-files (if sub-directory
192 (string-append directory "/" sub-directory)
193 directory))))
194
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
197search."
198 (fold-right (lambda (spec result)
199 (match spec
200 ((? string? directory)
201 (append (scheme-modules directory) result))
202 ((directory . sub-directory)
203 (append (scheme-modules directory sub-directory)
204 result))))
205 '()
206 path))
207
208(define (fold-packages proc init)
209 "Call (PROC PACKAGE RESULT) for each available package, using INIT as
210the initial value of RESULT. It is guaranteed to never traverse the
211same package twice."
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)))
221 result
222 seen
223 (module-map (lambda (sym var)
224 (false-if-exception (variable-ref var)))
225 module)))
226 init
227 vlist-null
228 (all-package-modules))))
229
230(define find-packages-by-name
231 (let ((packages (delay
232 (fold-packages (lambda (p r)
233 (vhash-cons (package-name p) p r))
234 vlist-null)))
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,
239then only return packages whose version is prefixed by VERSION, sorted in
240decreasing version order."
241 (let ((matching (sort (vhash-fold* cons '() name (force packages))
242 version>?)))
243 (if version
244 (filter (lambda (package)
245 (string-prefix? version (package-version package)))
246 matching)
247 matching)))))
248
249(define find-newest-available-packages
250 (mlambda ()
251 "Return a vhash keyed by package names, and with
252associated values of the form
253
254 (newest-version newest-package ...)
255
256where the preferred package is listed first."
257
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))
268 ((<) r)))
269 (#f (vhash-cons name `(,version ,p) r)))))
270 vlist-null)))
271
272(define (find-best-packages-by-name name version)
273 "If version is #f, return the list of packages named NAME with the highest
274version numbers; otherwise, return the list of packages named NAME and at
275VERSION."
276 (if version
277 (find-packages-by-name name version)
278 (match (vhash-assoc name (find-newest-available-packages))
279 ((_ version pkgs ...) pkgs)
280 (#f '()))))
281
282\f
283(define %sigint-prompt
284 ;; The prompt to jump to upon SIGINT.
285 (make-prompt-tag "interruptible"))
286
287(define (call-with-sigint-handler thunk handler)
288 "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
289number in the context of the continuation of the call to this function, and
290return its return value."
291 (call-with-prompt %sigint-prompt
292 (lambda ()
293 (sigaction SIGINT
294 (lambda (signum)
295 (sigaction SIGINT SIG_DFL)
296 (abort-to-prompt %sigint-prompt signum)))
297 (dynamic-wind
298 (const #t)
299 thunk
300 (cut sigaction SIGINT SIG_DFL)))
301 (lambda (k signum)
302 (handler signum))))
303
304\f
305;;;
306;;; Package specification.
307;;;
308
309(define* (%find-package spec name version)
310 (match (find-best-packages-by-name name version)
311 ((pkg . pkg*)
312 (unless (null? pkg*)
313 (warning (G_ "ambiguous package specification `~a'~%") spec)
314 (warning (G_ "choosing ~a@~a from ~a~%")
315 (package-name pkg) (package-version pkg)
316 (location->string (package-location pkg))))
317 (match (package-superseded pkg)
318 ((? package? new)
319 (info (G_ "package '~a' has been superseded by '~a'~%")
320 (package-name pkg) (package-name new))
321 new)
322 (#f
323 pkg)))
324 (x
325 (if version
326 (leave (G_ "~A: package not found for version ~a~%") name version)
327 (leave (G_ "~A: unknown package~%") name)))))
328
329(define (specification->package spec)
330 "Return a package matching SPEC. SPEC may be a package name, or a package
331name followed by an at-sign and a version number. If the version number is not
332present, return the preferred newest version."
333 (let-values (((name version) (package-name->name+version spec)))
334 (%find-package spec name version)))
335
336(define* (specification->package+output spec #:optional (output "out"))
337 "Return the package and output specified by SPEC, or #f and #f; SPEC may
338optionally contain a version number and an output name, as in these examples:
339
340 guile
341 guile@2.0.9
342 guile:debug
343 guile@2.0.9:debug
344
345If SPEC does not specify a version number, return the preferred newest
346version; 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)
350 (#f
351 (values #f #f))
352 (package
353 (if (member sub-drv (package-outputs package))
354 (values package sub-drv)
355 (leave (G_ "package `~a' lacks output `~a'~%")
356 (package-full-name package)
357 sub-drv))))))