1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2019, 2020 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 utils)
25 #:select (%current-system
26 source-properties->location
27 call-with-temporary-directory
28 version>? version-prefix?
30 #:use-module ((guix store)
31 #:select (store-connection-socket
32 store-connection-major-version
33 store-connection-minor-version
35 &store-protocol-error))
36 #:use-module ((guix derivations)
37 #:select (read-derivation-from-file))
38 #:use-module (guix gexp)
39 #:use-module (guix search-paths)
40 #:use-module (guix profiles)
41 #:use-module (guix channels)
42 #:use-module (guix monads)
43 #:use-module (guix store)
44 #:use-module (guix derivations)
45 #:use-module (guix base32)
46 #:use-module (gcrypt hash)
47 #:autoload (guix cache) (maybe-remove-expired-cache-entries
49 #:autoload (guix ui) (show-what-to-build*)
50 #:autoload (guix build utils) (mkdir-p)
51 #:use-module (srfi srfi-1)
52 #:use-module (srfi srfi-26)
53 #:autoload (ice-9 ftw) (scandir)
54 #:use-module (ice-9 match)
55 #:use-module (ice-9 popen)
56 #:use-module (ice-9 vlist)
57 #:use-module (ice-9 binary-ports)
58 #:use-module ((rnrs bytevectors) #:select (string->utf8))
64 inferior-eval-with-store
67 inferior-exception-arguments
68 inferior-exception-inferior
69 inferior-exception-stack
73 inferior-available-packages
74 lookup-inferior-packages
78 inferior-package-version
79 inferior-package-synopsis
80 inferior-package-description
81 inferior-package-home-page
82 inferior-package-location
83 inferior-package-inputs
84 inferior-package-native-inputs
85 inferior-package-propagated-inputs
86 inferior-package-transitive-propagated-inputs
87 inferior-package-native-search-paths
88 inferior-package-transitive-native-search-paths
89 inferior-package-search-paths
90 inferior-package-provenance
91 inferior-package-derivation
93 inferior-package->manifest-entry
95 gexp->derivation-in-inferior
97 %inferior-cache-directory
98 cached-channel-instance
99 inferior-for-channels))
103 ;;; This module provides a way to spawn Guix "inferior" processes and to talk
104 ;;; to them. It allows us, from one instance of Guix, to interact with
105 ;;; another instance of Guix coming from a different commit.
109 ;; Inferior Guix process.
110 (define-record-type <inferior>
111 (inferior pid socket close version packages table)
114 (socket inferior-socket)
115 (close inferior-close-socket) ;procedure
116 (version inferior-version) ;REPL protocol version
117 (packages inferior-package-promise) ;promise of inferior packages
118 (table inferior-package-table)) ;promise of vhash
120 (define* (inferior-pipe directory command error-port)
121 "Return an input/output pipe on the Guix instance in DIRECTORY. This runs
122 'DIRECTORY/COMMAND repl' if it exists, or falls back to some other method if
124 (let ((pipe (with-error-to-port error-port
126 (open-pipe* OPEN_BOTH
127 (string-append directory "/" command)
128 "repl" "-t" "machine")))))
129 (if (eof-object? (peek-char pipe))
133 ;; Older versions of Guix didn't have a 'guix repl' command, so
135 (with-error-to-port error-port
137 (open-pipe* OPEN_BOTH "guile"
138 "-L" (string-append directory "/share/guile/site/"
140 "-C" (string-append directory "/share/guile/site/"
142 "-C" (string-append directory "/lib/guile/"
143 (effective-version) "/site-ccache")
147 (primitive-load ,(search-path %load-path
149 ((@ (guix repl) machine-repl))))))))
152 (define* (port->inferior pipe #:optional (close close-port))
153 "Given PIPE, an input/output port, return an inferior that talks over PIPE.
154 PIPE is closed with CLOSE when 'close-inferior' is called on the returned
159 (('repl-version 0 rest ...)
160 (letrec ((result (inferior 'pipe pipe close (cons 0 rest)
161 (delay (%inferior-packages result))
162 (delay (%inferior-package-table result)))))
164 ;; For protocol (0 1) and later, send the protocol version we support.
168 (send-inferior-request '(() repl-version 0 1 1) result)))
172 (inferior-eval '(use-modules (guix)) result)
173 (inferior-eval '(use-modules (gnu)) result)
174 (inferior-eval '(use-modules (ice-9 match)) result)
175 (inferior-eval '(use-modules (srfi srfi-34)) result)
176 (inferior-eval '(define %package-table (make-hash-table))
182 (define* (open-inferior directory
183 #:key (command "bin/guix")
184 (error-port (%make-void-port "w")))
185 "Open the inferior Guix in DIRECTORY, running 'DIRECTORY/COMMAND repl' or
186 equivalent. Return #f if the inferior could not be launched."
188 (inferior-pipe directory command error-port))
190 (port->inferior pipe close-pipe))
192 (define (close-inferior inferior)
194 (let ((close (inferior-close-socket inferior)))
195 (close (inferior-socket inferior))))
197 ;; Non-self-quoting object of the inferior.
198 (define-record-type <inferior-object>
199 (inferior-object address appearance)
201 (address inferior-object-address)
202 (appearance inferior-object-appearance))
204 (define (write-inferior-object object port)
206 (($ <inferior-object> _ appearance)
207 (format port "#<inferior-object ~a>" appearance))))
209 (set-record-type-printer! <inferior-object> write-inferior-object)
211 ;; Reified exception thrown by an inferior.
212 (define-condition-type &inferior-exception &error
214 (arguments inferior-exception-arguments) ;key + arguments
215 (inferior inferior-exception-inferior) ;<inferior> | #f
216 (stack inferior-exception-stack)) ;list of (FILE COLUMN LINE)
218 (define* (read-repl-response port #:optional inferior)
219 "Read a (guix repl) response from PORT and return it as a Scheme object.
220 Raise '&inferior-exception' when an exception is read from PORT."
225 (('non-self-quoting address string)
226 (inferior-object address string))))
229 (('values objects ...)
230 (apply values (map sexp->object objects)))
231 (('exception ('arguments key objects ...)
233 ;; Protocol (0 1 1) and later.
234 (raise (condition (&inferior-exception
235 (arguments (cons key (map sexp->object objects)))
238 (('exception key objects ...)
240 (raise (condition (&inferior-exception
241 (arguments (cons key (map sexp->object objects)))
245 (define (read-inferior-response inferior)
246 (read-repl-response (inferior-socket inferior)
249 (define (send-inferior-request exp inferior)
250 (write exp (inferior-socket inferior))
251 (newline (inferior-socket inferior)))
253 (define (inferior-eval exp inferior)
254 "Evaluate EXP in INFERIOR."
255 (send-inferior-request exp inferior)
256 (read-inferior-response inferior))
260 ;;; Inferior packages.
263 (define-record-type <inferior-package>
264 (inferior-package inferior name version id)
266 (inferior inferior-package-inferior)
267 (name inferior-package-name)
268 (version inferior-package-version)
269 (id inferior-package-id))
271 (define (write-inferior-package package port)
273 (($ <inferior-package> _ name version)
274 (format port "#<inferior-package ~a@~a ~a>"
276 (number->string (object-address package) 16)))))
278 (set-record-type-printer! <inferior-package> write-inferior-package)
280 (define (%inferior-packages inferior)
281 "Compute the list of inferior packages from INFERIOR."
282 (let ((result (inferior-eval
283 '(fold-packages (lambda (package result)
284 (let ((id (object-address package)))
285 (hashv-set! %package-table id package)
286 (cons (list (package-name package)
287 (package-version package)
294 (inferior-package inferior name version id)))
297 (define (inferior-packages inferior)
298 "Return the list of packages known to INFERIOR."
299 (force (inferior-package-promise inferior)))
301 (define (%inferior-package-table inferior)
302 "Compute a package lookup table for INFERIOR."
303 (fold (lambda (package table)
304 (vhash-cons (inferior-package-name package) package
307 (inferior-packages inferior)))
309 (define (inferior-available-packages inferior)
310 "Return the list of name/version pairs corresponding to the set of packages
311 available in INFERIOR.
313 This is faster and requires less resource-intensive than calling
314 'inferior-packages'."
315 (if (inferior-eval '(defined? 'fold-available-packages)
317 (inferior-eval '(fold-available-packages
318 (lambda* (name version result
319 #:key supported? deprecated?
321 (if (and supported? (not deprecated?))
322 (acons name version result)
327 ;; As a last resort, if INFERIOR is old and lacks
328 ;; 'fold-available-packages', fall back to 'inferior-packages'.
329 (map (lambda (package)
330 (cons (inferior-package-name package)
331 (inferior-package-version package)))
332 (inferior-packages inferior))))
334 (define* (lookup-inferior-packages inferior name #:optional version)
335 "Return the sorted list of inferior packages matching NAME in INFERIOR, with
336 highest version numbers first. If VERSION is true, return only packages with
337 a version number prefixed by VERSION."
338 ;; This is the counterpart of 'find-packages-by-name'.
339 (sort (filter (lambda (package)
341 (version-prefix? version
342 (inferior-package-version package))))
343 (vhash-fold* cons '() name
344 (force (inferior-package-table inferior))))
346 (version>? (inferior-package-version p1)
347 (inferior-package-version p2)))))
349 (define (inferior-package-field package getter)
350 "Return the field of PACKAGE, an inferior package, accessed with GETTER."
351 (let ((inferior (inferior-package-inferior package))
352 (id (inferior-package-id package)))
353 (inferior-eval `(,getter (hashv-ref %package-table ,id))
356 (define* (inferior-package-synopsis package #:key (translate? #t))
357 "Return the Texinfo synopsis of PACKAGE, an inferior package. When
358 TRANSLATE? is true, translate it to the current locale's language."
359 (inferior-package-field package
361 '(compose (@ (guix ui) P_) package-synopsis)
364 (define* (inferior-package-description package #:key (translate? #t))
365 "Return the Texinfo description of PACKAGE, an inferior package. When
366 TRANSLATE? is true, translate it to the current locale's language."
367 (inferior-package-field package
369 '(compose (@ (guix ui) P_) package-description)
370 'package-description)))
372 (define (inferior-package-home-page package)
373 "Return the home page of PACKAGE."
374 (inferior-package-field package 'package-home-page))
376 (define (inferior-package-location package)
377 "Return the source code location of PACKAGE, either #f or a <location>
379 (source-properties->location
380 (inferior-package-field package
381 '(compose (lambda (loc)
383 (location->source-properties
387 (define (inferior-package-input-field package field)
388 "Return the input field FIELD (e.g., 'native-inputs') of PACKAGE, an
391 `(compose (lambda (inputs)
393 ;; XXX: Origins are not handled.
394 ((label (? package? package) rest ...)
395 (let ((id (object-address package)))
396 (hashv-set! %package-table id package)
397 `(,label (package ,id
398 ,(package-name package)
399 ,(package-version package))
407 (inferior-package-field package field*))
410 (inferior-package-inferior package))
413 ((label ('package id name version) . rest)
414 ;; XXX: eq?-ness of inferior packages is not preserved here.
415 `(,label ,(inferior-package inferior name version id)
420 (define inferior-package-inputs
421 (cut inferior-package-input-field <> 'package-inputs))
423 (define inferior-package-native-inputs
424 (cut inferior-package-input-field <> 'package-native-inputs))
426 (define inferior-package-propagated-inputs
427 (cut inferior-package-input-field <> 'package-propagated-inputs))
429 (define inferior-package-transitive-propagated-inputs
430 (cut inferior-package-input-field <> 'package-transitive-propagated-inputs))
432 (define (%inferior-package-search-paths package field)
433 "Return the list of search path specifications of PACKAGE, an inferior
436 (inferior-package-field package
437 `(compose (lambda (paths)
438 (map (@ (guix search-paths)
439 search-path-specification->sexp)
443 (map sexp->search-path-specification paths))
445 (define inferior-package-native-search-paths
446 (cut %inferior-package-search-paths <> 'package-native-search-paths))
448 (define inferior-package-search-paths
449 (cut %inferior-package-search-paths <> 'package-search-paths))
451 (define inferior-package-transitive-native-search-paths
452 (cut %inferior-package-search-paths <> 'package-transitive-native-search-paths))
454 (define (inferior-package-provenance package)
455 "Return a \"provenance sexp\" for PACKAGE, an inferior package. The result
456 is similar to the sexp returned by 'package-provenance' for regular packages."
457 (inferior-package-field package
460 (resolve-interface '(guix describe))))
464 'package-provenance))))
465 (or provenance (const #f)))))
467 (define (proxy client backend) ;adapted from (guix ssh)
468 "Proxy communication between CLIENT and BACKEND until CLIENT closes the
469 connection, at which point CLIENT is closed (both CLIENT and BACKEND must be
470 input/output ports.)"
471 (define (select* read write except)
472 ;; This is a workaround for <https://bugs.gnu.org/30365> in Guile < 2.2.4:
473 ;; since 'select' sometimes returns non-empty sets for no good reason,
474 ;; call 'select' a second time with a zero timeout to filter out incorrect
476 (match (select read write except)
478 (select read write except 0))))
480 ;; Use buffered ports so that 'get-bytevector-some' returns up to the
481 ;; whole buffer like read(2) would--see <https://bugs.gnu.org/30066>.
482 (setvbuf client 'block 65536)
483 (setvbuf backend 'block 65536)
486 (match (select* (list client backend) '() '())
488 (when (memq client reads)
489 (match (get-bytevector-some client)
493 (put-bytevector backend bv)
494 (force-output backend))))
495 (when (memq backend reads)
496 (match (get-bytevector-some backend)
498 (put-bytevector client bv)
499 (force-output client))))
500 (unless (port-closed? client)
503 (define (inferior-eval-with-store inferior store code)
504 "Evaluate CODE in INFERIOR, passing it STORE as its argument. CODE must
505 thus be the code of a one-argument procedure that accepts a store."
506 ;; Create a named socket in /tmp and let INFERIOR connect to it and use it
507 ;; as its store. This ensures the inferior uses the same store, with the
508 ;; same options, the same per-session GC roots, etc.
509 ;; FIXME: This strategy doesn't work for remote inferiors (SSH).
510 (call-with-temporary-directory
512 (chmod directory #o700)
513 (let* ((name (string-append directory "/inferior"))
514 (socket (socket AF_UNIX SOCK_STREAM 0))
515 (major (store-connection-major-version store))
516 (minor (store-connection-minor-version store))
517 (proto (logior major minor)))
518 (bind socket AF_UNIX name)
520 (send-inferior-request
522 (socket (socket AF_UNIX SOCK_STREAM 0))
523 (error? (if (defined? 'store-protocol-error?)
524 store-protocol-error?
525 nix-protocol-error?))
526 (error-message (if (defined? 'store-protocol-error-message)
527 store-protocol-error-message
528 nix-protocol-error-message)))
529 (connect socket AF_UNIX ,name)
531 ;; 'port->connection' appeared in June 2018 and we can hardly
532 ;; emulate it on older versions. Thus fall back to
533 ;; 'open-connection', at the risk of talking to the wrong daemon or
534 ;; having our build result reclaimed (XXX).
535 (let ((store (if (defined? 'port->connection)
536 (port->connection socket #:version ,proto)
541 ;; Serialize '&store-protocol-error' conditions. The
542 ;; exception serialization mechanism that
543 ;; 'read-repl-response' expects is unsuitable for SRFI-35
544 ;; error conditions, hence this special case.
545 (guard (c ((error? c)
546 `(store-protocol-error ,(error-message c))))
547 `(result ,(proc store))))
549 (close-connection store)
550 (close-port socket)))))
552 (match (accept socket)
554 (proxy client (store-connection-socket store))))
557 (match (read-inferior-response inferior)
558 (('store-protocol-error message)
560 (&store-protocol-error (message message)
565 (define* (inferior-package-derivation store package
567 (system (%current-system))
569 "Return the derivation for PACKAGE, an inferior package, built for SYSTEM
570 and cross-built for TARGET if TARGET is true. The inferior corresponding to
571 PACKAGE must be live."
574 (let* ((package (hashv-ref %package-table
575 ,(inferior-package-id package)))
577 `(package-cross-derivation store package
580 `(package-derivation store package
582 (derivation-file-name drv))))
584 (and=> (inferior-eval-with-store (inferior-package-inferior package) store
586 read-derivation-from-file))
588 (define inferior-package->derivation
589 (store-lift inferior-package-derivation))
591 (define-gexp-compiler (package-compiler (package <inferior-package>) system
593 ;; Compile PACKAGE for SYSTEM, optionally cross-building for TARGET.
594 (inferior-package->derivation package system #:target target))
596 (define* (gexp->derivation-in-inferior name exp guix
597 #:key silent-failure?
600 "Return a derivation that evaluates EXP with GUIX, an instance of Guix as
601 returned for example by 'channel-instances->derivation'. Other arguments are
602 passed as-is to 'gexp->derivation'.
604 When SILENT-FAILURE? is true, create an empty output directory instead of
605 failing when GUIX is too old and lacks the 'guix repl' command."
607 ;; EXP wrapped with a proper (set! %load-path …) prologue.
608 (scheme-file "inferior-script.scm" exp))
611 ;; This is a crude way to run EXP on GUIX. TODO: use 'raw-derivation' and
612 ;; make 'guix repl' the "builder"; this will require "opening up" the
613 ;; mechanisms behind 'gexp->derivation', and adding '-l' to 'guix repl'.
615 (use-modules (ice-9 popen))
617 (let ((pipe (open-pipe* OPEN_WRITE
618 #+(file-append guix "/bin/guix")
619 "repl" "-t" "machine")))
621 ;; XXX: EXP presumably refers to #$output but that reference is lost
622 ;; so explicitly reference it here.
625 (write `(primitive-load #$script) pipe)
627 (unless (zero? (close-pipe pipe))
628 (if #$silent-failure?
630 (error "inferior failed" #+guix))))))
632 (define (drop-extra-keyword lst)
638 ((#:silent-failure? _ . rest)
641 (loop tail (cons* value kw result))))))
643 (apply gexp->derivation name trampoline
644 (drop-extra-keyword rest)))
648 ;;; Manifest entries.
651 (define* (inferior-package->manifest-entry package
652 #:optional (output "out")
653 #:key (parent (delay #f))
655 "Return a manifest entry for the OUTPUT of package PACKAGE."
656 ;; For each dependency, keep a promise pointing to its "parent" entry.
657 (letrec* ((deps (map (match-lambda
659 (inferior-package->manifest-entry package
660 #:parent (delay entry)))
661 ((label package output)
662 (inferior-package->manifest-entry package output
663 #:parent (delay entry))))
664 (inferior-package-propagated-inputs package)))
665 (entry (manifest-entry
666 (name (inferior-package-name package))
667 (version (inferior-package-version package))
670 (dependencies (delete-duplicates deps))
672 (inferior-package-transitive-native-search-paths package))
674 (properties properties))))
679 ;;; Cached inferiors.
682 (define %inferior-cache-directory
683 ;; Directory for cached inferiors (GC roots).
684 (make-parameter (string-append (cache-directory #:ensure? #f)
687 (define* (cached-channel-instance store
691 (cache-directory (%inferior-cache-directory))
692 (ttl (* 3600 24 30)))
693 "Return a directory containing a guix filetree defined by CHANNELS, a list of channels.
694 The directory is a subdirectory of CACHE-DIRECTORY, where entries can be reclaimed after TTL seconds.
695 This procedure opens a new connection to the build daemon. AUTHENTICATE?
696 determines whether CHANNELS are authenticated."
698 (latest-channel-instances store channels
699 #:authenticate? authenticate?))
702 (bytevector->base32-string
705 (string-concatenate (map channel-instance-commit instances))))))
708 (string-append cache-directory "/" key))
710 (define (base32-encoded-sha256? str)
711 (= (string-length str) 52))
713 (define (cache-entries directory)
715 (string-append directory "/" file))
716 (scandir directory base32-encoded-sha256?)))
719 (lift2 symlink %store-monad))
721 (define add-indirect-root*
722 (store-lift add-indirect-root))
724 (mkdir-p cache-directory)
725 (maybe-remove-expired-cache-entries cache-directory
728 (file-expiration-time ttl))
730 (if (file-exists? cached)
732 (run-with-store store
733 (mlet %store-monad ((profile
734 (channel-instances->derivation instances)))
736 (show-what-to-build* (list profile))
737 (built-derivations (list profile))
738 ;; Note: Caching is fine even when AUTHENTICATE? is false because
739 ;; we always call 'latest-channel-instances?'.
740 (symlink* (derivation->output-path profile) cached)
741 (add-indirect-root* cached)
744 (define* (inferior-for-channels channels
746 (cache-directory (%inferior-cache-directory))
747 (ttl (* 3600 24 30)))
748 "Return an inferior for CHANNELS, a list of channels. Use the cache at
749 CACHE-DIRECTORY, where entries can be reclaimed after TTL seconds. This
750 procedure opens a new connection to the build daemon.
752 This is a convenience procedure that people may use in manifests passed to
753 'guix package -m', for instance."
756 (cached-channel-instance store
758 #:cache-directory cache-directory
760 (open-inferior cached))