1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
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.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
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.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (guix packages)
20 #:use-module (guix utils)
21 #:use-module (guix records)
22 #:use-module (guix store)
23 #:use-module (guix base32)
24 #:use-module (guix derivations)
25 #:use-module (guix build-system)
26 #:use-module (ice-9 match)
27 #:use-module (srfi srfi-1)
28 #:use-module (srfi srfi-9 gnu)
29 #:use-module (srfi srfi-26)
30 #:use-module (srfi srfi-34)
31 #:use-module (srfi srfi-35)
32 #:re-export (%current-system
33 %current-target-system)
46 origin-imported-modules
49 <search-path-specification>
50 search-path-specification
51 search-path-specification?
52 search-path-specification->sexp
64 package-propagated-inputs
66 package-native-search-paths
76 package-field-location
78 package-transitive-inputs
79 package-transitive-target-inputs
80 package-transitive-native-inputs
81 package-transitive-propagated-inputs
82 package-source-derivation
84 package-cross-derivation
92 package-error-invalid-input
93 &package-cross-build-system-error
94 package-cross-build-system-error?))
98 ;;; This module provides a high-level mechanism to define packages in a
99 ;;; Guix-based distribution.
103 ;; The source of a package, such as a tarball URL and fetcher---called
104 ;; "origin" to avoid name clash with `package-source', `source', etc.
105 (define-record-type* <origin>
108 (uri origin-uri) ; string
109 (method origin-method) ; symbol
110 (sha256 origin-sha256) ; bytevector
111 (file-name origin-file-name (default #f)) ; optional file name
112 (patches origin-patches (default '())) ; list of file names
113 (snippet origin-snippet (default #f)) ; sexp or #f
114 (patch-flags origin-patch-flags ; list of strings
117 ;; Patching requires Guile, GNU Patch, and a few more. These two fields are
118 ;; used to specify these dependencies when needed.
119 (patch-inputs origin-patch-inputs ; input list or #f
121 (modules origin-modules ; list of module names
123 (imported-modules origin-imported-modules ; list of module names
125 (patch-guile origin-patch-guile ; package or #f
128 (define-syntax base32
130 "Return the bytevector corresponding to the given Nix-base32
134 (string? (syntax->datum #'str))
135 ;; A literal string: do the conversion at expansion time.
136 (with-syntax ((bv (nix-base32-string->bytevector
137 (syntax->datum #'str))))
140 #'(nix-base32-string->bytevector str)))))
142 ;; The specification of a search path.
143 (define-record-type* <search-path-specification>
144 search-path-specification make-search-path-specification
145 search-path-specification?
146 (variable search-path-specification-variable)
147 (directories search-path-specification-directories)
148 (separator search-path-specification-separator (default ":")))
150 (define (search-path-specification->sexp spec)
151 "Return an sexp representing SPEC, a <search-path-specification>. The sexp
152 corresponds to the arguments expected by `set-path-environment-variable'."
154 (($ <search-path-specification> variable directories separator)
155 `(,variable ,directories ,separator))))
158 (define-record-type* <package>
161 (name package-name) ; string
162 (version package-version) ; string
163 (source package-source) ; <origin> instance
164 (build-system package-build-system) ; build system
165 (arguments package-arguments ; arguments for the build method
166 (default '()) (thunked))
168 (inputs package-inputs ; input packages or derivations
169 (default '()) (thunked))
170 (propagated-inputs package-propagated-inputs ; same, but propagated
171 (default '()) (thunked))
172 (native-inputs package-native-inputs ; native input packages/derivations
173 (default '()) (thunked))
174 (self-native-input? package-self-native-input? ; whether to use itself as
175 ; a native input when cross-
176 (default #f)) ; compiling
178 (outputs package-outputs ; list of strings
182 ; <search-path-specification>,
183 ; for native and cross
185 (native-search-paths package-native-search-paths (default '()))
186 (search-paths package-search-paths (default '()))
188 (synopsis package-synopsis) ; one-line description
189 (description package-description) ; one or two paragraphs
190 (license package-license)
191 (home-page package-home-page)
192 (platforms package-platforms (default '()))
193 (maintainers package-maintainers (default '()))
195 (properties package-properties (default '())) ; alist for anything else
197 (location package-location
198 (default (and=> (current-source-location)
199 source-properties->location))))
201 (set-record-type-printer! <package>
202 (lambda (package port)
203 (let ((loc (package-location package))
204 (format simple-format))
205 (format port "#<package ~a-~a ~a:~a ~a>"
206 (package-name package)
207 (package-version package)
210 (number->string (object-address
214 (define (package-field-location package field)
215 "Return the source code location of the definition of FIELD for PACKAGE, or
216 #f if it could not be determined."
217 (define (goto port line column)
218 (unless (and (= (port-column port) (- column 1))
219 (= (port-line port) (- line 1)))
220 (unless (eof-object? (read-char port))
221 (goto port line column))))
223 (match (package-location package)
224 (($ <location> file line column)
227 (call-with-input-file (search-path %load-path file)
229 (goto port line column)
231 (('package inits ...)
232 (let ((field (assoc field inits)))
235 ;; Put the `or' here, and not in the first argument of
236 ;; `and=>', to work around a compiler bug in 2.0.5.
237 (or (and=> (source-properties value)
238 source-properties->location)
239 (and=> (source-properties field)
240 source-properties->location)))
252 (define-condition-type &package-error &error
254 (package package-error-package))
256 (define-condition-type &package-input-error &package-error
258 (input package-error-invalid-input))
260 (define-condition-type &package-cross-build-system-error &package-error
261 package-cross-build-system-error?)
264 (define (package-full-name package)
265 "Return the full name of PACKAGE--i.e., `NAME-VERSION'."
266 (string-append (package-name package) "-" (package-version package)))
268 (define (%standard-patch-inputs)
269 (let ((ref (lambda (module var)
270 (module-ref (resolve-interface module) var))))
271 `(("tar" ,(ref '(gnu packages base) 'tar))
272 ("xz" ,(ref '(gnu packages compression) 'xz))
273 ("bzip2" ,(ref '(gnu packages compression) 'bzip2))
274 ("gzip" ,(ref '(gnu packages compression) 'gzip))
275 ("lzip" ,(ref '(gnu packages compression) 'lzip))
276 ("patch" ,(ref '(gnu packages base) 'patch)))))
278 (define (default-guile)
279 "Return the default Guile package for SYSTEM."
280 (let ((distro (resolve-interface '(gnu packages base))))
281 (module-ref distro 'guile-final)))
283 (define* (patch-and-repack store source patches
289 (imported-modules '())
290 (guile-for-build (%guile-for-build))
291 (system (%current-system)))
292 "Unpack SOURCE (a derivation or store path), apply all of PATCHES, and
293 repack the tarball using the tools listed in INPUTS. When SNIPPET is true,
294 it must be an s-expression that will run from within the directory where
295 SOURCE was unpacked, after all of PATCHES have been applied. MODULES and
296 IMPORTED-MODULES specify modules to use/import for use by SNIPPET."
297 (define source-file-name
298 ;; SOURCE is usually a derivation, but it could be a store file.
299 (if (derivation? source)
300 (derivation->output-path source)
303 (define decompression-type
304 (cond ((string-suffix? "gz" source-file-name) "gzip")
305 ((string-suffix? "bz2" source-file-name) "bzip2")
306 ((string-suffix? "lz" source-file-name) "lzip")
309 (define original-file-name
310 ;; Remove the store prefix plus the slash, hash, and hyphen.
311 (let* ((sans (string-drop source-file-name
312 (+ (string-length (%store-prefix)) 1)))
313 (dash (string-index sans #\-)))
314 (string-drop sans (+ 1 dash))))
317 (map (lambda (number patch)
318 (list (string-append "patch" (number->string number))
319 (add-to-store store (basename patch) #t
321 (iota (length patches))
327 (use-modules (ice-9 ftw)
330 (let ((out (assoc-ref %outputs "out"))
331 (xz (assoc-ref %build-inputs "xz"))
332 (decomp (assoc-ref %build-inputs ,decompression-type))
333 (source (assoc-ref %build-inputs "source"))
334 (tar (string-append (assoc-ref %build-inputs "tar")
336 (patch (string-append (assoc-ref %build-inputs "patch")
338 (define (apply-patch input)
339 (let ((patch* (assoc-ref %build-inputs input)))
340 (format (current-error-port) "applying '~a'...~%" patch*)
341 (zero? (system* patch "--batch" ,@flags "--input" patch*))))
343 (setenv "PATH" (string-append xz "/bin" ":"
345 (and (zero? (system* tar "xvf" source))
346 (let ((directory (car (scandir "."
351 (format (current-error-port)
352 "source is under '~a'~%" directory)
355 (and (every apply-patch ',(map car patch-inputs))
358 `((let ((module (make-fresh-user-module)))
359 (module-use-interfaces! module
360 (map resolve-interface
362 (module-define! module '%build-inputs
364 (module-define! module '%outputs %outputs)
365 ((@ (system base compile) compile)
368 #:opts %auto-compilation-options
372 (begin (chdir "..") #t)
373 (zero? (system* tar "cvfa" out directory))))))))
376 (let ((name (string-append (file-sans-extension original-file-name)
378 (inputs (filter-map (match-lambda
379 ((name (? package? p))
380 (and (member name (cons decompression-type
381 '("tar" "xz" "patch")))
383 (package-derivation store p
385 (or inputs (%standard-patch-inputs)))))
387 (build-expression->derivation store name system builder
391 #:modules imported-modules
392 #:guile-for-build guile-for-build)))
394 (define* (package-source-derivation store source
395 #:optional (system (%current-system)))
396 "Return the derivation path for SOURCE, a package source, for SYSTEM."
398 (($ <origin> uri method sha256 name () #f)
399 ;; No patches, no snippet: this is a fixed-output derivation.
400 (method store uri 'sha256 sha256 name
402 (($ <origin> uri method sha256 name (patches ...) snippet
403 (flags ...) inputs (modules ...) (imported-modules ...)
405 ;; Patches and/or a snippet.
406 (let ((source (method store uri 'sha256 sha256 name
408 (guile (match (or guile-for-build (%guile-for-build)
411 (package-derivation store p system))
414 (patch-and-repack store source patches
420 #:imported-modules modules
421 #:guile-for-build guile)))
422 ((and (? string?) (? store-path?) file)
425 (add-to-store store (basename file) #t "sha256" file))))
427 (define (transitive-inputs inputs)
428 (let loop ((inputs inputs)
432 (delete-duplicates (reverse result))) ; XXX: efficiency
433 (((and i (name (? package? p) sub ...)) rest ...)
434 (let ((t (map (match-lambda
435 ((dep-name derivation ...)
436 (cons (string-append name "/" dep-name)
438 (package-propagated-inputs p))))
439 (loop (append t rest)
440 (append t (cons i result)))))
442 (loop rest (cons input result))))))
444 (define (package-transitive-inputs package)
445 "Return the transitive inputs of PACKAGE---i.e., its direct inputs along
446 with their propagated inputs, recursively."
447 (transitive-inputs (append (package-native-inputs package)
448 (package-inputs package)
449 (package-propagated-inputs package))))
451 (define (package-transitive-target-inputs package)
452 "Return the transitive target inputs of PACKAGE---i.e., its direct inputs
453 along with their propagated inputs, recursively. This only includes inputs
454 for the target system, and not native inputs."
455 (transitive-inputs (append (package-inputs package)
456 (package-propagated-inputs package))))
458 (define (package-transitive-native-inputs package)
459 "Return the transitive native inputs of PACKAGE---i.e., its direct inputs
460 along with their propagated inputs, recursively. This only includes inputs
461 for the host system (\"native inputs\"), and not target inputs."
462 (transitive-inputs (package-native-inputs package)))
464 (define (package-transitive-propagated-inputs package)
465 "Return the propagated inputs of PACKAGE, and their propagated inputs,
467 (transitive-inputs (package-propagated-inputs package)))
471 ;;; Package derivations.
474 (define %derivation-cache
475 ;; Package to derivation-path mapping.
476 (make-weak-key-hash-table 100))
478 (define (cache package system thunk)
479 "Memoize the return values of THUNK as the derivation of PACKAGE on
481 (let ((vals (call-with-values thunk list)))
482 ;; Use `hashq-set!' instead of `hash-set!' because `hash' returns the
483 ;; same value for all structs (as of Guile 2.0.6), and because pointer
484 ;; equality is sufficient in practice.
485 (hashq-set! %derivation-cache package `((,system ,@vals)))
486 (apply values vals)))
488 (define-syntax-rule (cached package system body ...)
489 "Memoize the result of BODY for the arguments PACKAGE and SYSTEM.
490 Return the cached result when available."
491 (let ((thunk (lambda () body ...)))
492 (match (hashq-ref %derivation-cache package)
494 (match (assoc-ref alist system)
498 (cache package system thunk))))
500 (cache package system thunk)))))
502 (define* (expand-input store package input system #:optional cross-system)
503 "Expand INPUT, an input tuple, such that it contains only references to
504 derivation paths or store paths. PACKAGE is only used to provide contextual
505 information in exceptions."
506 (define (intern file)
507 ;; Add FILE to the store. Set the `recursive?' bit to #t, so that
508 ;; file permissions are preserved.
509 (add-to-store store (basename file) #t "sha256" file))
513 (cut package-cross-derivation store <> cross-system system)
514 (cut package-derivation store <> system)))
517 (((? string? name) (? package? package))
518 (list name (derivation package)))
519 (((? string? name) (? package? package)
521 (list name (derivation package)
524 (and (? string?) (? derivation-path?) drv))
527 (and (? string?) (? file-exists? file)))
528 ;; Add FILE to the store. When FILE is in the sub-directory of a
529 ;; store path, it needs to be added anyway, so it can be used as a
531 (list name (intern file)))
532 (((? string? name) (? origin? source))
533 (list name (package-source-derivation store source system)))
535 (raise (condition (&package-input-error
539 (define* (package-derivation store package
540 #:optional (system (%current-system)))
541 "Return the <derivation> object of PACKAGE for SYSTEM."
543 ;; Compute the derivation and cache the result. Caching is important
544 ;; because some derivations, such as the implicit inputs of the GNU build
545 ;; system, will be queried many, many times in a row.
546 (cached package system
548 ;; Bind %CURRENT-SYSTEM so that thunked field values can refer
550 (parameterize ((%current-system system)
551 (%current-target-system #f))
553 (($ <package> name version source (= build-system-builder builder)
554 args inputs propagated-inputs native-inputs self-native-input?
556 (let* ((inputs (package-transitive-inputs package))
557 (input-drvs (map (cut expand-input
558 store package <> system)
560 (paths (delete-duplicates
561 (append-map (match-lambda
562 ((_ (? package? p) _ ...)
563 (package-native-search-paths
569 store (package-full-name package)
571 (package-source-derivation store source system))
574 #:outputs outputs #:system system
577 (define* (package-cross-derivation store package target
578 #:optional (system (%current-system)))
579 "Cross-build PACKAGE for TARGET (a GNU triplet) from host SYSTEM (a Guix
580 system identifying string)."
581 (cached package (cons system target)
583 ;; Bind %CURRENT-SYSTEM so that thunked field values can refer
585 (parameterize ((%current-system system)
586 (%current-target-system target))
588 (($ <package> name version source
589 (= build-system-cross-builder builder)
590 args inputs propagated-inputs native-inputs self-native-input?
594 (&package-cross-build-system-error
595 (package package)))))
597 (let* ((inputs (package-transitive-target-inputs package))
598 (input-drvs (map (cut expand-input
602 (host (append (if self-native-input?
605 (package-transitive-native-inputs package)))
606 (host-drvs (map (cut expand-input
607 store package <> system)
609 (all (append host inputs))
610 (paths (delete-duplicates
611 (append-map (match-lambda
612 ((_ (? package? p) _ ...)
613 (package-search-paths p))
616 (npaths (delete-duplicates
617 (append-map (match-lambda
618 ((_ (? package? p) _ ...)
619 (package-native-search-paths
625 store (package-full-name package) target
627 (package-source-derivation store source system))
630 #:native-search-paths npaths
631 #:outputs outputs #:system system
634 (define* (package-output store package
635 #:optional (output "out") (system (%current-system)))
636 "Return the output path of PACKAGE's OUTPUT for SYSTEM---where OUTPUT is the
637 symbolic output name, such as \"out\". Note that this procedure calls
638 `package-derivation', which is costly."
639 (let ((drv (package-derivation store package system)))
640 (derivation->output-path drv output)))