1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2014, 2015 Mark H Weaver <mhw@netris.org>
4 ;;; Copyright © 2015 Eric Bavier <bavier@member.fsf.org>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (guix packages)
22 #:use-module (guix utils)
23 #:use-module (guix records)
24 #:use-module (guix store)
25 #:use-module (guix monads)
26 #:use-module (guix gexp)
27 #:use-module (guix base32)
28 #:use-module (guix grafts)
29 #:use-module (guix derivations)
30 #:use-module (guix build-system)
31 #:use-module (guix search-paths)
32 #:use-module (guix gexp)
33 #:use-module (guix sets)
34 #:use-module (ice-9 match)
35 #:use-module (ice-9 vlist)
36 #:use-module (srfi srfi-1)
37 #:use-module (srfi srfi-9 gnu)
38 #:use-module (srfi srfi-11)
39 #:use-module (srfi srfi-26)
40 #:use-module (srfi srfi-34)
41 #:use-module (srfi srfi-35)
42 #:use-module (web uri)
43 #:re-export (%current-system
44 %current-target-system
45 search-path-specification) ;for convenience
52 origin-actual-file-name
59 origin-imported-modules
72 package-propagated-inputs
74 package-native-search-paths
81 package-supported-systems
85 package-field-location
87 package-direct-sources
88 package-transitive-sources
90 package-transitive-inputs
91 package-transitive-target-inputs
92 package-transitive-native-inputs
93 package-transitive-propagated-inputs
94 package-transitive-native-search-paths
95 package-transitive-supported-systems
96 package-source-derivation
98 package-cross-derivation
102 transitive-input-references
106 %hydra-supported-systems
111 package-error-package
114 package-error-invalid-input
115 &package-cross-build-system-error
116 package-cross-build-system-error?
121 bag-transitive-inputs
122 bag-transitive-host-inputs
123 bag-transitive-build-inputs
124 bag-transitive-target-inputs
127 default-guile-derivation
131 package->cross-derivation
136 ;;; This module provides a high-level mechanism to define packages in a
137 ;;; Guix-based distribution.
141 ;; The source of a package, such as a tarball URL and fetcher---called
142 ;; "origin" to avoid name clash with `package-source', `source', etc.
143 (define-record-type* <origin>
146 (uri origin-uri) ; string
147 (method origin-method) ; procedure
148 (sha256 origin-sha256) ; bytevector
149 (file-name origin-file-name (default #f)) ; optional file name
151 ;; Patches are delayed so that the 'search-patch' calls are made lazily,
152 ;; which reduces I/O on startup and allows patch-not-found errors to be
153 ;; gracefully handled at run time.
154 (patches origin-patches ; list of file names
155 (default '()) (delayed))
157 (snippet origin-snippet (default #f)) ; sexp or #f
158 (patch-flags origin-patch-flags ; list of strings
161 ;; Patching requires Guile, GNU Patch, and a few more. These two fields are
162 ;; used to specify these dependencies when needed.
163 (patch-inputs origin-patch-inputs ; input list or #f
165 (modules origin-modules ; list of module names
167 (imported-modules origin-imported-modules ; list of module names
169 (patch-guile origin-patch-guile ; package or #f
172 (define (print-origin origin port)
173 "Write a concise representation of ORIGIN to PORT."
175 (($ <origin> uri method sha256 file-name patches)
176 (simple-format port "#<origin ~s ~a ~s ~a>"
177 uri (bytevector->base32-string sha256)
179 (number->string (object-address origin) 16)))))
181 (set-record-type-printer! <origin> print-origin)
183 (define-syntax base32
185 "Return the bytevector corresponding to the given Nix-base32
189 (string? (syntax->datum #'str))
190 ;; A literal string: do the conversion at expansion time.
191 (with-syntax ((bv (nix-base32-string->bytevector
192 (syntax->datum #'str))))
195 #'(nix-base32-string->bytevector str)))))
197 (define (origin-actual-file-name origin)
198 "Return the file name of ORIGIN, either its 'file-name' field or the file
200 (define (uri->file-name uri)
201 ;; Return the 'base name' of URI or URI itself, where URI is a string.
202 (let ((path (and=> (string->uri uri) uri-path)))
207 (or (origin-file-name origin)
208 (match (origin-uri origin)
210 (uri->file-name head))
212 (uri->file-name uri))
214 ;; git, svn, cvs, etc. reference
217 (define %supported-systems
218 ;; This is the list of system types that are supported. By default, we
219 ;; expect all packages to build successfully here.
220 '("x86_64-linux" "i686-linux" "armhf-linux" "mips64el-linux"))
222 (define %hurd-systems
223 ;; The GNU/Hurd systems for which support is being developed.
224 '("i585-gnu" "i686-gnu"))
226 (define %hydra-supported-systems
227 ;; This is the list of system types for which build slaves are available.
232 (define-record-type* <package>
235 (name package-name) ; string
236 (version package-version) ; string
237 (source package-source) ; <origin> instance
238 (build-system package-build-system) ; build system
239 (arguments package-arguments ; arguments for the build method
240 (default '()) (thunked))
242 (inputs package-inputs ; input packages or derivations
243 (default '()) (thunked))
244 (propagated-inputs package-propagated-inputs ; same, but propagated
245 (default '()) (thunked))
246 (native-inputs package-native-inputs ; native input packages/derivations
247 (default '()) (thunked))
248 (self-native-input? package-self-native-input? ; whether to use itself as
249 ; a native input when cross-
250 (default #f)) ; compiling
252 (outputs package-outputs ; list of strings
256 ; <search-path-specification>,
257 ; for native and cross
259 (native-search-paths package-native-search-paths (default '()))
260 (search-paths package-search-paths (default '()))
261 (replacement package-replacement ; package | #f
262 (default #f) (thunked))
264 (synopsis package-synopsis) ; one-line description
265 (description package-description) ; one or two paragraphs
266 (license package-license)
267 (home-page package-home-page)
268 (supported-systems package-supported-systems ; list of strings
269 (default %supported-systems))
270 (maintainers package-maintainers (default '()))
272 (properties package-properties (default '())) ; alist for anything else
274 (location package-location
275 (default (and=> (current-source-location)
276 source-properties->location))
279 (set-record-type-printer! <package>
280 (lambda (package port)
281 (let ((loc (package-location package))
282 (format simple-format))
283 (format port "#<package ~a@~a ~a~a>"
284 (package-name package)
285 (package-version package)
291 (number->string (object-address
295 (define (package-field-location package field)
296 "Return the source code location of the definition of FIELD for PACKAGE, or
297 #f if it could not be determined."
298 (define (goto port line column)
299 (unless (and (= (port-column port) (- column 1))
300 (= (port-line port) (- line 1)))
301 (unless (eof-object? (read-char port))
302 (goto port line column))))
304 (match (package-location package)
305 (($ <location> file line column)
308 ;; In general we want to keep relative file names for modules.
309 (with-fluids ((%file-port-name-canonicalization 'relative))
310 (call-with-input-file (search-path %load-path file)
312 (goto port line column)
314 (('package inits ...)
315 (let ((field (assoc field inits)))
318 ;; Put the `or' here, and not in the first argument of
319 ;; `and=>', to work around a compiler bug in 2.0.5.
320 (or (and=> (source-properties value)
321 source-properties->location)
322 (and=> (source-properties field)
323 source-properties->location)))
335 (define-condition-type &package-error &error
337 (package package-error-package))
339 (define-condition-type &package-input-error &package-error
341 (input package-error-invalid-input))
343 (define-condition-type &package-cross-build-system-error &package-error
344 package-cross-build-system-error?)
347 (define (package-full-name package)
348 "Return the full name of PACKAGE--i.e., `NAME-VERSION'."
349 (string-append (package-name package) "-" (package-version package)))
351 (define (%standard-patch-inputs)
352 (let* ((canonical (module-ref (resolve-interface '(gnu packages base))
354 (ref (lambda (module var)
356 (module-ref (resolve-interface module) var)))))
357 `(("tar" ,(ref '(gnu packages base) 'tar))
358 ("xz" ,(ref '(gnu packages compression) 'xz))
359 ("bzip2" ,(ref '(gnu packages compression) 'bzip2))
360 ("gzip" ,(ref '(gnu packages compression) 'gzip))
361 ("lzip" ,(ref '(gnu packages compression) 'lzip))
362 ("unzip" ,(ref '(gnu packages zip) 'unzip))
363 ("patch" ,(ref '(gnu packages base) 'patch))
364 ("locales" ,(ref '(gnu packages base) 'glibc-utf8-locales)))))
366 (define (default-guile)
367 "Return the default Guile package used to run the build code of
369 (let ((distro (resolve-interface '(gnu packages commencement))))
370 (module-ref distro 'guile-final)))
372 (define* (default-guile-derivation #:optional (system (%current-system)))
373 "Return the derivation for SYSTEM of the default Guile package used to run
374 the build code of derivation."
375 (package->derivation (default-guile) system
378 (define* (patch-and-repack source patches
384 (imported-modules '())
385 (guile-for-build (%guile-for-build))
386 (system (%current-system)))
387 "Unpack SOURCE (a derivation or store path), apply all of PATCHES, and
388 repack the tarball using the tools listed in INPUTS. When SNIPPET is true,
389 it must be an s-expression that will run from within the directory where
390 SOURCE was unpacked, after all of PATCHES have been applied. MODULES and
391 IMPORTED-MODULES specify modules to use/import for use by SNIPPET."
392 (define source-file-name
393 ;; SOURCE is usually a derivation, but it could be a store file.
394 (if (derivation? source)
395 (derivation->output-path source)
399 ;; The default value of the 'patch-inputs' field, and thus INPUTS is #f,
400 ;; so deal with that.
401 (let ((inputs (or inputs (%standard-patch-inputs))))
403 (match (assoc-ref inputs name)
407 (define decompression-type
408 (cond ((string-suffix? "gz" source-file-name) "gzip")
409 ((string-suffix? "Z" source-file-name) "gzip")
410 ((string-suffix? "bz2" source-file-name) "bzip2")
411 ((string-suffix? "lz" source-file-name) "lzip")
412 ((string-suffix? "zip" source-file-name) "unzip")
415 (define original-file-name
416 ;; Remove the store prefix plus the slash, hash, and hyphen.
417 (let* ((sans (string-drop source-file-name
418 (+ (string-length (%store-prefix)) 1)))
419 (dash (string-index sans #\-)))
420 (string-drop sans (+ 1 dash))))
422 (define (numeric-extension? file-name)
423 ;; Return true if FILE-NAME ends with digits.
424 (and=> (file-extension file-name)
425 (cut string-every char-set:hex-digit <>)))
427 (define (tarxz-name file-name)
428 ;; Return a '.tar.xz' file name based on FILE-NAME.
429 (let ((base (if (numeric-extension? file-name)
431 (file-sans-extension file-name))))
433 (if (equal? (file-extension base) "tar")
437 (define instantiate-patch
440 (interned-file patch #:recursive? #t))
442 (origin->derivation patch system))))
444 (mlet %store-monad ((tar -> (lookup-input "tar"))
445 (xz -> (lookup-input "xz"))
446 (patch -> (lookup-input "patch"))
447 (locales -> (lookup-input "locales"))
448 (decomp -> (lookup-input decompression-type))
449 (patches (sequence %store-monad
450 (map instantiate-patch patches))))
453 (use-modules (ice-9 ftw)
457 ;; The --sort option was added to GNU tar in version 1.28, released
458 ;; 2014-07-28. During bootstrap we must cope with older versions.
459 (define tar-supports-sort?
460 (zero? (system* (string-append #+tar "/bin/tar")
461 "cf" "/dev/null" "--files-from=/dev/null"
464 (define (apply-patch patch)
465 (format (current-error-port) "applying '~a'...~%" patch)
467 ;; Use '--force' so that patches that do not apply perfectly are
469 (zero? (system* (string-append #+patch "/bin/patch")
470 "--force" #+@flags "--input" patch)))
472 (define (first-file directory)
473 ;; Return the name of the first file in DIRECTORY.
474 (car (scandir directory
476 (not (member name '("." "..")))))))
478 ;; Encoding/decoding errors shouldn't be silent.
479 (fluid-set! %default-port-conversion-strategy 'error)
482 ;; First of all, install a UTF-8 locale so that UTF-8 file names
483 ;; are correctly interpreted. During bootstrap, LOCALES is #f.
485 (string-append #+locales "/lib/locale/"
487 (package-version locales))))
488 (setlocale LC_ALL "en_US.utf8"))
490 (setenv "PATH" (string-append #+xz "/bin" ":"
493 ;; SOURCE may be either a directory or a tarball.
494 (and (if (file-is-directory? #+source)
495 (let* ((store (%store-directory))
496 (len (+ 1 (string-length store)))
497 (base (string-drop #+source len))
498 (dash (string-index base #\-))
499 (directory (string-drop base (+ 1 dash))))
501 (copy-recursively #+source directory)
503 #+(if (string=? decompression-type "unzip")
504 #~(zero? (system* "unzip" #+source))
505 #~(zero? (system* (string-append #+tar "/bin/tar")
507 (let ((directory (first-file ".")))
508 (format (current-error-port)
509 "source is under '~a'~%" directory)
512 (and (every apply-patch '#+patches)
514 #~((let ((module (make-fresh-user-module)))
515 (module-use-interfaces! module
516 (map resolve-interface
518 ((@ (system base compile) compile)
521 #:opts %auto-compilation-options
525 (begin (chdir "..") #t)
527 (unless tar-supports-sort?
528 (call-with-output-file ".file_list"
530 (for-each (lambda (name) (format port "~a~%" name))
531 (find-files directory
533 #:fail-on-error? #t)))))
534 (zero? (apply system* (string-append #+tar "/bin/tar")
536 ;; avoid non-determinism in the archive
540 (if tar-supports-sort?
544 "--files-from=.file_list")))))))))
546 (let ((name (tarxz-name original-file-name))
547 (modules (delete-duplicates (cons '(guix build utils) modules))))
548 (gexp->derivation name build
552 #:guile-for-build guile-for-build))))
554 (define (transitive-inputs inputs)
555 "Return the closure of INPUTS when considering the 'propagated-inputs'
556 edges. Omit duplicate inputs, except for those already present in INPUTS
559 This is implemented as a breadth-first traversal such that INPUTS is
560 preserved, and only duplicate propagated inputs are removed."
561 (define (seen? seen item outputs)
562 (match (vhash-assq item seen)
563 ((_ . o) (equal? o outputs))
566 (let loop ((inputs inputs)
573 (if (null? propagated)
575 (loop (reverse (concatenate propagated)) result '() #f seen)))
576 (((and input (label (? package? package) outputs ...)) rest ...)
577 (if (and (not first?) (seen? seen package outputs))
578 (loop rest result propagated first? seen)
581 (cons (package-propagated-inputs package) propagated)
583 (vhash-consq package outputs seen))))
585 (loop rest (cons input result) propagated first? seen)))))
587 (define (package-direct-sources package)
588 "Return all source origins associated with PACKAGE; including origins in
590 `(,@(or (and=> (package-source package) list) '())
591 ,@(filter-map (match-lambda
592 ((_ (? origin? orig) _ ...)
595 (package-direct-inputs package))))
597 (define (package-transitive-sources package)
598 "Return PACKAGE's direct sources, and their direct sources, recursively."
600 (concatenate (filter-map (match-lambda
601 ((_ (? origin? orig) _ ...)
603 ((_ (? package? p) _ ...)
604 (package-direct-sources p))
606 (bag-transitive-inputs
607 (package->bag package))))))
609 (define (package-direct-inputs package)
610 "Return all the direct inputs of PACKAGE---i.e, its direct inputs along
611 with their propagated inputs."
612 (append (package-native-inputs package)
613 (package-inputs package)
614 (package-propagated-inputs package)))
616 (define (package-transitive-inputs package)
617 "Return the transitive inputs of PACKAGE---i.e., its direct inputs along
618 with their propagated inputs, recursively."
619 (transitive-inputs (package-direct-inputs package)))
621 (define (package-transitive-target-inputs package)
622 "Return the transitive target inputs of PACKAGE---i.e., its direct inputs
623 along with their propagated inputs, recursively. This only includes inputs
624 for the target system, and not native inputs."
625 (transitive-inputs (append (package-inputs package)
626 (package-propagated-inputs package))))
628 (define (package-transitive-native-inputs package)
629 "Return the transitive native inputs of PACKAGE---i.e., its direct inputs
630 along with their propagated inputs, recursively. This only includes inputs
631 for the host system (\"native inputs\"), and not target inputs."
632 (transitive-inputs (package-native-inputs package)))
634 (define (package-transitive-propagated-inputs package)
635 "Return the propagated inputs of PACKAGE, and their propagated inputs,
637 (transitive-inputs (package-propagated-inputs package)))
639 (define (package-transitive-native-search-paths package)
640 "Return the list of search paths for PACKAGE and its propagated inputs,
642 (append (package-native-search-paths package)
643 (append-map (match-lambda
644 ((label (? package? p) _ ...)
645 (package-native-search-paths p))
648 (package-transitive-propagated-inputs package))))
650 (define (transitive-input-references alist inputs)
651 "Return a list of (assoc-ref ALIST <label>) for each (<label> <package> . _)
652 in INPUTS and their transitive propagated inputs."
659 `(assoc-ref ,alist ,(label input)))
660 (transitive-inputs inputs)))
662 (define-syntax define-memoized/v
664 "Define a memoized single-valued unary procedure with docstring.
665 The procedure argument is compared to cached keys using `eqv?'."
667 ((_ (proc arg) docstring body body* ...)
668 (string? (syntax->datum #'docstring))
670 (let ((cache (make-hash-table)))
673 (match (hashv-get-handle cache arg)
677 (let ((result (let () body body* ...)))
678 (hashv-set! cache arg result)
682 (define-memoized/v (package-transitive-supported-systems package)
683 "Return the intersection of the systems supported by PACKAGE and those
684 supported by its dependencies."
685 (fold (lambda (input systems)
687 ((label (? package? p) . _)
689 string=? systems (package-transitive-supported-systems p)))
692 (package-supported-systems package)
693 (bag-direct-inputs (package->bag package))))
695 (define* (supported-package? package #:optional (system (%current-system)))
696 "Return true if PACKAGE is supported on SYSTEM--i.e., if PACKAGE and all its
697 dependencies are known to build on SYSTEM."
698 (member system (package-transitive-supported-systems package)))
700 (define (bag-direct-inputs bag)
701 "Same as 'package-direct-inputs', but applied to a bag."
702 (append (bag-build-inputs bag)
703 (bag-host-inputs bag)
704 (bag-target-inputs bag)))
706 (define (bag-transitive-inputs bag)
707 "Same as 'package-transitive-inputs', but applied to a bag."
708 (transitive-inputs (bag-direct-inputs bag)))
710 (define (bag-transitive-build-inputs bag)
711 "Same as 'package-transitive-native-inputs', but applied to a bag."
712 (transitive-inputs (bag-build-inputs bag)))
714 (define (bag-transitive-host-inputs bag)
715 "Same as 'package-transitive-target-inputs', but applied to a bag."
716 (transitive-inputs (bag-host-inputs bag)))
718 (define (bag-transitive-target-inputs bag)
719 "Return the \"target inputs\" of BAG, recursively."
720 (transitive-inputs (bag-target-inputs bag)))
724 ;;; Package derivations.
727 (define %derivation-cache
728 ;; Package to derivation-path mapping.
729 (make-weak-key-hash-table 100))
731 (define (cache! cache package system thunk)
732 "Memoize in CACHE the return values of THUNK as the derivation of PACKAGE on
734 ;; FIXME: This memoization should be associated with the open store, because
735 ;; otherwise it breaks when switching to a different store.
736 (let ((vals (call-with-values thunk list)))
737 ;; Use `hashq-set!' instead of `hash-set!' because `hash' returns the
738 ;; same value for all structs (as of Guile 2.0.6), and because pointer
739 ;; equality is sufficient in practice.
740 (hashq-set! cache package
742 ,@(or (hashq-ref cache package) '())))
743 (apply values vals)))
745 (define-syntax cached
747 "Memoize the result of BODY for the arguments PACKAGE and SYSTEM.
748 Return the cached result when available."
749 ((_ (=> cache) package system body ...)
750 (let ((thunk (lambda () body ...))
752 (match (hashq-ref cache package)
754 (match (assoc-ref alist key)
758 (cache! cache package key thunk))))
760 (cache! cache package key thunk)))))
761 ((_ package system body ...)
762 (cached (=> %derivation-cache) package system body ...))))
764 (define* (expand-input store package input system #:optional cross-system)
765 "Expand INPUT, an input tuple, such that it contains only references to
766 derivation paths or store paths. PACKAGE is only used to provide contextual
767 information in exceptions."
768 (define (intern file)
769 ;; Add FILE to the store. Set the `recursive?' bit to #t, so that
770 ;; file permissions are preserved.
771 (add-to-store store (basename file) #t "sha256" file))
775 (cut package-cross-derivation store <> cross-system system
777 (cut package-derivation store <> system #:graft? #f)))
780 (((? string? name) (? package? package))
781 (list name (derivation package)))
782 (((? string? name) (? package? package)
784 (list name (derivation package)
787 (and (? string?) (? derivation-path?) drv))
790 (and (? string?) (? file-exists? file)))
791 ;; Add FILE to the store. When FILE is in the sub-directory of a
792 ;; store path, it needs to be added anyway, so it can be used as a
794 (list name (intern file)))
795 (((? string? name) (? origin? source))
796 (list name (package-source-derivation store source system)))
798 (raise (condition (&package-input-error
803 ;; 'eq?' cache mapping packages to system+target+graft?-dependent bags.
804 ;; It significantly speeds things up when doing repeated calls to
805 ;; 'package->bag' as is the case when building a profile.
806 (make-weak-key-hash-table 200))
808 (define* (package->bag package #:optional
809 (system (%current-system))
810 (target (%current-target-system))
811 #:key (graft? (%graft?)))
812 "Compile PACKAGE into a bag for SYSTEM, possibly cross-compiled to TARGET,
814 (cached (=> %bag-cache)
815 package (list system target graft?)
816 ;; Bind %CURRENT-SYSTEM and %CURRENT-TARGET-SYSTEM so that thunked
817 ;; field values can refer to it.
818 (parameterize ((%current-system system)
819 (%current-target-system target))
821 (or (package-replacement package) package)
823 (($ <package> name version source build-system
824 args inputs propagated-inputs native-inputs
825 self-native-input? outputs)
826 (or (make-bag build-system (string-append name "-" version)
830 #:inputs (append (inputs)
833 #:native-inputs `(,@(if (and target
841 (&package-cross-build-system-error
845 (package package)))))))))))
848 ;; 'eq?' cache mapping package objects to a graft corresponding to their
849 ;; replacement package.
850 (make-weak-key-hash-table 200))
852 (define (input-graft store system)
853 "Return a procedure that, given a package with a graft, returns a graft, and
856 ((? package? package)
857 (let ((replacement (package-replacement package)))
859 (cached (=> %graft-cache) package system
860 (let ((orig (package-derivation store package system
862 (new (package-derivation store replacement system)))
865 (replacement new)))))))
869 (define (input-cross-graft store target system)
870 "Same as 'input-graft', but for cross-compilation inputs."
872 ((? package? package)
873 (let ((replacement (package-replacement package)))
875 (let ((orig (package-cross-derivation store package target system
877 (new (package-cross-derivation store replacement
881 (replacement new))))))
885 (define* (fold-bag-dependencies proc seed bag
887 "Fold PROC over the packages BAG depends on. Each package is visited only
888 once, in depth-first order. If NATIVE? is true, restrict to native
889 dependencies; otherwise, restrict to target dependencies."
892 (append (bag-build-inputs bag)
893 (bag-target-inputs bag)
896 (bag-host-inputs bag)))
897 (bag-host-inputs bag))
898 (((labels things _ ...) ...)
901 (let loop ((nodes nodes)
907 (((? package? head) . tail)
908 (if (set-contains? visited head)
909 (loop tail result visited)
910 (let ((inputs (bag-direct-inputs (package->bag head))))
912 (((labels things _ ...) ...)
913 (append things tail)))
915 (set-insert head visited)))))
917 (loop tail result visited)))))
919 (define* (bag-grafts store bag)
920 "Return the list of grafts potentially applicable to BAG. Potentially
921 applicable grafts are collected by looking at direct or indirect dependencies
922 of BAG that have a 'replacement'. Whether a graft is actually applicable
923 depends on whether the outputs of BAG depend on the items the grafts refer
924 to (see 'graft-derivation'.)"
925 (define system (bag-system bag))
926 (define target (bag-target bag))
928 (define native-grafts
929 (let ((->graft (input-graft store system)))
930 (fold-bag-dependencies (lambda (package grafts)
931 (match (->graft package)
933 (graft (cons graft grafts))))
937 (define target-grafts
939 (let ((->graft (input-cross-graft store target system)))
940 (fold-bag-dependencies (lambda (package grafts)
941 (match (->graft package)
943 (graft (cons graft grafts))))
949 ;; We can end up with several identical grafts if we stumble upon packages
950 ;; that are not 'eq?' but map to the same derivation (this can happen when
951 ;; using things like 'package-with-explicit-inputs'.) Hence the
952 ;; 'delete-duplicates' call.
954 (append native-grafts target-grafts)))
956 (define* (package-grafts store package
957 #:optional (system (%current-system))
959 "Return the list of grafts applicable to PACKAGE as built for SYSTEM and
961 (let* ((package (or (package-replacement package) package))
962 (bag (package->bag package system target)))
963 (bag-grafts store bag)))
965 (define* (bag->derivation store bag
967 "Return the derivation to build BAG for SYSTEM. Optionally, CONTEXT can be
968 a package object describing the context in which the call occurs, for improved
971 (bag->cross-derivation store bag)
972 (let* ((system (bag-system bag))
973 (inputs (bag-transitive-inputs bag))
974 (input-drvs (map (cut expand-input store context <> system)
976 (paths (delete-duplicates
977 (append-map (match-lambda
978 ((_ (? package? p) _ ...)
979 (package-native-search-paths
984 (apply (bag-build bag)
985 store (bag-name bag) input-drvs
987 #:outputs (bag-outputs bag) #:system system
988 (bag-arguments bag)))))
990 (define* (bag->cross-derivation store bag
992 "Return the derivation to build BAG, which is actually a cross build.
993 Optionally, CONTEXT can be a package object denoting the context of the call.
994 This is an internal procedure."
995 (let* ((system (bag-system bag))
996 (target (bag-target bag))
997 (host (bag-transitive-host-inputs bag))
998 (host-drvs (map (cut expand-input store context <> system target)
1000 (target* (bag-transitive-target-inputs bag))
1001 (target-drvs (map (cut expand-input store context <> system)
1003 (build (bag-transitive-build-inputs bag))
1004 (build-drvs (map (cut expand-input store context <> system)
1006 (all (append build target* host))
1007 (paths (delete-duplicates
1008 (append-map (match-lambda
1009 ((_ (? package? p) _ ...)
1010 (package-search-paths p))
1013 (npaths (delete-duplicates
1014 (append-map (match-lambda
1015 ((_ (? package? p) _ ...)
1016 (package-native-search-paths
1021 (apply (bag-build bag)
1022 store (bag-name bag)
1023 #:native-drvs build-drvs
1024 #:target-drvs (append host-drvs target-drvs)
1025 #:search-paths paths
1026 #:native-search-paths npaths
1027 #:outputs (bag-outputs bag)
1028 #:system system #:target target
1029 (bag-arguments bag))))
1031 (define* (package-derivation store package
1032 #:optional (system (%current-system))
1033 #:key (graft? (%graft?)))
1034 "Return the <derivation> object of PACKAGE for SYSTEM."
1036 ;; Compute the derivation and cache the result. Caching is important
1037 ;; because some derivations, such as the implicit inputs of the GNU build
1038 ;; system, will be queried many, many times in a row.
1039 (cached package (cons system graft?)
1040 (let* ((bag (package->bag package system #f #:graft? graft?))
1041 (drv (bag->derivation store bag package)))
1043 (match (bag-grafts store bag)
1047 (let ((guile (package-derivation store (default-guile)
1048 system #:graft? #f)))
1049 ;; TODO: As an optimization, we can simply graft the tip
1050 ;; of the derivation graph since 'graft-derivation'
1052 (graft-derivation store drv grafts
1057 (define* (package-cross-derivation store package target
1058 #:optional (system (%current-system))
1059 #:key (graft? (%graft?)))
1060 "Cross-build PACKAGE for TARGET (a GNU triplet) from host SYSTEM (a Guix
1061 system identifying string)."
1062 (cached package (list system target graft?)
1063 (let* ((bag (package->bag package system target #:graft? graft?))
1064 (drv (bag->derivation store bag package)))
1066 (match (bag-grafts store bag)
1070 (graft-derivation store drv grafts
1073 (package-derivation store (default-guile)
1074 system #:graft? #f))))
1077 (define* (package-output store package
1078 #:optional (output "out") (system (%current-system)))
1079 "Return the output path of PACKAGE's OUTPUT for SYSTEM---where OUTPUT is the
1080 symbolic output name, such as \"out\". Note that this procedure calls
1081 `package-derivation', which is costly."
1082 (let ((drv (package-derivation store package system)))
1083 (derivation->output-path drv output)))
1087 ;;; Monadic interface.
1090 (define (set-guile-for-build guile)
1091 "This monadic procedure changes the Guile currently used to run the build
1092 code of derivations to GUILE, a package object."
1094 (let ((guile (package-derivation store guile)))
1095 (values (%guile-for-build guile) store))))
1097 (define* (package-file package
1100 system (output "out") target)
1101 "Return as a monadic value the absolute file name of FILE within the
1102 OUTPUT directory of PACKAGE. When FILE is omitted, return the name of the
1103 OUTPUT directory of PACKAGE. When TARGET is true, use it as a
1104 cross-compilation target triplet."
1106 (define compute-derivation
1108 (cut package-cross-derivation <> <> target <>)
1109 package-derivation))
1111 (let* ((system (or system (%current-system)))
1112 (drv (compute-derivation store package system))
1113 (out (derivation->output-path drv output)))
1115 (string-append out "/" file)
1119 (define package->derivation
1120 (store-lift package-derivation))
1122 (define package->cross-derivation
1123 (store-lift package-cross-derivation))
1125 (define-gexp-compiler (package-compiler (package package?) system target)
1126 ;; Compile PACKAGE to a derivation for SYSTEM, optionally cross-compiled for
1127 ;; TARGET. This is used when referring to a package from within a gexp.
1129 (package->cross-derivation package target system)
1130 (package->derivation package system)))
1132 (define* (origin->derivation origin
1133 #:optional (system (%current-system)))
1134 "Return the derivation corresponding to ORIGIN."
1136 (($ <origin> uri method sha256 name (= force ()) #f)
1137 ;; No patches, no snippet: this is a fixed-output derivation.
1138 (method uri 'sha256 sha256 name #:system system))
1139 (($ <origin> uri method sha256 name (= force (patches ...)) snippet
1140 (flags ...) inputs (modules ...) (imported-modules ...)
1142 ;; Patches and/or a snippet.
1143 (mlet %store-monad ((source (method uri 'sha256 sha256 name
1145 (guile (package->derivation (or guile-for-build
1149 (patch-and-repack source patches
1155 #:imported-modules modules
1156 #:guile-for-build guile)))))
1158 (define-gexp-compiler (origin-compiler (origin origin?) system target)
1159 ;; Compile ORIGIN to a derivation for SYSTEM. This is used when referring
1160 ;; to an origin from within a gexp.
1161 (origin->derivation origin system))
1163 (define package-source-derivation ;somewhat deprecated
1164 (let ((lower (store-lower origin->derivation)))
1165 (lambda* (store source #:optional (system (%current-system)))
1166 "Return the derivation or file corresponding to SOURCE, which can be an
1167 <origin> or a file name. When SOURCE is a file name, return either the
1168 interned file name (if SOURCE is outside of the store) or SOURCE itself (if
1169 SOURCE is already a store item.)"
1171 ((and (? string?) (? direct-store-path?) file)
1174 (add-to-store store (basename file) #t "sha256" file))
1176 (lower store source system))))))