1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2013 Mark H Weaver <mhw@netris.org>
4 ;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (gnu packages)
22 #:use-module (guix packages)
23 #:use-module (guix ui)
24 #:use-module (guix utils)
25 #:use-module ((guix ftp-client) #:select (ftp-open))
26 #:use-module (guix gnu-maintenance)
27 #:use-module (ice-9 ftw)
28 #:use-module (ice-9 vlist)
29 #:use-module (ice-9 match)
30 #:use-module (srfi srfi-1)
31 #:use-module (srfi srfi-11)
32 #:use-module (srfi srfi-26)
33 #:use-module (srfi srfi-34)
34 #:use-module (srfi srfi-35)
35 #:use-module (srfi srfi-39)
36 #:export (search-patch
37 search-bootstrap-binary
39 %bootstrap-binaries-path
45 find-best-packages-by-name
46 find-newest-available-packages
48 package-direct-dependents
49 package-transitive-dependents
50 package-covering-dependents
52 check-package-freshness
54 specification->package
55 specification->package+output))
59 ;;; General utilities for the software distribution---i.e., the modules under
60 ;;; (gnu packages ...).
64 ;; By default, we store patches and bootstrap binaries alongside Guile
65 ;; modules. This is so that these extra files can be found without
66 ;; requiring a special setup, such as a specific installation directory
67 ;; and an extra environment variable. One advantage of this setup is
68 ;; that everything just works in an auto-compilation setting.
70 (define %bootstrap-binaries-path
72 (map (cut string-append <> "/gnu/packages/bootstrap")
75 (define (search-patch file-name)
76 "Search the patch FILE-NAME. Raise an error if not found."
77 (or (search-path (%patch-path) file-name)
79 (&message (message (format #f (_ "~a: patch not found")
82 (define (search-bootstrap-binary file-name system)
83 "Search the bootstrap binary FILE-NAME for SYSTEM. Raise an error if not
85 (or (search-path (%bootstrap-binaries-path)
86 (string-append system "/" file-name))
90 (format #f (_ "could not find bootstrap binary '~a' \
92 file-name system)))))))
94 (define %distro-root-directory
95 ;; Absolute file name of the module hierarchy.
96 (dirname (search-path %load-path "guix.scm")))
98 (define %package-module-path
99 ;; Search path for package modules. Each item must be either a directory
100 ;; name or a pair whose car is a directory and whose cdr is a sub-directory
101 ;; to narrow the search.
102 (let* ((not-colon (char-set-complement (char-set #\:)))
103 (environment (string-tokenize (or (getenv "GUIX_PACKAGE_PATH") "")
105 ;; Automatically add items from $GUIX_PACKAGE_PATH to Guile's search path.
106 (for-each (lambda (directory)
107 (set! %load-path (cons directory %load-path))
108 (set! %load-compiled-path
109 (cons directory %load-compiled-path)))
113 (append environment `((,%distro-root-directory . "gnu/packages"))))))
116 ;; Define it after '%package-module-path' so that '%load-path' contains user
117 ;; directories, allowing patches in $GUIX_PACKAGE_PATH to be found.
119 (map (lambda (directory)
120 (if (string=? directory %distro-root-directory)
121 (string-append directory "/gnu/packages/patches")
125 (define* (scheme-files directory)
126 "Return the list of Scheme files found under DIRECTORY, recursively. The
127 returned list is sorted in alphabetical order."
129 ;; Sort entries so that 'fold-packages' works in a deterministic fashion
130 ;; regardless of details of the underlying file system.
131 (sort (file-system-fold (const #t) ; enter?
132 (lambda (path stat result) ; leaf
133 (if (string-suffix? ".scm" path)
136 (lambda (path stat result) ; down
138 (lambda (path stat result) ; up
141 (lambda (path stat errno result)
142 (warning (_ "cannot access `~a': ~a~%")
143 path (strerror errno))
150 (define file-name->module-name
151 (let ((not-slash (char-set-complement (char-set #\/))))
153 "Return the module name (a list of symbols) corresponding to FILE."
155 (string-tokenize (string-drop-right file 4) not-slash)))))
157 (define* (package-modules directory #:optional sub-directory)
158 "Return the list of modules that provide packages for the distribution.
159 Optionally, narrow the search to SUB-DIRECTORY."
161 (string-length directory))
163 (filter-map (lambda (file)
164 (let* ((file (substring file prefix-len))
165 (module (file-name->module-name file)))
168 (resolve-interface module))
170 ;; Report the error, but keep going.
171 (warn-about-load-error module args)
173 (scheme-files (if sub-directory
174 (string-append directory "/" sub-directory)
177 (define* (all-package-modules #:optional (path (%package-module-path)))
178 "Return the list of package modules found in PATH, a list of directories to
180 (fold-right (lambda (spec result)
182 ((? string? directory)
183 (append (package-modules directory) result))
184 ((directory . sub-directory)
185 (append (package-modules directory sub-directory)
190 (define (fold-packages proc init)
191 "Call (PROC PACKAGE RESULT) for each available package, using INIT as
192 the initial value of RESULT. It is guaranteed to never traverse the
194 (identity ; discard second return value
195 (fold2 (lambda (module result seen)
196 (fold2 (lambda (var result seen)
197 (if (and (package? var)
198 (not (vhash-assq var seen)))
199 (values (proc var result)
200 (vhash-consq var #t seen))
201 (values result seen)))
204 (module-map (lambda (sym var)
205 (false-if-exception (variable-ref var)))
209 (all-package-modules))))
211 (define find-packages-by-name
212 (let ((packages (delay
213 (fold-packages (lambda (p r)
214 (vhash-cons (package-name p) p r))
216 (version>? (lambda (p1 p2)
217 (version>? (package-version p1) (package-version p2)))))
218 (lambda* (name #:optional version)
219 "Return the list of packages with the given NAME. If VERSION is not #f,
220 then only return packages whose version is prefixed by VERSION, sorted in
221 decreasing version order."
222 (let ((matching (sort (vhash-fold* cons '() name (force packages))
225 (filter (lambda (package)
226 (string-prefix? version (package-version package)))
230 (define find-newest-available-packages
233 "Return a vhash keyed by package names, and with
234 associated values of the form
236 (newest-version newest-package ...)
238 where the preferred package is listed first."
240 ;; FIXME: Currently, the preferred package is whichever one
241 ;; was found last by 'fold-packages'. Find a better solution.
242 (fold-packages (lambda (p r)
243 (let ((name (package-name p))
244 (version (package-version p)))
245 (match (vhash-assoc name r)
246 ((_ newest-so-far . pkgs)
247 (case (version-compare version newest-so-far)
248 ((>) (vhash-cons name `(,version ,p) r))
249 ((=) (vhash-cons name `(,version ,p ,@pkgs) r))
251 (#f (vhash-cons name `(,version ,p) r)))))
254 (define (find-best-packages-by-name name version)
255 "If version is #f, return the list of packages named NAME with the highest
256 version numbers; otherwise, return the list of packages named NAME and at
259 (find-packages-by-name name version)
260 (match (vhash-assoc name (find-newest-available-packages))
261 ((_ version pkgs ...) pkgs)
265 (define* (vhash-refq vhash key #:optional (dflt #f))
266 "Look up KEY in the vhash VHASH, and return the value (if any) associated
267 with it. If KEY is not found, return DFLT (or `#f' if no DFLT argument is
268 supplied). Uses `eq?' for equality testing."
269 (or (and=> (vhash-assq key vhash) cdr)
272 (define package-dependencies
275 "Return a vhash keyed by package, and with associated values that are a
276 list of packages that depend on that package."
278 (lambda (package dag)
281 ;; Insert a graph edge from each of package's inputs to package.
283 (cons package (vhash-refq d in '()))
286 (match (package-direct-inputs package)
287 (((labels packages . _) ...)
291 (define (package-direct-dependents packages)
292 "Return a list of packages from the distribution that directly depend on the
293 packages in PACKAGES."
297 (vhash-refq (package-dependencies) p '()))
300 (define (package-transitive-dependents packages)
301 "Return the transitive dependent packages of the distribution packages in
302 PACKAGES---i.e. the dependents of those packages, plus their dependents,
304 (let ((dependency-dag (package-dependencies)))
307 (lambda (node) (vhash-refq dependency-dag node))
308 ;; Start with the dependents to avoid including PACKAGES in the result.
309 (package-direct-dependents packages))))
311 (define (package-covering-dependents packages)
312 "Return a minimal list of packages from the distribution whose dependencies
313 include all of PACKAGES and all packages that depend on PACKAGES."
314 (let ((dependency-dag (package-dependencies)))
317 (lambda (node) (vhash-refq dependency-dag node))
318 ;; Start with the dependents to avoid including PACKAGES in the result.
319 (package-direct-dependents packages))))
322 (define %sigint-prompt
323 ;; The prompt to jump to upon SIGINT.
324 (make-prompt-tag "interruptible"))
326 (define (call-with-sigint-handler thunk handler)
327 "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
328 number in the context of the continuation of the call to this function, and
329 return its return value."
330 (call-with-prompt %sigint-prompt
334 (sigaction SIGINT SIG_DFL)
335 (abort-to-prompt %sigint-prompt signum)))
339 (cut sigaction SIGINT SIG_DFL)))
343 (define-syntax-rule (waiting exp fmt rest ...)
344 "Display the given message while EXP is being evaluated."
345 (let* ((message (format #f fmt rest ...))
346 (blank (make-string (string-length message) #\space)))
347 (display message (current-error-port))
348 (force-output (current-error-port))
349 (call-with-sigint-handler
356 (display #\cr (current-error-port))
357 (display blank (current-error-port))
358 (display #\cr (current-error-port))
359 (force-output (current-error-port)))))
361 (format (current-error-port) " interrupted by signal ~a~%" SIGINT)
365 ;; Memoizing version of `ftp-open'. The goal is to avoid initiating a new
366 ;; FTP connection for each package, esp. since most of them are to the same
367 ;; server. This has a noticeable impact when doing "guix upgrade -u".
370 (define (check-package-freshness package)
371 "Check whether PACKAGE has a newer version available upstream, and report
373 ;; TODO: Automatically inject the upstream version when desired.
377 (when (false-if-exception (gnu-package? package))
378 (let ((name (package-name package))
379 (full-name (package-full-name package)))
380 (match (waiting (latest-release name
382 #:ftp-close (const #f))
383 (_ "looking for the latest release of GNU ~a...") name)
384 ((? gnu-release? release)
385 (let ((latest-version
386 (string-append (gnu-release-package release) "-"
387 (gnu-release-version release))))
388 (when (version>? latest-version full-name)
389 (format (current-error-port)
390 (_ "~a: note: using ~a \
391 but ~a is available upstream~%")
392 (location->string (package-location package))
393 full-name latest-version))))
396 ;; Silently ignore networking errors rather than preventing
399 ((getaddrinfo-error ftp-error) #f)
400 (else (apply throw key args))))))
402 (define (specification->package spec)
403 "Return a package matching SPEC. SPEC may be a package name, or a package
404 name followed by a hyphen and a version number. If the version number is not
405 present, return the preferred newest version."
406 (let-values (((name version)
407 (package-name->name+version spec)))
408 (match (find-best-packages-by-name name version)
411 ((p x ...) ; several matches
412 (warning (_ "ambiguous package specification `~a'~%") spec)
413 (warning (_ "choosing ~a from ~a~%")
414 (package-full-name p)
415 (location->string (package-location p)))
419 (leave (_ "~A: package not found for version ~a~%")
421 (leave (_ "~A: unknown package~%") name))))))
423 (define* (specification->package+output spec #:optional (output "out"))
424 "Return the package and output specified by SPEC, or #f and #f; SPEC may
425 optionally contain a version number and an output name, as in these examples:
432 If SPEC does not specify a version number, return the preferred newest
433 version; if SPEC does not specify an output, return OUTPUT."
434 (define (ensure-output p sub-drv)
435 (if (member sub-drv (package-outputs p))
437 (leave (_ "package `~a' lacks output `~a'~%")
438 (package-full-name p)
441 (let-values (((name version sub-drv)
442 (package-specification->name+version+output spec output)))
443 (match (find-best-packages-by-name name version)
445 (values p (ensure-output p sub-drv)))
447 (warning (_ "ambiguous package specification `~a'~%")
449 (warning (_ "choosing ~a from ~a~%")
450 (package-full-name p)
451 (location->string (package-location p)))
452 (values p (ensure-output p sub-drv)))
454 (leave (_ "~a: package not found~%") spec)))))