1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (guix inferior)
20 #:use-module (srfi srfi-9)
21 #:use-module (srfi srfi-9 gnu)
22 #:use-module (srfi srfi-34)
23 #:use-module (srfi srfi-35)
24 #:use-module ((guix diagnostics)
25 #:select (source-properties->location))
26 #:use-module ((guix utils)
27 #:select (%current-system
28 call-with-temporary-directory
29 version>? version-prefix?
31 #:use-module ((guix store)
32 #:select (store-connection-socket
33 store-connection-major-version
34 store-connection-minor-version
36 &store-protocol-error))
37 #:use-module ((guix derivations)
38 #:select (read-derivation-from-file))
39 #:use-module (guix gexp)
40 #:use-module (guix search-paths)
41 #:use-module (guix profiles)
42 #:use-module (guix channels)
43 #:use-module ((guix git) #:select (update-cached-checkout))
44 #:use-module (guix monads)
45 #:use-module (guix store)
46 #:use-module (guix derivations)
47 #:use-module (guix base32)
48 #:use-module (gcrypt hash)
49 #:autoload (guix cache) (maybe-remove-expired-cache-entries
51 #:autoload (guix ui) (show-what-to-build*)
52 #:autoload (guix build utils) (mkdir-p)
53 #:use-module (srfi srfi-1)
54 #:use-module (srfi srfi-26)
55 #:use-module (srfi srfi-71)
56 #:autoload (ice-9 ftw) (scandir)
57 #:use-module (ice-9 match)
58 #:use-module (ice-9 popen)
59 #:use-module (ice-9 vlist)
60 #:use-module (ice-9 binary-ports)
61 #:use-module ((rnrs bytevectors) #:select (string->utf8))
67 inferior-eval-with-store
70 inferior-exception-arguments
71 inferior-exception-inferior
72 inferior-exception-stack
76 inferior-available-packages
77 lookup-inferior-packages
81 inferior-package-version
82 inferior-package-synopsis
83 inferior-package-description
84 inferior-package-home-page
85 inferior-package-location
86 inferior-package-inputs
87 inferior-package-native-inputs
88 inferior-package-propagated-inputs
89 inferior-package-transitive-propagated-inputs
90 inferior-package-native-search-paths
91 inferior-package-transitive-native-search-paths
92 inferior-package-search-paths
93 inferior-package-replacement
94 inferior-package-provenance
95 inferior-package-derivation
97 inferior-package->manifest-entry
99 gexp->derivation-in-inferior
101 %inferior-cache-directory
102 cached-channel-instance
103 inferior-for-channels))
107 ;;; This module provides a way to spawn Guix "inferior" processes and to talk
108 ;;; to them. It allows us, from one instance of Guix, to interact with
109 ;;; another instance of Guix coming from a different commit.
113 ;; Inferior Guix process.
114 (define-record-type <inferior>
115 (inferior pid socket close version packages table)
118 (socket inferior-socket)
119 (close inferior-close-socket) ;procedure
120 (version inferior-version) ;REPL protocol version
121 (packages inferior-package-promise) ;promise of inferior packages
122 (table inferior-package-table)) ;promise of vhash
124 (define (write-inferior inferior port)
126 (($ <inferior> pid _ _ version)
127 (format port "#<inferior ~a ~a ~a>"
129 (number->string (object-address inferior) 16)))))
131 (set-record-type-printer! <inferior> write-inferior)
133 (define* (inferior-pipe directory command error-port)
134 "Return an input/output pipe on the Guix instance in DIRECTORY. This runs
135 'DIRECTORY/COMMAND repl' if it exists, or falls back to some other method if
137 (let ((pipe (with-error-to-port error-port
139 (open-pipe* OPEN_BOTH
140 (string-append directory "/" command)
141 "repl" "-t" "machine")))))
142 (if (eof-object? (peek-char pipe))
146 ;; Older versions of Guix didn't have a 'guix repl' command, so
148 (with-error-to-port error-port
150 (open-pipe* OPEN_BOTH "guile"
151 "-L" (string-append directory "/share/guile/site/"
153 "-C" (string-append directory "/share/guile/site/"
155 "-C" (string-append directory "/lib/guile/"
156 (effective-version) "/site-ccache")
160 (primitive-load ,(search-path %load-path
162 ((@ (guix repl) machine-repl))))))))
165 (define* (port->inferior pipe #:optional (close close-port))
166 "Given PIPE, an input/output port, return an inferior that talks over PIPE.
167 PIPE is closed with CLOSE when 'close-inferior' is called on the returned
172 (('repl-version 0 rest ...)
173 (letrec ((result (inferior 'pipe pipe close (cons 0 rest)
174 (delay (%inferior-packages result))
175 (delay (%inferior-package-table result)))))
177 ;; For protocol (0 1) and later, send the protocol version we support.
181 (send-inferior-request '(() repl-version 0 1 1) result)))
185 (inferior-eval '(use-modules (guix)) result)
186 (inferior-eval '(use-modules (gnu)) result)
187 (inferior-eval '(use-modules (ice-9 match)) result)
188 (inferior-eval '(use-modules (srfi srfi-34)) result)
189 (inferior-eval '(define %package-table (make-hash-table))
195 (define* (open-inferior directory
196 #:key (command "bin/guix")
197 (error-port (%make-void-port "w")))
198 "Open the inferior Guix in DIRECTORY, running 'DIRECTORY/COMMAND repl' or
199 equivalent. Return #f if the inferior could not be launched."
201 (inferior-pipe directory command error-port))
203 (port->inferior pipe close-pipe))
205 (define (close-inferior inferior)
207 (let ((close (inferior-close-socket inferior)))
208 (close (inferior-socket inferior))))
210 ;; Non-self-quoting object of the inferior.
211 (define-record-type <inferior-object>
212 (inferior-object address appearance)
214 (address inferior-object-address)
215 (appearance inferior-object-appearance))
217 (define (write-inferior-object object port)
219 (($ <inferior-object> _ appearance)
220 (format port "#<inferior-object ~a>" appearance))))
222 (set-record-type-printer! <inferior-object> write-inferior-object)
224 ;; Reified exception thrown by an inferior.
225 (define-condition-type &inferior-exception &error
227 (arguments inferior-exception-arguments) ;key + arguments
228 (inferior inferior-exception-inferior) ;<inferior> | #f
229 (stack inferior-exception-stack)) ;list of (FILE COLUMN LINE)
231 (define* (read-repl-response port #:optional inferior)
232 "Read a (guix repl) response from PORT and return it as a Scheme object.
233 Raise '&inferior-exception' when an exception is read from PORT."
238 (('non-self-quoting address string)
239 (inferior-object address string))))
242 (('values objects ...)
243 (apply values (map sexp->object objects)))
244 (('exception ('arguments key objects ...)
246 ;; Protocol (0 1 1) and later.
247 (raise (condition (&inferior-exception
248 (arguments (cons key (map sexp->object objects)))
251 (('exception key objects ...)
253 (raise (condition (&inferior-exception
254 (arguments (cons key (map sexp->object objects)))
258 (define (read-inferior-response inferior)
259 (read-repl-response (inferior-socket inferior)
262 (define (send-inferior-request exp inferior)
263 (write exp (inferior-socket inferior))
264 (newline (inferior-socket inferior)))
266 (define (inferior-eval exp inferior)
267 "Evaluate EXP in INFERIOR."
268 (send-inferior-request exp inferior)
269 (read-inferior-response inferior))
273 ;;; Inferior packages.
276 (define-record-type <inferior-package>
277 (inferior-package inferior name version id)
279 (inferior inferior-package-inferior)
280 (name inferior-package-name)
281 (version inferior-package-version)
282 (id inferior-package-id))
284 (define (write-inferior-package package port)
286 (($ <inferior-package> _ name version)
287 (format port "#<inferior-package ~a@~a ~a>"
289 (number->string (object-address package) 16)))))
291 (set-record-type-printer! <inferior-package> write-inferior-package)
293 (define (%inferior-packages inferior)
294 "Compute the list of inferior packages from INFERIOR."
295 (let ((result (inferior-eval
296 '(fold-packages (lambda (package result)
297 (let ((id (object-address package)))
298 (hashv-set! %package-table id package)
299 (cons (list (package-name package)
300 (package-version package)
307 (inferior-package inferior name version id)))
310 (define (inferior-packages inferior)
311 "Return the list of packages known to INFERIOR."
312 (force (inferior-package-promise inferior)))
314 (define (%inferior-package-table inferior)
315 "Compute a package lookup table for INFERIOR."
316 (fold (lambda (package table)
317 (vhash-cons (inferior-package-name package) package
320 (inferior-packages inferior)))
322 (define (inferior-available-packages inferior)
323 "Return the list of name/version pairs corresponding to the set of packages
324 available in INFERIOR.
326 This is faster and less resource-intensive than calling 'inferior-packages'."
327 (if (inferior-eval '(defined? 'fold-available-packages)
329 (inferior-eval '(fold-available-packages
330 (lambda* (name version result
331 #:key supported? deprecated?
333 (if (and supported? (not deprecated?))
334 (acons name version result)
339 ;; As a last resort, if INFERIOR is old and lacks
340 ;; 'fold-available-packages', fall back to 'inferior-packages'.
341 (map (lambda (package)
342 (cons (inferior-package-name package)
343 (inferior-package-version package)))
344 (inferior-packages inferior))))
346 (define* (lookup-inferior-packages inferior name #:optional version)
347 "Return the sorted list of inferior packages matching NAME in INFERIOR, with
348 highest version numbers first. If VERSION is true, return only packages with
349 a version number prefixed by VERSION."
350 ;; This is the counterpart of 'find-packages-by-name'.
351 (sort (filter (lambda (package)
353 (version-prefix? version
354 (inferior-package-version package))))
355 (vhash-fold* cons '() name
356 (force (inferior-package-table inferior))))
358 (version>? (inferior-package-version p1)
359 (inferior-package-version p2)))))
361 (define (inferior-package-field package getter)
362 "Return the field of PACKAGE, an inferior package, accessed with GETTER."
363 (let ((inferior (inferior-package-inferior package))
364 (id (inferior-package-id package)))
365 (inferior-eval `(,getter (hashv-ref %package-table ,id))
368 (define* (inferior-package-synopsis package #:key (translate? #t))
369 "Return the Texinfo synopsis of PACKAGE, an inferior package. When
370 TRANSLATE? is true, translate it to the current locale's language."
371 (inferior-package-field package
373 '(compose (@ (guix ui) P_) package-synopsis)
376 (define* (inferior-package-description package #:key (translate? #t))
377 "Return the Texinfo description of PACKAGE, an inferior package. When
378 TRANSLATE? is true, translate it to the current locale's language."
379 (inferior-package-field package
381 '(compose (@ (guix ui) P_) package-description)
382 'package-description)))
384 (define (inferior-package-home-page package)
385 "Return the home page of PACKAGE."
386 (inferior-package-field package 'package-home-page))
388 (define (inferior-package-location package)
389 "Return the source code location of PACKAGE, either #f or a <location>
391 (source-properties->location
392 (inferior-package-field package
393 '(compose (lambda (loc)
395 (location->source-properties
399 (define (inferior-package-input-field package field)
400 "Return the input field FIELD (e.g., 'native-inputs') of PACKAGE, an
403 `(compose (lambda (inputs)
405 ;; XXX: Origins are not handled.
406 ((label (? package? package) rest ...)
407 (let ((id (object-address package)))
408 (hashv-set! %package-table id package)
409 `(,label (package ,id
410 ,(package-name package)
411 ,(package-version package))
419 (inferior-package-field package field*))
422 (inferior-package-inferior package))
425 ((label ('package id name version) . rest)
426 ;; XXX: eq?-ness of inferior packages is not preserved here.
427 `(,label ,(inferior-package inferior name version id)
432 (define inferior-package-inputs
433 (cut inferior-package-input-field <> 'package-inputs))
435 (define inferior-package-native-inputs
436 (cut inferior-package-input-field <> 'package-native-inputs))
438 (define inferior-package-propagated-inputs
439 (cut inferior-package-input-field <> 'package-propagated-inputs))
441 (define inferior-package-transitive-propagated-inputs
442 (cut inferior-package-input-field <> 'package-transitive-propagated-inputs))
444 (define (%inferior-package-search-paths package field)
445 "Return the list of search path specifications of PACKAGE, an inferior
448 (inferior-package-field package
449 `(compose (lambda (paths)
450 (map (@ (guix search-paths)
451 search-path-specification->sexp)
455 (map sexp->search-path-specification paths))
457 (define inferior-package-native-search-paths
458 (cut %inferior-package-search-paths <> 'package-native-search-paths))
460 (define inferior-package-search-paths
461 (cut %inferior-package-search-paths <> 'package-search-paths))
463 (define inferior-package-transitive-native-search-paths
464 (cut %inferior-package-search-paths <> 'package-transitive-native-search-paths))
466 (define (inferior-package-replacement package)
467 "Return the replacement for PACKAGE. This will either be an inferior
469 (match (inferior-package-field
471 '(compose (match-lambda
472 ((? package? package)
473 (let ((id (object-address package)))
474 (hashv-set! %package-table id package)
476 (package-name package)
477 (package-version package))))
479 package-replacement))
482 (inferior-package (inferior-package-inferior package)
487 (define (inferior-package-provenance package)
488 "Return a \"provenance sexp\" for PACKAGE, an inferior package. The result
489 is similar to the sexp returned by 'package-provenance' for regular packages."
490 (inferior-package-field package
493 (resolve-interface '(guix describe))))
497 'package-provenance))))
498 (or provenance (const #f)))))
500 (define (proxy client backend) ;adapted from (guix ssh)
501 "Proxy communication between CLIENT and BACKEND until CLIENT closes the
502 connection, at which point CLIENT is closed (both CLIENT and BACKEND must be
503 input/output ports.)"
504 ;; Use buffered ports so that 'get-bytevector-some' returns up to the
505 ;; whole buffer like read(2) would--see <https://bugs.gnu.org/30066>.
506 (setvbuf client 'block 65536)
507 (setvbuf backend 'block 65536)
510 (match (select (list client backend) '() '())
512 (when (memq client reads)
513 (match (get-bytevector-some client)
517 (put-bytevector backend bv)
518 (force-output backend))))
519 (when (memq backend reads)
520 (match (get-bytevector-some backend)
522 (put-bytevector client bv)
523 (force-output client))))
524 (unless (port-closed? client)
527 (define (inferior-eval-with-store inferior store code)
528 "Evaluate CODE in INFERIOR, passing it STORE as its argument. CODE must
529 thus be the code of a one-argument procedure that accepts a store."
530 ;; Create a named socket in /tmp and let INFERIOR connect to it and use it
531 ;; as its store. This ensures the inferior uses the same store, with the
532 ;; same options, the same per-session GC roots, etc.
533 ;; FIXME: This strategy doesn't work for remote inferiors (SSH).
534 (call-with-temporary-directory
536 (chmod directory #o700)
537 (let* ((name (string-append directory "/inferior"))
538 (socket (socket AF_UNIX SOCK_STREAM 0))
539 (major (store-connection-major-version store))
540 (minor (store-connection-minor-version store))
541 (proto (logior major minor)))
542 (bind socket AF_UNIX name)
544 (send-inferior-request
546 (socket (socket AF_UNIX SOCK_STREAM 0))
547 (error? (if (defined? 'store-protocol-error?)
548 store-protocol-error?
549 nix-protocol-error?))
550 (error-message (if (defined? 'store-protocol-error-message)
551 store-protocol-error-message
552 nix-protocol-error-message)))
553 (connect socket AF_UNIX ,name)
555 ;; 'port->connection' appeared in June 2018 and we can hardly
556 ;; emulate it on older versions. Thus fall back to
557 ;; 'open-connection', at the risk of talking to the wrong daemon or
558 ;; having our build result reclaimed (XXX).
559 (let ((store (if (defined? 'port->connection)
560 (port->connection socket #:version ,proto)
565 ;; Serialize '&store-protocol-error' conditions. The
566 ;; exception serialization mechanism that
567 ;; 'read-repl-response' expects is unsuitable for SRFI-35
568 ;; error conditions, hence this special case.
569 (guard (c ((error? c)
570 `(store-protocol-error ,(error-message c))))
571 `(result ,(proc store))))
573 (close-connection store)
574 (close-port socket)))))
576 (match (accept socket)
578 (proxy client (store-connection-socket store))))
581 (match (read-inferior-response inferior)
582 (('store-protocol-error message)
584 (&store-protocol-error (message message)
589 (define* (inferior-package-derivation store package
591 (system (%current-system))
593 "Return the derivation for PACKAGE, an inferior package, built for SYSTEM
594 and cross-built for TARGET if TARGET is true. The inferior corresponding to
595 PACKAGE must be live."
598 (let* ((package (hashv-ref %package-table
599 ,(inferior-package-id package)))
601 `(package-cross-derivation store package
604 `(package-derivation store package
606 (derivation-file-name drv))))
608 (and=> (inferior-eval-with-store (inferior-package-inferior package) store
610 read-derivation-from-file))
612 (define inferior-package->derivation
613 (store-lift inferior-package-derivation))
615 (define-gexp-compiler (package-compiler (package <inferior-package>) system
617 ;; Compile PACKAGE for SYSTEM, optionally cross-building for TARGET.
618 (inferior-package->derivation package system #:target target))
620 (define* (gexp->derivation-in-inferior name exp guix
621 #:key silent-failure?
624 "Return a derivation that evaluates EXP with GUIX, an instance of Guix as
625 returned for example by 'channel-instances->derivation'. Other arguments are
626 passed as-is to 'gexp->derivation'.
628 When SILENT-FAILURE? is true, create an empty output directory instead of
629 failing when GUIX is too old and lacks the 'guix repl' command."
631 ;; EXP wrapped with a proper (set! %load-path …) prologue.
632 (scheme-file "inferior-script.scm" exp))
635 ;; This is a crude way to run EXP on GUIX. TODO: use 'raw-derivation' and
636 ;; make 'guix repl' the "builder"; this will require "opening up" the
637 ;; mechanisms behind 'gexp->derivation', and adding '-l' to 'guix repl'.
639 (use-modules (ice-9 popen))
641 (let ((pipe (open-pipe* OPEN_WRITE
642 #+(file-append guix "/bin/guix")
643 "repl" "-t" "machine")))
645 ;; XXX: EXP presumably refers to #$output but that reference is lost
646 ;; so explicitly reference it here.
649 (write `(primitive-load #$script) pipe)
651 (unless (zero? (close-pipe pipe))
652 (if #$silent-failure?
654 (error "inferior failed" #+guix))))))
656 (define (drop-extra-keyword lst)
662 ((#:silent-failure? _ . rest)
665 (loop tail (cons* value kw result))))))
667 (apply gexp->derivation name trampoline
668 (drop-extra-keyword rest)))
672 ;;; Manifest entries.
675 (define* (inferior-package->manifest-entry package
676 #:optional (output "out")
677 #:key (properties '()))
678 "Return a manifest entry for the OUTPUT of package PACKAGE."
682 (define-syntax-rule (memoized package output exp)
683 ;; Memoize the entry returned by EXP for PACKAGE/OUTPUT. This is
684 ;; important as the same package may be traversed many times through
685 ;; propagated inputs, and querying the inferior is costly. Use
686 ;; 'hash'/'equal?', which is okay since <inferior-package> is simple.
687 (let ((compute (lambda () exp))
688 (key (cons package output)))
689 (or (hash-ref cache key)
690 (let ((result (compute)))
691 (hash-set! cache key result)
694 (let loop ((package package)
697 (memoized package output
698 ;; For each dependency, keep a promise pointing to its "parent" entry.
699 (letrec* ((deps (map (match-lambda
701 (loop package "out" (delay entry)))
702 ((label package output)
703 (loop package output (delay entry))))
704 (inferior-package-propagated-inputs package)))
705 (entry (manifest-entry
706 (name (inferior-package-name package))
707 (version (inferior-package-version package))
710 (dependencies (delete-duplicates deps))
712 (inferior-package-transitive-native-search-paths package))
714 (properties properties))))
719 ;;; Cached inferiors.
722 (define %inferior-cache-directory
723 ;; Directory for cached inferiors (GC roots).
724 (make-parameter (string-append (cache-directory #:ensure? #f)
727 (define (channel-full-commit channel)
728 "Return the commit designated by CHANNEL as quickly as possible. If
729 CHANNEL's 'commit' field is a full SHA1, return it as-is; if it's a SHA1
730 prefix, resolve it; and if 'commit' is unset, fetch CHANNEL's branch tip."
731 (let ((commit (channel-commit channel))
732 (branch (channel-branch channel)))
733 (if (and commit (= (string-length commit) 40))
735 (let* ((ref (if commit `(commit . ,commit) `(branch . ,branch)))
736 (cache commit relation
737 (update-cached-checkout (channel-url channel)
742 (define* (cached-channel-instance store
746 (cache-directory (%inferior-cache-directory))
747 (ttl (* 3600 24 30)))
748 "Return a directory containing a guix filetree defined by CHANNELS, a list of channels.
749 The directory is a subdirectory of CACHE-DIRECTORY, where entries can be reclaimed after TTL seconds.
750 This procedure opens a new connection to the build daemon. AUTHENTICATE?
751 determines whether CHANNELS are authenticated."
753 ;; Since computing the instances of CHANNELS is I/O-intensive, use a
754 ;; cheaper way to get the commit list of CHANNELS. This limits overhead
755 ;; to the minimum in case of a cache hit.
756 (map channel-full-commit channels))
759 (bytevector->base32-string
761 (string->utf8 (string-concatenate commits)))))
764 (string-append cache-directory "/" key))
766 (define (base32-encoded-sha256? str)
767 (= (string-length str) 52))
769 (define (cache-entries directory)
771 (string-append directory "/" file))
772 (scandir directory base32-encoded-sha256?)))
774 (define (symlink/safe old new)
779 (unless (= EEXIST (system-error-errno args))
780 (apply throw args)))))
783 (lift2 symlink/safe %store-monad))
785 (define add-indirect-root*
786 (store-lift add-indirect-root))
788 (mkdir-p cache-directory)
789 (maybe-remove-expired-cache-entries cache-directory
792 (file-expiration-time ttl))
794 (if (file-exists? cached)
796 (run-with-store store
797 (mlet* %store-monad ((instances
798 -> (latest-channel-instances store channels
802 (channel-instances->derivation instances)))
804 (show-what-to-build* (list profile))
805 (built-derivations (list profile))
806 ;; Note: Caching is fine even when AUTHENTICATE? is false because
807 ;; we always call 'latest-channel-instances?'.
808 (symlink* (derivation->output-path profile) cached)
809 (add-indirect-root* cached)
812 (define* (inferior-for-channels channels
814 (cache-directory (%inferior-cache-directory))
815 (ttl (* 3600 24 30)))
816 "Return an inferior for CHANNELS, a list of channels. Use the cache at
817 CACHE-DIRECTORY, where entries can be reclaimed after TTL seconds. This
818 procedure opens a new connection to the build daemon.
820 This is a convenience procedure that people may use in manifests passed to
821 'guix package -m', for instance."
824 (cached-channel-instance store
826 #:cache-directory cache-directory
828 (open-inferior cached))
831 ;;; eval: (put 'memoized 'scheme-indent-function 1)