1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2014 Mark H Weaver <mhw@netris.org>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (guix packages)
21 #:use-module (guix utils)
22 #:use-module (guix records)
23 #:use-module (guix store)
24 #:use-module (guix base32)
25 #:use-module (guix derivations)
26 #:use-module (guix build-system)
27 #:use-module (ice-9 match)
28 #:use-module (ice-9 vlist)
29 #:use-module (srfi srfi-1)
30 #:use-module (srfi srfi-9 gnu)
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 #:re-export (%current-system
36 %current-target-system)
49 origin-imported-modules
52 <search-path-specification>
53 search-path-specification
54 search-path-specification?
55 search-path-specification->sexp
67 package-propagated-inputs
69 package-native-search-paths
76 package-supported-systems
80 package-field-location
83 package-transitive-inputs
84 package-transitive-target-inputs
85 package-transitive-native-inputs
86 package-transitive-propagated-inputs
87 package-transitive-supported-systems
88 package-source-derivation
90 package-cross-derivation
101 package-error-invalid-input
102 &package-cross-build-system-error
103 package-cross-build-system-error?
108 bag-transitive-inputs
109 bag-transitive-host-inputs
110 bag-transitive-build-inputs
111 bag-transitive-target-inputs))
115 ;;; This module provides a high-level mechanism to define packages in a
116 ;;; Guix-based distribution.
120 ;; The source of a package, such as a tarball URL and fetcher---called
121 ;; "origin" to avoid name clash with `package-source', `source', etc.
122 (define-record-type* <origin>
125 (uri origin-uri) ; string
126 (method origin-method) ; procedure
127 (sha256 origin-sha256) ; bytevector
128 (file-name origin-file-name (default #f)) ; optional file name
129 (patches origin-patches (default '())) ; list of file names
130 (snippet origin-snippet (default #f)) ; sexp or #f
131 (patch-flags origin-patch-flags ; list of strings
134 ;; Patching requires Guile, GNU Patch, and a few more. These two fields are
135 ;; used to specify these dependencies when needed.
136 (patch-inputs origin-patch-inputs ; input list or #f
138 (modules origin-modules ; list of module names
140 (imported-modules origin-imported-modules ; list of module names
142 (patch-guile origin-patch-guile ; package or #f
145 (define (print-origin origin port)
146 "Write a concise representation of ORIGIN to PORT."
148 (($ <origin> uri method sha256 file-name patches)
149 (simple-format port "#<origin ~s ~a ~s ~a>"
150 uri (bytevector->base32-string sha256)
152 (number->string (object-address origin) 16)))))
154 (set-record-type-printer! <origin> print-origin)
156 (define-syntax base32
158 "Return the bytevector corresponding to the given Nix-base32
162 (string? (syntax->datum #'str))
163 ;; A literal string: do the conversion at expansion time.
164 (with-syntax ((bv (nix-base32-string->bytevector
165 (syntax->datum #'str))))
168 #'(nix-base32-string->bytevector str)))))
170 ;; The specification of a search path.
171 (define-record-type* <search-path-specification>
172 search-path-specification make-search-path-specification
173 search-path-specification?
174 (variable search-path-specification-variable)
175 (directories search-path-specification-directories)
176 (separator search-path-specification-separator (default ":")))
178 (define (search-path-specification->sexp spec)
179 "Return an sexp representing SPEC, a <search-path-specification>. The sexp
180 corresponds to the arguments expected by `set-path-environment-variable'."
182 (($ <search-path-specification> variable directories separator)
183 `(,variable ,directories ,separator))))
185 (define %supported-systems
186 ;; This is the list of system types that are supported. By default, we
187 ;; expect all packages to build successfully here.
188 '("x86_64-linux" "i686-linux" "mips64el-linux"))
191 (define-record-type* <package>
194 (name package-name) ; string
195 (version package-version) ; string
196 (source package-source) ; <origin> instance
197 (build-system package-build-system) ; build system
198 (arguments package-arguments ; arguments for the build method
199 (default '()) (thunked))
201 (inputs package-inputs ; input packages or derivations
202 (default '()) (thunked))
203 (propagated-inputs package-propagated-inputs ; same, but propagated
204 (default '()) (thunked))
205 (native-inputs package-native-inputs ; native input packages/derivations
206 (default '()) (thunked))
207 (self-native-input? package-self-native-input? ; whether to use itself as
208 ; a native input when cross-
209 (default #f)) ; compiling
211 (outputs package-outputs ; list of strings
215 ; <search-path-specification>,
216 ; for native and cross
218 (native-search-paths package-native-search-paths (default '()))
219 (search-paths package-search-paths (default '()))
220 (replacement package-replacement ; package | #f
221 (default #f) (thunked))
223 (synopsis package-synopsis) ; one-line description
224 (description package-description) ; one or two paragraphs
225 (license package-license)
226 (home-page package-home-page)
227 (supported-systems package-supported-systems ; list of strings
228 (default %supported-systems))
229 (maintainers package-maintainers (default '()))
231 (properties package-properties (default '())) ; alist for anything else
233 (location package-location
234 (default (and=> (current-source-location)
235 source-properties->location))))
237 (set-record-type-printer! <package>
238 (lambda (package port)
239 (let ((loc (package-location package))
240 (format simple-format))
241 (format port "#<package ~a-~a ~a~a>"
242 (package-name package)
243 (package-version package)
249 (number->string (object-address
253 (define (package-field-location package field)
254 "Return the source code location of the definition of FIELD for PACKAGE, or
255 #f if it could not be determined."
256 (define (goto port line column)
257 (unless (and (= (port-column port) (- column 1))
258 (= (port-line port) (- line 1)))
259 (unless (eof-object? (read-char port))
260 (goto port line column))))
262 (match (package-location package)
263 (($ <location> file line column)
266 ;; In general we want to keep relative file names for modules.
267 (with-fluids ((%file-port-name-canonicalization 'relative))
268 (call-with-input-file (search-path %load-path file)
270 (goto port line column)
272 (('package inits ...)
273 (let ((field (assoc field inits)))
276 ;; Put the `or' here, and not in the first argument of
277 ;; `and=>', to work around a compiler bug in 2.0.5.
278 (or (and=> (source-properties value)
279 source-properties->location)
280 (and=> (source-properties field)
281 source-properties->location)))
293 (define-condition-type &package-error &error
295 (package package-error-package))
297 (define-condition-type &package-input-error &package-error
299 (input package-error-invalid-input))
301 (define-condition-type &package-cross-build-system-error &package-error
302 package-cross-build-system-error?)
305 (define (package-full-name package)
306 "Return the full name of PACKAGE--i.e., `NAME-VERSION'."
307 (string-append (package-name package) "-" (package-version package)))
309 (define (%standard-patch-inputs)
310 (let ((ref (lambda (module var)
311 (module-ref (resolve-interface module) var))))
312 `(("tar" ,(ref '(gnu packages base) 'tar))
313 ("xz" ,(ref '(gnu packages compression) 'xz))
314 ("bzip2" ,(ref '(gnu packages compression) 'bzip2))
315 ("gzip" ,(ref '(gnu packages compression) 'gzip))
316 ("lzip" ,(ref '(gnu packages compression) 'lzip))
317 ("patch" ,(ref '(gnu packages base) 'patch)))))
319 (define (default-guile)
320 "Return the default Guile package for SYSTEM."
321 (let ((distro (resolve-interface '(gnu packages commencement))))
322 (module-ref distro 'guile-final)))
324 (define* (patch-and-repack store source patches
330 (imported-modules '())
331 (guile-for-build (%guile-for-build))
332 (system (%current-system)))
333 "Unpack SOURCE (a derivation or store path), apply all of PATCHES, and
334 repack the tarball using the tools listed in INPUTS. When SNIPPET is true,
335 it must be an s-expression that will run from within the directory where
336 SOURCE was unpacked, after all of PATCHES have been applied. MODULES and
337 IMPORTED-MODULES specify modules to use/import for use by SNIPPET."
338 (define source-file-name
339 ;; SOURCE is usually a derivation, but it could be a store file.
340 (if (derivation? source)
341 (derivation->output-path source)
344 (define decompression-type
345 (cond ((string-suffix? "gz" source-file-name) "gzip")
346 ((string-suffix? "bz2" source-file-name) "bzip2")
347 ((string-suffix? "lz" source-file-name) "lzip")
350 (define original-file-name
351 ;; Remove the store prefix plus the slash, hash, and hyphen.
352 (let* ((sans (string-drop source-file-name
353 (+ (string-length (%store-prefix)) 1)))
354 (dash (string-index sans #\-)))
355 (string-drop sans (+ 1 dash))))
357 (define (numeric-extension? file-name)
358 ;; Return true if FILE-NAME ends with digits.
359 (and=> (file-extension file-name)
360 (cut string-every char-set:hex-digit <>)))
362 (define (tarxz-name file-name)
363 ;; Return a '.tar.xz' file name based on FILE-NAME.
364 (let ((base (if (numeric-extension? file-name)
366 (file-sans-extension file-name))))
368 (if (equal? (file-extension base) "tar")
373 (map (lambda (number patch)
374 (list (string-append "patch" (number->string number))
377 (add-to-store store (basename patch) #t
380 (package-source-derivation store patch system)))))
381 (iota (length patches))
387 (use-modules (ice-9 ftw)
391 (let ((out (assoc-ref %outputs "out"))
392 (xz (assoc-ref %build-inputs "xz"))
393 (decomp (assoc-ref %build-inputs ,decompression-type))
394 (source (assoc-ref %build-inputs "source"))
395 (tar (string-append (assoc-ref %build-inputs "tar")
397 (patch (string-append (assoc-ref %build-inputs "patch")
399 (define (apply-patch input)
400 (let ((patch* (assoc-ref %build-inputs input)))
401 (format (current-error-port) "applying '~a'...~%" patch*)
402 (zero? (system* patch "--batch" ,@flags "--input" patch*))))
404 (define (first-file directory)
405 ;; Return the name of the first file in DIRECTORY.
406 (car (scandir directory
408 (not (member name '("." "..")))))))
410 (setenv "PATH" (string-append xz "/bin" ":"
413 ;; SOURCE may be either a directory or a tarball.
414 (and (if (file-is-directory? source)
415 (let* ((store (or (getenv "NIX_STORE") "/gnu/store"))
416 (len (+ 1 (string-length store)))
417 (base (string-drop source len))
418 (dash (string-index base #\-))
419 (directory (string-drop base (+ 1 dash))))
421 (copy-recursively source directory)
423 (zero? (system* tar "xvf" source)))
424 (let ((directory (first-file ".")))
425 (format (current-error-port)
426 "source is under '~a'~%" directory)
429 (and (every apply-patch ',(map car patch-inputs))
432 `((let ((module (make-fresh-user-module)))
433 (module-use-interfaces! module
434 (map resolve-interface
436 (module-define! module '%build-inputs
438 (module-define! module '%outputs %outputs)
439 ((@ (system base compile) compile)
442 #:opts %auto-compilation-options
446 (begin (chdir "..") #t)
447 (zero? (system* tar "cvfa" out directory))))))))
450 (let ((name (tarxz-name original-file-name))
451 (inputs (filter-map (match-lambda
452 ((name (? package? p))
453 (and (member name (cons decompression-type
454 '("tar" "xz" "patch")))
456 (package-derivation store p system
458 (or inputs (%standard-patch-inputs))))
459 (modules (delete-duplicates (cons '(guix build utils) modules))))
461 (build-expression->derivation store name builder
462 #:inputs `(("source" ,source)
467 #:guile-for-build guile-for-build)))
469 (define* (package-source-derivation store source
470 #:optional (system (%current-system)))
471 "Return the derivation path for SOURCE, a package source, for SYSTEM."
473 (($ <origin> uri method sha256 name () #f)
474 ;; No patches, no snippet: this is a fixed-output derivation.
475 (method store uri 'sha256 sha256 name
477 (($ <origin> uri method sha256 name (patches ...) snippet
478 (flags ...) inputs (modules ...) (imported-modules ...)
480 ;; Patches and/or a snippet.
481 (let ((source (method store uri 'sha256 sha256 name
483 (guile (match (or guile-for-build (default-guile))
485 (package-derivation store p system
487 (patch-and-repack store source patches
493 #:imported-modules modules
494 #:guile-for-build guile)))
495 ((and (? string?) (? direct-store-path?) file)
498 (add-to-store store (basename file) #t "sha256" file))))
500 (define (transitive-inputs inputs)
501 (let loop ((inputs inputs)
505 (delete-duplicates (reverse result))) ; XXX: efficiency
506 (((and i (name (? package? p) sub ...)) rest ...)
507 (let ((t (map (match-lambda
508 ((dep-name derivation ...)
509 (cons (string-append name "/" dep-name)
511 (package-propagated-inputs p))))
512 (loop (append t rest)
513 (append t (cons i result)))))
515 (loop rest (cons input result))))))
517 (define (package-direct-inputs package)
518 "Return all the direct inputs of PACKAGE---i.e, its direct inputs along
519 with their propagated inputs."
520 (append (package-native-inputs package)
521 (package-inputs package)
522 (package-propagated-inputs package)))
524 (define (package-transitive-inputs package)
525 "Return the transitive inputs of PACKAGE---i.e., its direct inputs along
526 with their propagated inputs, recursively."
527 (transitive-inputs (package-direct-inputs package)))
529 (define (package-transitive-target-inputs package)
530 "Return the transitive target inputs of PACKAGE---i.e., its direct inputs
531 along with their propagated inputs, recursively. This only includes inputs
532 for the target system, and not native inputs."
533 (transitive-inputs (append (package-inputs package)
534 (package-propagated-inputs package))))
536 (define (package-transitive-native-inputs package)
537 "Return the transitive native inputs of PACKAGE---i.e., its direct inputs
538 along with their propagated inputs, recursively. This only includes inputs
539 for the host system (\"native inputs\"), and not target inputs."
540 (transitive-inputs (package-native-inputs package)))
542 (define (package-transitive-propagated-inputs package)
543 "Return the propagated inputs of PACKAGE, and their propagated inputs,
545 (transitive-inputs (package-propagated-inputs package)))
547 (define-syntax define-memoized/v
549 "Define a memoized single-valued unary procedure with docstring.
550 The procedure argument is compared to cached keys using `eqv?'."
552 ((_ (proc arg) docstring body body* ...)
553 (string? (syntax->datum #'docstring))
555 (let ((cache (make-hash-table)))
558 (match (hashv-get-handle cache arg)
562 (let ((result (let () body body* ...)))
563 (hashv-set! cache arg result)
567 (define-memoized/v (package-transitive-supported-systems package)
568 "Return the intersection of the systems supported by PACKAGE and those
569 supported by its dependencies."
570 (fold (lambda (input systems)
572 ((label (? package? p) . _)
574 string=? systems (package-transitive-supported-systems p)))
577 (package-supported-systems package)
578 (package-direct-inputs package)))
580 (define (bag-transitive-inputs bag)
581 "Same as 'package-transitive-inputs', but applied to a bag."
582 (transitive-inputs (append (bag-build-inputs bag)
583 (bag-host-inputs bag)
584 (bag-target-inputs bag))))
586 (define (bag-transitive-build-inputs bag)
587 "Same as 'package-transitive-native-inputs', but applied to a bag."
588 (transitive-inputs (bag-build-inputs bag)))
590 (define (bag-transitive-host-inputs bag)
591 "Same as 'package-transitive-target-inputs', but applied to a bag."
592 (transitive-inputs (bag-host-inputs bag)))
594 (define (bag-transitive-target-inputs bag)
595 "Return the \"target inputs\" of BAG, recursively."
596 (transitive-inputs (bag-target-inputs bag)))
600 ;;; Package derivations.
603 (define %derivation-cache
604 ;; Package to derivation-path mapping.
605 (make-weak-key-hash-table 100))
607 (define (cache package system thunk)
608 "Memoize the return values of THUNK as the derivation of PACKAGE on
610 ;; FIXME: This memoization should be associated with the open store, because
611 ;; otherwise it breaks when switching to a different store.
612 (let ((vals (call-with-values thunk list)))
613 ;; Use `hashq-set!' instead of `hash-set!' because `hash' returns the
614 ;; same value for all structs (as of Guile 2.0.6), and because pointer
615 ;; equality is sufficient in practice.
616 (hashq-set! %derivation-cache package
618 ,@(or (hashq-ref %derivation-cache package)
620 (apply values vals)))
622 (define-syntax-rule (cached package system body ...)
623 "Memoize the result of BODY for the arguments PACKAGE and SYSTEM.
624 Return the cached result when available."
625 (let ((thunk (lambda () body ...))
627 (match (hashq-ref %derivation-cache package)
629 (match (assoc-ref alist key)
633 (cache package key thunk))))
635 (cache package key thunk)))))
637 (define* (expand-input store package input system #:optional cross-system)
638 "Expand INPUT, an input tuple, such that it contains only references to
639 derivation paths or store paths. PACKAGE is only used to provide contextual
640 information in exceptions."
641 (define (intern file)
642 ;; Add FILE to the store. Set the `recursive?' bit to #t, so that
643 ;; file permissions are preserved.
644 (add-to-store store (basename file) #t "sha256" file))
648 (cut package-cross-derivation store <> cross-system system
650 (cut package-derivation store <> system #:graft? #f)))
653 (((? string? name) (? package? package))
654 (list name (derivation package)))
655 (((? string? name) (? package? package)
657 (list name (derivation package)
660 (and (? string?) (? derivation-path?) drv))
663 (and (? string?) (? file-exists? file)))
664 ;; Add FILE to the store. When FILE is in the sub-directory of a
665 ;; store path, it needs to be added anyway, so it can be used as a
667 (list name (intern file)))
668 (((? string? name) (? origin? source))
669 (list name (package-source-derivation store source system)))
671 (raise (condition (&package-input-error
676 ;; Whether to honor package grafts by default.
679 (define* (package->bag package #:optional
680 (system (%current-system))
681 (target (%current-target-system))
682 #:key (graft? (%graft?)))
683 "Compile PACKAGE into a bag for SYSTEM, possibly cross-compiled to TARGET,
685 ;; Bind %CURRENT-SYSTEM and %CURRENT-TARGET-SYSTEM so that thunked field
686 ;; values can refer to it.
687 (parameterize ((%current-system system)
688 (%current-target-system target))
690 (or (package-replacement package) package)
692 (($ <package> name version source build-system
693 args inputs propagated-inputs native-inputs self-native-input?
695 (or (make-bag build-system (string-append name "-" version)
699 #:inputs (append (inputs)
702 #:native-inputs `(,@(if (and target self-native-input?)
709 (&package-cross-build-system-error
713 (package package))))))))))
715 (define (input-graft store system)
716 "Return a procedure that, given an input referring to a package with a
717 graft, returns a pair with the original derivation and the graft's derivation,
718 and returns #f for other inputs."
720 ((label (? package? package) sub-drv ...)
721 (let ((replacement (package-replacement package)))
723 (let ((orig (package-derivation store package system
725 (new (package-derivation store replacement system)))
729 (origin-output (match sub-drv
732 (replacement-output origin-output))))))
736 (define (input-cross-graft store target system)
737 "Same as 'input-graft', but for cross-compilation inputs."
739 ((label (? package? package) sub-drv ...)
740 (let ((replacement (package-replacement package)))
742 (let ((orig (package-cross-derivation store package target system
744 (new (package-cross-derivation store replacement
749 (origin-output (match sub-drv
752 (replacement-output origin-output))))))
756 (define* (bag-grafts store bag)
757 "Return the list of grafts applicable to BAG. Each graft is a <graft>
759 (let ((target (bag-target bag))
760 (system (bag-system bag)))
761 (define native-grafts
762 (filter-map (input-graft store system)
763 (append (bag-transitive-build-inputs bag)
764 (bag-transitive-target-inputs bag)
767 (bag-transitive-host-inputs bag)))))
769 (define target-grafts
771 (filter-map (input-cross-graft store target system)
772 (bag-transitive-host-inputs bag))
775 (append native-grafts target-grafts)))
777 (define* (package-grafts store package
778 #:optional (system (%current-system))
780 "Return the list of grafts applicable to PACKAGE as built for SYSTEM and
782 (let* ((package (or (package-replacement package) package))
783 (bag (package->bag package system target)))
784 (bag-grafts store bag)))
786 (define* (bag->derivation store bag
788 "Return the derivation to build BAG for SYSTEM. Optionally, CONTEXT can be
789 a package object describing the context in which the call occurs, for improved
792 (bag->cross-derivation store bag)
793 (let* ((system (bag-system bag))
794 (inputs (bag-transitive-inputs bag))
795 (input-drvs (map (cut expand-input store context <> system)
797 (paths (delete-duplicates
798 (append-map (match-lambda
799 ((_ (? package? p) _ ...)
800 (package-native-search-paths
805 (apply (bag-build bag)
806 store (bag-name bag) input-drvs
808 #:outputs (bag-outputs bag) #:system system
809 (bag-arguments bag)))))
811 (define* (bag->cross-derivation store bag
813 "Return the derivation to build BAG, which is actually a cross build.
814 Optionally, CONTEXT can be a package object denoting the context of the call.
815 This is an internal procedure."
816 (let* ((system (bag-system bag))
817 (target (bag-target bag))
818 (host (bag-transitive-host-inputs bag))
819 (host-drvs (map (cut expand-input store context <> system target)
821 (target* (bag-transitive-target-inputs bag))
822 (target-drvs (map (cut expand-input store context <> system)
824 (build (bag-transitive-build-inputs bag))
825 (build-drvs (map (cut expand-input store context <> system)
827 (all (append build target* host))
828 (paths (delete-duplicates
829 (append-map (match-lambda
830 ((_ (? package? p) _ ...)
831 (package-search-paths p))
834 (npaths (delete-duplicates
835 (append-map (match-lambda
836 ((_ (? package? p) _ ...)
837 (package-native-search-paths
842 (apply (bag-build bag)
844 #:native-drvs build-drvs
845 #:target-drvs (append host-drvs target-drvs)
847 #:native-search-paths npaths
848 #:outputs (bag-outputs bag)
849 #:system system #:target target
850 (bag-arguments bag))))
852 (define* (package-derivation store package
853 #:optional (system (%current-system))
854 #:key (graft? (%graft?)))
855 "Return the <derivation> object of PACKAGE for SYSTEM."
857 ;; Compute the derivation and cache the result. Caching is important
858 ;; because some derivations, such as the implicit inputs of the GNU build
859 ;; system, will be queried many, many times in a row.
860 (cached package (cons system graft?)
861 (let* ((bag (package->bag package system #f #:graft? graft?))
862 (drv (bag->derivation store bag package)))
864 (match (bag-grafts store bag)
868 (let ((guile (package-derivation store (default-guile)
869 system #:graft? #f)))
870 (graft-derivation store (bag-name bag) drv grafts
875 (define* (package-cross-derivation store package target
876 #:optional (system (%current-system))
877 #:key (graft? (%graft?)))
878 "Cross-build PACKAGE for TARGET (a GNU triplet) from host SYSTEM (a Guix
879 system identifying string)."
880 (cached package (list system target graft?)
881 (let* ((bag (package->bag package system target #:graft? graft?))
882 (drv (bag->derivation store bag package)))
884 (match (bag-grafts store bag)
888 (graft-derivation store (bag-name bag) drv grafts
891 (package-derivation store (default-guile)
892 system #:graft? #f))))
895 (define* (package-output store package
896 #:optional (output "out") (system (%current-system)))
897 "Return the output path of PACKAGE's OUTPUT for SYSTEM---where OUTPUT is the
898 symbolic output name, such as \"out\". Note that this procedure calls
899 `package-derivation', which is costly."
900 (let ((drv (package-derivation store package system)))
901 (derivation->output-path drv output)))