build: Add iso9660 system image generator.
[jackhill/guix/guix.git] / gnu / packages.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
f9704f17 2;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
c2868b1e 3;;; Copyright © 2013 Mark H Weaver <mhw@netris.org>
7d193ec3 4;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
96eaa55f 5;;; Copyright © 2016, 2017 Alex Kost <alezost@gmail.com>
fad155d4 6;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
6b1891b0 7;;;
233e7676 8;;; This file is part of GNU Guix.
6b1891b0 9;;;
233e7676 10;;; GNU Guix is free software; you can redistribute it and/or modify it
6b1891b0
LC
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;;;
233e7676 15;;; GNU Guix is distributed in the hope that it will be useful, but
6b1891b0
LC
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
233e7676 21;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
6b1891b0 22
59a43334 23(define-module (gnu packages)
6b1891b0 24 #:use-module (guix packages)
98eb8cbe 25 #:use-module (guix ui)
800cdeef 26 #:use-module (guix utils)
cd903ef7 27 #:use-module (guix discovery)
f9704f17 28 #:use-module (guix memoization)
95cd4971
LC
29 #:use-module ((guix build utils)
30 #:select ((package-name->name+version
31 . hyphen-separated-name->name+version)))
c08ea55e 32 #:autoload (guix profiles) (packages->manifest)
c2868b1e 33 #:use-module (ice-9 vlist)
dc5669cd 34 #:use-module (ice-9 match)
6b1891b0 35 #:use-module (srfi srfi-1)
5e3b388b 36 #:use-module (srfi srfi-11)
6b1891b0 37 #:use-module (srfi srfi-26)
dbab5150
LC
38 #:use-module (srfi srfi-34)
39 #:use-module (srfi srfi-35)
800cdeef
LC
40 #:use-module (srfi srfi-39)
41 #:export (search-patch
25897079 42 search-patches
96eaa55f 43 search-auxiliary-file
ac5aa288 44 search-bootstrap-binary
0492f4a2 45 %patch-path
96eaa55f 46 %auxiliary-files-path
0b3651bc 47 %bootstrap-binaries-path
c107b541 48 %package-module-path
7d193ec3 49
ba326ce4 50 fold-packages
7d193ec3 51
dc5669cd 52 find-packages-by-name
3f26bfc1 53 find-best-packages-by-name
7d193ec3
EB
54 find-newest-available-packages
55
84189ebc 56 specification->package
c08ea55e
LC
57 specification->package+output
58 specifications->manifest))
6b1891b0
LC
59
60;;; Commentary:
61;;;
62;;; General utilities for the software distribution---i.e., the modules under
59a43334 63;;; (gnu packages ...).
6b1891b0
LC
64;;;
65;;; Code:
66
96eaa55f
AK
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.
a9f60c42 73
a9f60c42 74(define %bootstrap-binaries-path
ac5aa288 75 (make-parameter
1ffa7090 76 (map (cut string-append <> "/gnu/packages/bootstrap")
0b3651bc 77 %load-path)))
ac5aa288 78
96eaa55f
AK
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
800cdeef 88(define (search-patch file-name)
dbab5150
LC
89 "Search the patch FILE-NAME. Raise an error if not found."
90 (or (search-path (%patch-path) file-name)
91 (raise (condition
69daee23 92 (&message (message (format #f (G_ "~a: patch not found")
dbab5150 93 file-name)))))))
800cdeef 94
25897079
AK
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
ac5aa288 100(define (search-bootstrap-binary file-name system)
dfba5489
LC
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
69daee23 108 (format #f (G_ "could not find bootstrap binary '~a' \
dfba5489
LC
109for system '~a'")
110 file-name system)))))))
ac5aa288 111
84836a57
LC
112(define %distro-root-directory
113 ;; Absolute file name of the module hierarchy.
114 (dirname (search-path %load-path "guix.scm")))
6b1891b0 115
c107b541
LC
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.
8689901f
LC
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"))))))
c107b541 132
ee06af5b
LC
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
ba326ce4
LC
143(define (fold-packages proc init)
144 "Call (PROC PACKAGE RESULT) for each available package, using INIT as
c2868b1e
MW
145the initial value of RESULT. It is guaranteed to never traverse the
146same package twice."
cd903ef7
LC
147 (fold-module-public-variables (lambda (object result)
148 (if (and (package? object)
149 (not (hidden-package? object)))
150 (proc object result)
151 result))
152 init
153 (all-modules (%package-module-path))))
ba326ce4 154
9ffc1c00
LC
155(define find-packages-by-name
156 (let ((packages (delay
157 (fold-packages (lambda (p r)
158 (vhash-cons (package-name p) p r))
724311a2
LC
159 vlist-null)))
160 (version>? (lambda (p1 p2)
161 (version>? (package-version p1) (package-version p2)))))
9ffc1c00
LC
162 (lambda* (name #:optional version)
163 "Return the list of packages with the given NAME. If VERSION is not #f,
724311a2
LC
164then only return packages whose version is prefixed by VERSION, sorted in
165decreasing version order."
166 (let ((matching (sort (vhash-fold* cons '() name (force packages))
167 version>?)))
9ffc1c00
LC
168 (if version
169 (filter (lambda (package)
724311a2 170 (string-prefix? version (package-version package)))
9ffc1c00
LC
171 matching)
172 matching)))))
dc5669cd 173
3f26bfc1 174(define find-newest-available-packages
55b2d921
LC
175 (mlambda ()
176 "Return a vhash keyed by package names, and with
dc5669cd
MW
177associated values of the form
178
179 (newest-version newest-package ...)
180
181where the preferred package is listed first."
182
55b2d921
LC
183 ;; FIXME: Currently, the preferred package is whichever one
184 ;; was found last by 'fold-packages'. Find a better solution.
185 (fold-packages (lambda (p r)
186 (let ((name (package-name p))
187 (version (package-version p)))
188 (match (vhash-assoc name r)
189 ((_ newest-so-far . pkgs)
190 (case (version-compare version newest-so-far)
191 ((>) (vhash-cons name `(,version ,p) r))
192 ((=) (vhash-cons name `(,version ,p ,@pkgs) r))
193 ((<) r)))
194 (#f (vhash-cons name `(,version ,p) r)))))
195 vlist-null)))
3f26bfc1
LC
196
197(define (find-best-packages-by-name name version)
198 "If version is #f, return the list of packages named NAME with the highest
199version numbers; otherwise, return the list of packages named NAME and at
200VERSION."
201 (if version
202 (find-packages-by-name name version)
203 (match (vhash-assoc name (find-newest-available-packages))
204 ((_ version pkgs ...) pkgs)
205 (#f '()))))
7d193ec3
EB
206
207\f
4ea44419
AK
208(define %sigint-prompt
209 ;; The prompt to jump to upon SIGINT.
210 (make-prompt-tag "interruptible"))
211
212(define (call-with-sigint-handler thunk handler)
213 "Call THUNK and return its value. Upon SIGINT, call HANDLER with the signal
214number in the context of the continuation of the call to this function, and
215return its return value."
216 (call-with-prompt %sigint-prompt
217 (lambda ()
218 (sigaction SIGINT
219 (lambda (signum)
220 (sigaction SIGINT SIG_DFL)
221 (abort-to-prompt %sigint-prompt signum)))
222 (dynamic-wind
223 (const #t)
224 thunk
225 (cut sigaction SIGINT SIG_DFL)))
226 (lambda (k signum)
227 (handler signum))))
228
fad155d4
ML
229\f
230;;;
231;;; Package specification.
232;;;
233
e30c2be1 234(define* (%find-package spec name version)
fad155d4
ML
235 (match (find-best-packages-by-name name version)
236 ((pkg . pkg*)
237 (unless (null? pkg*)
69daee23
LC
238 (warning (G_ "ambiguous package specification `~a'~%") spec)
239 (warning (G_ "choosing ~a@~a from ~a~%")
d75e8f36 240 (package-name pkg) (package-version pkg)
fad155d4 241 (location->string (package-location pkg))))
01afdab8
LC
242 (match (package-superseded pkg)
243 ((? package? new)
69daee23 244 (info (G_ "package '~a' has been superseded by '~a'~%")
01afdab8
LC
245 (package-name pkg) (package-name new))
246 new)
247 (#f
248 pkg)))
e465d9e1 249 (x
fad155d4 250 (if version
69daee23
LC
251 (leave (G_ "~A: package not found for version ~a~%") name version)
252 (leave (G_ "~A: unknown package~%") name)))))
fad155d4 253
5e3b388b
CR
254(define (specification->package spec)
255 "Return a package matching SPEC. SPEC may be a package name, or a package
1b846da8 256name followed by an at-sign and a version number. If the version number is not
5e3b388b 257present, return the preferred newest version."
fad155d4
ML
258 (let-values (((name version) (package-name->name+version spec)))
259 (%find-package spec name version)))
84189ebc
LC
260
261(define* (specification->package+output spec #:optional (output "out"))
262 "Return the package and output specified by SPEC, or #f and #f; SPEC may
263optionally contain a version number and an output name, as in these examples:
264
265 guile
1b846da8 266 guile@2.0.9
84189ebc 267 guile:debug
1b846da8 268 guile@2.0.9:debug
84189ebc
LC
269
270If SPEC does not specify a version number, return the preferred newest
271version; if SPEC does not specify an output, return OUTPUT."
84189ebc
LC
272 (let-values (((name version sub-drv)
273 (package-specification->name+version+output spec output)))
fad155d4
ML
274 (match (%find-package spec name version)
275 (#f
276 (values #f #f))
277 (package
278 (if (member sub-drv (package-outputs package))
279 (values package sub-drv)
69daee23 280 (leave (G_ "package `~a' lacks output `~a'~%")
fad155d4
ML
281 (package-full-name package)
282 sub-drv))))))
c08ea55e
LC
283
284(define (specifications->manifest specs)
285 "Given SPECS, a list of specifications such as \"emacs@25.2\" or
286\"guile:debug\", return a profile manifest."
287 ;; This procedure exists mostly so users of 'guix package -m' don't have to
288 ;; fiddle with multiple-value returns.
289 (packages->manifest
290 (map (compose list specification->package+output) specs)))