gnu: Add audacity.
[guix.git] / gnu / packages.scm
blob57a3e21bd6114ea700c1711071e9094d7c7ad1e6
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>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
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.
12 ;;;
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.
17 ;;;
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
38             %patch-directory
39             %bootstrap-binaries-path
40             %package-module-path
42             fold-packages
44             find-packages-by-name
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))
56 ;;; Commentary:
57 ;;;
58 ;;; General utilities for the software distribution---i.e., the modules under
59 ;;; (gnu packages ...).
60 ;;;
61 ;;; Code:
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
70   (make-parameter
71    (map (cut string-append <> "/gnu/packages/bootstrap")
72         %load-path)))
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)
77       (raise (condition
78               (&message (message (format #f (_ "~a: patch not found")
79                                          file-name)))))))
81 (define (search-bootstrap-binary file-name system)
82   "Search the bootstrap binary FILE-NAME for SYSTEM.  Raise an error if not
83 found."
84   (or (search-path (%bootstrap-binaries-path)
85                    (string-append system "/" file-name))
86       (raise (condition
87               (&message
88                (message
89                 (format #f (_ "could not find bootstrap binary '~a' \
90 for system '~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") "")
103                                        not-colon)))
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)))
109               environment)
111     (make-parameter
112      (append environment `((,%distro-root-directory . "gnu/packages"))))))
114 (define %patch-path
115   ;; Define it after '%package-module-path' so that '%load-path' contains user
116   ;; directories, allowing patches in $GUIX_PACKAGE_PATH to be found.
117   (make-parameter
118    (map (lambda (directory)
119           (if (string=? directory %distro-root-directory)
120               (string-append directory "/gnu/packages/patches")
121               directory))
122         %load-path)))
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)
133                                 (cons path result)
134                                 result))
135                           (lambda (path stat result)   ; down
136                             result)
137                           (lambda (path stat result)   ; up
138                             result)
139                           (const #f)                   ; skip
140                           (lambda (path stat errno result)
141                             (warning (_ "cannot access `~a': ~a~%")
142                                      path (strerror errno))
143                             result)
144                           '()
145                           directory
146                           stat)
147         string<?))
149 (define file-name->module-name
150   (let ((not-slash (char-set-complement (char-set #\/))))
151     (lambda (file)
152       "Return the module name (a list of symbols) corresponding to FILE."
153       (map string->symbol
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."
159   (define prefix-len
160     (string-length directory))
162   (filter-map (lambda (file)
163                 (let ((file (substring file prefix-len)))
164                   (false-if-exception
165                    (resolve-interface (file-name->module-name file)))))
166               (scheme-files (if sub-directory
167                                 (string-append directory "/" sub-directory)
168                                 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
172 search."
173   (fold-right (lambda (spec result)
174                 (match spec
175                   ((? string? directory)
176                    (append (package-modules directory) result))
177                   ((directory . sub-directory)
178                    (append (package-modules directory sub-directory)
179                            result))))
180               '()
181               path))
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
186 same package twice."
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)))
195                    result
196                    seen
197                    (module-map (lambda (sym var)
198                                  (false-if-exception (variable-ref var)))
199                                module)))
200           init
201           vlist-null
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))
208                                    vlist-null))))
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))))
213         (if version
214             (filter (lambda (package)
215                       (string=? (package-version package) version))
216                     matching)
217             matching)))))
219 (define find-newest-available-packages
220   (memoize
221    (lambda ()
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))
239                              ((<) r)))
240                           (#f (vhash-cons name `(,version ,p) r)))))
241                     vlist-null))))
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
246 VERSION."
247   (if version
248       (find-packages-by-name name version)
249       (match (vhash-assoc name (find-newest-available-packages))
250         ((_ version pkgs ...) pkgs)
251         (#f '()))))
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)
259       dflt))
261 (define package-dependencies
262   (memoize
263    (lambda ()
264      "Return a vhash keyed by package, and with associated values that are a
265 list of packages that depend on that package."
266      (fold-packages
267       (lambda (package dag)
268         (fold
269          (lambda (in d)
270            ;; Insert a graph edge from each of package's inputs to package.
271            (vhash-consq in
272                         (cons package (vhash-refq d in '()))
273                         (vhash-delq in d)))
274          dag
275          (match (package-direct-inputs package)
276            (((labels packages . _) ...)
277             packages) )))
278       vlist-null))))
280 (define (package-direct-dependents packages)
281   "Return a list of packages from the distribution that directly depend on the
282 packages in PACKAGES."
283   (delete-duplicates
284    (concatenate
285     (map (lambda (p)
286            (vhash-refq (package-dependencies) p '()))
287          packages))))
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,
292 recursively."
293   (let ((dependency-dag (package-dependencies)))
294     (fold-tree
295      cons '()
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)))
304     (fold-tree-leaves
305      cons '()
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
320                     (lambda ()
321                       (sigaction SIGINT
322                         (lambda (signum)
323                           (sigaction SIGINT SIG_DFL)
324                           (abort-to-prompt %sigint-prompt signum)))
325                       (dynamic-wind
326                         (const #t)
327                         thunk
328                         (cut sigaction SIGINT SIG_DFL)))
329                     (lambda (k signum)
330                       (handler signum))))
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
339      (lambda ()
340        (dynamic-wind
341          (const #f)
342          (lambda () exp)
343          (lambda ()
344            ;; Clear the line.
345            (display #\cr (current-error-port))
346            (display blank (current-error-port))
347            (display #\cr (current-error-port))
348            (force-output (current-error-port)))))
349      (lambda (signum)
350        (format (current-error-port) "  interrupted by signal ~a~%" SIGINT)
351        #f))))
353 (define ftp-open*
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".
357   (memoize ftp-open))
359 (define (check-package-freshness package)
360   "Check whether PACKAGE has a newer version available upstream, and report
361 it."
362   ;; TODO: Automatically inject the upstream version when desired.
364   (catch #t
365     (lambda ()
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
370                                           #:ftp-open ftp-open*
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))))
383             (_ #t)))))
384     (lambda (key . args)
385       ;; Silently ignore networking errors rather than preventing
386       ;; installation.
387       (case key
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)
398       ((p)                                      ; one match
399        p)
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)))
405        p)
406       (_                                        ; no matches
407        (if version
408            (leave (_ "~A: package not found for version ~a~%")
409                   name version)
410            (leave (_ "~A: unknown package~%") name))))))