1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015 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 monads)
25 #:use-module (guix gexp)
26 #:use-module (guix base32)
27 #:use-module (guix derivations)
28 #:use-module (guix build-system)
29 #:use-module (guix search-paths)
30 #:use-module (guix gexp)
31 #:use-module (ice-9 match)
32 #:use-module (ice-9 vlist)
33 #:use-module (srfi srfi-1)
34 #:use-module (srfi srfi-9 gnu)
35 #:use-module (srfi srfi-11)
36 #:use-module (srfi srfi-26)
37 #:use-module (srfi srfi-34)
38 #:use-module (srfi srfi-35)
39 #:re-export (%current-system
40 %current-target-system
41 search-path-specification) ;for convenience
54 origin-imported-modules
67 package-propagated-inputs
69 package-native-search-paths
76 package-supported-systems
80 package-field-location
82 package-direct-sources
83 package-transitive-sources
85 package-transitive-inputs
86 package-transitive-target-inputs
87 package-transitive-native-inputs
88 package-transitive-propagated-inputs
89 package-transitive-supported-systems
90 package-source-derivation
92 package-cross-derivation
97 %hydra-supported-systems
102 package-error-package
105 package-error-invalid-input
106 &package-cross-build-system-error
107 package-cross-build-system-error?
112 bag-transitive-inputs
113 bag-transitive-host-inputs
114 bag-transitive-build-inputs
115 bag-transitive-target-inputs
118 default-guile-derivation
122 package->cross-derivation
127 ;;; This module provides a high-level mechanism to define packages in a
128 ;;; Guix-based distribution.
132 ;; The source of a package, such as a tarball URL and fetcher---called
133 ;; "origin" to avoid name clash with `package-source', `source', etc.
134 (define-record-type* <origin>
137 (uri origin-uri) ; string
138 (method origin-method) ; procedure
139 (sha256 origin-sha256) ; bytevector
140 (file-name origin-file-name (default #f)) ; optional file name
142 ;; Patches are delayed so that the 'search-patch' calls are made lazily,
143 ;; which reduces I/O on startup and allows patch-not-found errors to be
144 ;; gracefully handled at run time.
145 (patches origin-patches ; list of file names
146 (default '()) (delayed))
148 (snippet origin-snippet (default #f)) ; sexp or #f
149 (patch-flags origin-patch-flags ; list of strings
152 ;; Patching requires Guile, GNU Patch, and a few more. These two fields are
153 ;; used to specify these dependencies when needed.
154 (patch-inputs origin-patch-inputs ; input list or #f
156 (modules origin-modules ; list of module names
158 (imported-modules origin-imported-modules ; list of module names
160 (patch-guile origin-patch-guile ; package or #f
163 (define (print-origin origin port)
164 "Write a concise representation of ORIGIN to PORT."
166 (($ <origin> uri method sha256 file-name patches)
167 (simple-format port "#<origin ~s ~a ~s ~a>"
168 uri (bytevector->base32-string sha256)
170 (number->string (object-address origin) 16)))))
172 (set-record-type-printer! <origin> print-origin)
174 (define-syntax base32
176 "Return the bytevector corresponding to the given Nix-base32
180 (string? (syntax->datum #'str))
181 ;; A literal string: do the conversion at expansion time.
182 (with-syntax ((bv (nix-base32-string->bytevector
183 (syntax->datum #'str))))
186 #'(nix-base32-string->bytevector str)))))
188 (define %supported-systems
189 ;; This is the list of system types that are supported. By default, we
190 ;; expect all packages to build successfully here.
191 '("x86_64-linux" "i686-linux" "armhf-linux" "mips64el-linux"))
193 (define %hydra-supported-systems
194 ;; This is the list of system types for which build slaves are available.
195 (delete "armhf-linux" %supported-systems))
199 (define-record-type* <package>
202 (name package-name) ; string
203 (version package-version) ; string
204 (source package-source) ; <origin> instance
205 (build-system package-build-system) ; build system
206 (arguments package-arguments ; arguments for the build method
207 (default '()) (thunked))
209 (inputs package-inputs ; input packages or derivations
210 (default '()) (thunked))
211 (propagated-inputs package-propagated-inputs ; same, but propagated
212 (default '()) (thunked))
213 (native-inputs package-native-inputs ; native input packages/derivations
214 (default '()) (thunked))
215 (self-native-input? package-self-native-input? ; whether to use itself as
216 ; a native input when cross-
217 (default #f)) ; compiling
219 (outputs package-outputs ; list of strings
223 ; <search-path-specification>,
224 ; for native and cross
226 (native-search-paths package-native-search-paths (default '()))
227 (search-paths package-search-paths (default '()))
228 (replacement package-replacement ; package | #f
229 (default #f) (thunked))
231 (synopsis package-synopsis) ; one-line description
232 (description package-description) ; one or two paragraphs
233 (license package-license)
234 (home-page package-home-page)
235 (supported-systems package-supported-systems ; list of strings
236 (default %supported-systems))
237 (maintainers package-maintainers (default '()))
239 (properties package-properties (default '())) ; alist for anything else
241 (location package-location
242 (default (and=> (current-source-location)
243 source-properties->location))
246 (set-record-type-printer! <package>
247 (lambda (package port)
248 (let ((loc (package-location package))
249 (format simple-format))
250 (format port "#<package ~a-~a ~a~a>"
251 (package-name package)
252 (package-version package)
258 (number->string (object-address
262 (define (package-field-location package field)
263 "Return the source code location of the definition of FIELD for PACKAGE, or
264 #f if it could not be determined."
265 (define (goto port line column)
266 (unless (and (= (port-column port) (- column 1))
267 (= (port-line port) (- line 1)))
268 (unless (eof-object? (read-char port))
269 (goto port line column))))
271 (match (package-location package)
272 (($ <location> file line column)
275 ;; In general we want to keep relative file names for modules.
276 (with-fluids ((%file-port-name-canonicalization 'relative))
277 (call-with-input-file (search-path %load-path file)
279 (goto port line column)
281 (('package inits ...)
282 (let ((field (assoc field inits)))
285 ;; Put the `or' here, and not in the first argument of
286 ;; `and=>', to work around a compiler bug in 2.0.5.
287 (or (and=> (source-properties value)
288 source-properties->location)
289 (and=> (source-properties field)
290 source-properties->location)))
302 (define-condition-type &package-error &error
304 (package package-error-package))
306 (define-condition-type &package-input-error &package-error
308 (input package-error-invalid-input))
310 (define-condition-type &package-cross-build-system-error &package-error
311 package-cross-build-system-error?)
314 (define (package-full-name package)
315 "Return the full name of PACKAGE--i.e., `NAME-VERSION'."
316 (string-append (package-name package) "-" (package-version package)))
318 (define (%standard-patch-inputs)
319 (let* ((canonical (module-ref (resolve-interface '(gnu packages base))
321 (ref (lambda (module var)
323 (module-ref (resolve-interface module) var)))))
324 `(("tar" ,(ref '(gnu packages base) 'tar))
325 ("xz" ,(ref '(gnu packages compression) 'xz))
326 ("bzip2" ,(ref '(gnu packages compression) 'bzip2))
327 ("gzip" ,(ref '(gnu packages compression) 'gzip))
328 ("lzip" ,(ref '(gnu packages compression) 'lzip))
329 ("unzip" ,(ref '(gnu packages zip) 'unzip))
330 ("patch" ,(ref '(gnu packages base) 'patch))
331 ("locales" ,(ref '(gnu packages base) 'glibc-utf8-locales)))))
333 (define (default-guile)
334 "Return the default Guile package used to run the build code of
336 (let ((distro (resolve-interface '(gnu packages commencement))))
337 (module-ref distro 'guile-final)))
339 (define* (default-guile-derivation #:optional (system (%current-system)))
340 "Return the derivation for SYSTEM of the default Guile package used to run
341 the build code of derivation."
342 (package->derivation (default-guile) system
345 (define* (patch-and-repack source patches
351 (imported-modules '())
352 (guile-for-build (%guile-for-build))
353 (system (%current-system)))
354 "Unpack SOURCE (a derivation or store path), apply all of PATCHES, and
355 repack the tarball using the tools listed in INPUTS. When SNIPPET is true,
356 it must be an s-expression that will run from within the directory where
357 SOURCE was unpacked, after all of PATCHES have been applied. MODULES and
358 IMPORTED-MODULES specify modules to use/import for use by SNIPPET."
359 (define source-file-name
360 ;; SOURCE is usually a derivation, but it could be a store file.
361 (if (derivation? source)
362 (derivation->output-path source)
366 ;; The default value of the 'patch-inputs' field, and thus INPUTS is #f,
367 ;; so deal with that.
368 (let ((inputs (or inputs (%standard-patch-inputs))))
370 (match (assoc-ref inputs name)
374 (define decompression-type
375 (cond ((string-suffix? "gz" source-file-name) "gzip")
376 ((string-suffix? "bz2" source-file-name) "bzip2")
377 ((string-suffix? "lz" source-file-name) "lzip")
378 ((string-suffix? "zip" source-file-name) "unzip")
381 (define original-file-name
382 ;; Remove the store prefix plus the slash, hash, and hyphen.
383 (let* ((sans (string-drop source-file-name
384 (+ (string-length (%store-prefix)) 1)))
385 (dash (string-index sans #\-)))
386 (string-drop sans (+ 1 dash))))
388 (define (numeric-extension? file-name)
389 ;; Return true if FILE-NAME ends with digits.
390 (and=> (file-extension file-name)
391 (cut string-every char-set:hex-digit <>)))
393 (define (tarxz-name file-name)
394 ;; Return a '.tar.xz' file name based on FILE-NAME.
395 (let ((base (if (numeric-extension? file-name)
397 (file-sans-extension file-name))))
399 (if (equal? (file-extension base) "tar")
403 (define instantiate-patch
406 (interned-file patch #:recursive? #t))
408 (origin->derivation patch system))))
410 (mlet %store-monad ((tar -> (lookup-input "tar"))
411 (xz -> (lookup-input "xz"))
412 (patch -> (lookup-input "patch"))
413 (locales -> (lookup-input "locales"))
414 (decomp -> (lookup-input decompression-type))
415 (patches (sequence %store-monad
416 (map instantiate-patch patches))))
419 (use-modules (ice-9 ftw)
423 (define (apply-patch patch)
424 (format (current-error-port) "applying '~a'...~%" patch)
426 ;; Use '--force' so that patches that do not apply perfectly are
428 (zero? (system* (string-append #+patch "/bin/patch")
429 "--force" #+@flags "--input" patch)))
431 (define (first-file directory)
432 ;; Return the name of the first file in DIRECTORY.
433 (car (scandir directory
435 (not (member name '("." "..")))))))
437 ;; Encoding/decoding errors shouldn't be silent.
438 (fluid-set! %default-port-conversion-strategy 'error)
441 ;; First of all, install a UTF-8 locale so that UTF-8 file names
442 ;; are correctly interpreted. During bootstrap, LOCALES is #f.
443 (setenv "LOCPATH" (string-append #+locales "/lib/locale"))
444 (setlocale LC_ALL "en_US.UTF-8"))
446 (setenv "PATH" (string-append #+xz "/bin" ":"
449 ;; SOURCE may be either a directory or a tarball.
450 (and (if (file-is-directory? #+source)
451 (let* ((store (%store-directory))
452 (len (+ 1 (string-length store)))
453 (base (string-drop #+source len))
454 (dash (string-index base #\-))
455 (directory (string-drop base (+ 1 dash))))
457 (copy-recursively #+source directory)
459 #+(if (string=? decompression-type "unzip")
460 #~(zero? (system* "unzip" #+source))
461 #~(zero? (system* (string-append #+tar "/bin/tar")
463 (let ((directory (first-file ".")))
464 (format (current-error-port)
465 "source is under '~a'~%" directory)
468 (and (every apply-patch '#+patches)
470 #~((let ((module (make-fresh-user-module)))
471 (module-use-interfaces! module
472 (map resolve-interface
474 ((@ (system base compile) compile)
477 #:opts %auto-compilation-options
481 (begin (chdir "..") #t)
482 (zero? (system* (string-append #+tar "/bin/tar")
483 "cvfa" #$output directory)))))))
485 (let ((name (tarxz-name original-file-name))
486 (modules (delete-duplicates (cons '(guix build utils) modules))))
487 (gexp->derivation name build
491 #:guile-for-build guile-for-build))))
493 (define (transitive-inputs inputs)
494 (let loop ((inputs inputs)
498 (delete-duplicates (reverse result))) ; XXX: efficiency
499 (((and i (name (? package? p) sub ...)) rest ...)
500 (let ((t (map (match-lambda
501 ((dep-name derivation ...)
502 (cons (string-append name "/" dep-name)
504 (package-propagated-inputs p))))
505 (loop (append t rest)
506 (append t (cons i result)))))
508 (loop rest (cons input result))))))
510 (define (package-direct-sources package)
511 "Return all source origins associated with PACKAGE; including origins in
513 `(,@(or (and=> (package-source package) list) '())
514 ,@(filter-map (match-lambda
515 ((_ (? origin? orig) _ ...)
518 (package-direct-inputs package))))
520 (define (package-transitive-sources package)
521 "Return PACKAGE's direct sources, and their direct sources, recursively."
523 (concatenate (filter-map (match-lambda
524 ((_ (? origin? orig) _ ...)
526 ((_ (? package? p) _ ...)
527 (package-direct-sources p))
529 (bag-transitive-inputs
530 (package->bag package))))))
532 (define (package-direct-inputs package)
533 "Return all the direct inputs of PACKAGE---i.e, its direct inputs along
534 with their propagated inputs."
535 (append (package-native-inputs package)
536 (package-inputs package)
537 (package-propagated-inputs package)))
539 (define (package-transitive-inputs package)
540 "Return the transitive inputs of PACKAGE---i.e., its direct inputs along
541 with their propagated inputs, recursively."
542 (transitive-inputs (package-direct-inputs package)))
544 (define (package-transitive-target-inputs package)
545 "Return the transitive target inputs of PACKAGE---i.e., its direct inputs
546 along with their propagated inputs, recursively. This only includes inputs
547 for the target system, and not native inputs."
548 (transitive-inputs (append (package-inputs package)
549 (package-propagated-inputs package))))
551 (define (package-transitive-native-inputs package)
552 "Return the transitive native inputs of PACKAGE---i.e., its direct inputs
553 along with their propagated inputs, recursively. This only includes inputs
554 for the host system (\"native inputs\"), and not target inputs."
555 (transitive-inputs (package-native-inputs package)))
557 (define (package-transitive-propagated-inputs package)
558 "Return the propagated inputs of PACKAGE, and their propagated inputs,
560 (transitive-inputs (package-propagated-inputs package)))
562 (define-syntax define-memoized/v
564 "Define a memoized single-valued unary procedure with docstring.
565 The procedure argument is compared to cached keys using `eqv?'."
567 ((_ (proc arg) docstring body body* ...)
568 (string? (syntax->datum #'docstring))
570 (let ((cache (make-hash-table)))
573 (match (hashv-get-handle cache arg)
577 (let ((result (let () body body* ...)))
578 (hashv-set! cache arg result)
582 (define-memoized/v (package-transitive-supported-systems package)
583 "Return the intersection of the systems supported by PACKAGE and those
584 supported by its dependencies."
585 (fold (lambda (input systems)
587 ((label (? package? p) . _)
589 string=? systems (package-transitive-supported-systems p)))
592 (package-supported-systems package)
593 (bag-direct-inputs (package->bag package))))
595 (define* (supported-package? package #:optional (system (%current-system)))
596 "Return true if PACKAGE is supported on SYSTEM--i.e., if PACKAGE and all its
597 dependencies are known to build on SYSTEM."
598 (member system (package-transitive-supported-systems package)))
600 (define (bag-direct-inputs bag)
601 "Same as 'package-direct-inputs', but applied to a bag."
602 (append (bag-build-inputs bag)
603 (bag-host-inputs bag)
604 (bag-target-inputs bag)))
606 (define (bag-transitive-inputs bag)
607 "Same as 'package-transitive-inputs', but applied to a bag."
608 (transitive-inputs (bag-direct-inputs bag)))
610 (define (bag-transitive-build-inputs bag)
611 "Same as 'package-transitive-native-inputs', but applied to a bag."
612 (transitive-inputs (bag-build-inputs bag)))
614 (define (bag-transitive-host-inputs bag)
615 "Same as 'package-transitive-target-inputs', but applied to a bag."
616 (transitive-inputs (bag-host-inputs bag)))
618 (define (bag-transitive-target-inputs bag)
619 "Return the \"target inputs\" of BAG, recursively."
620 (transitive-inputs (bag-target-inputs bag)))
624 ;;; Package derivations.
627 (define %derivation-cache
628 ;; Package to derivation-path mapping.
629 (make-weak-key-hash-table 100))
631 (define (cache package system thunk)
632 "Memoize the return values of THUNK as the derivation of PACKAGE on
634 ;; FIXME: This memoization should be associated with the open store, because
635 ;; otherwise it breaks when switching to a different store.
636 (let ((vals (call-with-values thunk list)))
637 ;; Use `hashq-set!' instead of `hash-set!' because `hash' returns the
638 ;; same value for all structs (as of Guile 2.0.6), and because pointer
639 ;; equality is sufficient in practice.
640 (hashq-set! %derivation-cache package
642 ,@(or (hashq-ref %derivation-cache package)
644 (apply values vals)))
646 (define-syntax-rule (cached package system body ...)
647 "Memoize the result of BODY for the arguments PACKAGE and SYSTEM.
648 Return the cached result when available."
649 (let ((thunk (lambda () body ...))
651 (match (hashq-ref %derivation-cache package)
653 (match (assoc-ref alist key)
657 (cache package key thunk))))
659 (cache package key thunk)))))
661 (define* (expand-input store package input system #:optional cross-system)
662 "Expand INPUT, an input tuple, such that it contains only references to
663 derivation paths or store paths. PACKAGE is only used to provide contextual
664 information in exceptions."
665 (define (intern file)
666 ;; Add FILE to the store. Set the `recursive?' bit to #t, so that
667 ;; file permissions are preserved.
668 (add-to-store store (basename file) #t "sha256" file))
672 (cut package-cross-derivation store <> cross-system system
674 (cut package-derivation store <> system #:graft? #f)))
677 (((? string? name) (? package? package))
678 (list name (derivation package)))
679 (((? string? name) (? package? package)
681 (list name (derivation package)
684 (and (? string?) (? derivation-path?) drv))
687 (and (? string?) (? file-exists? file)))
688 ;; Add FILE to the store. When FILE is in the sub-directory of a
689 ;; store path, it needs to be added anyway, so it can be used as a
691 (list name (intern file)))
692 (((? string? name) (? origin? source))
693 (list name (package-source-derivation store source system)))
695 (raise (condition (&package-input-error
699 (define* (package->bag package #:optional
700 (system (%current-system))
701 (target (%current-target-system))
702 #:key (graft? (%graft?)))
703 "Compile PACKAGE into a bag for SYSTEM, possibly cross-compiled to TARGET,
705 ;; Bind %CURRENT-SYSTEM and %CURRENT-TARGET-SYSTEM so that thunked field
706 ;; values can refer to it.
707 (parameterize ((%current-system system)
708 (%current-target-system target))
710 (or (package-replacement package) package)
712 (($ <package> name version source build-system
713 args inputs propagated-inputs native-inputs self-native-input?
715 (or (make-bag build-system (string-append name "-" version)
719 #:inputs (append (inputs)
722 #:native-inputs `(,@(if (and target self-native-input?)
729 (&package-cross-build-system-error
733 (package package))))))))))
735 (define (input-graft store system)
736 "Return a procedure that, given an input referring to a package with a
737 graft, returns a pair with the original derivation and the graft's derivation,
738 and returns #f for other inputs."
740 ((label (? package? package) sub-drv ...)
741 (let ((replacement (package-replacement package)))
743 (let ((orig (package-derivation store package system
745 (new (package-derivation store replacement system)))
749 (origin-output (match sub-drv
752 (replacement-output origin-output))))))
756 (define (input-cross-graft store target system)
757 "Same as 'input-graft', but for cross-compilation inputs."
759 ((label (? package? package) sub-drv ...)
760 (let ((replacement (package-replacement package)))
762 (let ((orig (package-cross-derivation store package target system
764 (new (package-cross-derivation store replacement
769 (origin-output (match sub-drv
772 (replacement-output origin-output))))))
776 (define* (bag-grafts store bag)
777 "Return the list of grafts applicable to BAG. Each graft is a <graft>
779 (let ((target (bag-target bag))
780 (system (bag-system bag)))
781 (define native-grafts
782 (filter-map (input-graft store system)
783 (append (bag-transitive-build-inputs bag)
784 (bag-transitive-target-inputs bag)
787 (bag-transitive-host-inputs bag)))))
789 (define target-grafts
791 (filter-map (input-cross-graft store target system)
792 (bag-transitive-host-inputs bag))
795 (append native-grafts target-grafts)))
797 (define* (package-grafts store package
798 #:optional (system (%current-system))
800 "Return the list of grafts applicable to PACKAGE as built for SYSTEM and
802 (let* ((package (or (package-replacement package) package))
803 (bag (package->bag package system target)))
804 (bag-grafts store bag)))
806 (define* (bag->derivation store bag
808 "Return the derivation to build BAG for SYSTEM. Optionally, CONTEXT can be
809 a package object describing the context in which the call occurs, for improved
812 (bag->cross-derivation store bag)
813 (let* ((system (bag-system bag))
814 (inputs (bag-transitive-inputs bag))
815 (input-drvs (map (cut expand-input store context <> system)
817 (paths (delete-duplicates
818 (append-map (match-lambda
819 ((_ (? package? p) _ ...)
820 (package-native-search-paths
825 (apply (bag-build bag)
826 store (bag-name bag) input-drvs
828 #:outputs (bag-outputs bag) #:system system
829 (bag-arguments bag)))))
831 (define* (bag->cross-derivation store bag
833 "Return the derivation to build BAG, which is actually a cross build.
834 Optionally, CONTEXT can be a package object denoting the context of the call.
835 This is an internal procedure."
836 (let* ((system (bag-system bag))
837 (target (bag-target bag))
838 (host (bag-transitive-host-inputs bag))
839 (host-drvs (map (cut expand-input store context <> system target)
841 (target* (bag-transitive-target-inputs bag))
842 (target-drvs (map (cut expand-input store context <> system)
844 (build (bag-transitive-build-inputs bag))
845 (build-drvs (map (cut expand-input store context <> system)
847 (all (append build target* host))
848 (paths (delete-duplicates
849 (append-map (match-lambda
850 ((_ (? package? p) _ ...)
851 (package-search-paths p))
854 (npaths (delete-duplicates
855 (append-map (match-lambda
856 ((_ (? package? p) _ ...)
857 (package-native-search-paths
862 (apply (bag-build bag)
864 #:native-drvs build-drvs
865 #:target-drvs (append host-drvs target-drvs)
867 #:native-search-paths npaths
868 #:outputs (bag-outputs bag)
869 #:system system #:target target
870 (bag-arguments bag))))
872 (define* (package-derivation store package
873 #:optional (system (%current-system))
874 #:key (graft? (%graft?)))
875 "Return the <derivation> object of PACKAGE for SYSTEM."
877 ;; Compute the derivation and cache the result. Caching is important
878 ;; because some derivations, such as the implicit inputs of the GNU build
879 ;; system, will be queried many, many times in a row.
880 (cached package (cons system graft?)
881 (let* ((bag (package->bag package system #f #:graft? graft?))
882 (drv (bag->derivation store bag package)))
884 (match (bag-grafts store bag)
888 (let ((guile (package-derivation store (default-guile)
889 system #:graft? #f)))
890 (graft-derivation store (bag-name bag) drv grafts
895 (define* (package-cross-derivation store package target
896 #:optional (system (%current-system))
897 #:key (graft? (%graft?)))
898 "Cross-build PACKAGE for TARGET (a GNU triplet) from host SYSTEM (a Guix
899 system identifying string)."
900 (cached package (list system target graft?)
901 (let* ((bag (package->bag package system target #:graft? graft?))
902 (drv (bag->derivation store bag package)))
904 (match (bag-grafts store bag)
908 (graft-derivation store (bag-name bag) drv grafts
911 (package-derivation store (default-guile)
912 system #:graft? #f))))
915 (define* (package-output store package
916 #:optional (output "out") (system (%current-system)))
917 "Return the output path of PACKAGE's OUTPUT for SYSTEM---where OUTPUT is the
918 symbolic output name, such as \"out\". Note that this procedure calls
919 `package-derivation', which is costly."
920 (let ((drv (package-derivation store package system)))
921 (derivation->output-path drv output)))
925 ;;; Monadic interface.
928 (define (set-guile-for-build guile)
929 "This monadic procedure changes the Guile currently used to run the build
930 code of derivations to GUILE, a package object."
932 (let ((guile (package-derivation store guile)))
933 (values (%guile-for-build guile) store))))
935 (define* (package-file package
938 system (output "out") target)
939 "Return as a monadic value the absolute file name of FILE within the
940 OUTPUT directory of PACKAGE. When FILE is omitted, return the name of the
941 OUTPUT directory of PACKAGE. When TARGET is true, use it as a
942 cross-compilation target triplet."
944 (define compute-derivation
946 (cut package-cross-derivation <> <> target <>)
949 (let* ((system (or system (%current-system)))
950 (drv (compute-derivation store package system))
951 (out (derivation->output-path drv output)))
953 (string-append out "/" file)
957 (define package->derivation
958 (store-lift package-derivation))
960 (define package->cross-derivation
961 (store-lift package-cross-derivation))
963 (define-gexp-compiler (package-compiler (package package?) system target)
964 ;; Compile PACKAGE to a derivation for SYSTEM, optionally cross-compiled for
965 ;; TARGET. This is used when referring to a package from within a gexp.
967 (package->cross-derivation package target system)
968 (package->derivation package system)))
970 (define* (origin->derivation source
971 #:optional (system (%current-system)))
972 "When SOURCE is an <origin> object, return its derivation for SYSTEM. When
973 SOURCE is a file name, return either the interned file name (if SOURCE is
974 outside of the store) or SOURCE itself (if SOURCE is already a store item.)"
976 (($ <origin> uri method sha256 name (= force ()) #f)
977 ;; No patches, no snippet: this is a fixed-output derivation.
978 (method uri 'sha256 sha256 name #:system system))
979 (($ <origin> uri method sha256 name (= force (patches ...)) snippet
980 (flags ...) inputs (modules ...) (imported-modules ...)
982 ;; Patches and/or a snippet.
983 (mlet %store-monad ((source (method uri 'sha256 sha256 name
985 (guile (package->derivation (or guile-for-build
989 (patch-and-repack source patches
995 #:imported-modules modules
996 #:guile-for-build guile)))
997 ((and (? string?) (? direct-store-path?) file)
998 (with-monad %store-monad
1001 (interned-file file (basename file)
1004 (define-gexp-compiler (origin-compiler (origin origin?) system target)
1005 ;; Compile ORIGIN to a derivation for SYSTEM. This is used when referring
1006 ;; to an origin from within a gexp.
1007 (origin->derivation origin system))
1009 (define package-source-derivation
1010 (store-lower origin->derivation))