1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2014, 2015, 2017, 2018 Mark H Weaver <mhw@netris.org>
4 ;;; Copyright © 2015 Eric Bavier <bavier@member.fsf.org>
5 ;;; Copyright © 2016 Alex Kost <alezost@gmail.com>
6 ;;; Copyright © 2017, 2019, 2020 Efraim Flashner <efraim@flashner.co.il>
7 ;;; Copyright © 2019 Marius Bakke <mbakke@fastmail.com>
9 ;;; This file is part of GNU Guix.
11 ;;; GNU Guix is free software; you can redistribute it and/or modify it
12 ;;; under the terms of the GNU General Public License as published by
13 ;;; the Free Software Foundation; either version 3 of the License, or (at
14 ;;; your option) any later version.
16 ;;; GNU Guix is distributed in the hope that it will be useful, but
17 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;;; GNU General Public License for more details.
21 ;;; You should have received a copy of the GNU General Public License
22 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
24 (define-module (guix packages)
25 #:use-module (guix utils)
26 #:use-module (guix records)
27 #:use-module (guix store)
28 #:use-module (guix monads)
29 #:use-module (guix gexp)
30 #:use-module (guix base32)
31 #:autoload (guix base64) (base64-decode)
32 #:use-module (guix grafts)
33 #:use-module (guix derivations)
34 #:use-module (guix memoization)
35 #:use-module (guix build-system)
36 #:use-module (guix search-paths)
37 #:use-module (guix sets)
38 #:use-module (guix deprecation)
39 #:use-module (guix i18n)
40 #:use-module (ice-9 match)
41 #:use-module (ice-9 vlist)
42 #:use-module (ice-9 regex)
43 #:use-module (srfi srfi-1)
44 #:use-module (srfi srfi-9 gnu)
45 #:use-module (srfi srfi-11)
46 #:use-module (srfi srfi-26)
47 #:use-module (srfi srfi-34)
48 #:use-module (srfi srfi-35)
49 #:use-module (rnrs bytevectors)
50 #:use-module (web uri)
51 #:re-export (%current-system
52 %current-target-system
53 search-path-specification) ;for convenience
54 #:export (content-hash
56 content-hash-algorithm
65 origin-sha256 ;deprecated
67 origin-actual-file-name
89 package-propagated-inputs
91 package-native-search-paths
98 package-supported-systems
105 package-field-location
107 package-direct-sources
108 package-transitive-sources
109 package-direct-inputs
110 package-transitive-inputs
111 package-transitive-target-inputs
112 package-transitive-native-inputs
113 package-transitive-propagated-inputs
114 package-transitive-native-search-paths
115 package-transitive-supported-systems
117 package-input-rewriting
118 package-input-rewriting/spec
119 package-source-derivation
121 package-cross-derivation
124 package-patched-vulnerabilities
126 package-with-extra-patches
127 package-with-c-toolchain
130 transitive-input-references
134 %hydra-supported-systems
139 package-error-package
142 package-error-invalid-input
143 &package-cross-build-system-error
144 package-cross-build-system-error?
149 bag-transitive-inputs
150 bag-transitive-host-inputs
151 bag-transitive-build-inputs
152 bag-transitive-target-inputs
156 default-guile-derivation
160 package->cross-derivation
165 ;;; This module provides a high-level mechanism to define packages in a
166 ;;; Guix-based distribution.
170 ;; Crytographic content hash.
171 (define-immutable-record-type <content-hash>
172 (%content-hash algorithm value)
174 (algorithm content-hash-algorithm) ;symbol
175 (value content-hash-value)) ;bytevector
177 (define-syntax-rule (define-content-hash-constructor name
178 (algorithm size) ...)
179 "Define NAME as a <content-hash> constructor that ensures that (1) its
180 second argument is among the listed ALGORITHM, and (2), when possible, that
181 its first argument has the right size for the chosen algorithm."
184 (syntax-case s (algorithm ...)
186 (let ((bv* (syntax->datum #'bv)))
187 (when (and (bytevector? bv*)
188 (not (= size (bytevector-length bv*))))
189 (syntax-violation 'content-hash "invalid content hash length" s))
190 #'(%content-hash 'algorithm bv)))
193 (define-content-hash-constructor build-content-hash
200 (define-syntax content-hash
202 "Return a content hash with the given parameters. The default hash
203 algorithm is sha256. If the first argument is a literal string, it is decoded
204 as base32. Otherwise, it must be a bytevector."
205 ;; What we'd really want here is something like C++ 'constexpr'.
208 (string? (syntax->datum #'str))
209 #'(content-hash str sha256))
211 (string? (syntax->datum #'str))
212 (with-syntax ((bv (base32 (syntax->datum #'str))))
213 #'(content-hash bv algorithm)))
214 ((_ (id str) algorithm)
215 (and (string? (syntax->datum #'str))
216 (free-identifier=? #'id #'base32))
217 (with-syntax ((bv (nix-base32-string->bytevector (syntax->datum #'str))))
218 #'(content-hash bv algorithm)))
219 ((_ (id str) algorithm)
220 (and (string? (syntax->datum #'str))
221 (free-identifier=? #'id #'base64))
222 (with-syntax ((bv (base64-decode (syntax->datum #'str))))
223 #'(content-hash bv algorithm)))
225 #'(content-hash bv sha256))
227 #'(build-content-hash bv hash)))))
229 (define (print-content-hash hash port)
230 (format port "#<content-hash ~a:~a>"
231 (content-hash-algorithm hash)
232 (and=> (content-hash-value hash)
233 bytevector->nix-base32-string)))
235 (set-record-type-printer! <content-hash> print-content-hash)
238 ;; The source of a package, such as a tarball URL and fetcher---called
239 ;; "origin" to avoid name clash with `package-source', `source', etc.
240 (define-record-type* <origin>
244 (uri origin-uri) ; string
245 (method origin-method) ; procedure
246 (hash origin-hash) ; <content-hash>
247 (file-name origin-file-name (default #f)) ; optional file name
249 ;; Patches are delayed so that the 'search-patch' calls are made lazily,
250 ;; which reduces I/O on startup and allows patch-not-found errors to be
251 ;; gracefully handled at run time.
252 (patches origin-patches ; list of file names
253 (default '()) (delayed))
255 (snippet origin-snippet (default #f)) ; sexp or #f
256 (patch-flags origin-patch-flags ; list of strings
259 ;; Patching requires Guile, GNU Patch, and a few more. These two fields are
260 ;; used to specify these dependencies when needed.
261 (patch-inputs origin-patch-inputs ; input list or #f
263 (modules origin-modules ; list of module names
266 (patch-guile origin-patch-guile ; package or #f
269 (define-syntax origin-compatibility-helper
270 (syntax-rules (sha256)
272 (%origin fields ...))
273 ((_ ((sha256 exp) rest ...) (others ...))
275 (hash (content-hash exp sha256))
277 ((_ (field rest ...) (others ...))
278 (origin-compatibility-helper (rest ...)
279 (others ... field)))))
281 (define-syntax-rule (origin fields ...)
282 "Build an <origin> record, automatically converting 'sha256' field
283 specifications to 'hash'."
284 (origin-compatibility-helper (fields ...) ()))
286 (define-deprecated (origin-sha256 origin)
288 (let ((hash (origin-hash origin)))
289 (unless (eq? (content-hash-algorithm hash) 'sha256)
290 (raise (condition (&message
291 (message (G_ "no SHA256 hash for origin"))))))
292 (content-hash-value hash)))
294 (define (print-origin origin port)
295 "Write a concise representation of ORIGIN to PORT."
297 (($ <origin> uri method hash file-name patches)
298 (simple-format port "#<origin ~s ~a ~s ~a>"
301 (number->string (object-address origin) 16)))))
303 (set-record-type-printer! <origin> print-origin)
305 (define-syntax-rule (define-compile-time-decoder name string->bytevector)
306 "Define NAME as a macro that runs STRING->BYTEVECTOR at macro expansion time
310 "Return the bytevector corresponding to the given textual
314 (string? (syntax->datum #'str))
315 ;; A literal string: do the conversion at expansion time.
316 (with-syntax ((bv (string->bytevector (syntax->datum #'str))))
319 #'(string->bytevector str))))))
321 (define-compile-time-decoder base32 nix-base32-string->bytevector)
322 (define-compile-time-decoder base64 base64-decode)
324 (define (origin-actual-file-name origin)
325 "Return the file name of ORIGIN, either its 'file-name' field or the file
327 (define (uri->file-name uri)
328 ;; Return the 'base name' of URI or URI itself, where URI is a string.
329 (let ((path (and=> (string->uri uri) uri-path)))
334 (or (origin-file-name origin)
335 (match (origin-uri origin)
337 (uri->file-name head))
339 (uri->file-name uri))
341 ;; git, svn, cvs, etc. reference
345 (define %supported-systems
346 ;; This is the list of system types that are supported. By default, we
347 ;; expect all packages to build successfully here.
348 '("x86_64-linux" "i686-linux" "armhf-linux" "aarch64-linux" "mips64el-linux" "i586-gnu"))
350 (define %hurd-systems
351 ;; The GNU/Hurd systems for which support is being developed.
352 '("i586-gnu" "i686-gnu"))
354 (define %hydra-supported-systems
355 ;; This is the list of system types for which build machines are available.
357 ;; XXX: MIPS is unavailable in CI:
358 ;; <https://lists.gnu.org/archive/html/guix-devel/2017-03/msg00790.html>.
359 (fold delete %supported-systems '("mips64el-linux")))
363 (define-record-type* <package>
367 (name package-name) ; string
368 (version package-version) ; string
369 (source package-source) ; <origin> instance
370 (build-system package-build-system) ; build system
371 (arguments package-arguments ; arguments for the build method
372 (default '()) (thunked))
374 (inputs package-inputs ; input packages or derivations
375 (default '()) (thunked))
376 (propagated-inputs package-propagated-inputs ; same, but propagated
377 (default '()) (thunked))
378 (native-inputs package-native-inputs ; native input packages/derivations
379 (default '()) (thunked))
381 (outputs package-outputs ; list of strings
385 ; <search-path-specification>,
386 ; for native and cross
388 (native-search-paths package-native-search-paths (default '()))
389 (search-paths package-search-paths (default '()))
391 ;; The 'replacement' field is marked as "innate" because it never makes
392 ;; sense to inherit a replacement as is. See the 'package/inherit' macro.
393 (replacement package-replacement ; package | #f
394 (default #f) (thunked) (innate))
396 (synopsis package-synopsis) ; one-line description
397 (description package-description) ; one or two paragraphs
398 (license package-license)
399 (home-page package-home-page)
400 (supported-systems package-supported-systems ; list of strings
401 (default %supported-systems))
403 (properties package-properties (default '())) ; alist for anything else
405 (location package-location
406 (default (and=> (current-source-location)
407 source-properties->location))
410 (set-record-type-printer! <package>
411 (lambda (package port)
412 (let ((loc (package-location package))
413 (format simple-format))
414 (format port "#<package ~a@~a ~a~a>"
415 (package-name package)
416 (package-version package)
422 (number->string (object-address
426 (define-syntax-rule (package/inherit p overrides ...)
427 "Like (package (inherit P) OVERRIDES ...), except that the same
428 transformation is done to the package replacement, if any. P must be a bare
429 identifier, and will be bound to either P or its replacement when evaluating
434 (replacement (and=> (package-replacement p) loop)))))
436 (define (package-upstream-name package)
437 "Return the upstream name of PACKAGE, which could be different from the name
439 (or (assq-ref (package-properties package) 'upstream-name)
440 (package-name package)))
442 (define (hidden-package p)
443 "Return a \"hidden\" version of P--i.e., one that 'fold-packages' and thus,
444 user interfaces, ignores."
447 (properties `((hidden? . #t)
448 ,@(package-properties p)))))
450 (define (hidden-package? p)
451 "Return true if P is \"hidden\"--i.e., must not be visible to user
453 (assoc-ref (package-properties p) 'hidden?))
455 (define (package-superseded p)
456 "Return the package the supersedes P, or #f if P is still current."
457 (assoc-ref (package-properties p) 'superseded))
459 (define (deprecated-package old-name p)
460 "Return a package called OLD-NAME and marked as superseded by P, a package
465 (properties `((superseded . ,p)))))
467 (define (package-field-location package field)
468 "Return the source code location of the definition of FIELD for PACKAGE, or
469 #f if it could not be determined."
470 (define (goto port line column)
471 (unless (and (= (port-column port) (- column 1))
472 (= (port-line port) (- line 1)))
473 (unless (eof-object? (read-char port))
474 (goto port line column))))
476 (match (package-location package)
477 (($ <location> file line column)
480 ;; In general we want to keep relative file names for modules.
481 (call-with-input-file (search-path %load-path file)
483 (goto port line column)
485 (('package inits ...)
486 (let ((field (assoc field inits)))
489 (let ((loc (and=> (source-properties value)
490 source-properties->location)))
492 ;; Preserve the original file name, which may be a
493 ;; relative file name.
494 (set-field loc (location-file) file))))
506 (define-condition-type &package-error &error
508 (package package-error-package))
510 (define-condition-type &package-input-error &package-error
512 (input package-error-invalid-input))
514 (define-condition-type &package-cross-build-system-error &package-error
515 package-cross-build-system-error?)
517 (define* (package-full-name package #:optional (delimiter "@"))
518 "Return the full name of PACKAGE--i.e., `NAME@VERSION'. By specifying
519 DELIMITER (a string), you can customize what will appear between the name and
520 the version. By default, DELIMITER is \"@\"."
521 (string-append (package-name package) delimiter (package-version package)))
523 (define (patch-file-name patch)
524 "Return the basename of PATCH's file name, or #f if the file name could not
530 (and=> (origin-actual-file-name patch) basename))))
532 (define %vulnerability-regexp
533 ;; Regexp matching a CVE identifier in patch file names.
534 (make-regexp "CVE-[0-9]{4}-[0-9]+"))
536 (define (package-patched-vulnerabilities package)
537 "Return the list of patched vulnerabilities of PACKAGE as a list of CVE
538 identifiers. The result is inferred from the file names of patches."
539 (define (patch-vulnerabilities patch)
540 (map (cut match:substring <> 0)
541 (list-matches %vulnerability-regexp patch)))
543 (let ((patches (filter-map patch-file-name
544 (or (and=> (package-source package)
547 (append-map patch-vulnerabilities patches)))
549 (define (%standard-patch-inputs)
550 (let* ((canonical (module-ref (resolve-interface '(gnu packages base))
552 (ref (lambda (module var)
554 (module-ref (resolve-interface module) var)))))
555 `(("tar" ,(ref '(gnu packages base) 'tar))
556 ("xz" ,(ref '(gnu packages compression) 'xz))
557 ("bzip2" ,(ref '(gnu packages compression) 'bzip2))
558 ("gzip" ,(ref '(gnu packages compression) 'gzip))
559 ("lzip" ,(ref '(gnu packages compression) 'lzip))
560 ("unzip" ,(ref '(gnu packages compression) 'unzip))
561 ("patch" ,(ref '(gnu packages base) 'patch))
562 ("locales" ,(ref '(gnu packages base) 'glibc-utf8-locales)))))
564 (define (default-guile)
565 "Return the default Guile package used to run the build code of
567 (let ((distro (resolve-interface '(gnu packages commencement))))
568 (module-ref distro 'guile-final)))
570 (define (guile-for-grafts)
571 "Return the Guile package used to build grafting derivations."
572 ;; Guile 2.2 would not work due to <https://bugs.gnu.org/28211> when
573 ;; grafting packages.
574 (let ((distro (resolve-interface '(gnu packages guile))))
575 (module-ref distro 'guile-2.0)))
577 (define* (default-guile-derivation #:optional (system (%current-system)))
578 "Return the derivation for SYSTEM of the default Guile package used to run
579 the build code of derivation."
580 (package->derivation (default-guile) system
583 (define* (patch-and-repack source patches
589 (guile-for-build (%guile-for-build))
590 (system (%current-system)))
591 "Unpack SOURCE (a derivation or store path), apply all of PATCHES, and
592 repack the tarball using the tools listed in INPUTS. When SNIPPET is true,
593 it must be an s-expression that will run from within the directory where
594 SOURCE was unpacked, after all of PATCHES have been applied. MODULES
595 specifies modules in scope when evaluating SNIPPET."
596 (define source-file-name
597 ;; SOURCE is usually a derivation, but it could be a store file.
598 (if (derivation? source)
599 (derivation->output-path source)
603 ;; The default value of the 'patch-inputs' field, and thus INPUTS is #f,
604 ;; so deal with that.
605 (let ((inputs (or inputs (%standard-patch-inputs))))
607 (match (assoc-ref inputs name)
611 (define decompression-type
612 (cond ((string-suffix? "gz" source-file-name) "gzip")
613 ((string-suffix? "Z" source-file-name) "gzip")
614 ((string-suffix? "bz2" source-file-name) "bzip2")
615 ((string-suffix? "lz" source-file-name) "lzip")
616 ((string-suffix? "zip" source-file-name) "unzip")
619 (define original-file-name
620 ;; Remove the store prefix plus the slash, hash, and hyphen.
621 (let* ((sans (string-drop source-file-name
622 (+ (string-length (%store-prefix)) 1)))
623 (dash (string-index sans #\-)))
624 (string-drop sans (+ 1 dash))))
626 (define (numeric-extension? file-name)
627 ;; Return true if FILE-NAME ends with digits.
628 (and=> (file-extension file-name)
629 (cut string-every char-set:hex-digit <>)))
631 (define (checkout? directory)
632 ;; Return true if DIRECTORY is a checkout (git, svn, etc).
633 (string-suffix? "-checkout" directory))
635 (define (tarxz-name file-name)
636 ;; Return a '.tar.xz' file name based on FILE-NAME.
637 (let ((base (cond ((numeric-extension? file-name)
639 ((checkout? file-name)
640 (string-drop-right file-name 9))
641 (else (file-sans-extension file-name)))))
643 (if (equal? (file-extension base) "tar")
647 (define instantiate-patch
649 ((? string? patch) ;deprecated
650 (interned-file patch #:recursive? #t))
651 ((? struct? patch) ;origin, local-file, etc.
652 (lower-object patch system))))
654 (mlet %store-monad ((tar -> (lookup-input "tar"))
655 (xz -> (lookup-input "xz"))
656 (patch -> (lookup-input "patch"))
657 (locales -> (lookup-input "locales"))
658 (decomp -> (lookup-input decompression-type))
659 (patches (sequence %store-monad
660 (map instantiate-patch patches))))
662 (with-imported-modules '((guix build utils))
664 (use-modules (ice-9 ftw)
668 ;; The --sort option was added to GNU tar in version 1.28, released
669 ;; 2014-07-28. During bootstrap we must cope with older versions.
670 (define tar-supports-sort?
671 (zero? (system* (string-append #+tar "/bin/tar")
672 "cf" "/dev/null" "--files-from=/dev/null"
675 (define (apply-patch patch)
676 (format (current-error-port) "applying '~a'...~%" patch)
678 ;; Use '--force' so that patches that do not apply perfectly are
679 ;; rejected. Use '--no-backup-if-mismatch' to prevent making
680 ;; "*.orig" file if a patch is applied with offset.
681 (invoke (string-append #+patch "/bin/patch")
682 "--force" "--no-backup-if-mismatch"
683 #+@flags "--input" patch))
685 (define (first-file directory)
686 ;; Return the name of the first file in DIRECTORY.
687 (car (scandir directory
689 (not (member name '("." "..")))))))
691 ;; Encoding/decoding errors shouldn't be silent.
692 (fluid-set! %default-port-conversion-strategy 'error)
695 ;; First of all, install a UTF-8 locale so that UTF-8 file names
696 ;; are correctly interpreted. During bootstrap, LOCALES is #f.
698 (string-append #+locales "/lib/locale/"
701 (package-version locales)))))
702 (setlocale LC_ALL "en_US.utf8"))
704 (setenv "PATH" (string-append #+xz "/bin" ":"
707 ;; SOURCE may be either a directory or a tarball.
708 (if (file-is-directory? #+source)
709 (let* ((store (%store-directory))
710 (len (+ 1 (string-length store)))
711 (base (string-drop #+source len))
712 (dash (string-index base #\-))
713 (directory (string-drop base (+ 1 dash))))
715 (copy-recursively #+source directory))
716 #+(if (string=? decompression-type "unzip")
717 #~(invoke "unzip" #+source)
718 #~(invoke (string-append #+tar "/bin/tar")
721 (let ((directory (first-file ".")))
722 (format (current-error-port)
723 "source is under '~a'~%" directory)
726 (for-each apply-patch '#+patches)
728 (let ((result #+(if snippet
729 #~(let ((module (make-fresh-user-module)))
730 (module-use-interfaces!
732 (map resolve-interface '#+modules))
733 ((@ (system base compile) compile)
736 #:opts %auto-compilation-options
739 ;; Issue a warning unless the result is #t.
740 (unless (eqv? result #t)
741 (format (current-error-port) "\
742 ## WARNING: the snippet returned `~s'. Return values other than #t
743 ## are deprecated. Please migrate this package so that its snippet
744 ## reports errors by raising an exception, and otherwise returns #t.~%"
747 (error "snippet returned false")))
751 (unless tar-supports-sort?
752 (call-with-output-file ".file_list"
754 (for-each (lambda (name)
755 (format port "~a~%" name))
756 (find-files directory
758 #:fail-on-error? #t)))))
760 (string-append #+tar "/bin/tar")
762 ;; Avoid non-determinism in the archive. Set the mtime
763 ;; to 1 as is the case in the store (software like gzip
764 ;; behaves differently when it stumbles upon mtime = 0).
768 (if tar-supports-sort?
772 "--files-from=.file_list")))))))
774 (let ((name (tarxz-name original-file-name)))
775 (gexp->derivation name build
778 #:guile-for-build guile-for-build
779 #:properties `((type . origin)
780 (patches . ,(length patches)))))))
782 (define (package-with-patches original patches)
783 "Return package ORIGINAL with PATCHES applied."
784 (package (inherit original)
785 (source (origin (inherit (package-source original))
786 (patches patches)))))
788 (define (package-with-extra-patches original patches)
789 "Return package ORIGINAL with all PATCHES appended to its list of patches."
790 (package-with-patches original
791 (append (origin-patches (package-source original))
794 (define (package-with-c-toolchain package toolchain)
795 "Return a variant of PACKAGE that uses TOOLCHAIN instead of the default GNU
796 C/C++ toolchain. TOOLCHAIN must be a list of inputs (label/package tuples)
797 providing equivalent functionality, such as the 'gcc-toolchain' package."
798 (let ((bs (package-build-system package)))
799 (package/inherit package
800 (build-system (build-system-with-c-toolchain bs toolchain)))))
802 (define (transitive-inputs inputs)
803 "Return the closure of INPUTS when considering the 'propagated-inputs'
804 edges. Omit duplicate inputs, except for those already present in INPUTS
807 This is implemented as a breadth-first traversal such that INPUTS is
808 preserved, and only duplicate propagated inputs are removed."
809 (define (seen? seen item outputs)
810 ;; FIXME: We're using pointer identity here, which is extremely sensitive
811 ;; to memoization in package-producing procedures; see
812 ;; <https://bugs.gnu.org/30155>.
813 (match (vhash-assq item seen)
814 ((_ . o) (equal? o outputs))
817 (let loop ((inputs inputs)
824 (if (null? propagated)
826 (loop (reverse (concatenate propagated)) result '() #f seen)))
827 (((and input (label (? package? package) outputs ...)) rest ...)
828 (if (and (not first?) (seen? seen package outputs))
829 (loop rest result propagated first? seen)
832 (cons (package-propagated-inputs package) propagated)
834 (vhash-consq package outputs seen))))
836 (loop rest (cons input result) propagated first? seen)))))
838 (define (package-direct-sources package)
839 "Return all source origins associated with PACKAGE; including origins in
841 `(,@(or (and=> (package-source package) list) '())
842 ,@(filter-map (match-lambda
843 ((_ (? origin? orig) _ ...)
846 (package-direct-inputs package))))
848 (define (package-transitive-sources package)
849 "Return PACKAGE's direct sources, and their direct sources, recursively."
851 (concatenate (filter-map (match-lambda
852 ((_ (? origin? orig) _ ...)
854 ((_ (? package? p) _ ...)
855 (package-direct-sources p))
857 (bag-transitive-inputs
858 (package->bag package))))))
860 (define (package-direct-inputs package)
861 "Return all the direct inputs of PACKAGE---i.e, its direct inputs along
862 with their propagated inputs."
863 (append (package-native-inputs package)
864 (package-inputs package)
865 (package-propagated-inputs package)))
867 (define (package-transitive-inputs package)
868 "Return the transitive inputs of PACKAGE---i.e., its direct inputs along
869 with their propagated inputs, recursively."
870 (transitive-inputs (package-direct-inputs package)))
872 (define (package-transitive-target-inputs package)
873 "Return the transitive target inputs of PACKAGE---i.e., its direct inputs
874 along with their propagated inputs, recursively. This only includes inputs
875 for the target system, and not native inputs."
876 (transitive-inputs (append (package-inputs package)
877 (package-propagated-inputs package))))
879 (define (package-transitive-native-inputs package)
880 "Return the transitive native inputs of PACKAGE---i.e., its direct inputs
881 along with their propagated inputs, recursively. This only includes inputs
882 for the host system (\"native inputs\"), and not target inputs."
883 (transitive-inputs (package-native-inputs package)))
885 (define (package-transitive-propagated-inputs package)
886 "Return the propagated inputs of PACKAGE, and their propagated inputs,
888 (transitive-inputs (package-propagated-inputs package)))
890 (define (package-transitive-native-search-paths package)
891 "Return the list of search paths for PACKAGE and its propagated inputs,
893 (append (package-native-search-paths package)
894 (append-map (match-lambda
895 ((label (? package? p) _ ...)
896 (package-native-search-paths p))
899 (package-transitive-propagated-inputs package))))
901 (define (transitive-input-references alist inputs)
902 "Return a list of (assoc-ref ALIST <label>) for each (<label> <package> . _)
903 in INPUTS and their transitive propagated inputs."
910 `(assoc-ref ,alist ,(label input)))
911 (transitive-inputs inputs)))
913 (define package-transitive-supported-systems
915 (define supported-systems
916 (mlambda (package system)
917 (parameterize ((%current-system system))
918 (fold (lambda (input systems)
920 ((label (? package? package) . _)
921 (lset-intersection string=? systems
922 (supported-systems package system)))
925 (package-supported-systems package)
926 (bag-direct-inputs (package->bag package))))))
928 (lambda* (package #:optional (system (%current-system)))
929 "Return the intersection of the systems supported by PACKAGE and those
930 supported by its dependencies."
931 (supported-systems package system))))
933 (define* (supported-package? package #:optional (system (%current-system)))
934 "Return true if PACKAGE is supported on SYSTEM--i.e., if PACKAGE and all its
935 dependencies are known to build on SYSTEM."
936 (member system (package-transitive-supported-systems package system)))
938 (define (bag-direct-inputs bag)
939 "Same as 'package-direct-inputs', but applied to a bag."
940 (append (bag-build-inputs bag)
941 (bag-host-inputs bag)
942 (bag-target-inputs bag)))
944 (define (bag-transitive-inputs bag)
945 "Same as 'package-transitive-inputs', but applied to a bag."
946 (parameterize ((%current-target-system #f)
947 (%current-system (bag-system bag)))
948 (transitive-inputs (bag-direct-inputs bag))))
950 (define (bag-transitive-build-inputs bag)
951 "Same as 'package-transitive-native-inputs', but applied to a bag."
952 (parameterize ((%current-target-system #f)
953 (%current-system (bag-system bag)))
954 (transitive-inputs (bag-build-inputs bag))))
956 (define (bag-transitive-host-inputs bag)
957 "Same as 'package-transitive-target-inputs', but applied to a bag."
958 (parameterize ((%current-target-system (bag-target bag))
959 (%current-system (bag-system bag)))
960 (transitive-inputs (bag-host-inputs bag))))
962 (define (bag-transitive-target-inputs bag)
963 "Return the \"target inputs\" of BAG, recursively."
964 (parameterize ((%current-target-system (bag-target bag))
965 (%current-system (bag-system bag)))
966 (transitive-inputs (bag-target-inputs bag))))
968 (define* (package-closure packages #:key (system (%current-system)))
969 "Return the closure of PACKAGES on SYSTEM--i.e., PACKAGES and the list of
970 packages they depend on, recursively."
971 (let loop ((packages packages)
973 (closure (list->setq packages)))
978 (if (vhash-assq package visited)
979 (loop rest visited closure)
980 (let* ((bag (package->bag package system))
981 (dependencies (filter-map (match-lambda
982 ((label (? package? package) . _)
985 (bag-direct-inputs bag))))
986 (loop (append dependencies rest)
987 (vhash-consq package #t visited)
988 (fold set-insert closure dependencies))))))))
990 (define (build-system-with-package-mapping bs rewrite)
991 "Return a variant of BS, a build system, that rewrites a bag's inputs by
992 passing them through REWRITE, a procedure that takes an input tuplet and
993 returns a \"rewritten\" input tuplet."
995 (build-system-lower bs))
997 (define (lower* . args)
998 (let ((lowered (apply lower args)))
1001 (build-inputs (map rewrite (bag-build-inputs lowered)))
1002 (host-inputs (map rewrite (bag-host-inputs lowered)))
1003 (target-inputs (map rewrite (bag-target-inputs lowered))))))
1009 (define* (package-mapping proc #:optional (cut? (const #f))
1011 "Return a procedure that, given a package, applies PROC to all the packages
1012 depended on and returns the resulting package. The procedure stops recursion
1013 when CUT? returns true for a given package. When DEEP? is true, PROC is
1014 applied to implicit inputs as well."
1015 (define (rewrite input)
1017 ((label (? package? package) outputs ...)
1018 (cons* label (replace package) outputs))
1022 (define mapping-property
1023 ;; Property indicating whether the package has already been processed.
1024 (gensym " package-mapping-done"))
1028 ;; If P is the result of a previous call, return it.
1029 (cond ((assq-ref (package-properties p) mapping-property)
1033 ;; Since P's propagated inputs are really inputs of its dependents,
1034 ;; rewrite them as well, unless we're doing a "shallow" rewrite.
1037 (null? (package-propagated-inputs p)))
1041 (location (package-location p))
1042 (replacement (package-replacement p))
1043 (propagated-inputs (map rewrite (package-propagated-inputs p)))
1044 (properties `((,mapping-property . #t)
1045 ,@(package-properties p)))))))
1048 ;; Return a variant of P with PROC applied to P and its explicit
1049 ;; dependencies, recursively. Memoize the transformations. Failing
1050 ;; to do that, we would build a huge object graph with lots of
1051 ;; duplicates, which in turns prevents us from benefiting from
1052 ;; memoization in 'package-derivation'.
1056 (location (package-location p))
1057 (build-system (if deep?
1058 (build-system-with-package-mapping
1059 (package-build-system p) rewrite)
1060 (package-build-system p)))
1061 (inputs (map rewrite (package-inputs p)))
1062 (native-inputs (map rewrite (package-native-inputs p)))
1063 (propagated-inputs (map rewrite (package-propagated-inputs p)))
1064 (replacement (and=> (package-replacement p) replace))
1065 (properties `((,mapping-property . #t)
1066 ,@(package-properties p)))))))))
1070 (define* (package-input-rewriting replacements
1071 #:optional (rewrite-name identity)
1073 "Return a procedure that, when passed a package, replaces its direct and
1074 indirect dependencies, including implicit inputs when DEEP? is true, according
1075 to REPLACEMENTS. REPLACEMENTS is a list of package pairs; the first element
1076 of each pair is the package to replace, and the second one is the replacement.
1078 Optionally, REWRITE-NAME is a one-argument procedure that takes the name of a
1079 package and returns its new name after rewrite."
1080 (define replacement-property
1081 ;; Property to tag right-hand sides in REPLACEMENTS.
1082 (gensym " package-replacement"))
1085 (if (assq-ref (package-properties p) replacement-property)
1087 (match (assq-ref replacements p)
1088 (#f (package/inherit p
1089 (name (rewrite-name (package-name p)))))
1091 (package/inherit new
1092 (properties `((,replacement-property . #t)
1093 ,@(package-properties new))))
1097 (or (assq-ref (package-properties p) replacement-property)
1098 (assq-ref replacements p)))
1100 (package-mapping rewrite cut?
1103 (define* (package-input-rewriting/spec replacements #:key (deep? #t))
1104 "Return a procedure that, given a package, applies the given REPLACEMENTS to
1105 all the package graph, including implicit inputs unless DEEP? is false.
1106 REPLACEMENTS is a list of spec/procedures pair; each spec is a package
1107 specification such as \"gcc\" or \"guile@2\", and each procedure takes a
1108 matching package and returns a replacement for that package."
1110 (fold (lambda (replacement table)
1113 (let-values (((name version)
1114 (package-name->name+version spec)))
1115 (vhash-cons name (list version proc) table)))))
1119 (define (find-replacement package)
1120 (vhash-fold* (lambda (item proc)
1126 (and (version-prefix? version
1127 (package-version package))
1130 (package-name package)
1133 (define replacement-property
1134 (gensym " package-replacement"))
1137 (if (assq-ref (package-properties p) replacement-property)
1139 (match (find-replacement p)
1142 (let ((new (proc p)))
1143 ;; Mark NEW as already processed.
1144 (package/inherit new
1145 (properties `((,replacement-property . #t)
1146 ,@(package-properties new)))))))))
1149 (or (assq-ref (package-properties p) replacement-property)
1150 (find-replacement p)))
1152 (package-mapping rewrite cut?
1157 ;;; Package derivations.
1160 (define %derivation-cache
1161 ;; Package to derivation-path mapping.
1162 (make-weak-key-hash-table 100))
1164 (define (cache! cache package system thunk)
1165 "Memoize in CACHE the return values of THUNK as the derivation of PACKAGE on
1167 ;; FIXME: This memoization should be associated with the open store, because
1168 ;; otherwise it breaks when switching to a different store.
1169 (let ((result (thunk)))
1170 ;; Use `hashq-set!' instead of `hash-set!' because `hash' returns the
1171 ;; same value for all structs (as of Guile 2.0.6), and because pointer
1172 ;; equality is sufficient in practice.
1173 (hashq-set! cache package
1174 `((,system . ,result)
1175 ,@(or (hashq-ref cache package) '())))
1178 (define-syntax cached
1180 "Memoize the result of BODY for the arguments PACKAGE and SYSTEM.
1181 Return the cached result when available."
1182 ((_ (=> cache) package system body ...)
1183 (let ((thunk (lambda () body ...))
1185 (match (hashq-ref cache package)
1187 (match (assoc-ref alist key)
1188 (#f (cache! cache package key thunk))
1191 (cache! cache package key thunk)))))
1192 ((_ package system body ...)
1193 (cached (=> %derivation-cache) package system body ...))))
1195 (define* (expand-input store package input system #:optional cross-system)
1196 "Expand INPUT, an input tuple, such that it contains only references to
1197 derivation paths or store paths. PACKAGE is only used to provide contextual
1198 information in exceptions."
1199 (define (intern file)
1200 ;; Add FILE to the store. Set the `recursive?' bit to #t, so that
1201 ;; file permissions are preserved.
1202 (add-to-store store (basename file) #t "sha256" file))
1206 (cut package-cross-derivation store <> cross-system system
1208 (cut package-derivation store <> system #:graft? #f)))
1211 (((? string? name) (? package? package))
1212 (list name (derivation package)))
1213 (((? string? name) (? package? package)
1214 (? string? sub-drv))
1215 (list name (derivation package)
1218 (and (? string?) (? derivation-path?) drv))
1221 (and (? string?) (? file-exists? file)))
1222 ;; Add FILE to the store. When FILE is in the sub-directory of a
1223 ;; store path, it needs to be added anyway, so it can be used as a
1225 (list name (intern file)))
1226 (((? string? name) (? struct? source))
1227 ;; 'package-source-derivation' calls 'lower-object', which can throw
1228 ;; '&gexp-input-error'. However '&gexp-input-error' lacks source
1229 ;; location info, so we catch and rethrow here (XXX: not optimal
1230 ;; performance-wise).
1231 (guard (c ((gexp-input-error? c)
1233 (&package-input-error
1235 (input (gexp-error-invalid-input c)))))))
1236 (list name (package-source-derivation store source system))))
1238 (raise (condition (&package-input-error
1243 ;; 'eq?' cache mapping packages to system+target+graft?-dependent bags.
1244 ;; It significantly speeds things up when doing repeated calls to
1245 ;; 'package->bag' as is the case when building a profile.
1246 (make-weak-key-hash-table 200))
1248 (define* (package->bag package #:optional
1249 (system (%current-system))
1250 (target (%current-target-system))
1251 #:key (graft? (%graft?)))
1252 "Compile PACKAGE into a bag for SYSTEM, possibly cross-compiled to TARGET,
1254 (let ((package (or (and graft? (package-replacement package))
1256 (cached (=> %bag-cache)
1257 package (list system target)
1258 ;; Bind %CURRENT-SYSTEM and %CURRENT-TARGET-SYSTEM so that thunked
1259 ;; field values can refer to it.
1260 (parameterize ((%current-system system)
1261 (%current-target-system target))
1264 ($ <package> name version source build-system
1265 args inputs propagated-inputs native-inputs
1267 ;; Even though we prefer to use "@" to separate the package
1268 ;; name from the package version in various user-facing parts
1269 ;; of Guix, checkStoreName (in nix/libstore/store-api.cc)
1270 ;; prohibits the use of "@", so use "-" instead.
1271 (or (make-bag build-system (string-append name "-" version)
1275 #:inputs (append (inputs self)
1276 (propagated-inputs self))
1278 #:native-inputs (native-inputs self)
1279 #:arguments (args self))
1282 (&package-cross-build-system-error
1286 (package package))))))))))))
1288 (define %graft-cache
1289 ;; 'eq?' cache mapping package objects to a graft corresponding to their
1290 ;; replacement package.
1291 (make-weak-key-hash-table 200))
1293 (define (input-graft store system)
1294 "Return a procedure that, given a package with a replacement and an output name,
1295 returns a graft, and #f otherwise."
1297 (((? package? package) output)
1298 (let ((replacement (package-replacement package)))
1300 (cached (=> %graft-cache) package (cons output system)
1301 (let ((orig (package-derivation store package system
1303 (new (package-derivation store replacement system
1307 (origin-output output)
1309 (replacement-output output)))))))))
1311 (define (input-cross-graft store target system)
1312 "Same as 'input-graft', but for cross-compilation inputs."
1314 (((? package? package) output)
1315 (let ((replacement (package-replacement package)))
1317 (let ((orig (package-cross-derivation store package target system
1319 (new (package-cross-derivation store replacement
1324 (origin-output output)
1326 (replacement-output output))))))))
1328 (define* (fold-bag-dependencies proc seed bag
1330 "Fold PROC over the packages BAG depends on. Each package is visited only
1331 once, in depth-first order. If NATIVE? is true, restrict to native
1332 dependencies; otherwise, restrict to target dependencies."
1333 (define bag-direct-inputs*
1336 (append (bag-build-inputs bag)
1337 (bag-target-inputs bag)
1338 (if (bag-target bag)
1340 (bag-host-inputs bag))))
1343 (let loop ((inputs (bag-direct-inputs* bag))
1345 (visited vlist-null))
1349 (((label (? package? head) . rest) . tail)
1350 (let ((output (match rest (() "out") ((output) output)))
1351 (outputs (vhash-foldq* cons '() head visited)))
1352 (if (member output outputs)
1353 (loop tail result visited)
1354 (let ((inputs (bag-direct-inputs* (package->bag head))))
1355 (loop (append inputs tail)
1356 (proc head output result)
1357 (vhash-consq head output visited))))))
1359 (loop tail result visited)))))
1361 (define* (bag-grafts store bag)
1362 "Return the list of grafts potentially applicable to BAG. Potentially
1363 applicable grafts are collected by looking at direct or indirect dependencies
1364 of BAG that have a 'replacement'. Whether a graft is actually applicable
1365 depends on whether the outputs of BAG depend on the items the grafts refer
1366 to (see 'graft-derivation'.)"
1367 (define system (bag-system bag))
1368 (define target (bag-target bag))
1370 (define native-grafts
1371 (let ((->graft (input-graft store system)))
1372 (parameterize ((%current-system system)
1373 (%current-target-system #f))
1374 (fold-bag-dependencies (lambda (package output grafts)
1375 (match (->graft package output)
1377 (graft (cons graft grafts))))
1381 (define target-grafts
1383 (let ((->graft (input-cross-graft store target system)))
1384 (parameterize ((%current-system system)
1385 (%current-target-system target))
1386 (fold-bag-dependencies (lambda (package output grafts)
1387 (match (->graft package output)
1389 (graft (cons graft grafts))))
1395 ;; We can end up with several identical grafts if we stumble upon packages
1396 ;; that are not 'eq?' but map to the same derivation (this can happen when
1397 ;; using things like 'package-with-explicit-inputs'.) Hence the
1398 ;; 'delete-duplicates' call.
1400 (append native-grafts target-grafts)))
1402 (define* (package-grafts store package
1403 #:optional (system (%current-system))
1405 "Return the list of grafts applicable to PACKAGE as built for SYSTEM and
1407 (let* ((package (or (package-replacement package) package))
1408 (bag (package->bag package system target)))
1409 (bag-grafts store bag)))
1411 (define* (bag->derivation store bag
1413 "Return the derivation to build BAG for SYSTEM. Optionally, CONTEXT can be
1414 a package object describing the context in which the call occurs, for improved
1416 (if (bag-target bag)
1417 (bag->cross-derivation store bag)
1418 (let* ((system (bag-system bag))
1419 (inputs (bag-transitive-inputs bag))
1420 (input-drvs (map (cut expand-input store context <> system)
1422 (paths (delete-duplicates
1423 (append-map (match-lambda
1424 ((_ (? package? p) _ ...)
1425 (package-native-search-paths
1430 (apply (bag-build bag)
1431 store (bag-name bag) input-drvs
1432 #:search-paths paths
1433 #:outputs (bag-outputs bag) #:system system
1434 (bag-arguments bag)))))
1436 (define* (bag->cross-derivation store bag
1438 "Return the derivation to build BAG, which is actually a cross build.
1439 Optionally, CONTEXT can be a package object denoting the context of the call.
1440 This is an internal procedure."
1441 (let* ((system (bag-system bag))
1442 (target (bag-target bag))
1443 (host (bag-transitive-host-inputs bag))
1444 (host-drvs (map (cut expand-input store context <> system target)
1446 (target* (bag-transitive-target-inputs bag))
1447 (target-drvs (map (cut expand-input store context <> system)
1449 (build (bag-transitive-build-inputs bag))
1450 (build-drvs (map (cut expand-input store context <> system)
1452 (all (append build target* host))
1453 (paths (delete-duplicates
1454 (append-map (match-lambda
1455 ((_ (? package? p) _ ...)
1456 (package-search-paths p))
1459 (npaths (delete-duplicates
1460 (append-map (match-lambda
1461 ((_ (? package? p) _ ...)
1462 (package-native-search-paths
1467 (apply (bag-build bag)
1468 store (bag-name bag)
1469 #:native-drvs build-drvs
1470 #:target-drvs (append host-drvs target-drvs)
1471 #:search-paths paths
1472 #:native-search-paths npaths
1473 #:outputs (bag-outputs bag)
1474 #:system system #:target target
1475 (bag-arguments bag))))
1477 (define* (package-derivation store package
1478 #:optional (system (%current-system))
1479 #:key (graft? (%graft?)))
1480 "Return the <derivation> object of PACKAGE for SYSTEM."
1482 ;; Compute the derivation and cache the result. Caching is important
1483 ;; because some derivations, such as the implicit inputs of the GNU build
1484 ;; system, will be queried many, many times in a row.
1485 (cached package (cons system graft?)
1486 (let* ((bag (package->bag package system #f #:graft? graft?))
1487 (drv (bag->derivation store bag package)))
1489 (match (bag-grafts store bag)
1493 (let ((guile (package-derivation store (guile-for-grafts)
1494 system #:graft? #f)))
1495 ;; TODO: As an optimization, we can simply graft the tip
1496 ;; of the derivation graph since 'graft-derivation'
1498 (graft-derivation store drv grafts
1503 (define* (package-cross-derivation store package target
1504 #:optional (system (%current-system))
1505 #:key (graft? (%graft?)))
1506 "Cross-build PACKAGE for TARGET (a GNU triplet) from host SYSTEM (a Guix
1507 system identifying string)."
1508 (cached package (list system target graft?)
1509 (let* ((bag (package->bag package system target #:graft? graft?))
1510 (drv (bag->derivation store bag package)))
1512 (match (bag-grafts store bag)
1516 (graft-derivation store drv grafts
1519 (package-derivation store (guile-for-grafts)
1520 system #:graft? #f))))
1523 (define* (package-output store package
1524 #:optional (output "out") (system (%current-system)))
1525 "Return the output path of PACKAGE's OUTPUT for SYSTEM---where OUTPUT is the
1526 symbolic output name, such as \"out\". Note that this procedure calls
1527 `package-derivation', which is costly."
1528 (let ((drv (package-derivation store package system)))
1529 (derivation->output-path drv output)))
1533 ;;; Monadic interface.
1536 (define (set-guile-for-build guile)
1537 "This monadic procedure changes the Guile currently used to run the build
1538 code of derivations to GUILE, a package object."
1540 (let ((guile (package-derivation store guile)))
1541 (values (%guile-for-build guile) store))))
1543 (define* (package-file package
1546 system (output "out") target)
1547 "Return as a monadic value the absolute file name of FILE within the
1548 OUTPUT directory of PACKAGE. When FILE is omitted, return the name of the
1549 OUTPUT directory of PACKAGE. When TARGET is true, use it as a
1550 cross-compilation target triplet.
1552 Note that this procedure does _not_ build PACKAGE. Thus, the result might or
1553 might not designate an existing file. We recommend not using this procedure
1554 unless you know what you are doing."
1556 (define compute-derivation
1558 (cut package-cross-derivation <> <> target <>)
1559 package-derivation))
1561 (let* ((system (or system (%current-system)))
1562 (drv (compute-derivation store package system))
1563 (out (derivation->output-path drv output)))
1565 (string-append out "/" file)
1569 (define package->derivation
1570 (store-lift package-derivation))
1572 (define package->cross-derivation
1573 (store-lift package-cross-derivation))
1575 (define-gexp-compiler (package-compiler (package <package>) system target)
1576 ;; Compile PACKAGE to a derivation for SYSTEM, optionally cross-compiled for
1577 ;; TARGET. This is used when referring to a package from within a gexp.
1579 (package->cross-derivation package target system)
1580 (package->derivation package system)))
1582 (define* (origin->derivation origin
1583 #:optional (system (%current-system)))
1584 "Return the derivation corresponding to ORIGIN."
1586 (($ <origin> uri method hash name (= force ()) #f)
1587 ;; No patches, no snippet: this is a fixed-output derivation.
1589 (content-hash-algorithm hash)
1590 (content-hash-value hash)
1591 name #:system system))
1592 (($ <origin> uri method hash name (= force (patches ...)) snippet
1593 (flags ...) inputs (modules ...) guile-for-build)
1594 ;; Patches and/or a snippet.
1595 (mlet %store-monad ((source (method uri
1596 (content-hash-algorithm hash)
1597 (content-hash-value hash)
1598 name #:system system))
1599 (guile (package->derivation (or guile-for-build
1603 (patch-and-repack source patches
1609 #:guile-for-build guile)))))
1611 (define-gexp-compiler (origin-compiler (origin <origin>) system target)
1612 ;; Compile ORIGIN to a derivation for SYSTEM. This is used when referring
1613 ;; to an origin from within a gexp.
1614 (origin->derivation origin system))
1616 (define package-source-derivation ;somewhat deprecated
1617 (let ((lower (store-lower lower-object)))
1618 (lambda* (store source #:optional (system (%current-system)))
1619 "Return the derivation or file corresponding to SOURCE, which can be an
1620 a file name or any object handled by 'lower-object', such as an <origin>.
1621 When SOURCE is a file name, return either the interned file name (if SOURCE is
1622 outside of the store) or SOURCE itself (if SOURCE is already a store item.)"
1624 ((and (? string?) (? direct-store-path?) file)
1627 (add-to-store store (basename file) #t "sha256" file))
1629 (lower store source system))))))