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-provenance
94 inferior-package-derivation
96 inferior-package->manifest-entry
98 gexp->derivation-in-inferior
100 %inferior-cache-directory
101 cached-channel-instance
102 inferior-for-channels))
106 ;;; This module provides a way to spawn Guix "inferior" processes and to talk
107 ;;; to them. It allows us, from one instance of Guix, to interact with
108 ;;; another instance of Guix coming from a different commit.
112 ;; Inferior Guix process.
113 (define-record-type <inferior>
114 (inferior pid socket close version packages table)
117 (socket inferior-socket)
118 (close inferior-close-socket) ;procedure
119 (version inferior-version) ;REPL protocol version
120 (packages inferior-package-promise) ;promise of inferior packages
121 (table inferior-package-table)) ;promise of vhash
123 (define* (inferior-pipe directory command error-port)
124 "Return an input/output pipe on the Guix instance in DIRECTORY. This runs
125 'DIRECTORY/COMMAND repl' if it exists, or falls back to some other method if
127 (let ((pipe (with-error-to-port error-port
129 (open-pipe* OPEN_BOTH
130 (string-append directory "/" command)
131 "repl" "-t" "machine")))))
132 (if (eof-object? (peek-char pipe))
136 ;; Older versions of Guix didn't have a 'guix repl' command, so
138 (with-error-to-port error-port
140 (open-pipe* OPEN_BOTH "guile"
141 "-L" (string-append directory "/share/guile/site/"
143 "-C" (string-append directory "/share/guile/site/"
145 "-C" (string-append directory "/lib/guile/"
146 (effective-version) "/site-ccache")
150 (primitive-load ,(search-path %load-path
152 ((@ (guix repl) machine-repl))))))))
155 (define* (port->inferior pipe #:optional (close close-port))
156 "Given PIPE, an input/output port, return an inferior that talks over PIPE.
157 PIPE is closed with CLOSE when 'close-inferior' is called on the returned
162 (('repl-version 0 rest ...)
163 (letrec ((result (inferior 'pipe pipe close (cons 0 rest)
164 (delay (%inferior-packages result))
165 (delay (%inferior-package-table result)))))
167 ;; For protocol (0 1) and later, send the protocol version we support.
171 (send-inferior-request '(() repl-version 0 1 1) result)))
175 (inferior-eval '(use-modules (guix)) result)
176 (inferior-eval '(use-modules (gnu)) result)
177 (inferior-eval '(use-modules (ice-9 match)) result)
178 (inferior-eval '(use-modules (srfi srfi-34)) result)
179 (inferior-eval '(define %package-table (make-hash-table))
185 (define* (open-inferior directory
186 #:key (command "bin/guix")
187 (error-port (%make-void-port "w")))
188 "Open the inferior Guix in DIRECTORY, running 'DIRECTORY/COMMAND repl' or
189 equivalent. Return #f if the inferior could not be launched."
191 (inferior-pipe directory command error-port))
193 (port->inferior pipe close-pipe))
195 (define (close-inferior inferior)
197 (let ((close (inferior-close-socket inferior)))
198 (close (inferior-socket inferior))))
200 ;; Non-self-quoting object of the inferior.
201 (define-record-type <inferior-object>
202 (inferior-object address appearance)
204 (address inferior-object-address)
205 (appearance inferior-object-appearance))
207 (define (write-inferior-object object port)
209 (($ <inferior-object> _ appearance)
210 (format port "#<inferior-object ~a>" appearance))))
212 (set-record-type-printer! <inferior-object> write-inferior-object)
214 ;; Reified exception thrown by an inferior.
215 (define-condition-type &inferior-exception &error
217 (arguments inferior-exception-arguments) ;key + arguments
218 (inferior inferior-exception-inferior) ;<inferior> | #f
219 (stack inferior-exception-stack)) ;list of (FILE COLUMN LINE)
221 (define* (read-repl-response port #:optional inferior)
222 "Read a (guix repl) response from PORT and return it as a Scheme object.
223 Raise '&inferior-exception' when an exception is read from PORT."
228 (('non-self-quoting address string)
229 (inferior-object address string))))
232 (('values objects ...)
233 (apply values (map sexp->object objects)))
234 (('exception ('arguments key objects ...)
236 ;; Protocol (0 1 1) and later.
237 (raise (condition (&inferior-exception
238 (arguments (cons key (map sexp->object objects)))
241 (('exception key objects ...)
243 (raise (condition (&inferior-exception
244 (arguments (cons key (map sexp->object objects)))
248 (define (read-inferior-response inferior)
249 (read-repl-response (inferior-socket inferior)
252 (define (send-inferior-request exp inferior)
253 (write exp (inferior-socket inferior))
254 (newline (inferior-socket inferior)))
256 (define (inferior-eval exp inferior)
257 "Evaluate EXP in INFERIOR."
258 (send-inferior-request exp inferior)
259 (read-inferior-response inferior))
263 ;;; Inferior packages.
266 (define-record-type <inferior-package>
267 (inferior-package inferior name version id)
269 (inferior inferior-package-inferior)
270 (name inferior-package-name)
271 (version inferior-package-version)
272 (id inferior-package-id))
274 (define (write-inferior-package package port)
276 (($ <inferior-package> _ name version)
277 (format port "#<inferior-package ~a@~a ~a>"
279 (number->string (object-address package) 16)))))
281 (set-record-type-printer! <inferior-package> write-inferior-package)
283 (define (%inferior-packages inferior)
284 "Compute the list of inferior packages from INFERIOR."
285 (let ((result (inferior-eval
286 '(fold-packages (lambda (package result)
287 (let ((id (object-address package)))
288 (hashv-set! %package-table id package)
289 (cons (list (package-name package)
290 (package-version package)
297 (inferior-package inferior name version id)))
300 (define (inferior-packages inferior)
301 "Return the list of packages known to INFERIOR."
302 (force (inferior-package-promise inferior)))
304 (define (%inferior-package-table inferior)
305 "Compute a package lookup table for INFERIOR."
306 (fold (lambda (package table)
307 (vhash-cons (inferior-package-name package) package
310 (inferior-packages inferior)))
312 (define (inferior-available-packages inferior)
313 "Return the list of name/version pairs corresponding to the set of packages
314 available in INFERIOR.
316 This is faster and less resource-intensive than calling 'inferior-packages'."
317 (if (inferior-eval '(defined? 'fold-available-packages)
319 (inferior-eval '(fold-available-packages
320 (lambda* (name version result
321 #:key supported? deprecated?
323 (if (and supported? (not deprecated?))
324 (acons name version result)
329 ;; As a last resort, if INFERIOR is old and lacks
330 ;; 'fold-available-packages', fall back to 'inferior-packages'.
331 (map (lambda (package)
332 (cons (inferior-package-name package)
333 (inferior-package-version package)))
334 (inferior-packages inferior))))
336 (define* (lookup-inferior-packages inferior name #:optional version)
337 "Return the sorted list of inferior packages matching NAME in INFERIOR, with
338 highest version numbers first. If VERSION is true, return only packages with
339 a version number prefixed by VERSION."
340 ;; This is the counterpart of 'find-packages-by-name'.
341 (sort (filter (lambda (package)
343 (version-prefix? version
344 (inferior-package-version package))))
345 (vhash-fold* cons '() name
346 (force (inferior-package-table inferior))))
348 (version>? (inferior-package-version p1)
349 (inferior-package-version p2)))))
351 (define (inferior-package-field package getter)
352 "Return the field of PACKAGE, an inferior package, accessed with GETTER."
353 (let ((inferior (inferior-package-inferior package))
354 (id (inferior-package-id package)))
355 (inferior-eval `(,getter (hashv-ref %package-table ,id))
358 (define* (inferior-package-synopsis package #:key (translate? #t))
359 "Return the Texinfo synopsis of PACKAGE, an inferior package. When
360 TRANSLATE? is true, translate it to the current locale's language."
361 (inferior-package-field package
363 '(compose (@ (guix ui) P_) package-synopsis)
366 (define* (inferior-package-description package #:key (translate? #t))
367 "Return the Texinfo description of PACKAGE, an inferior package. When
368 TRANSLATE? is true, translate it to the current locale's language."
369 (inferior-package-field package
371 '(compose (@ (guix ui) P_) package-description)
372 'package-description)))
374 (define (inferior-package-home-page package)
375 "Return the home page of PACKAGE."
376 (inferior-package-field package 'package-home-page))
378 (define (inferior-package-location package)
379 "Return the source code location of PACKAGE, either #f or a <location>
381 (source-properties->location
382 (inferior-package-field package
383 '(compose (lambda (loc)
385 (location->source-properties
389 (define (inferior-package-input-field package field)
390 "Return the input field FIELD (e.g., 'native-inputs') of PACKAGE, an
393 `(compose (lambda (inputs)
395 ;; XXX: Origins are not handled.
396 ((label (? package? package) rest ...)
397 (let ((id (object-address package)))
398 (hashv-set! %package-table id package)
399 `(,label (package ,id
400 ,(package-name package)
401 ,(package-version package))
409 (inferior-package-field package field*))
412 (inferior-package-inferior package))
415 ((label ('package id name version) . rest)
416 ;; XXX: eq?-ness of inferior packages is not preserved here.
417 `(,label ,(inferior-package inferior name version id)
422 (define inferior-package-inputs
423 (cut inferior-package-input-field <> 'package-inputs))
425 (define inferior-package-native-inputs
426 (cut inferior-package-input-field <> 'package-native-inputs))
428 (define inferior-package-propagated-inputs
429 (cut inferior-package-input-field <> 'package-propagated-inputs))
431 (define inferior-package-transitive-propagated-inputs
432 (cut inferior-package-input-field <> 'package-transitive-propagated-inputs))
434 (define (%inferior-package-search-paths package field)
435 "Return the list of search path specifications of PACKAGE, an inferior
438 (inferior-package-field package
439 `(compose (lambda (paths)
440 (map (@ (guix search-paths)
441 search-path-specification->sexp)
445 (map sexp->search-path-specification paths))
447 (define inferior-package-native-search-paths
448 (cut %inferior-package-search-paths <> 'package-native-search-paths))
450 (define inferior-package-search-paths
451 (cut %inferior-package-search-paths <> 'package-search-paths))
453 (define inferior-package-transitive-native-search-paths
454 (cut %inferior-package-search-paths <> 'package-transitive-native-search-paths))
456 (define (inferior-package-provenance package)
457 "Return a \"provenance sexp\" for PACKAGE, an inferior package. The result
458 is similar to the sexp returned by 'package-provenance' for regular packages."
459 (inferior-package-field package
462 (resolve-interface '(guix describe))))
466 'package-provenance))))
467 (or provenance (const #f)))))
469 (define (proxy client backend) ;adapted from (guix ssh)
470 "Proxy communication between CLIENT and BACKEND until CLIENT closes the
471 connection, at which point CLIENT is closed (both CLIENT and BACKEND must be
472 input/output ports.)"
473 ;; Use buffered ports so that 'get-bytevector-some' returns up to the
474 ;; whole buffer like read(2) would--see <https://bugs.gnu.org/30066>.
475 (setvbuf client 'block 65536)
476 (setvbuf backend 'block 65536)
479 (match (select (list client backend) '() '())
481 (when (memq client reads)
482 (match (get-bytevector-some client)
486 (put-bytevector backend bv)
487 (force-output backend))))
488 (when (memq backend reads)
489 (match (get-bytevector-some backend)
491 (put-bytevector client bv)
492 (force-output client))))
493 (unless (port-closed? client)
496 (define (inferior-eval-with-store inferior store code)
497 "Evaluate CODE in INFERIOR, passing it STORE as its argument. CODE must
498 thus be the code of a one-argument procedure that accepts a store."
499 ;; Create a named socket in /tmp and let INFERIOR connect to it and use it
500 ;; as its store. This ensures the inferior uses the same store, with the
501 ;; same options, the same per-session GC roots, etc.
502 ;; FIXME: This strategy doesn't work for remote inferiors (SSH).
503 (call-with-temporary-directory
505 (chmod directory #o700)
506 (let* ((name (string-append directory "/inferior"))
507 (socket (socket AF_UNIX SOCK_STREAM 0))
508 (major (store-connection-major-version store))
509 (minor (store-connection-minor-version store))
510 (proto (logior major minor)))
511 (bind socket AF_UNIX name)
513 (send-inferior-request
515 (socket (socket AF_UNIX SOCK_STREAM 0))
516 (error? (if (defined? 'store-protocol-error?)
517 store-protocol-error?
518 nix-protocol-error?))
519 (error-message (if (defined? 'store-protocol-error-message)
520 store-protocol-error-message
521 nix-protocol-error-message)))
522 (connect socket AF_UNIX ,name)
524 ;; 'port->connection' appeared in June 2018 and we can hardly
525 ;; emulate it on older versions. Thus fall back to
526 ;; 'open-connection', at the risk of talking to the wrong daemon or
527 ;; having our build result reclaimed (XXX).
528 (let ((store (if (defined? 'port->connection)
529 (port->connection socket #:version ,proto)
534 ;; Serialize '&store-protocol-error' conditions. The
535 ;; exception serialization mechanism that
536 ;; 'read-repl-response' expects is unsuitable for SRFI-35
537 ;; error conditions, hence this special case.
538 (guard (c ((error? c)
539 `(store-protocol-error ,(error-message c))))
540 `(result ,(proc store))))
542 (close-connection store)
543 (close-port socket)))))
545 (match (accept socket)
547 (proxy client (store-connection-socket store))))
550 (match (read-inferior-response inferior)
551 (('store-protocol-error message)
553 (&store-protocol-error (message message)
558 (define* (inferior-package-derivation store package
560 (system (%current-system))
562 "Return the derivation for PACKAGE, an inferior package, built for SYSTEM
563 and cross-built for TARGET if TARGET is true. The inferior corresponding to
564 PACKAGE must be live."
567 (let* ((package (hashv-ref %package-table
568 ,(inferior-package-id package)))
570 `(package-cross-derivation store package
573 `(package-derivation store package
575 (derivation-file-name drv))))
577 (and=> (inferior-eval-with-store (inferior-package-inferior package) store
579 read-derivation-from-file))
581 (define inferior-package->derivation
582 (store-lift inferior-package-derivation))
584 (define-gexp-compiler (package-compiler (package <inferior-package>) system
586 ;; Compile PACKAGE for SYSTEM, optionally cross-building for TARGET.
587 (inferior-package->derivation package system #:target target))
589 (define* (gexp->derivation-in-inferior name exp guix
590 #:key silent-failure?
593 "Return a derivation that evaluates EXP with GUIX, an instance of Guix as
594 returned for example by 'channel-instances->derivation'. Other arguments are
595 passed as-is to 'gexp->derivation'.
597 When SILENT-FAILURE? is true, create an empty output directory instead of
598 failing when GUIX is too old and lacks the 'guix repl' command."
600 ;; EXP wrapped with a proper (set! %load-path …) prologue.
601 (scheme-file "inferior-script.scm" exp))
604 ;; This is a crude way to run EXP on GUIX. TODO: use 'raw-derivation' and
605 ;; make 'guix repl' the "builder"; this will require "opening up" the
606 ;; mechanisms behind 'gexp->derivation', and adding '-l' to 'guix repl'.
608 (use-modules (ice-9 popen))
610 (let ((pipe (open-pipe* OPEN_WRITE
611 #+(file-append guix "/bin/guix")
612 "repl" "-t" "machine")))
614 ;; XXX: EXP presumably refers to #$output but that reference is lost
615 ;; so explicitly reference it here.
618 (write `(primitive-load #$script) pipe)
620 (unless (zero? (close-pipe pipe))
621 (if #$silent-failure?
623 (error "inferior failed" #+guix))))))
625 (define (drop-extra-keyword lst)
631 ((#:silent-failure? _ . rest)
634 (loop tail (cons* value kw result))))))
636 (apply gexp->derivation name trampoline
637 (drop-extra-keyword rest)))
641 ;;; Manifest entries.
644 (define* (inferior-package->manifest-entry package
645 #:optional (output "out")
646 #:key (properties '()))
647 "Return a manifest entry for the OUTPUT of package PACKAGE."
651 (define-syntax-rule (memoized package output exp)
652 ;; Memoize the entry returned by EXP for PACKAGE/OUTPUT. This is
653 ;; important as the same package may be traversed many times through
654 ;; propagated inputs, and querying the inferior is costly. Use
655 ;; 'hash'/'equal?', which is okay since <inferior-package> is simple.
656 (let ((compute (lambda () exp))
657 (key (cons package output)))
658 (or (hash-ref cache key)
659 (let ((result (compute)))
660 (hash-set! cache key result)
663 (let loop ((package package)
666 (memoized package output
667 ;; For each dependency, keep a promise pointing to its "parent" entry.
668 (letrec* ((deps (map (match-lambda
670 (loop package "out" (delay entry)))
671 ((label package output)
672 (loop package output (delay entry))))
673 (inferior-package-propagated-inputs package)))
674 (entry (manifest-entry
675 (name (inferior-package-name package))
676 (version (inferior-package-version package))
679 (dependencies (delete-duplicates deps))
681 (inferior-package-transitive-native-search-paths package))
683 (properties properties))))
688 ;;; Cached inferiors.
691 (define %inferior-cache-directory
692 ;; Directory for cached inferiors (GC roots).
693 (make-parameter (string-append (cache-directory #:ensure? #f)
696 (define (channel-full-commit channel)
697 "Return the commit designated by CHANNEL as quickly as possible. If
698 CHANNEL's 'commit' field is a full SHA1, return it as-is; if it's a SHA1
699 prefix, resolve it; and if 'commit' is unset, fetch CHANNEL's branch tip."
700 (let ((commit (channel-commit channel))
701 (branch (channel-branch channel)))
702 (if (and commit (= (string-length commit) 40))
704 (let* ((ref (if commit `(commit . ,commit) `(branch . ,branch)))
705 (cache commit relation
706 (update-cached-checkout (channel-url channel)
711 (define* (cached-channel-instance store
715 (cache-directory (%inferior-cache-directory))
716 (ttl (* 3600 24 30)))
717 "Return a directory containing a guix filetree defined by CHANNELS, a list of channels.
718 The directory is a subdirectory of CACHE-DIRECTORY, where entries can be reclaimed after TTL seconds.
719 This procedure opens a new connection to the build daemon. AUTHENTICATE?
720 determines whether CHANNELS are authenticated."
722 ;; Since computing the instances of CHANNELS is I/O-intensive, use a
723 ;; cheaper way to get the commit list of CHANNELS. This limits overhead
724 ;; to the minimum in case of a cache hit.
725 (map channel-full-commit channels))
728 (bytevector->base32-string
730 (string->utf8 (string-concatenate commits)))))
733 (string-append cache-directory "/" key))
735 (define (base32-encoded-sha256? str)
736 (= (string-length str) 52))
738 (define (cache-entries directory)
740 (string-append directory "/" file))
741 (scandir directory base32-encoded-sha256?)))
744 (lift2 symlink %store-monad))
746 (define add-indirect-root*
747 (store-lift add-indirect-root))
749 (mkdir-p cache-directory)
750 (maybe-remove-expired-cache-entries cache-directory
753 (file-expiration-time ttl))
755 (if (file-exists? cached)
757 (run-with-store store
758 (mlet* %store-monad ((instances
759 -> (latest-channel-instances store channels
763 (channel-instances->derivation instances)))
765 (show-what-to-build* (list profile))
766 (built-derivations (list profile))
767 ;; Note: Caching is fine even when AUTHENTICATE? is false because
768 ;; we always call 'latest-channel-instances?'.
769 (unless (file-exists? cached)
770 (symlink* (derivation->output-path profile) cached)
771 (add-indirect-root* cached))
774 (define* (inferior-for-channels channels
776 (cache-directory (%inferior-cache-directory))
777 (ttl (* 3600 24 30)))
778 "Return an inferior for CHANNELS, a list of channels. Use the cache at
779 CACHE-DIRECTORY, where entries can be reclaimed after TTL seconds. This
780 procedure opens a new connection to the build daemon.
782 This is a convenience procedure that people may use in manifests passed to
783 'guix package -m', for instance."
786 (cached-channel-instance store
788 #:cache-directory cache-directory
790 (open-inferior cached))
793 ;;; eval: (put 'memoized 'scheme-indent-function 1)