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 (write-inferior inferior port)
125 (($ <inferior> pid _ _ version)
126 (format port "#<inferior ~a ~a ~a>"
128 (number->string (object-address inferior) 16)))))
130 (set-record-type-printer! <inferior> write-inferior)
132 (define* (inferior-pipe directory command error-port)
133 "Return an input/output pipe on the Guix instance in DIRECTORY. This runs
134 'DIRECTORY/COMMAND repl' if it exists, or falls back to some other method if
136 (let ((pipe (with-error-to-port error-port
138 (open-pipe* OPEN_BOTH
139 (string-append directory "/" command)
140 "repl" "-t" "machine")))))
141 (if (eof-object? (peek-char pipe))
145 ;; Older versions of Guix didn't have a 'guix repl' command, so
147 (with-error-to-port error-port
149 (open-pipe* OPEN_BOTH "guile"
150 "-L" (string-append directory "/share/guile/site/"
152 "-C" (string-append directory "/share/guile/site/"
154 "-C" (string-append directory "/lib/guile/"
155 (effective-version) "/site-ccache")
159 (primitive-load ,(search-path %load-path
161 ((@ (guix repl) machine-repl))))))))
164 (define* (port->inferior pipe #:optional (close close-port))
165 "Given PIPE, an input/output port, return an inferior that talks over PIPE.
166 PIPE is closed with CLOSE when 'close-inferior' is called on the returned
171 (('repl-version 0 rest ...)
172 (letrec ((result (inferior 'pipe pipe close (cons 0 rest)
173 (delay (%inferior-packages result))
174 (delay (%inferior-package-table result)))))
176 ;; For protocol (0 1) and later, send the protocol version we support.
180 (send-inferior-request '(() repl-version 0 1 1) result)))
184 (inferior-eval '(use-modules (guix)) result)
185 (inferior-eval '(use-modules (gnu)) result)
186 (inferior-eval '(use-modules (ice-9 match)) result)
187 (inferior-eval '(use-modules (srfi srfi-34)) result)
188 (inferior-eval '(define %package-table (make-hash-table))
194 (define* (open-inferior directory
195 #:key (command "bin/guix")
196 (error-port (%make-void-port "w")))
197 "Open the inferior Guix in DIRECTORY, running 'DIRECTORY/COMMAND repl' or
198 equivalent. Return #f if the inferior could not be launched."
200 (inferior-pipe directory command error-port))
202 (port->inferior pipe close-pipe))
204 (define (close-inferior inferior)
206 (let ((close (inferior-close-socket inferior)))
207 (close (inferior-socket inferior))))
209 ;; Non-self-quoting object of the inferior.
210 (define-record-type <inferior-object>
211 (inferior-object address appearance)
213 (address inferior-object-address)
214 (appearance inferior-object-appearance))
216 (define (write-inferior-object object port)
218 (($ <inferior-object> _ appearance)
219 (format port "#<inferior-object ~a>" appearance))))
221 (set-record-type-printer! <inferior-object> write-inferior-object)
223 ;; Reified exception thrown by an inferior.
224 (define-condition-type &inferior-exception &error
226 (arguments inferior-exception-arguments) ;key + arguments
227 (inferior inferior-exception-inferior) ;<inferior> | #f
228 (stack inferior-exception-stack)) ;list of (FILE COLUMN LINE)
230 (define* (read-repl-response port #:optional inferior)
231 "Read a (guix repl) response from PORT and return it as a Scheme object.
232 Raise '&inferior-exception' when an exception is read from PORT."
237 (('non-self-quoting address string)
238 (inferior-object address string))))
241 (('values objects ...)
242 (apply values (map sexp->object objects)))
243 (('exception ('arguments key objects ...)
245 ;; Protocol (0 1 1) and later.
246 (raise (condition (&inferior-exception
247 (arguments (cons key (map sexp->object objects)))
250 (('exception key objects ...)
252 (raise (condition (&inferior-exception
253 (arguments (cons key (map sexp->object objects)))
257 (define (read-inferior-response inferior)
258 (read-repl-response (inferior-socket inferior)
261 (define (send-inferior-request exp inferior)
262 (write exp (inferior-socket inferior))
263 (newline (inferior-socket inferior)))
265 (define (inferior-eval exp inferior)
266 "Evaluate EXP in INFERIOR."
267 (send-inferior-request exp inferior)
268 (read-inferior-response inferior))
272 ;;; Inferior packages.
275 (define-record-type <inferior-package>
276 (inferior-package inferior name version id)
278 (inferior inferior-package-inferior)
279 (name inferior-package-name)
280 (version inferior-package-version)
281 (id inferior-package-id))
283 (define (write-inferior-package package port)
285 (($ <inferior-package> _ name version)
286 (format port "#<inferior-package ~a@~a ~a>"
288 (number->string (object-address package) 16)))))
290 (set-record-type-printer! <inferior-package> write-inferior-package)
292 (define (%inferior-packages inferior)
293 "Compute the list of inferior packages from INFERIOR."
294 (let ((result (inferior-eval
295 '(fold-packages (lambda (package result)
296 (let ((id (object-address package)))
297 (hashv-set! %package-table id package)
298 (cons (list (package-name package)
299 (package-version package)
306 (inferior-package inferior name version id)))
309 (define (inferior-packages inferior)
310 "Return the list of packages known to INFERIOR."
311 (force (inferior-package-promise inferior)))
313 (define (%inferior-package-table inferior)
314 "Compute a package lookup table for INFERIOR."
315 (fold (lambda (package table)
316 (vhash-cons (inferior-package-name package) package
319 (inferior-packages inferior)))
321 (define (inferior-available-packages inferior)
322 "Return the list of name/version pairs corresponding to the set of packages
323 available in INFERIOR.
325 This is faster and less resource-intensive than calling 'inferior-packages'."
326 (if (inferior-eval '(defined? 'fold-available-packages)
328 (inferior-eval '(fold-available-packages
329 (lambda* (name version result
330 #:key supported? deprecated?
332 (if (and supported? (not deprecated?))
333 (acons name version result)
338 ;; As a last resort, if INFERIOR is old and lacks
339 ;; 'fold-available-packages', fall back to 'inferior-packages'.
340 (map (lambda (package)
341 (cons (inferior-package-name package)
342 (inferior-package-version package)))
343 (inferior-packages inferior))))
345 (define* (lookup-inferior-packages inferior name #:optional version)
346 "Return the sorted list of inferior packages matching NAME in INFERIOR, with
347 highest version numbers first. If VERSION is true, return only packages with
348 a version number prefixed by VERSION."
349 ;; This is the counterpart of 'find-packages-by-name'.
350 (sort (filter (lambda (package)
352 (version-prefix? version
353 (inferior-package-version package))))
354 (vhash-fold* cons '() name
355 (force (inferior-package-table inferior))))
357 (version>? (inferior-package-version p1)
358 (inferior-package-version p2)))))
360 (define (inferior-package-field package getter)
361 "Return the field of PACKAGE, an inferior package, accessed with GETTER."
362 (let ((inferior (inferior-package-inferior package))
363 (id (inferior-package-id package)))
364 (inferior-eval `(,getter (hashv-ref %package-table ,id))
367 (define* (inferior-package-synopsis package #:key (translate? #t))
368 "Return the Texinfo synopsis 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-synopsis)
375 (define* (inferior-package-description package #:key (translate? #t))
376 "Return the Texinfo description of PACKAGE, an inferior package. When
377 TRANSLATE? is true, translate it to the current locale's language."
378 (inferior-package-field package
380 '(compose (@ (guix ui) P_) package-description)
381 'package-description)))
383 (define (inferior-package-home-page package)
384 "Return the home page of PACKAGE."
385 (inferior-package-field package 'package-home-page))
387 (define (inferior-package-location package)
388 "Return the source code location of PACKAGE, either #f or a <location>
390 (source-properties->location
391 (inferior-package-field package
392 '(compose (lambda (loc)
394 (location->source-properties
398 (define (inferior-package-input-field package field)
399 "Return the input field FIELD (e.g., 'native-inputs') of PACKAGE, an
402 `(compose (lambda (inputs)
404 ;; XXX: Origins are not handled.
405 ((label (? package? package) rest ...)
406 (let ((id (object-address package)))
407 (hashv-set! %package-table id package)
408 `(,label (package ,id
409 ,(package-name package)
410 ,(package-version package))
418 (inferior-package-field package field*))
421 (inferior-package-inferior package))
424 ((label ('package id name version) . rest)
425 ;; XXX: eq?-ness of inferior packages is not preserved here.
426 `(,label ,(inferior-package inferior name version id)
431 (define inferior-package-inputs
432 (cut inferior-package-input-field <> 'package-inputs))
434 (define inferior-package-native-inputs
435 (cut inferior-package-input-field <> 'package-native-inputs))
437 (define inferior-package-propagated-inputs
438 (cut inferior-package-input-field <> 'package-propagated-inputs))
440 (define inferior-package-transitive-propagated-inputs
441 (cut inferior-package-input-field <> 'package-transitive-propagated-inputs))
443 (define (%inferior-package-search-paths package field)
444 "Return the list of search path specifications of PACKAGE, an inferior
447 (inferior-package-field package
448 `(compose (lambda (paths)
449 (map (@ (guix search-paths)
450 search-path-specification->sexp)
454 (map sexp->search-path-specification paths))
456 (define inferior-package-native-search-paths
457 (cut %inferior-package-search-paths <> 'package-native-search-paths))
459 (define inferior-package-search-paths
460 (cut %inferior-package-search-paths <> 'package-search-paths))
462 (define inferior-package-transitive-native-search-paths
463 (cut %inferior-package-search-paths <> 'package-transitive-native-search-paths))
465 (define (inferior-package-provenance package)
466 "Return a \"provenance sexp\" for PACKAGE, an inferior package. The result
467 is similar to the sexp returned by 'package-provenance' for regular packages."
468 (inferior-package-field package
471 (resolve-interface '(guix describe))))
475 'package-provenance))))
476 (or provenance (const #f)))))
478 (define (proxy client backend) ;adapted from (guix ssh)
479 "Proxy communication between CLIENT and BACKEND until CLIENT closes the
480 connection, at which point CLIENT is closed (both CLIENT and BACKEND must be
481 input/output ports.)"
482 ;; Use buffered ports so that 'get-bytevector-some' returns up to the
483 ;; whole buffer like read(2) would--see <https://bugs.gnu.org/30066>.
484 (setvbuf client 'block 65536)
485 (setvbuf backend 'block 65536)
488 (match (select (list client backend) '() '())
490 (when (memq client reads)
491 (match (get-bytevector-some client)
495 (put-bytevector backend bv)
496 (force-output backend))))
497 (when (memq backend reads)
498 (match (get-bytevector-some backend)
500 (put-bytevector client bv)
501 (force-output client))))
502 (unless (port-closed? client)
505 (define (inferior-eval-with-store inferior store code)
506 "Evaluate CODE in INFERIOR, passing it STORE as its argument. CODE must
507 thus be the code of a one-argument procedure that accepts a store."
508 ;; Create a named socket in /tmp and let INFERIOR connect to it and use it
509 ;; as its store. This ensures the inferior uses the same store, with the
510 ;; same options, the same per-session GC roots, etc.
511 ;; FIXME: This strategy doesn't work for remote inferiors (SSH).
512 (call-with-temporary-directory
514 (chmod directory #o700)
515 (let* ((name (string-append directory "/inferior"))
516 (socket (socket AF_UNIX SOCK_STREAM 0))
517 (major (store-connection-major-version store))
518 (minor (store-connection-minor-version store))
519 (proto (logior major minor)))
520 (bind socket AF_UNIX name)
522 (send-inferior-request
524 (socket (socket AF_UNIX SOCK_STREAM 0))
525 (error? (if (defined? 'store-protocol-error?)
526 store-protocol-error?
527 nix-protocol-error?))
528 (error-message (if (defined? 'store-protocol-error-message)
529 store-protocol-error-message
530 nix-protocol-error-message)))
531 (connect socket AF_UNIX ,name)
533 ;; 'port->connection' appeared in June 2018 and we can hardly
534 ;; emulate it on older versions. Thus fall back to
535 ;; 'open-connection', at the risk of talking to the wrong daemon or
536 ;; having our build result reclaimed (XXX).
537 (let ((store (if (defined? 'port->connection)
538 (port->connection socket #:version ,proto)
543 ;; Serialize '&store-protocol-error' conditions. The
544 ;; exception serialization mechanism that
545 ;; 'read-repl-response' expects is unsuitable for SRFI-35
546 ;; error conditions, hence this special case.
547 (guard (c ((error? c)
548 `(store-protocol-error ,(error-message c))))
549 `(result ,(proc store))))
551 (close-connection store)
552 (close-port socket)))))
554 (match (accept socket)
556 (proxy client (store-connection-socket store))))
559 (match (read-inferior-response inferior)
560 (('store-protocol-error message)
562 (&store-protocol-error (message message)
567 (define* (inferior-package-derivation store package
569 (system (%current-system))
571 "Return the derivation for PACKAGE, an inferior package, built for SYSTEM
572 and cross-built for TARGET if TARGET is true. The inferior corresponding to
573 PACKAGE must be live."
576 (let* ((package (hashv-ref %package-table
577 ,(inferior-package-id package)))
579 `(package-cross-derivation store package
582 `(package-derivation store package
584 (derivation-file-name drv))))
586 (and=> (inferior-eval-with-store (inferior-package-inferior package) store
588 read-derivation-from-file))
590 (define inferior-package->derivation
591 (store-lift inferior-package-derivation))
593 (define-gexp-compiler (package-compiler (package <inferior-package>) system
595 ;; Compile PACKAGE for SYSTEM, optionally cross-building for TARGET.
596 (inferior-package->derivation package system #:target target))
598 (define* (gexp->derivation-in-inferior name exp guix
599 #:key silent-failure?
602 "Return a derivation that evaluates EXP with GUIX, an instance of Guix as
603 returned for example by 'channel-instances->derivation'. Other arguments are
604 passed as-is to 'gexp->derivation'.
606 When SILENT-FAILURE? is true, create an empty output directory instead of
607 failing when GUIX is too old and lacks the 'guix repl' command."
609 ;; EXP wrapped with a proper (set! %load-path …) prologue.
610 (scheme-file "inferior-script.scm" exp))
613 ;; This is a crude way to run EXP on GUIX. TODO: use 'raw-derivation' and
614 ;; make 'guix repl' the "builder"; this will require "opening up" the
615 ;; mechanisms behind 'gexp->derivation', and adding '-l' to 'guix repl'.
617 (use-modules (ice-9 popen))
619 (let ((pipe (open-pipe* OPEN_WRITE
620 #+(file-append guix "/bin/guix")
621 "repl" "-t" "machine")))
623 ;; XXX: EXP presumably refers to #$output but that reference is lost
624 ;; so explicitly reference it here.
627 (write `(primitive-load #$script) pipe)
629 (unless (zero? (close-pipe pipe))
630 (if #$silent-failure?
632 (error "inferior failed" #+guix))))))
634 (define (drop-extra-keyword lst)
640 ((#:silent-failure? _ . rest)
643 (loop tail (cons* value kw result))))))
645 (apply gexp->derivation name trampoline
646 (drop-extra-keyword rest)))
650 ;;; Manifest entries.
653 (define* (inferior-package->manifest-entry package
654 #:optional (output "out")
655 #:key (properties '()))
656 "Return a manifest entry for the OUTPUT of package PACKAGE."
660 (define-syntax-rule (memoized package output exp)
661 ;; Memoize the entry returned by EXP for PACKAGE/OUTPUT. This is
662 ;; important as the same package may be traversed many times through
663 ;; propagated inputs, and querying the inferior is costly. Use
664 ;; 'hash'/'equal?', which is okay since <inferior-package> is simple.
665 (let ((compute (lambda () exp))
666 (key (cons package output)))
667 (or (hash-ref cache key)
668 (let ((result (compute)))
669 (hash-set! cache key result)
672 (let loop ((package package)
675 (memoized package output
676 ;; For each dependency, keep a promise pointing to its "parent" entry.
677 (letrec* ((deps (map (match-lambda
679 (loop package "out" (delay entry)))
680 ((label package output)
681 (loop package output (delay entry))))
682 (inferior-package-propagated-inputs package)))
683 (entry (manifest-entry
684 (name (inferior-package-name package))
685 (version (inferior-package-version package))
688 (dependencies (delete-duplicates deps))
690 (inferior-package-transitive-native-search-paths package))
692 (properties properties))))
697 ;;; Cached inferiors.
700 (define %inferior-cache-directory
701 ;; Directory for cached inferiors (GC roots).
702 (make-parameter (string-append (cache-directory #:ensure? #f)
705 (define (channel-full-commit channel)
706 "Return the commit designated by CHANNEL as quickly as possible. If
707 CHANNEL's 'commit' field is a full SHA1, return it as-is; if it's a SHA1
708 prefix, resolve it; and if 'commit' is unset, fetch CHANNEL's branch tip."
709 (let ((commit (channel-commit channel))
710 (branch (channel-branch channel)))
711 (if (and commit (= (string-length commit) 40))
713 (let* ((ref (if commit `(commit . ,commit) `(branch . ,branch)))
714 (cache commit relation
715 (update-cached-checkout (channel-url channel)
720 (define* (cached-channel-instance store
724 (cache-directory (%inferior-cache-directory))
725 (ttl (* 3600 24 30)))
726 "Return a directory containing a guix filetree defined by CHANNELS, a list of channels.
727 The directory is a subdirectory of CACHE-DIRECTORY, where entries can be reclaimed after TTL seconds.
728 This procedure opens a new connection to the build daemon. AUTHENTICATE?
729 determines whether CHANNELS are authenticated."
731 ;; Since computing the instances of CHANNELS is I/O-intensive, use a
732 ;; cheaper way to get the commit list of CHANNELS. This limits overhead
733 ;; to the minimum in case of a cache hit.
734 (map channel-full-commit channels))
737 (bytevector->base32-string
739 (string->utf8 (string-concatenate commits)))))
742 (string-append cache-directory "/" key))
744 (define (base32-encoded-sha256? str)
745 (= (string-length str) 52))
747 (define (cache-entries directory)
749 (string-append directory "/" file))
750 (scandir directory base32-encoded-sha256?)))
752 (define (symlink/safe old new)
757 (unless (= EEXIST (system-error-errno args))
758 (apply throw args)))))
761 (lift2 symlink/safe %store-monad))
763 (define add-indirect-root*
764 (store-lift add-indirect-root))
766 (mkdir-p cache-directory)
767 (maybe-remove-expired-cache-entries cache-directory
770 (file-expiration-time ttl))
772 (if (file-exists? cached)
774 (run-with-store store
775 (mlet* %store-monad ((instances
776 -> (latest-channel-instances store channels
780 (channel-instances->derivation instances)))
782 (show-what-to-build* (list profile))
783 (built-derivations (list profile))
784 ;; Note: Caching is fine even when AUTHENTICATE? is false because
785 ;; we always call 'latest-channel-instances?'.
786 (symlink* (derivation->output-path profile) cached)
787 (add-indirect-root* cached)
790 (define* (inferior-for-channels channels
792 (cache-directory (%inferior-cache-directory))
793 (ttl (* 3600 24 30)))
794 "Return an inferior for CHANNELS, a list of channels. Use the cache at
795 CACHE-DIRECTORY, where entries can be reclaimed after TTL seconds. This
796 procedure opens a new connection to the build daemon.
798 This is a convenience procedure that people may use in manifests passed to
799 'guix package -m', for instance."
802 (cached-channel-instance store
804 #:cache-directory cache-directory
806 (open-inferior cached))
809 ;;; eval: (put 'memoized 'scheme-indent-function 1)