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
129 transitive-input-references
133 %hydra-supported-systems
138 package-error-package
141 package-error-invalid-input
142 &package-cross-build-system-error
143 package-cross-build-system-error?
148 bag-transitive-inputs
149 bag-transitive-host-inputs
150 bag-transitive-build-inputs
151 bag-transitive-target-inputs
155 default-guile-derivation
159 package->cross-derivation
164 ;;; This module provides a high-level mechanism to define packages in a
165 ;;; Guix-based distribution.
169 ;; Crytographic content hash.
170 (define-immutable-record-type <content-hash>
171 (%content-hash algorithm value)
173 (algorithm content-hash-algorithm) ;symbol
174 (value content-hash-value)) ;bytevector
176 (define-syntax-rule (define-content-hash-constructor name
177 (algorithm size) ...)
178 "Define NAME as a <content-hash> constructor that ensures that (1) its
179 second argument is among the listed ALGORITHM, and (2), when possible, that
180 its first argument has the right size for the chosen algorithm."
183 (syntax-case s (algorithm ...)
185 (let ((bv* (syntax->datum #'bv)))
186 (when (and (bytevector? bv*)
187 (not (= size (bytevector-length bv*))))
188 (syntax-violation 'content-hash "invalid content hash length" s))
189 #'(%content-hash 'algorithm bv)))
192 (define-content-hash-constructor build-content-hash
199 (define-syntax content-hash
201 "Return a content hash with the given parameters. The default hash
202 algorithm is sha256. If the first argument is a literal string, it is decoded
203 as base32. Otherwise, it must be a bytevector."
204 ;; What we'd really want here is something like C++ 'constexpr'.
207 (string? (syntax->datum #'str))
208 #'(content-hash str sha256))
210 (string? (syntax->datum #'str))
211 (with-syntax ((bv (base32 (syntax->datum #'str))))
212 #'(content-hash bv algorithm)))
213 ((_ (id str) algorithm)
214 (and (string? (syntax->datum #'str))
215 (free-identifier=? #'id #'base32))
216 (with-syntax ((bv (nix-base32-string->bytevector (syntax->datum #'str))))
217 #'(content-hash bv algorithm)))
218 ((_ (id str) algorithm)
219 (and (string? (syntax->datum #'str))
220 (free-identifier=? #'id #'base64))
221 (with-syntax ((bv (base64-decode (syntax->datum #'str))))
222 #'(content-hash bv algorithm)))
224 #'(content-hash bv sha256))
226 #'(build-content-hash bv hash)))))
228 (define (print-content-hash hash port)
229 (format port "#<content-hash ~a:~a>"
230 (content-hash-algorithm hash)
231 (and=> (content-hash-value hash)
232 bytevector->nix-base32-string)))
234 (set-record-type-printer! <content-hash> print-content-hash)
237 ;; The source of a package, such as a tarball URL and fetcher---called
238 ;; "origin" to avoid name clash with `package-source', `source', etc.
239 (define-record-type* <origin>
243 (uri origin-uri) ; string
244 (method origin-method) ; procedure
245 (hash origin-hash) ; <content-hash>
246 (file-name origin-file-name (default #f)) ; optional file name
248 ;; Patches are delayed so that the 'search-patch' calls are made lazily,
249 ;; which reduces I/O on startup and allows patch-not-found errors to be
250 ;; gracefully handled at run time.
251 (patches origin-patches ; list of file names
252 (default '()) (delayed))
254 (snippet origin-snippet (default #f)) ; sexp or #f
255 (patch-flags origin-patch-flags ; list of strings
258 ;; Patching requires Guile, GNU Patch, and a few more. These two fields are
259 ;; used to specify these dependencies when needed.
260 (patch-inputs origin-patch-inputs ; input list or #f
262 (modules origin-modules ; list of module names
265 (patch-guile origin-patch-guile ; package or #f
268 (define-syntax origin-compatibility-helper
269 (syntax-rules (sha256)
271 (%origin fields ...))
272 ((_ ((sha256 exp) rest ...) (others ...))
274 (hash (content-hash exp sha256))
276 ((_ (field rest ...) (others ...))
277 (origin-compatibility-helper (rest ...)
278 (others ... field)))))
280 (define-syntax-rule (origin fields ...)
281 "Build an <origin> record, automatically converting 'sha256' field
282 specifications to 'hash'."
283 (origin-compatibility-helper (fields ...) ()))
285 (define-deprecated (origin-sha256 origin)
287 (let ((hash (origin-hash origin)))
288 (unless (eq? (content-hash-algorithm hash) 'sha256)
289 (raise (condition (&message
290 (message (G_ "no SHA256 hash for origin"))))))
291 (content-hash-value hash)))
293 (define (print-origin origin port)
294 "Write a concise representation of ORIGIN to PORT."
296 (($ <origin> uri method hash file-name patches)
297 (simple-format port "#<origin ~s ~a ~s ~a>"
300 (number->string (object-address origin) 16)))))
302 (set-record-type-printer! <origin> print-origin)
304 (define-syntax-rule (define-compile-time-decoder name string->bytevector)
305 "Define NAME as a macro that runs STRING->BYTEVECTOR at macro expansion time
309 "Return the bytevector corresponding to the given textual
313 (string? (syntax->datum #'str))
314 ;; A literal string: do the conversion at expansion time.
315 (with-syntax ((bv (string->bytevector (syntax->datum #'str))))
318 #'(string->bytevector str))))))
320 (define-compile-time-decoder base32 nix-base32-string->bytevector)
321 (define-compile-time-decoder base64 base64-decode)
323 (define (origin-actual-file-name origin)
324 "Return the file name of ORIGIN, either its 'file-name' field or the file
326 (define (uri->file-name uri)
327 ;; Return the 'base name' of URI or URI itself, where URI is a string.
328 (let ((path (and=> (string->uri uri) uri-path)))
333 (or (origin-file-name origin)
334 (match (origin-uri origin)
336 (uri->file-name head))
338 (uri->file-name uri))
340 ;; git, svn, cvs, etc. reference
344 (define %supported-systems
345 ;; This is the list of system types that are supported. By default, we
346 ;; expect all packages to build successfully here.
347 '("x86_64-linux" "i686-linux" "armhf-linux" "aarch64-linux" "mips64el-linux" "i586-gnu"))
349 (define %hurd-systems
350 ;; The GNU/Hurd systems for which support is being developed.
351 '("i586-gnu" "i686-gnu"))
353 (define %hydra-supported-systems
354 ;; This is the list of system types for which build machines are available.
356 ;; XXX: MIPS is unavailable in CI:
357 ;; <https://lists.gnu.org/archive/html/guix-devel/2017-03/msg00790.html>.
358 (fold delete %supported-systems '("mips64el-linux")))
362 (define-record-type* <package>
366 (name package-name) ; string
367 (version package-version) ; string
368 (source package-source) ; <origin> instance
369 (build-system package-build-system) ; build system
370 (arguments package-arguments ; arguments for the build method
371 (default '()) (thunked))
373 (inputs package-inputs ; input packages or derivations
374 (default '()) (thunked))
375 (propagated-inputs package-propagated-inputs ; same, but propagated
376 (default '()) (thunked))
377 (native-inputs package-native-inputs ; native input packages/derivations
378 (default '()) (thunked))
380 (outputs package-outputs ; list of strings
384 ; <search-path-specification>,
385 ; for native and cross
387 (native-search-paths package-native-search-paths (default '()))
388 (search-paths package-search-paths (default '()))
390 ;; The 'replacement' field is marked as "innate" because it never makes
391 ;; sense to inherit a replacement as is. See the 'package/inherit' macro.
392 (replacement package-replacement ; package | #f
393 (default #f) (thunked) (innate))
395 (synopsis package-synopsis) ; one-line description
396 (description package-description) ; one or two paragraphs
397 (license package-license)
398 (home-page package-home-page)
399 (supported-systems package-supported-systems ; list of strings
400 (default %supported-systems))
402 (properties package-properties (default '())) ; alist for anything else
404 (location package-location
405 (default (and=> (current-source-location)
406 source-properties->location))
409 (set-record-type-printer! <package>
410 (lambda (package port)
411 (let ((loc (package-location package))
412 (format simple-format))
413 (format port "#<package ~a@~a ~a~a>"
414 (package-name package)
415 (package-version package)
421 (number->string (object-address
425 (define (package-upstream-name package)
426 "Return the upstream name of PACKAGE, which could be different from the name
428 (or (assq-ref (package-properties package) 'upstream-name)
429 (package-name package)))
431 (define (hidden-package p)
432 "Return a \"hidden\" version of P--i.e., one that 'fold-packages' and thus,
433 user interfaces, ignores."
436 (properties `((hidden? . #t)
437 ,@(package-properties p)))))
439 (define (hidden-package? p)
440 "Return true if P is \"hidden\"--i.e., must not be visible to user
442 (assoc-ref (package-properties p) 'hidden?))
444 (define (package-superseded p)
445 "Return the package the supersedes P, or #f if P is still current."
446 (assoc-ref (package-properties p) 'superseded))
448 (define (deprecated-package old-name p)
449 "Return a package called OLD-NAME and marked as superseded by P, a package
454 (properties `((superseded . ,p)))))
456 (define (package-field-location package field)
457 "Return the source code location of the definition of FIELD for PACKAGE, or
458 #f if it could not be determined."
459 (define (goto port line column)
460 (unless (and (= (port-column port) (- column 1))
461 (= (port-line port) (- line 1)))
462 (unless (eof-object? (read-char port))
463 (goto port line column))))
465 (match (package-location package)
466 (($ <location> file line column)
469 ;; In general we want to keep relative file names for modules.
470 (call-with-input-file (search-path %load-path file)
472 (goto port line column)
474 (('package inits ...)
475 (let ((field (assoc field inits)))
478 (let ((loc (and=> (source-properties value)
479 source-properties->location)))
481 ;; Preserve the original file name, which may be a
482 ;; relative file name.
483 (set-field loc (location-file) file))))
495 (define-condition-type &package-error &error
497 (package package-error-package))
499 (define-condition-type &package-input-error &package-error
501 (input package-error-invalid-input))
503 (define-condition-type &package-cross-build-system-error &package-error
504 package-cross-build-system-error?)
506 (define* (package-full-name package #:optional (delimiter "@"))
507 "Return the full name of PACKAGE--i.e., `NAME@VERSION'. By specifying
508 DELIMITER (a string), you can customize what will appear between the name and
509 the version. By default, DELIMITER is \"@\"."
510 (string-append (package-name package) delimiter (package-version package)))
512 (define (patch-file-name patch)
513 "Return the basename of PATCH's file name, or #f if the file name could not
519 (and=> (origin-actual-file-name patch) basename))))
521 (define %vulnerability-regexp
522 ;; Regexp matching a CVE identifier in patch file names.
523 (make-regexp "CVE-[0-9]{4}-[0-9]+"))
525 (define (package-patched-vulnerabilities package)
526 "Return the list of patched vulnerabilities of PACKAGE as a list of CVE
527 identifiers. The result is inferred from the file names of patches."
528 (define (patch-vulnerabilities patch)
529 (map (cut match:substring <> 0)
530 (list-matches %vulnerability-regexp patch)))
532 (let ((patches (filter-map patch-file-name
533 (or (and=> (package-source package)
536 (append-map patch-vulnerabilities patches)))
538 (define (%standard-patch-inputs)
539 (let* ((canonical (module-ref (resolve-interface '(gnu packages base))
541 (ref (lambda (module var)
543 (module-ref (resolve-interface module) var)))))
544 `(("tar" ,(ref '(gnu packages base) 'tar))
545 ("xz" ,(ref '(gnu packages compression) 'xz))
546 ("bzip2" ,(ref '(gnu packages compression) 'bzip2))
547 ("gzip" ,(ref '(gnu packages compression) 'gzip))
548 ("lzip" ,(ref '(gnu packages compression) 'lzip))
549 ("unzip" ,(ref '(gnu packages compression) 'unzip))
550 ("patch" ,(ref '(gnu packages base) 'patch))
551 ("locales" ,(ref '(gnu packages base) 'glibc-utf8-locales)))))
553 (define (default-guile)
554 "Return the default Guile package used to run the build code of
556 (let ((distro (resolve-interface '(gnu packages commencement))))
557 (module-ref distro 'guile-final)))
559 (define (guile-for-grafts)
560 "Return the Guile package used to build grafting derivations."
561 ;; Guile 2.2 would not work due to <https://bugs.gnu.org/28211> when
562 ;; grafting packages.
563 (let ((distro (resolve-interface '(gnu packages guile))))
564 (module-ref distro 'guile-2.0)))
566 (define* (default-guile-derivation #:optional (system (%current-system)))
567 "Return the derivation for SYSTEM of the default Guile package used to run
568 the build code of derivation."
569 (package->derivation (default-guile) system
572 (define* (patch-and-repack source patches
578 (guile-for-build (%guile-for-build))
579 (system (%current-system)))
580 "Unpack SOURCE (a derivation or store path), apply all of PATCHES, and
581 repack the tarball using the tools listed in INPUTS. When SNIPPET is true,
582 it must be an s-expression that will run from within the directory where
583 SOURCE was unpacked, after all of PATCHES have been applied. MODULES
584 specifies modules in scope when evaluating SNIPPET."
585 (define source-file-name
586 ;; SOURCE is usually a derivation, but it could be a store file.
587 (if (derivation? source)
588 (derivation->output-path source)
592 ;; The default value of the 'patch-inputs' field, and thus INPUTS is #f,
593 ;; so deal with that.
594 (let ((inputs (or inputs (%standard-patch-inputs))))
596 (match (assoc-ref inputs name)
600 (define decompression-type
601 (cond ((string-suffix? "gz" source-file-name) "gzip")
602 ((string-suffix? "Z" source-file-name) "gzip")
603 ((string-suffix? "bz2" source-file-name) "bzip2")
604 ((string-suffix? "lz" source-file-name) "lzip")
605 ((string-suffix? "zip" source-file-name) "unzip")
608 (define original-file-name
609 ;; Remove the store prefix plus the slash, hash, and hyphen.
610 (let* ((sans (string-drop source-file-name
611 (+ (string-length (%store-prefix)) 1)))
612 (dash (string-index sans #\-)))
613 (string-drop sans (+ 1 dash))))
615 (define (numeric-extension? file-name)
616 ;; Return true if FILE-NAME ends with digits.
617 (and=> (file-extension file-name)
618 (cut string-every char-set:hex-digit <>)))
620 (define (checkout? directory)
621 ;; Return true if DIRECTORY is a checkout (git, svn, etc).
622 (string-suffix? "-checkout" directory))
624 (define (tarxz-name file-name)
625 ;; Return a '.tar.xz' file name based on FILE-NAME.
626 (let ((base (cond ((numeric-extension? file-name)
628 ((checkout? file-name)
629 (string-drop-right file-name 9))
630 (else (file-sans-extension file-name)))))
632 (if (equal? (file-extension base) "tar")
636 (define instantiate-patch
638 ((? string? patch) ;deprecated
639 (interned-file patch #:recursive? #t))
640 ((? struct? patch) ;origin, local-file, etc.
641 (lower-object patch system))))
643 (mlet %store-monad ((tar -> (lookup-input "tar"))
644 (xz -> (lookup-input "xz"))
645 (patch -> (lookup-input "patch"))
646 (locales -> (lookup-input "locales"))
647 (decomp -> (lookup-input decompression-type))
648 (patches (sequence %store-monad
649 (map instantiate-patch patches))))
651 (with-imported-modules '((guix build utils))
653 (use-modules (ice-9 ftw)
657 ;; The --sort option was added to GNU tar in version 1.28, released
658 ;; 2014-07-28. During bootstrap we must cope with older versions.
659 (define tar-supports-sort?
660 (zero? (system* (string-append #+tar "/bin/tar")
661 "cf" "/dev/null" "--files-from=/dev/null"
664 (define (apply-patch patch)
665 (format (current-error-port) "applying '~a'...~%" patch)
667 ;; Use '--force' so that patches that do not apply perfectly are
668 ;; rejected. Use '--no-backup-if-mismatch' to prevent making
669 ;; "*.orig" file if a patch is applied with offset.
670 (invoke (string-append #+patch "/bin/patch")
671 "--force" "--no-backup-if-mismatch"
672 #+@flags "--input" patch))
674 (define (first-file directory)
675 ;; Return the name of the first file in DIRECTORY.
676 (car (scandir directory
678 (not (member name '("." "..")))))))
680 ;; Encoding/decoding errors shouldn't be silent.
681 (fluid-set! %default-port-conversion-strategy 'error)
684 ;; First of all, install a UTF-8 locale so that UTF-8 file names
685 ;; are correctly interpreted. During bootstrap, LOCALES is #f.
687 (string-append #+locales "/lib/locale/"
690 (package-version locales)))))
691 (setlocale LC_ALL "en_US.utf8"))
693 (setenv "PATH" (string-append #+xz "/bin" ":"
696 ;; SOURCE may be either a directory or a tarball.
697 (if (file-is-directory? #+source)
698 (let* ((store (%store-directory))
699 (len (+ 1 (string-length store)))
700 (base (string-drop #+source len))
701 (dash (string-index base #\-))
702 (directory (string-drop base (+ 1 dash))))
704 (copy-recursively #+source directory))
705 #+(if (string=? decompression-type "unzip")
706 #~(invoke "unzip" #+source)
707 #~(invoke (string-append #+tar "/bin/tar")
710 (let ((directory (first-file ".")))
711 (format (current-error-port)
712 "source is under '~a'~%" directory)
715 (for-each apply-patch '#+patches)
717 (let ((result #+(if snippet
718 #~(let ((module (make-fresh-user-module)))
719 (module-use-interfaces!
721 (map resolve-interface '#+modules))
722 ((@ (system base compile) compile)
725 #:opts %auto-compilation-options
728 ;; Issue a warning unless the result is #t.
729 (unless (eqv? result #t)
730 (format (current-error-port) "\
731 ## WARNING: the snippet returned `~s'. Return values other than #t
732 ## are deprecated. Please migrate this package so that its snippet
733 ## reports errors by raising an exception, and otherwise returns #t.~%"
736 (error "snippet returned false")))
740 (unless tar-supports-sort?
741 (call-with-output-file ".file_list"
743 (for-each (lambda (name)
744 (format port "~a~%" name))
745 (find-files directory
747 #:fail-on-error? #t)))))
749 (string-append #+tar "/bin/tar")
751 ;; Avoid non-determinism in the archive. Set the mtime
752 ;; to 1 as is the case in the store (software like gzip
753 ;; behaves differently when it stumbles upon mtime = 0).
757 (if tar-supports-sort?
761 "--files-from=.file_list")))))))
763 (let ((name (tarxz-name original-file-name)))
764 (gexp->derivation name build
767 #:guile-for-build guile-for-build
768 #:properties `((type . origin)
769 (patches . ,(length patches)))))))
771 (define (package-with-patches original patches)
772 "Return package ORIGINAL with PATCHES applied."
773 (package (inherit original)
774 (source (origin (inherit (package-source original))
775 (patches patches)))))
777 (define (package-with-extra-patches original patches)
778 "Return package ORIGINAL with all PATCHES appended to its list of patches."
779 (package-with-patches original
780 (append (origin-patches (package-source original))
783 (define (transitive-inputs inputs)
784 "Return the closure of INPUTS when considering the 'propagated-inputs'
785 edges. Omit duplicate inputs, except for those already present in INPUTS
788 This is implemented as a breadth-first traversal such that INPUTS is
789 preserved, and only duplicate propagated inputs are removed."
790 (define (seen? seen item outputs)
791 ;; FIXME: We're using pointer identity here, which is extremely sensitive
792 ;; to memoization in package-producing procedures; see
793 ;; <https://bugs.gnu.org/30155>.
794 (match (vhash-assq item seen)
795 ((_ . o) (equal? o outputs))
798 (let loop ((inputs inputs)
805 (if (null? propagated)
807 (loop (reverse (concatenate propagated)) result '() #f seen)))
808 (((and input (label (? package? package) outputs ...)) rest ...)
809 (if (and (not first?) (seen? seen package outputs))
810 (loop rest result propagated first? seen)
813 (cons (package-propagated-inputs package) propagated)
815 (vhash-consq package outputs seen))))
817 (loop rest (cons input result) propagated first? seen)))))
819 (define (package-direct-sources package)
820 "Return all source origins associated with PACKAGE; including origins in
822 `(,@(or (and=> (package-source package) list) '())
823 ,@(filter-map (match-lambda
824 ((_ (? origin? orig) _ ...)
827 (package-direct-inputs package))))
829 (define (package-transitive-sources package)
830 "Return PACKAGE's direct sources, and their direct sources, recursively."
832 (concatenate (filter-map (match-lambda
833 ((_ (? origin? orig) _ ...)
835 ((_ (? package? p) _ ...)
836 (package-direct-sources p))
838 (bag-transitive-inputs
839 (package->bag package))))))
841 (define (package-direct-inputs package)
842 "Return all the direct inputs of PACKAGE---i.e, its direct inputs along
843 with their propagated inputs."
844 (append (package-native-inputs package)
845 (package-inputs package)
846 (package-propagated-inputs package)))
848 (define (package-transitive-inputs package)
849 "Return the transitive inputs of PACKAGE---i.e., its direct inputs along
850 with their propagated inputs, recursively."
851 (transitive-inputs (package-direct-inputs package)))
853 (define (package-transitive-target-inputs package)
854 "Return the transitive target inputs of PACKAGE---i.e., its direct inputs
855 along with their propagated inputs, recursively. This only includes inputs
856 for the target system, and not native inputs."
857 (transitive-inputs (append (package-inputs package)
858 (package-propagated-inputs package))))
860 (define (package-transitive-native-inputs package)
861 "Return the transitive native inputs of PACKAGE---i.e., its direct inputs
862 along with their propagated inputs, recursively. This only includes inputs
863 for the host system (\"native inputs\"), and not target inputs."
864 (transitive-inputs (package-native-inputs package)))
866 (define (package-transitive-propagated-inputs package)
867 "Return the propagated inputs of PACKAGE, and their propagated inputs,
869 (transitive-inputs (package-propagated-inputs package)))
871 (define (package-transitive-native-search-paths package)
872 "Return the list of search paths for PACKAGE and its propagated inputs,
874 (append (package-native-search-paths package)
875 (append-map (match-lambda
876 ((label (? package? p) _ ...)
877 (package-native-search-paths p))
880 (package-transitive-propagated-inputs package))))
882 (define (transitive-input-references alist inputs)
883 "Return a list of (assoc-ref ALIST <label>) for each (<label> <package> . _)
884 in INPUTS and their transitive propagated inputs."
891 `(assoc-ref ,alist ,(label input)))
892 (transitive-inputs inputs)))
894 (define package-transitive-supported-systems
896 (define supported-systems
897 (mlambda (package system)
898 (parameterize ((%current-system system))
899 (fold (lambda (input systems)
901 ((label (? package? package) . _)
902 (lset-intersection string=? systems
903 (supported-systems package system)))
906 (package-supported-systems package)
907 (bag-direct-inputs (package->bag package))))))
909 (lambda* (package #:optional (system (%current-system)))
910 "Return the intersection of the systems supported by PACKAGE and those
911 supported by its dependencies."
912 (supported-systems package system))))
914 (define* (supported-package? package #:optional (system (%current-system)))
915 "Return true if PACKAGE is supported on SYSTEM--i.e., if PACKAGE and all its
916 dependencies are known to build on SYSTEM."
917 (member system (package-transitive-supported-systems package system)))
919 (define (bag-direct-inputs bag)
920 "Same as 'package-direct-inputs', but applied to a bag."
921 (append (bag-build-inputs bag)
922 (bag-host-inputs bag)
923 (bag-target-inputs bag)))
925 (define (bag-transitive-inputs bag)
926 "Same as 'package-transitive-inputs', but applied to a bag."
927 (parameterize ((%current-target-system #f)
928 (%current-system (bag-system bag)))
929 (transitive-inputs (bag-direct-inputs bag))))
931 (define (bag-transitive-build-inputs bag)
932 "Same as 'package-transitive-native-inputs', but applied to a bag."
933 (parameterize ((%current-target-system #f)
934 (%current-system (bag-system bag)))
935 (transitive-inputs (bag-build-inputs bag))))
937 (define (bag-transitive-host-inputs bag)
938 "Same as 'package-transitive-target-inputs', but applied to a bag."
939 (parameterize ((%current-target-system (bag-target bag))
940 (%current-system (bag-system bag)))
941 (transitive-inputs (bag-host-inputs bag))))
943 (define (bag-transitive-target-inputs bag)
944 "Return the \"target inputs\" of BAG, recursively."
945 (parameterize ((%current-target-system (bag-target bag))
946 (%current-system (bag-system bag)))
947 (transitive-inputs (bag-target-inputs bag))))
949 (define* (package-closure packages #:key (system (%current-system)))
950 "Return the closure of PACKAGES on SYSTEM--i.e., PACKAGES and the list of
951 packages they depend on, recursively."
952 (let loop ((packages packages)
954 (closure (list->setq packages)))
959 (if (vhash-assq package visited)
960 (loop rest visited closure)
961 (let* ((bag (package->bag package system))
962 (dependencies (filter-map (match-lambda
963 ((label (? package? package) . _)
966 (bag-direct-inputs bag))))
967 (loop (append dependencies rest)
968 (vhash-consq package #t visited)
969 (fold set-insert closure dependencies))))))))
971 (define* (package-mapping proc #:optional (cut? (const #f)))
972 "Return a procedure that, given a package, applies PROC to all the packages
973 depended on and returns the resulting package. The procedure stops recursion
974 when CUT? returns true for a given package."
975 (define (rewrite input)
977 ((label (? package? package) outputs ...)
978 (let ((proc (if (cut? package) proc replace)))
979 (cons* label (proc package) outputs)))
985 ;; Return a variant of P with PROC applied to P and its explicit
986 ;; dependencies, recursively. Memoize the transformations. Failing to
987 ;; do that, we would build a huge object graph with lots of duplicates,
988 ;; which in turns prevents us from benefiting from memoization in
989 ;; 'package-derivation'.
993 (location (package-location p))
994 (inputs (map rewrite (package-inputs p)))
995 (native-inputs (map rewrite (package-native-inputs p)))
996 (propagated-inputs (map rewrite (package-propagated-inputs p)))
997 (replacement (and=> (package-replacement p) proc))))))
1001 (define* (package-input-rewriting replacements
1002 #:optional (rewrite-name identity))
1003 "Return a procedure that, when passed a package, replaces its direct and
1004 indirect dependencies (but not its implicit inputs) according to REPLACEMENTS.
1005 REPLACEMENTS is a list of package pairs; the first element of each pair is the
1006 package to replace, and the second one is the replacement.
1008 Optionally, REWRITE-NAME is a one-argument procedure that takes the name of a
1009 package and returns its new name after rewrite."
1011 (match (assq-ref replacements p)
1014 (name (rewrite-name (package-name p)))))
1017 (package-mapping rewrite (cut assq <> replacements)))
1019 (define (package-input-rewriting/spec replacements)
1020 "Return a procedure that, given a package, applies the given REPLACEMENTS to
1021 all the package graph (excluding implicit inputs). REPLACEMENTS is a list of
1022 spec/procedures pair; each spec is a package specification such as \"gcc\" or
1023 \"guile@2\", and each procedure takes a matching package and returns a
1024 replacement for that package."
1026 (fold (lambda (replacement table)
1029 (let-values (((name version)
1030 (package-name->name+version spec)))
1031 (vhash-cons name (list version proc) table)))))
1035 (define (find-replacement package)
1036 (vhash-fold* (lambda (item proc)
1042 (and (version-prefix? version
1043 (package-version package))
1046 (package-name package)
1049 (define (rewrite package)
1050 (match (find-replacement package)
1052 (proc (proc package))))
1054 (package-mapping rewrite find-replacement))
1056 (define-syntax-rule (package/inherit p overrides ...)
1057 "Like (package (inherit P) OVERRIDES ...), except that the same
1058 transformation is done to the package replacement, if any. P must be a bare
1059 identifier, and will be bound to either P or its replacement when evaluating
1062 (package (inherit p)
1064 (replacement (and=> (package-replacement p) loop)))))
1068 ;;; Package derivations.
1071 (define %derivation-cache
1072 ;; Package to derivation-path mapping.
1073 (make-weak-key-hash-table 100))
1075 (define (cache! cache package system thunk)
1076 "Memoize in CACHE the return values of THUNK as the derivation of PACKAGE on
1078 ;; FIXME: This memoization should be associated with the open store, because
1079 ;; otherwise it breaks when switching to a different store.
1080 (let ((result (thunk)))
1081 ;; Use `hashq-set!' instead of `hash-set!' because `hash' returns the
1082 ;; same value for all structs (as of Guile 2.0.6), and because pointer
1083 ;; equality is sufficient in practice.
1084 (hashq-set! cache package
1085 `((,system . ,result)
1086 ,@(or (hashq-ref cache package) '())))
1089 (define-syntax cached
1091 "Memoize the result of BODY for the arguments PACKAGE and SYSTEM.
1092 Return the cached result when available."
1093 ((_ (=> cache) package system body ...)
1094 (let ((thunk (lambda () body ...))
1096 (match (hashq-ref cache package)
1098 (match (assoc-ref alist key)
1099 (#f (cache! cache package key thunk))
1102 (cache! cache package key thunk)))))
1103 ((_ package system body ...)
1104 (cached (=> %derivation-cache) package system body ...))))
1106 (define* (expand-input store package input system #:optional cross-system)
1107 "Expand INPUT, an input tuple, such that it contains only references to
1108 derivation paths or store paths. PACKAGE is only used to provide contextual
1109 information in exceptions."
1110 (define (intern file)
1111 ;; Add FILE to the store. Set the `recursive?' bit to #t, so that
1112 ;; file permissions are preserved.
1113 (add-to-store store (basename file) #t "sha256" file))
1117 (cut package-cross-derivation store <> cross-system system
1119 (cut package-derivation store <> system #:graft? #f)))
1122 (((? string? name) (? package? package))
1123 (list name (derivation package)))
1124 (((? string? name) (? package? package)
1125 (? string? sub-drv))
1126 (list name (derivation package)
1129 (and (? string?) (? derivation-path?) drv))
1132 (and (? string?) (? file-exists? file)))
1133 ;; Add FILE to the store. When FILE is in the sub-directory of a
1134 ;; store path, it needs to be added anyway, so it can be used as a
1136 (list name (intern file)))
1137 (((? string? name) (? struct? source))
1138 ;; 'package-source-derivation' calls 'lower-object', which can throw
1139 ;; '&gexp-input-error'. However '&gexp-input-error' lacks source
1140 ;; location info, so we catch and rethrow here (XXX: not optimal
1141 ;; performance-wise).
1142 (guard (c ((gexp-input-error? c)
1144 (&package-input-error
1146 (input (gexp-error-invalid-input c)))))))
1147 (list name (package-source-derivation store source system))))
1149 (raise (condition (&package-input-error
1154 ;; 'eq?' cache mapping packages to system+target+graft?-dependent bags.
1155 ;; It significantly speeds things up when doing repeated calls to
1156 ;; 'package->bag' as is the case when building a profile.
1157 (make-weak-key-hash-table 200))
1159 (define* (package->bag package #:optional
1160 (system (%current-system))
1161 (target (%current-target-system))
1162 #:key (graft? (%graft?)))
1163 "Compile PACKAGE into a bag for SYSTEM, possibly cross-compiled to TARGET,
1165 (let ((package (or (and graft? (package-replacement package))
1167 (cached (=> %bag-cache)
1168 package (list system target)
1169 ;; Bind %CURRENT-SYSTEM and %CURRENT-TARGET-SYSTEM so that thunked
1170 ;; field values can refer to it.
1171 (parameterize ((%current-system system)
1172 (%current-target-system target))
1175 ($ <package> name version source build-system
1176 args inputs propagated-inputs native-inputs
1178 ;; Even though we prefer to use "@" to separate the package
1179 ;; name from the package version in various user-facing parts
1180 ;; of Guix, checkStoreName (in nix/libstore/store-api.cc)
1181 ;; prohibits the use of "@", so use "-" instead.
1182 (or (make-bag build-system (string-append name "-" version)
1186 #:inputs (append (inputs self)
1187 (propagated-inputs self))
1189 #:native-inputs (native-inputs self)
1190 #:arguments (args self))
1193 (&package-cross-build-system-error
1197 (package package))))))))))))
1199 (define %graft-cache
1200 ;; 'eq?' cache mapping package objects to a graft corresponding to their
1201 ;; replacement package.
1202 (make-weak-key-hash-table 200))
1204 (define (input-graft store system)
1205 "Return a procedure that, given a package with a replacement and an output name,
1206 returns a graft, and #f otherwise."
1208 (((? package? package) output)
1209 (let ((replacement (package-replacement package)))
1211 (cached (=> %graft-cache) package (cons output system)
1212 (let ((orig (package-derivation store package system
1214 (new (package-derivation store replacement system
1218 (origin-output output)
1220 (replacement-output output)))))))))
1222 (define (input-cross-graft store target system)
1223 "Same as 'input-graft', but for cross-compilation inputs."
1225 (((? package? package) output)
1226 (let ((replacement (package-replacement package)))
1228 (let ((orig (package-cross-derivation store package target system
1230 (new (package-cross-derivation store replacement
1235 (origin-output output)
1237 (replacement-output output))))))))
1239 (define* (fold-bag-dependencies proc seed bag
1241 "Fold PROC over the packages BAG depends on. Each package is visited only
1242 once, in depth-first order. If NATIVE? is true, restrict to native
1243 dependencies; otherwise, restrict to target dependencies."
1244 (define bag-direct-inputs*
1247 (append (bag-build-inputs bag)
1248 (bag-target-inputs bag)
1249 (if (bag-target bag)
1251 (bag-host-inputs bag))))
1254 (let loop ((inputs (bag-direct-inputs* bag))
1256 (visited vlist-null))
1260 (((label (? package? head) . rest) . tail)
1261 (let ((output (match rest (() "out") ((output) output)))
1262 (outputs (vhash-foldq* cons '() head visited)))
1263 (if (member output outputs)
1264 (loop tail result visited)
1265 (let ((inputs (bag-direct-inputs* (package->bag head))))
1266 (loop (append inputs tail)
1267 (proc head output result)
1268 (vhash-consq head output visited))))))
1270 (loop tail result visited)))))
1272 (define* (bag-grafts store bag)
1273 "Return the list of grafts potentially applicable to BAG. Potentially
1274 applicable grafts are collected by looking at direct or indirect dependencies
1275 of BAG that have a 'replacement'. Whether a graft is actually applicable
1276 depends on whether the outputs of BAG depend on the items the grafts refer
1277 to (see 'graft-derivation'.)"
1278 (define system (bag-system bag))
1279 (define target (bag-target bag))
1281 (define native-grafts
1282 (let ((->graft (input-graft store system)))
1283 (parameterize ((%current-system system)
1284 (%current-target-system #f))
1285 (fold-bag-dependencies (lambda (package output grafts)
1286 (match (->graft package output)
1288 (graft (cons graft grafts))))
1292 (define target-grafts
1294 (let ((->graft (input-cross-graft store target system)))
1295 (parameterize ((%current-system system)
1296 (%current-target-system target))
1297 (fold-bag-dependencies (lambda (package output grafts)
1298 (match (->graft package output)
1300 (graft (cons graft grafts))))
1306 ;; We can end up with several identical grafts if we stumble upon packages
1307 ;; that are not 'eq?' but map to the same derivation (this can happen when
1308 ;; using things like 'package-with-explicit-inputs'.) Hence the
1309 ;; 'delete-duplicates' call.
1311 (append native-grafts target-grafts)))
1313 (define* (package-grafts store package
1314 #:optional (system (%current-system))
1316 "Return the list of grafts applicable to PACKAGE as built for SYSTEM and
1318 (let* ((package (or (package-replacement package) package))
1319 (bag (package->bag package system target)))
1320 (bag-grafts store bag)))
1322 (define* (bag->derivation store bag
1324 "Return the derivation to build BAG for SYSTEM. Optionally, CONTEXT can be
1325 a package object describing the context in which the call occurs, for improved
1327 (if (bag-target bag)
1328 (bag->cross-derivation store bag)
1329 (let* ((system (bag-system bag))
1330 (inputs (bag-transitive-inputs bag))
1331 (input-drvs (map (cut expand-input store context <> system)
1333 (paths (delete-duplicates
1334 (append-map (match-lambda
1335 ((_ (? package? p) _ ...)
1336 (package-native-search-paths
1341 (apply (bag-build bag)
1342 store (bag-name bag) input-drvs
1343 #:search-paths paths
1344 #:outputs (bag-outputs bag) #:system system
1345 (bag-arguments bag)))))
1347 (define* (bag->cross-derivation store bag
1349 "Return the derivation to build BAG, which is actually a cross build.
1350 Optionally, CONTEXT can be a package object denoting the context of the call.
1351 This is an internal procedure."
1352 (let* ((system (bag-system bag))
1353 (target (bag-target bag))
1354 (host (bag-transitive-host-inputs bag))
1355 (host-drvs (map (cut expand-input store context <> system target)
1357 (target* (bag-transitive-target-inputs bag))
1358 (target-drvs (map (cut expand-input store context <> system)
1360 (build (bag-transitive-build-inputs bag))
1361 (build-drvs (map (cut expand-input store context <> system)
1363 (all (append build target* host))
1364 (paths (delete-duplicates
1365 (append-map (match-lambda
1366 ((_ (? package? p) _ ...)
1367 (package-search-paths p))
1370 (npaths (delete-duplicates
1371 (append-map (match-lambda
1372 ((_ (? package? p) _ ...)
1373 (package-native-search-paths
1378 (apply (bag-build bag)
1379 store (bag-name bag)
1380 #:native-drvs build-drvs
1381 #:target-drvs (append host-drvs target-drvs)
1382 #:search-paths paths
1383 #:native-search-paths npaths
1384 #:outputs (bag-outputs bag)
1385 #:system system #:target target
1386 (bag-arguments bag))))
1388 (define* (package-derivation store package
1389 #:optional (system (%current-system))
1390 #:key (graft? (%graft?)))
1391 "Return the <derivation> object of PACKAGE for SYSTEM."
1393 ;; Compute the derivation and cache the result. Caching is important
1394 ;; because some derivations, such as the implicit inputs of the GNU build
1395 ;; system, will be queried many, many times in a row.
1396 (cached package (cons system graft?)
1397 (let* ((bag (package->bag package system #f #:graft? graft?))
1398 (drv (bag->derivation store bag package)))
1400 (match (bag-grafts store bag)
1404 (let ((guile (package-derivation store (guile-for-grafts)
1405 system #:graft? #f)))
1406 ;; TODO: As an optimization, we can simply graft the tip
1407 ;; of the derivation graph since 'graft-derivation'
1409 (graft-derivation store drv grafts
1414 (define* (package-cross-derivation store package target
1415 #:optional (system (%current-system))
1416 #:key (graft? (%graft?)))
1417 "Cross-build PACKAGE for TARGET (a GNU triplet) from host SYSTEM (a Guix
1418 system identifying string)."
1419 (cached package (list system target graft?)
1420 (let* ((bag (package->bag package system target #:graft? graft?))
1421 (drv (bag->derivation store bag package)))
1423 (match (bag-grafts store bag)
1427 (graft-derivation store drv grafts
1430 (package-derivation store (guile-for-grafts)
1431 system #:graft? #f))))
1434 (define* (package-output store package
1435 #:optional (output "out") (system (%current-system)))
1436 "Return the output path of PACKAGE's OUTPUT for SYSTEM---where OUTPUT is the
1437 symbolic output name, such as \"out\". Note that this procedure calls
1438 `package-derivation', which is costly."
1439 (let ((drv (package-derivation store package system)))
1440 (derivation->output-path drv output)))
1444 ;;; Monadic interface.
1447 (define (set-guile-for-build guile)
1448 "This monadic procedure changes the Guile currently used to run the build
1449 code of derivations to GUILE, a package object."
1451 (let ((guile (package-derivation store guile)))
1452 (values (%guile-for-build guile) store))))
1454 (define* (package-file package
1457 system (output "out") target)
1458 "Return as a monadic value the absolute file name of FILE within the
1459 OUTPUT directory of PACKAGE. When FILE is omitted, return the name of the
1460 OUTPUT directory of PACKAGE. When TARGET is true, use it as a
1461 cross-compilation target triplet.
1463 Note that this procedure does _not_ build PACKAGE. Thus, the result might or
1464 might not designate an existing file. We recommend not using this procedure
1465 unless you know what you are doing."
1467 (define compute-derivation
1469 (cut package-cross-derivation <> <> target <>)
1470 package-derivation))
1472 (let* ((system (or system (%current-system)))
1473 (drv (compute-derivation store package system))
1474 (out (derivation->output-path drv output)))
1476 (string-append out "/" file)
1480 (define package->derivation
1481 (store-lift package-derivation))
1483 (define package->cross-derivation
1484 (store-lift package-cross-derivation))
1486 (define-gexp-compiler (package-compiler (package <package>) system target)
1487 ;; Compile PACKAGE to a derivation for SYSTEM, optionally cross-compiled for
1488 ;; TARGET. This is used when referring to a package from within a gexp.
1490 (package->cross-derivation package target system)
1491 (package->derivation package system)))
1493 (define* (origin->derivation origin
1494 #:optional (system (%current-system)))
1495 "Return the derivation corresponding to ORIGIN."
1497 (($ <origin> uri method hash name (= force ()) #f)
1498 ;; No patches, no snippet: this is a fixed-output derivation.
1500 (content-hash-algorithm hash)
1501 (content-hash-value hash)
1502 name #:system system))
1503 (($ <origin> uri method hash name (= force (patches ...)) snippet
1504 (flags ...) inputs (modules ...) guile-for-build)
1505 ;; Patches and/or a snippet.
1506 (mlet %store-monad ((source (method uri
1507 (content-hash-algorithm hash)
1508 (content-hash-value hash)
1509 name #:system system))
1510 (guile (package->derivation (or guile-for-build
1514 (patch-and-repack source patches
1520 #:guile-for-build guile)))))
1522 (define-gexp-compiler (origin-compiler (origin <origin>) system target)
1523 ;; Compile ORIGIN to a derivation for SYSTEM. This is used when referring
1524 ;; to an origin from within a gexp.
1525 (origin->derivation origin system))
1527 (define package-source-derivation ;somewhat deprecated
1528 (let ((lower (store-lower lower-object)))
1529 (lambda* (store source #:optional (system (%current-system)))
1530 "Return the derivation or file corresponding to SOURCE, which can be an
1531 a file name or any object handled by 'lower-object', such as an <origin>.
1532 When SOURCE is a file name, return either the interned file name (if SOURCE is
1533 outside of the store) or SOURCE itself (if SOURCE is already a store item.)"
1535 ((and (? string?) (? direct-store-path?) file)
1538 (add-to-store store (basename file) #t "sha256" file))
1540 (lower store source system))))))