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 channels->cached-profile
102 instances->cached-profile
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* (inferior-pipe directory command error-port)
125 "Return an input/output pipe on the Guix instance in DIRECTORY. This runs
126 'DIRECTORY/COMMAND repl' if it exists, or falls back to some other method if
128 (let ((pipe (with-error-to-port error-port
130 (open-pipe* OPEN_BOTH
131 (string-append directory "/" command)
132 "repl" "-t" "machine")))))
133 (if (eof-object? (peek-char pipe))
137 ;; Older versions of Guix didn't have a 'guix repl' command, so
139 (with-error-to-port error-port
141 (open-pipe* OPEN_BOTH "guile"
142 "-L" (string-append directory "/share/guile/site/"
144 "-C" (string-append directory "/share/guile/site/"
146 "-C" (string-append directory "/lib/guile/"
147 (effective-version) "/site-ccache")
151 (primitive-load ,(search-path %load-path
153 ((@ (guix repl) machine-repl))))))))
156 (define* (port->inferior pipe #:optional (close close-port))
157 "Given PIPE, an input/output port, return an inferior that talks over PIPE.
158 PIPE is closed with CLOSE when 'close-inferior' is called on the returned
163 (('repl-version 0 rest ...)
164 (letrec ((result (inferior 'pipe pipe close (cons 0 rest)
165 (delay (%inferior-packages result))
166 (delay (%inferior-package-table result)))))
168 ;; For protocol (0 1) and later, send the protocol version we support.
172 (send-inferior-request '(() repl-version 0 1 1) result)))
176 (inferior-eval '(use-modules (guix)) result)
177 (inferior-eval '(use-modules (gnu)) result)
178 (inferior-eval '(use-modules (ice-9 match)) result)
179 (inferior-eval '(use-modules (srfi srfi-34)) result)
180 (inferior-eval '(define %package-table (make-hash-table))
186 (define* (open-inferior directory
187 #:key (command "bin/guix")
188 (error-port (%make-void-port "w")))
189 "Open the inferior Guix in DIRECTORY, running 'DIRECTORY/COMMAND repl' or
190 equivalent. Return #f if the inferior could not be launched."
192 (inferior-pipe directory command error-port))
194 (port->inferior pipe close-pipe))
196 (define (close-inferior inferior)
198 (let ((close (inferior-close-socket inferior)))
199 (close (inferior-socket inferior))))
201 ;; Non-self-quoting object of the inferior.
202 (define-record-type <inferior-object>
203 (inferior-object address appearance)
205 (address inferior-object-address)
206 (appearance inferior-object-appearance))
208 (define (write-inferior-object object port)
210 (($ <inferior-object> _ appearance)
211 (format port "#<inferior-object ~a>" appearance))))
213 (set-record-type-printer! <inferior-object> write-inferior-object)
215 ;; Reified exception thrown by an inferior.
216 (define-condition-type &inferior-exception &error
218 (arguments inferior-exception-arguments) ;key + arguments
219 (inferior inferior-exception-inferior) ;<inferior> | #f
220 (stack inferior-exception-stack)) ;list of (FILE COLUMN LINE)
222 (define* (read-repl-response port #:optional inferior)
223 "Read a (guix repl) response from PORT and return it as a Scheme object.
224 Raise '&inferior-exception' when an exception is read from PORT."
229 (('non-self-quoting address string)
230 (inferior-object address string))))
233 (('values objects ...)
234 (apply values (map sexp->object objects)))
235 (('exception ('arguments key objects ...)
237 ;; Protocol (0 1 1) and later.
238 (raise (condition (&inferior-exception
239 (arguments (cons key (map sexp->object objects)))
242 (('exception key objects ...)
244 (raise (condition (&inferior-exception
245 (arguments (cons key (map sexp->object objects)))
249 (define (read-inferior-response inferior)
250 (read-repl-response (inferior-socket inferior)
253 (define (send-inferior-request exp inferior)
254 (write exp (inferior-socket inferior))
255 (newline (inferior-socket inferior)))
257 (define (inferior-eval exp inferior)
258 "Evaluate EXP in INFERIOR."
259 (send-inferior-request exp inferior)
260 (read-inferior-response inferior))
264 ;;; Inferior packages.
267 (define-record-type <inferior-package>
268 (inferior-package inferior name version id)
270 (inferior inferior-package-inferior)
271 (name inferior-package-name)
272 (version inferior-package-version)
273 (id inferior-package-id))
275 (define (write-inferior-package package port)
277 (($ <inferior-package> _ name version)
278 (format port "#<inferior-package ~a@~a ~a>"
280 (number->string (object-address package) 16)))))
282 (set-record-type-printer! <inferior-package> write-inferior-package)
284 (define (%inferior-packages inferior)
285 "Compute the list of inferior packages from INFERIOR."
286 (let ((result (inferior-eval
287 '(fold-packages (lambda (package result)
288 (let ((id (object-address package)))
289 (hashv-set! %package-table id package)
290 (cons (list (package-name package)
291 (package-version package)
298 (inferior-package inferior name version id)))
301 (define (inferior-packages inferior)
302 "Return the list of packages known to INFERIOR."
303 (force (inferior-package-promise inferior)))
305 (define (%inferior-package-table inferior)
306 "Compute a package lookup table for INFERIOR."
307 (fold (lambda (package table)
308 (vhash-cons (inferior-package-name package) package
311 (inferior-packages inferior)))
313 (define (inferior-available-packages inferior)
314 "Return the list of name/version pairs corresponding to the set of packages
315 available in INFERIOR.
317 This is faster and less resource-intensive than calling 'inferior-packages'."
318 (if (inferior-eval '(defined? 'fold-available-packages)
320 (inferior-eval '(fold-available-packages
321 (lambda* (name version result
322 #:key supported? deprecated?
324 (if (and supported? (not deprecated?))
325 (acons name version result)
330 ;; As a last resort, if INFERIOR is old and lacks
331 ;; 'fold-available-packages', fall back to 'inferior-packages'.
332 (map (lambda (package)
333 (cons (inferior-package-name package)
334 (inferior-package-version package)))
335 (inferior-packages inferior))))
337 (define* (lookup-inferior-packages inferior name #:optional version)
338 "Return the sorted list of inferior packages matching NAME in INFERIOR, with
339 highest version numbers first. If VERSION is true, return only packages with
340 a version number prefixed by VERSION."
341 ;; This is the counterpart of 'find-packages-by-name'.
342 (sort (filter (lambda (package)
344 (version-prefix? version
345 (inferior-package-version package))))
346 (vhash-fold* cons '() name
347 (force (inferior-package-table inferior))))
349 (version>? (inferior-package-version p1)
350 (inferior-package-version p2)))))
352 (define (inferior-package-field package getter)
353 "Return the field of PACKAGE, an inferior package, accessed with GETTER."
354 (let ((inferior (inferior-package-inferior package))
355 (id (inferior-package-id package)))
356 (inferior-eval `(,getter (hashv-ref %package-table ,id))
359 (define* (inferior-package-synopsis package #:key (translate? #t))
360 "Return the Texinfo synopsis of PACKAGE, an inferior package. When
361 TRANSLATE? is true, translate it to the current locale's language."
362 (inferior-package-field package
364 '(compose (@ (guix ui) P_) package-synopsis)
367 (define* (inferior-package-description package #:key (translate? #t))
368 "Return the Texinfo description of PACKAGE, an inferior package. When
369 TRANSLATE? is true, translate it to the current locale's language."
370 (inferior-package-field package
372 '(compose (@ (guix ui) P_) package-description)
373 'package-description)))
375 (define (inferior-package-home-page package)
376 "Return the home page of PACKAGE."
377 (inferior-package-field package 'package-home-page))
379 (define (inferior-package-location package)
380 "Return the source code location of PACKAGE, either #f or a <location>
382 (source-properties->location
383 (inferior-package-field package
384 '(compose (lambda (loc)
386 (location->source-properties
390 (define (inferior-package-input-field package field)
391 "Return the input field FIELD (e.g., 'native-inputs') of PACKAGE, an
394 `(compose (lambda (inputs)
396 ;; XXX: Origins are not handled.
397 ((label (? package? package) rest ...)
398 (let ((id (object-address package)))
399 (hashv-set! %package-table id package)
400 `(,label (package ,id
401 ,(package-name package)
402 ,(package-version package))
410 (inferior-package-field package field*))
413 (inferior-package-inferior package))
416 ((label ('package id name version) . rest)
417 ;; XXX: eq?-ness of inferior packages is not preserved here.
418 `(,label ,(inferior-package inferior name version id)
423 (define inferior-package-inputs
424 (cut inferior-package-input-field <> 'package-inputs))
426 (define inferior-package-native-inputs
427 (cut inferior-package-input-field <> 'package-native-inputs))
429 (define inferior-package-propagated-inputs
430 (cut inferior-package-input-field <> 'package-propagated-inputs))
432 (define inferior-package-transitive-propagated-inputs
433 (cut inferior-package-input-field <> 'package-transitive-propagated-inputs))
435 (define (%inferior-package-search-paths package field)
436 "Return the list of search path specifications of PACKAGE, an inferior
439 (inferior-package-field package
440 `(compose (lambda (paths)
441 (map (@ (guix search-paths)
442 search-path-specification->sexp)
446 (map sexp->search-path-specification paths))
448 (define inferior-package-native-search-paths
449 (cut %inferior-package-search-paths <> 'package-native-search-paths))
451 (define inferior-package-search-paths
452 (cut %inferior-package-search-paths <> 'package-search-paths))
454 (define inferior-package-transitive-native-search-paths
455 (cut %inferior-package-search-paths <> 'package-transitive-native-search-paths))
457 (define (inferior-package-provenance package)
458 "Return a \"provenance sexp\" for PACKAGE, an inferior package. The result
459 is similar to the sexp returned by 'package-provenance' for regular packages."
460 (inferior-package-field package
463 (resolve-interface '(guix describe))))
467 'package-provenance))))
468 (or provenance (const #f)))))
470 (define (proxy client backend) ;adapted from (guix ssh)
471 "Proxy communication between CLIENT and BACKEND until CLIENT closes the
472 connection, at which point CLIENT is closed (both CLIENT and BACKEND must be
473 input/output ports.)"
474 ;; Use buffered ports so that 'get-bytevector-some' returns up to the
475 ;; whole buffer like read(2) would--see <https://bugs.gnu.org/30066>.
476 (setvbuf client 'block 65536)
477 (setvbuf backend 'block 65536)
480 (match (select (list client backend) '() '())
482 (when (memq client reads)
483 (match (get-bytevector-some client)
487 (put-bytevector backend bv)
488 (force-output backend))))
489 (when (memq backend reads)
490 (match (get-bytevector-some backend)
492 (put-bytevector client bv)
493 (force-output client))))
494 (unless (port-closed? client)
497 (define (inferior-eval-with-store inferior store code)
498 "Evaluate CODE in INFERIOR, passing it STORE as its argument. CODE must
499 thus be the code of a one-argument procedure that accepts a store."
500 ;; Create a named socket in /tmp and let INFERIOR connect to it and use it
501 ;; as its store. This ensures the inferior uses the same store, with the
502 ;; same options, the same per-session GC roots, etc.
503 ;; FIXME: This strategy doesn't work for remote inferiors (SSH).
504 (call-with-temporary-directory
506 (chmod directory #o700)
507 (let* ((name (string-append directory "/inferior"))
508 (socket (socket AF_UNIX SOCK_STREAM 0))
509 (major (store-connection-major-version store))
510 (minor (store-connection-minor-version store))
511 (proto (logior major minor)))
512 (bind socket AF_UNIX name)
514 (send-inferior-request
516 (socket (socket AF_UNIX SOCK_STREAM 0))
517 (error? (if (defined? 'store-protocol-error?)
518 store-protocol-error?
519 nix-protocol-error?))
520 (error-message (if (defined? 'store-protocol-error-message)
521 store-protocol-error-message
522 nix-protocol-error-message)))
523 (connect socket AF_UNIX ,name)
525 ;; 'port->connection' appeared in June 2018 and we can hardly
526 ;; emulate it on older versions. Thus fall back to
527 ;; 'open-connection', at the risk of talking to the wrong daemon or
528 ;; having our build result reclaimed (XXX).
529 (let ((store (if (defined? 'port->connection)
530 (port->connection socket #:version ,proto)
535 ;; Serialize '&store-protocol-error' conditions. The
536 ;; exception serialization mechanism that
537 ;; 'read-repl-response' expects is unsuitable for SRFI-35
538 ;; error conditions, hence this special case.
539 (guard (c ((error? c)
540 `(store-protocol-error ,(error-message c))))
541 `(result ,(proc store))))
543 (close-connection store)
544 (close-port socket)))))
546 (match (accept socket)
548 (proxy client (store-connection-socket store))))
551 (match (read-inferior-response inferior)
552 (('store-protocol-error message)
554 (&store-protocol-error (message message)
559 (define* (inferior-package-derivation store package
561 (system (%current-system))
563 "Return the derivation for PACKAGE, an inferior package, built for SYSTEM
564 and cross-built for TARGET if TARGET is true. The inferior corresponding to
565 PACKAGE must be live."
568 (let* ((package (hashv-ref %package-table
569 ,(inferior-package-id package)))
571 `(package-cross-derivation store package
574 `(package-derivation store package
576 (derivation-file-name drv))))
578 (and=> (inferior-eval-with-store (inferior-package-inferior package) store
580 read-derivation-from-file))
582 (define inferior-package->derivation
583 (store-lift inferior-package-derivation))
585 (define-gexp-compiler (package-compiler (package <inferior-package>) system
587 ;; Compile PACKAGE for SYSTEM, optionally cross-building for TARGET.
588 (inferior-package->derivation package system #:target target))
590 (define* (gexp->derivation-in-inferior name exp guix
591 #:key silent-failure?
594 "Return a derivation that evaluates EXP with GUIX, an instance of Guix as
595 returned for example by 'channel-instances->derivation'. Other arguments are
596 passed as-is to 'gexp->derivation'.
598 When SILENT-FAILURE? is true, create an empty output directory instead of
599 failing when GUIX is too old and lacks the 'guix repl' command."
601 ;; EXP wrapped with a proper (set! %load-path …) prologue.
602 (scheme-file "inferior-script.scm" exp))
605 ;; This is a crude way to run EXP on GUIX. TODO: use 'raw-derivation' and
606 ;; make 'guix repl' the "builder"; this will require "opening up" the
607 ;; mechanisms behind 'gexp->derivation', and adding '-l' to 'guix repl'.
609 (use-modules (ice-9 popen))
611 (let ((pipe (open-pipe* OPEN_WRITE
612 #+(file-append guix "/bin/guix")
613 "repl" "-t" "machine")))
615 ;; XXX: EXP presumably refers to #$output but that reference is lost
616 ;; so explicitly reference it here.
619 (write `(primitive-load #$script) pipe)
621 (unless (zero? (close-pipe pipe))
622 (if #$silent-failure?
624 (error "inferior failed" #+guix))))))
626 (define (drop-extra-keyword lst)
632 ((#:silent-failure? _ . rest)
635 (loop tail (cons* value kw result))))))
637 (apply gexp->derivation name trampoline
638 (drop-extra-keyword rest)))
642 ;;; Manifest entries.
645 (define* (inferior-package->manifest-entry package
646 #:optional (output "out")
647 #:key (properties '()))
648 "Return a manifest entry for the OUTPUT of package PACKAGE."
652 (define-syntax-rule (memoized package output exp)
653 ;; Memoize the entry returned by EXP for PACKAGE/OUTPUT. This is
654 ;; important as the same package may be traversed many times through
655 ;; propagated inputs, and querying the inferior is costly. Use
656 ;; 'hash'/'equal?', which is okay since <inferior-package> is simple.
657 (let ((compute (lambda () exp))
658 (key (cons package output)))
659 (or (hash-ref cache key)
660 (let ((result (compute)))
661 (hash-set! cache key result)
664 (let loop ((package package)
667 (memoized package output
668 ;; For each dependency, keep a promise pointing to its "parent" entry.
669 (letrec* ((deps (map (match-lambda
671 (loop package "out" (delay entry)))
672 ((label package output)
673 (loop package output (delay entry))))
674 (inferior-package-propagated-inputs package)))
675 (entry (manifest-entry
676 (name (inferior-package-name package))
677 (version (inferior-package-version package))
680 (dependencies (delete-duplicates deps))
682 (inferior-package-transitive-native-search-paths package))
684 (properties properties))))
689 ;;; Cached inferiors.
692 (define %inferior-cache-directory
693 ;; Directory for cached inferiors (GC roots).
694 (make-parameter (string-append (cache-directory #:ensure? #f)
697 (define (channel-full-commit channel)
698 "Return the commit designated by CHANNEL as quickly as possible. If
699 CHANNEL's 'commit' field is a full SHA1, return it as-is; if it's a SHA1
700 prefix, resolve it; and if 'commit' is unset, fetch CHANNEL's branch tip."
701 (let ((commit (channel-commit channel))
702 (branch (channel-branch channel)))
703 (if (and commit (= (string-length commit) 40))
705 (let* ((ref (if commit `(commit . ,commit) `(branch . ,branch)))
706 (cache commit relation
707 (update-cached-checkout (channel-url channel)
712 (define* (cached-profile store instances
716 "Return a directory containing a guix filetree defined by INSTANCES, a
717 procedure returning a list of channel instances. The directory is a
718 subdirectory of CACHE-DIRECTORY, where entries can be reclaimed after TTL
719 seconds. This procedure opens a new connection to the build daemon."
721 (bytevector->base32-string
723 (string->utf8 (string-concatenate commits)))))
726 (string-append cache-directory "/" key))
728 (define (base32-encoded-sha256? str)
729 (= (string-length str) 52))
731 (define (cache-entries directory)
733 (string-append directory "/" file))
734 (scandir directory base32-encoded-sha256?)))
737 (lift2 symlink %store-monad))
739 (define add-indirect-root*
740 (store-lift add-indirect-root))
742 (mkdir-p cache-directory)
743 (maybe-remove-expired-cache-entries cache-directory
746 (file-expiration-time ttl))
748 (if (file-exists? cached)
750 (run-with-store store
751 (mlet* %store-monad ((profile
752 (channel-instances->derivation (instances))))
754 (show-what-to-build* (list profile))
755 (built-derivations (list profile))
756 ;; Note: Caching is fine even when AUTHENTICATE? is false because
757 ;; we always call 'latest-channel-instances?'.
758 (symlink* (derivation->output-path profile) cached)
759 (add-indirect-root* cached)
762 (define* (channels->cached-profile store channels
766 (%inferior-cache-directory))
767 (ttl (* 3600 24 30)))
768 "Return a cached profile from CHANNELS using the CACHED-PROFILE procedure.
769 AUTHENTICATE? determines whether CHANNELS are authenticated."
771 ;; Since computing the instances of CHANNELS is I/O-intensive, use a
772 ;; cheaper way to get the commit list of CHANNELS. This limits overhead
773 ;; to the minimum in case of a cache hit.
774 (map channel-full-commit channels))
778 (latest-channel-instances store channels
779 #:authenticate? authenticate?)))
781 (cached-profile store instances
782 #:cache-directory cache-directory
786 (define* (instances->cached-profile store instances
789 (%inferior-cache-directory))
790 (ttl (* 3600 24 30)))
791 "Return a cached profile from INSTANCES a list of channel instances using
792 the CACHED-PROFILE procedure."
794 (map channel-instance-commit instances))
796 (cached-profile store (lambda () instances)
797 #:cache-directory cache-directory
801 (define* (inferior-for-channels channels
803 (cache-directory (%inferior-cache-directory))
804 (ttl (* 3600 24 30)))
805 "Return an inferior for CHANNELS, a list of channels. Use the cache at
806 CACHE-DIRECTORY, where entries can be reclaimed after TTL seconds. This
807 procedure opens a new connection to the build daemon.
809 This is a convenience procedure that people may use in manifests passed to
810 'guix package -m', for instance."
813 (channels->cached-profile store
815 #:cache-directory cache-directory
817 (open-inferior cached))
820 ;;; eval: (put 'memoized 'scheme-indent-function 1)