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))
58 ;;; General utilities for the software distribution---i.e., the modules under
59 ;;; (gnu packages ...).
63 ;; By default, we store patches and bootstrap binaries alongside Guile
64 ;; modules. This is so that these extra files can be found without
65 ;; requiring a special setup, such as a specific installation directory
66 ;; and an extra environment variable. One advantage of this setup is
67 ;; that everything just works in an auto-compilation setting.
69 (define %bootstrap-binaries-path
71 (map (cut string-append <> "/gnu/packages/bootstrap")
74 (define (search-patch file-name)
75 "Search the patch FILE-NAME. Raise an error if not found."
76 (or (search-path (%patch-path) file-name)
78 (&message (message (format #f (_ "~a: patch not found")
81 (define (search-bootstrap-binary file-name system)
82 "Search the bootstrap binary FILE-NAME for SYSTEM. Raise an error if not
84 (or (search-path (%bootstrap-binaries-path)
85 (string-append system "/" file-name))
89 (format #f (_ "could not find bootstrap binary '~a' \
91 file-name system)))))))
93 (define %distro-root-directory
94 ;; Absolute file name of the module hierarchy.
95 (dirname (search-path %load-path "guix.scm")))
97 (define %package-module-path
98 ;; Search path for package modules. Each item must be either a directory
99 ;; name or a pair whose car is a directory and whose cdr is a sub-directory
100 ;; to narrow the search.
101 (let* ((not-colon (char-set-complement (char-set #\:)))
102 (environment (string-tokenize (or (getenv "GUIX_PACKAGE_PATH") "")
104 ;; Automatically add items from $GUIX_PACKAGE_PATH to Guile's search path.
105 (for-each (lambda (directory)
106 (set! %load-path (cons directory %load-path))
107 (set! %load-compiled-path
108 (cons directory %load-compiled-path)))
112 (append environment `((,%distro-root-directory . "gnu/packages"))))))
115 ;; Define it after '%package-module-path' so that '%load-path' contains user
116 ;; directories, allowing patches in $GUIX_PACKAGE_PATH to be found.
118 (map (lambda (directory)
119 (if (string=? directory %distro-root-directory)
120 (string-append directory "/gnu/packages/patches")
124 (define* (scheme-files directory)
125 "Return the list of Scheme files found under DIRECTORY, recursively. The
126 returned list is sorted in alphabetical order."
128 ;; Sort entries so that 'fold-packages' works in a deterministic fashion
129 ;; regardless of details of the underlying file system.
130 (sort (file-system-fold (const #t) ; enter?
131 (lambda (path stat result) ; leaf
132 (if (string-suffix? ".scm" path)
135 (lambda (path stat result) ; down
137 (lambda (path stat result) ; up
140 (lambda (path stat errno result)
141 (warning (_ "cannot access `~a': ~a~%")
142 path (strerror errno))
149 (define file-name->module-name
150 (let ((not-slash (char-set-complement (char-set #\/))))
152 "Return the module name (a list of symbols) corresponding to FILE."
154 (string-tokenize (string-drop-right file 4) not-slash)))))
156 (define* (package-modules directory #:optional sub-directory)
157 "Return the list of modules that provide packages for the distribution.
158 Optionally, narrow the search to SUB-DIRECTORY."
160 (string-length directory))
162 (filter-map (lambda (file)
163 (let ((file (substring file prefix-len)))
165 (resolve-interface (file-name->module-name file)))))
166 (scheme-files (if sub-directory
167 (string-append directory "/" sub-directory)
170 (define* (all-package-modules #:optional (path (%package-module-path)))
171 "Return the list of package modules found in PATH, a list of directories to
173 (fold-right (lambda (spec result)
175 ((? string? directory)
176 (append (package-modules directory) result))
177 ((directory . sub-directory)
178 (append (package-modules directory sub-directory)
183 (define (fold-packages proc init)
184 "Call (PROC PACKAGE RESULT) for each available package, using INIT as
185 the initial value of RESULT. It is guaranteed to never traverse the
187 (identity ; discard second return value
188 (fold2 (lambda (module result seen)
189 (fold2 (lambda (var result seen)
190 (if (and (package? var)
191 (not (vhash-assq var seen)))
192 (values (proc var result)
193 (vhash-consq var #t seen))
194 (values result seen)))
197 (module-map (lambda (sym var)
198 (false-if-exception (variable-ref var)))
202 (all-package-modules))))
204 (define find-packages-by-name
205 (let ((packages (delay
206 (fold-packages (lambda (p r)
207 (vhash-cons (package-name p) p r))
209 (lambda* (name #:optional version)
210 "Return the list of packages with the given NAME. If VERSION is not #f,
211 then only return packages whose version is equal to VERSION."
212 (let ((matching (vhash-fold* cons '() name (force packages))))
214 (filter (lambda (package)
215 (string=? (package-version package) version))
219 (define find-newest-available-packages
222 "Return a vhash keyed by package names, and with
223 associated values of the form
225 (newest-version newest-package ...)
227 where the preferred package is listed first."
229 ;; FIXME: Currently, the preferred package is whichever one
230 ;; was found last by 'fold-packages'. Find a better solution.
231 (fold-packages (lambda (p r)
232 (let ((name (package-name p))
233 (version (package-version p)))
234 (match (vhash-assoc name r)
235 ((_ newest-so-far . pkgs)
236 (case (version-compare version newest-so-far)
237 ((>) (vhash-cons name `(,version ,p) r))
238 ((=) (vhash-cons name `(,version ,p ,@pkgs) r))
240 (#f (vhash-cons name `(,version ,p) r)))))
243 (define (find-best-packages-by-name name version)
244 "If version is #f, return the list of packages named NAME with the highest
245 version numbers; otherwise, return the list of packages named NAME and at
248 (find-packages-by-name name version)
249 (match (vhash-assoc name (find-newest-available-packages))
250 ((_ version pkgs ...) pkgs)
254 (define* (vhash-refq vhash key #:optional (dflt #f))
255 "Look up KEY in the vhash VHASH, and return the value (if any) associated
256 with it. If KEY is not found, return DFLT (or `#f' if no DFLT argument is
257 supplied). Uses `eq?' for equality testing."
258 (or (and=> (vhash-assq key vhash) cdr)
261 (define package-dependencies
264 "Return a vhash keyed by package, and with associated values that are a
265 list of packages that depend on that package."
267 (lambda (package dag)
270 ;; Insert a graph edge from each of package's inputs to package.
272 (cons package (vhash-refq d in '()))
275 (match (package-direct-inputs package)
276 (((labels packages . _) ...)
280 (define (package-direct-dependents packages)
281 "Return a list of packages from the distribution that directly depend on the
282 packages in PACKAGES."
286 (vhash-refq (package-dependencies) p '()))
289 (define (package-transitive-dependents packages)
290 "Return the transitive dependent packages of the distribution packages in
291 PACKAGES---i.e. the dependents of those packages, plus their dependents,
293 (let ((dependency-dag (package-dependencies)))
296 (lambda (node) (vhash-refq dependency-dag node))
297 ;; Start with the dependents to avoid including PACKAGES in the result.
298 (package-direct-dependents packages))))
300 (define (package-covering-dependents packages)
301 "Return a minimal list of packages from the distribution whose dependencies
302 include all of PACKAGES and all packages that depend on PACKAGES."
303 (let ((dependency-dag (package-dependencies)))
306 (lambda (node) (vhash-refq dependency-dag node))
307 ;; Start with the dependents to avoid including PACKAGES in the result.
308 (package-direct-dependents packages))))
311 (define %sigint-prompt
312 ;; The prompt to jump to upon SIGINT.
313 (make-prompt-tag "interruptible"))
315 (define (call-with-sigint-handler thunk handler)
316 "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
317 number in the context of the continuation of the call to this function, and
318 return its return value."
319 (call-with-prompt %sigint-prompt
323 (sigaction SIGINT SIG_DFL)
324 (abort-to-prompt %sigint-prompt signum)))
328 (cut sigaction SIGINT SIG_DFL)))
332 (define-syntax-rule (waiting exp fmt rest ...)
333 "Display the given message while EXP is being evaluated."
334 (let* ((message (format #f fmt rest ...))
335 (blank (make-string (string-length message) #\space)))
336 (display message (current-error-port))
337 (force-output (current-error-port))
338 (call-with-sigint-handler
345 (display #\cr (current-error-port))
346 (display blank (current-error-port))
347 (display #\cr (current-error-port))
348 (force-output (current-error-port)))))
350 (format (current-error-port) " interrupted by signal ~a~%" SIGINT)
354 ;; Memoizing version of `ftp-open'. The goal is to avoid initiating a new
355 ;; FTP connection for each package, esp. since most of them are to the same
356 ;; server. This has a noticeable impact when doing "guix upgrade -u".
359 (define (check-package-freshness package)
360 "Check whether PACKAGE has a newer version available upstream, and report
362 ;; TODO: Automatically inject the upstream version when desired.
366 (when (false-if-exception (gnu-package? package))
367 (let ((name (package-name package))
368 (full-name (package-full-name package)))
369 (match (waiting (latest-release name
371 #:ftp-close (const #f))
372 (_ "looking for the latest release of GNU ~a...") name)
373 ((? gnu-release? release)
374 (let ((latest-version
375 (string-append (gnu-release-package release) "-"
376 (gnu-release-version release))))
377 (when (version>? latest-version full-name)
378 (format (current-error-port)
379 (_ "~a: note: using ~a \
380 but ~a is available upstream~%")
381 (location->string (package-location package))
382 full-name latest-version))))
385 ;; Silently ignore networking errors rather than preventing
388 ((getaddrinfo-error ftp-error) #f)
389 (else (apply throw key args))))))
391 (define (specification->package spec)
392 "Return a package matching SPEC. SPEC may be a package name, or a package
393 name followed by a hyphen and a version number. If the version number is not
394 present, return the preferred newest version."
395 (let-values (((name version)
396 (package-name->name+version spec)))
397 (match (find-best-packages-by-name name version)
400 ((p x ...) ; several matches
401 (warning (_ "ambiguous package specification `~a'~%") spec)
402 (warning (_ "choosing ~a from ~a~%")
403 (package-full-name p)
404 (location->string (package-location p)))
408 (leave (_ "~A: package not found for version ~a~%")
410 (leave (_ "~A: unknown package~%") name))))))