Commit | Line | Data |
---|---|---|
233e7676 | 1 | ;;; GNU Guix --- Functional package management for GNU |
aba326f7 | 2 | ;;; Copyright © 2012, 2013 Ludovic Courtès <ludo@gnu.org> |
e3ce5d70 | 3 | ;;; |
233e7676 | 4 | ;;; This file is part of GNU Guix. |
e3ce5d70 | 5 | ;;; |
233e7676 | 6 | ;;; GNU Guix is free software; you can redistribute it and/or modify it |
e3ce5d70 LC |
7 | ;;; under the terms of the GNU General Public License as published by |
8 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
9 | ;;; your option) any later version. | |
10 | ;;; | |
233e7676 | 11 | ;;; GNU Guix is distributed in the hope that it will be useful, but |
e3ce5d70 LC |
12 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ;;; GNU General Public License for more details. | |
15 | ;;; | |
16 | ;;; You should have received a copy of the GNU General Public License | |
233e7676 | 17 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. |
e3ce5d70 LC |
18 | |
19 | (define-module (guix packages) | |
20 | #:use-module (guix utils) | |
c0cd1b3e | 21 | #:use-module (guix records) |
e3ce5d70 | 22 | #:use-module (guix store) |
ddc29a78 | 23 | #:use-module (guix base32) |
d510ab46 | 24 | #:use-module (guix derivations) |
e3ce5d70 LC |
25 | #:use-module (guix build-system) |
26 | #:use-module (ice-9 match) | |
062c6927 | 27 | #:use-module (srfi srfi-1) |
946b72c9 | 28 | #:use-module (srfi srfi-9 gnu) |
d510ab46 | 29 | #:use-module (srfi srfi-11) |
d36622dc LC |
30 | #:use-module (srfi srfi-34) |
31 | #:use-module (srfi srfi-35) | |
cab24961 | 32 | #:re-export (%current-system) |
ff352cfb | 33 | #:export (origin |
90c68be8 LC |
34 | origin? |
35 | origin-uri | |
36 | origin-method | |
37 | origin-sha256 | |
38 | origin-file-name | |
e4c245f8 | 39 | base32 |
e3ce5d70 | 40 | |
a18eda27 LC |
41 | <search-path-specification> |
42 | search-path-specification | |
43 | search-path-specification? | |
44 | search-path-specification->sexp | |
45 | ||
e3ce5d70 LC |
46 | package |
47 | package? | |
48 | package-name | |
49 | package-version | |
2847050a | 50 | package-full-name |
e3ce5d70 LC |
51 | package-source |
52 | package-build-system | |
53 | package-arguments | |
54 | package-inputs | |
55 | package-native-inputs | |
062c6927 | 56 | package-propagated-inputs |
e3ce5d70 | 57 | package-outputs |
a18eda27 | 58 | package-native-search-paths |
e3ce5d70 | 59 | package-search-paths |
d45122f5 | 60 | package-synopsis |
e3ce5d70 | 61 | package-description |
e3ce5d70 | 62 | package-license |
52bda18a | 63 | package-home-page |
e3ce5d70 LC |
64 | package-platforms |
65 | package-maintainers | |
062c6927 | 66 | package-properties |
35f3c5f5 | 67 | package-location |
d66c7096 | 68 | package-field-location |
e3ce5d70 | 69 | |
a3d73f59 | 70 | package-transitive-inputs |
113aef68 | 71 | package-transitive-propagated-inputs |
e3ce5d70 LC |
72 | package-source-derivation |
73 | package-derivation | |
d36622dc | 74 | package-cross-derivation |
d510ab46 | 75 | package-output |
d36622dc LC |
76 | |
77 | &package-error | |
07783858 | 78 | package-error? |
d36622dc LC |
79 | package-error-package |
80 | &package-input-error | |
07783858 | 81 | package-input-error? |
d36622dc | 82 | package-error-invalid-input)) |
e3ce5d70 LC |
83 | |
84 | ;;; Commentary: | |
85 | ;;; | |
86 | ;;; This module provides a high-level mechanism to define packages in a | |
87 | ;;; Guix-based distribution. | |
88 | ;;; | |
89 | ;;; Code: | |
90 | ||
90c68be8 LC |
91 | ;; The source of a package, such as a tarball URL and fetcher---called |
92 | ;; "origin" to avoid name clash with `package-source', `source', etc. | |
93 | (define-record-type* <origin> | |
94 | origin make-origin | |
95 | origin? | |
96 | (uri origin-uri) ; string | |
97 | (method origin-method) ; symbol | |
98 | (sha256 origin-sha256) ; bytevector | |
99 | (file-name origin-file-name (default #f))) ; optional file name | |
e3ce5d70 | 100 | |
e4c245f8 LC |
101 | (define-syntax base32 |
102 | (lambda (s) | |
103 | "Return the bytevector corresponding to the given Nix-base32 | |
104 | representation." | |
105 | (syntax-case s () | |
106 | ((_ str) | |
107 | (string? (syntax->datum #'str)) | |
aba326f7 | 108 | ;; A literal string: do the conversion at expansion time. |
e4c245f8 LC |
109 | (with-syntax ((bv (nix-base32-string->bytevector |
110 | (syntax->datum #'str)))) | |
aba326f7 LC |
111 | #''bv)) |
112 | ((_ str) | |
113 | #'(nix-base32-string->bytevector str))))) | |
e4c245f8 | 114 | |
a18eda27 LC |
115 | ;; The specification of a search path. |
116 | (define-record-type* <search-path-specification> | |
117 | search-path-specification make-search-path-specification | |
118 | search-path-specification? | |
119 | (variable search-path-specification-variable) | |
120 | (directories search-path-specification-directories) | |
121 | (separator search-path-specification-separator (default ":"))) | |
122 | ||
123 | (define (search-path-specification->sexp spec) | |
124 | "Return an sexp representing SPEC, a <search-path-specification>. The sexp | |
125 | corresponds to the arguments expected by `set-path-environment-variable'." | |
126 | (match spec | |
127 | (($ <search-path-specification> variable directories separator) | |
128 | `(,variable ,directories ,separator)))) | |
d36622dc | 129 | |
a18eda27 | 130 | ;; A package. |
e3ce5d70 LC |
131 | (define-record-type* <package> |
132 | package make-package | |
133 | package? | |
134 | (name package-name) ; string | |
135 | (version package-version) ; string | |
90c68be8 | 136 | (source package-source) ; <origin> instance |
e3ce5d70 | 137 | (build-system package-build-system) ; build system |
64fddd74 | 138 | (arguments package-arguments ; arguments for the build method |
21c203a5 | 139 | (default '()) (thunked)) |
062c6927 | 140 | |
e3ce5d70 | 141 | (inputs package-inputs ; input packages or derivations |
dd6b9a37 | 142 | (default '()) (thunked)) |
062c6927 LC |
143 | (propagated-inputs package-propagated-inputs ; same, but propagated |
144 | (default '())) | |
e3ce5d70 LC |
145 | (native-inputs package-native-inputs ; native input packages/derivations |
146 | (default '())) | |
c9d01150 LC |
147 | (self-native-input? package-self-native-input? ; whether to use itself as |
148 | ; a native input when cross- | |
149 | (default #f)) ; compiling | |
062c6927 | 150 | |
e3ce5d70 LC |
151 | (outputs package-outputs ; list of strings |
152 | (default '("out"))) | |
a18eda27 LC |
153 | |
154 | ; lists of | |
155 | ; <search-path-specification>, | |
156 | ; for native and cross | |
157 | ; inputs | |
158 | (native-search-paths package-native-search-paths (default '())) | |
159 | (search-paths package-search-paths (default '())) | |
e3ce5d70 | 160 | |
d45122f5 LC |
161 | (synopsis package-synopsis) ; one-line description |
162 | (description package-description) ; one or two paragraphs | |
1fb78cb2 | 163 | (license package-license) |
45753b65 | 164 | (home-page package-home-page) |
e3ce5d70 | 165 | (platforms package-platforms (default '())) |
35f3c5f5 | 166 | (maintainers package-maintainers (default '())) |
45753b65 | 167 | |
062c6927 LC |
168 | (properties package-properties (default '())) ; alist for anything else |
169 | ||
35f3c5f5 LC |
170 | (location package-location |
171 | (default (and=> (current-source-location) | |
172 | source-properties->location)))) | |
e3ce5d70 | 173 | |
946b72c9 LC |
174 | (set-record-type-printer! <package> |
175 | (lambda (package port) | |
176 | (let ((loc (package-location package)) | |
177 | (format simple-format)) | |
178 | (format port "#<package ~a-~a ~a:~a ~a>" | |
179 | (package-name package) | |
180 | (package-version package) | |
181 | (location-file loc) | |
182 | (location-line loc) | |
183 | (number->string (object-address | |
184 | package) | |
185 | 16))))) | |
186 | ||
d66c7096 | 187 | (define (package-field-location package field) |
f903dc05 LC |
188 | "Return the source code location of the definition of FIELD for PACKAGE, or |
189 | #f if it could not be determined." | |
190 | (define (goto port line column) | |
191 | (unless (and (= (port-column port) (- column 1)) | |
192 | (= (port-line port) (- line 1))) | |
193 | (unless (eof-object? (read-char port)) | |
194 | (goto port line column)))) | |
d66c7096 LC |
195 | |
196 | (match (package-location package) | |
197 | (($ <location> file line column) | |
198 | (catch 'system | |
199 | (lambda () | |
200 | (call-with-input-file (search-path %load-path file) | |
201 | (lambda (port) | |
f903dc05 LC |
202 | (goto port line column) |
203 | (match (read port) | |
204 | (('package inits ...) | |
205 | (let ((field (assoc field inits))) | |
206 | (match field | |
207 | ((_ value) | |
8e77f41e LC |
208 | ;; Put the `or' here, and not in the first argument of |
209 | ;; `and=>', to work around a compiler bug in 2.0.5. | |
210 | (or (and=> (source-properties value) | |
211 | source-properties->location) | |
212 | (and=> (source-properties field) | |
213 | source-properties->location))) | |
f903dc05 LC |
214 | (_ |
215 | #f)))) | |
216 | (_ | |
217 | #f))))) | |
d66c7096 | 218 | (lambda _ |
f903dc05 | 219 | #f))) |
d66c7096 LC |
220 | (_ #f))) |
221 | ||
d36622dc LC |
222 | |
223 | ;; Error conditions. | |
224 | ||
225 | (define-condition-type &package-error &error | |
226 | package-error? | |
227 | (package package-error-package)) | |
228 | ||
229 | (define-condition-type &package-input-error &package-error | |
230 | package-input-error? | |
231 | (input package-error-invalid-input)) | |
232 | ||
233 | ||
2847050a LC |
234 | (define (package-full-name package) |
235 | "Return the full name of PACKAGE--i.e., `NAME-VERSION'." | |
236 | (string-append (package-name package) "-" (package-version package))) | |
237 | ||
b642e4b8 LC |
238 | (define* (package-source-derivation store source |
239 | #:optional (system (%current-system))) | |
240 | "Return the derivation path for SOURCE, a package source, for SYSTEM." | |
e3ce5d70 | 241 | (match source |
90c68be8 | 242 | (($ <origin> uri method sha256 name) |
b642e4b8 LC |
243 | (method store uri 'sha256 sha256 name |
244 | #:system system)))) | |
e3ce5d70 | 245 | |
113aef68 LC |
246 | (define (transitive-inputs inputs) |
247 | (let loop ((inputs inputs) | |
a3d73f59 LC |
248 | (result '())) |
249 | (match inputs | |
250 | (() | |
251 | (delete-duplicates (reverse result))) ; XXX: efficiency | |
252 | (((and i (name (? package? p) sub ...)) rest ...) | |
253 | (let ((t (map (match-lambda | |
254 | ((dep-name derivation ...) | |
255 | (cons (string-append name "/" dep-name) | |
256 | derivation))) | |
257 | (package-propagated-inputs p)))) | |
258 | (loop (append t rest) | |
259 | (append t (cons i result))))) | |
260 | ((input rest ...) | |
261 | (loop rest (cons input result)))))) | |
262 | ||
113aef68 LC |
263 | (define (package-transitive-inputs package) |
264 | "Return the transitive inputs of PACKAGE---i.e., its direct inputs along | |
265 | with their propagated inputs, recursively." | |
266 | (transitive-inputs (append (package-native-inputs package) | |
267 | (package-inputs package) | |
268 | (package-propagated-inputs package)))) | |
269 | ||
270 | (define (package-transitive-propagated-inputs package) | |
271 | "Return the propagated inputs of PACKAGE, and their propagated inputs, | |
272 | recursively." | |
273 | (transitive-inputs (package-propagated-inputs package))) | |
274 | ||
a2ebaddd LC |
275 | \f |
276 | ;;; | |
277 | ;;; Package derivations. | |
278 | ;;; | |
279 | ||
280 | (define %derivation-cache | |
281 | ;; Package to derivation-path mapping. | |
e4588af9 | 282 | (make-weak-key-hash-table 100)) |
a2ebaddd | 283 | |
e509d152 LC |
284 | (define (cache package system thunk) |
285 | "Memoize the return values of THUNK as the derivation of PACKAGE on | |
286 | SYSTEM." | |
287 | (let ((vals (call-with-values thunk list))) | |
288 | ;; Use `hashq-set!' instead of `hash-set!' because `hash' returns the | |
289 | ;; same value for all structs (as of Guile 2.0.6), and because pointer | |
290 | ;; equality is sufficient in practice. | |
291 | (hashq-set! %derivation-cache package `((,system ,@vals))) | |
292 | (apply values vals))) | |
293 | ||
294 | (define-syntax-rule (cached package system body ...) | |
295 | "Memoize the result of BODY for the arguments PACKAGE and SYSTEM. | |
296 | Return the cached result when available." | |
297 | (let ((thunk (lambda () body ...))) | |
298 | (match (hashq-ref %derivation-cache package) | |
299 | ((alist (... ...)) | |
300 | (match (assoc-ref alist system) | |
301 | ((vals (... ...)) | |
302 | (apply values vals)) | |
303 | (#f | |
304 | (cache package system thunk)))) | |
305 | (#f | |
306 | (cache package system thunk))))) | |
a2ebaddd | 307 | |
e3ce5d70 LC |
308 | (define* (package-derivation store package |
309 | #:optional (system (%current-system))) | |
e509d152 LC |
310 | "Return the derivation path and corresponding <derivation> object of |
311 | PACKAGE for SYSTEM." | |
592ef6c8 LC |
312 | (define (intern file) |
313 | ;; Add FILE to the store. Set the `recursive?' bit to #t, so that | |
314 | ;; file permissions are preserved. | |
a9ebd9ef | 315 | (add-to-store store (basename file) #t "sha256" file)) |
592ef6c8 LC |
316 | |
317 | (define expand-input | |
318 | ;; Expand the given input tuple such that it contains only | |
319 | ;; references to derivation paths or store paths. | |
320 | (match-lambda | |
321 | (((? string? name) (? package? package)) | |
b642e4b8 | 322 | (list name (package-derivation store package system))) |
592ef6c8 LC |
323 | (((? string? name) (? package? package) |
324 | (? string? sub-drv)) | |
b642e4b8 | 325 | (list name (package-derivation store package system) |
592ef6c8 LC |
326 | sub-drv)) |
327 | (((? string? name) | |
328 | (and (? string?) (? derivation-path?) drv)) | |
329 | (list name drv)) | |
330 | (((? string? name) | |
331 | (and (? string?) (? file-exists? file))) | |
332 | ;; Add FILE to the store. When FILE is in the sub-directory of a | |
333 | ;; store path, it needs to be added anyway, so it can be used as a | |
334 | ;; source. | |
335 | (list name (intern file))) | |
336 | (((? string? name) (? origin? source)) | |
b642e4b8 | 337 | (list name (package-source-derivation store source system))) |
592ef6c8 LC |
338 | (x |
339 | (raise (condition (&package-input-error | |
340 | (package package) | |
341 | (input x))))))) | |
342 | ||
e509d152 LC |
343 | ;; Compute the derivation and cache the result. Caching is important |
344 | ;; because some derivations, such as the implicit inputs of the GNU build | |
345 | ;; system, will be queried many, many times in a row. | |
346 | (cached package system | |
21c203a5 LC |
347 | |
348 | ;; Bind %CURRENT-SYSTEM so that thunked field values can refer | |
349 | ;; to it. | |
350 | (parameterize ((%current-system system)) | |
351 | (match package | |
352 | (($ <package> name version source (= build-system-builder builder) | |
353 | args inputs propagated-inputs native-inputs self-native-input? | |
354 | outputs) | |
a18eda27 LC |
355 | (let* ((inputs (package-transitive-inputs package)) |
356 | (input-drvs (map expand-input inputs)) | |
357 | (paths (delete-duplicates | |
358 | (append-map (match-lambda | |
359 | ((_ (? package? p) _ ...) | |
360 | (package-native-search-paths | |
361 | p)) | |
362 | (_ '())) | |
363 | inputs)))) | |
21c203a5 LC |
364 | |
365 | (apply builder | |
366 | store (package-full-name package) | |
367 | (and source | |
368 | (package-source-derivation store source system)) | |
a18eda27 LC |
369 | input-drvs |
370 | #:search-paths paths | |
21c203a5 LC |
371 | #:outputs outputs #:system system |
372 | (args)))))))) | |
e3ce5d70 LC |
373 | |
374 | (define* (package-cross-derivation store package) | |
375 | ;; TODO | |
376 | #f) | |
d510ab46 LC |
377 | |
378 | (define* (package-output store package output | |
379 | #:optional (system (%current-system))) | |
380 | "Return the output path of PACKAGE's OUTPUT for SYSTEM---where OUTPUT is the | |
381 | symbolic output name, such as \"out\". Note that this procedure calls | |
382 | `package-derivation', which is costly." | |
383 | (let-values (((_ drv) | |
384 | (package-derivation store package system))) | |
385 | (derivation-output-path | |
386 | (assoc-ref (derivation-outputs drv) output)))) |