1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2019 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)
48 #:autoload (guix ui) (show-what-to-build*)
49 #:autoload (guix build utils) (mkdir-p)
50 #:use-module (srfi srfi-1)
51 #:use-module (srfi srfi-26)
52 #:autoload (ice-9 ftw) (scandir)
53 #:use-module (ice-9 match)
54 #:use-module (ice-9 popen)
55 #:use-module (ice-9 vlist)
56 #:use-module (ice-9 binary-ports)
57 #:use-module ((rnrs bytevectors) #:select (string->utf8))
63 inferior-eval-with-store
68 inferior-available-packages
69 lookup-inferior-packages
73 inferior-package-version
74 inferior-package-synopsis
75 inferior-package-description
76 inferior-package-home-page
77 inferior-package-location
78 inferior-package-inputs
79 inferior-package-native-inputs
80 inferior-package-propagated-inputs
81 inferior-package-transitive-propagated-inputs
82 inferior-package-native-search-paths
83 inferior-package-transitive-native-search-paths
84 inferior-package-search-paths
85 inferior-package-derivation
87 inferior-package->manifest-entry
89 gexp->derivation-in-inferior
91 %inferior-cache-directory
92 cached-channel-instance
93 inferior-for-channels))
97 ;;; This module provides a way to spawn Guix "inferior" processes and to talk
98 ;;; to them. It allows us, from one instance of Guix, to interact with
99 ;;; another instance of Guix coming from a different commit.
103 ;; Inferior Guix process.
104 (define-record-type <inferior>
105 (inferior pid socket close version packages table)
108 (socket inferior-socket)
109 (close inferior-close-socket) ;procedure
110 (version inferior-version) ;REPL protocol version
111 (packages inferior-package-promise) ;promise of inferior packages
112 (table inferior-package-table)) ;promise of vhash
114 (define* (inferior-pipe directory command error-port)
115 "Return an input/output pipe on the Guix instance in DIRECTORY. This runs
116 'DIRECTORY/COMMAND repl' if it exists, or falls back to some other method if
118 (let ((pipe (with-error-to-port error-port
120 (open-pipe* OPEN_BOTH
121 (string-append directory "/" command)
122 "repl" "-t" "machine")))))
123 (if (eof-object? (peek-char pipe))
127 ;; Older versions of Guix didn't have a 'guix repl' command, so
129 (with-error-to-port error-port
131 (open-pipe* OPEN_BOTH "guile"
132 "-L" (string-append directory "/share/guile/site/"
134 "-C" (string-append directory "/share/guile/site/"
136 "-C" (string-append directory "/lib/guile/"
137 (effective-version) "/site-ccache")
141 (primitive-load ,(search-path %load-path
143 ((@ (guix repl) machine-repl))))))))
146 (define* (port->inferior pipe #:optional (close close-port))
147 "Given PIPE, an input/output port, return an inferior that talks over PIPE.
148 PIPE is closed with CLOSE when 'close-inferior' is called on the returned
153 (('repl-version 0 rest ...)
154 (letrec ((result (inferior 'pipe pipe close (cons 0 rest)
155 (delay (%inferior-packages result))
156 (delay (%inferior-package-table result)))))
157 (inferior-eval '(use-modules (guix)) result)
158 (inferior-eval '(use-modules (gnu)) result)
159 (inferior-eval '(use-modules (ice-9 match)) result)
160 (inferior-eval '(use-modules (srfi srfi-34)) result)
161 (inferior-eval '(define %package-table (make-hash-table))
167 (define* (open-inferior directory
168 #:key (command "bin/guix")
169 (error-port (%make-void-port "w")))
170 "Open the inferior Guix in DIRECTORY, running 'DIRECTORY/COMMAND repl' or
171 equivalent. Return #f if the inferior could not be launched."
173 (inferior-pipe directory command error-port))
175 (port->inferior pipe close-pipe))
177 (define (close-inferior inferior)
179 (let ((close (inferior-close-socket inferior)))
180 (close (inferior-socket inferior))))
182 ;; Non-self-quoting object of the inferior.
183 (define-record-type <inferior-object>
184 (inferior-object address appearance)
186 (address inferior-object-address)
187 (appearance inferior-object-appearance))
189 (define (write-inferior-object object port)
191 (($ <inferior-object> _ appearance)
192 (format port "#<inferior-object ~a>" appearance))))
194 (set-record-type-printer! <inferior-object> write-inferior-object)
196 (define (read-repl-response port)
197 "Read a (guix repl) response from PORT and return it as a Scheme object."
202 (('non-self-quoting address string)
203 (inferior-object address string))))
206 (('values objects ...)
207 (apply values (map sexp->object objects)))
208 (('exception key objects ...)
209 (apply throw key (map sexp->object objects)))))
211 (define (read-inferior-response inferior)
212 (read-repl-response (inferior-socket inferior)))
214 (define (send-inferior-request exp inferior)
215 (write exp (inferior-socket inferior))
216 (newline (inferior-socket inferior)))
218 (define (inferior-eval exp inferior)
219 "Evaluate EXP in INFERIOR."
220 (send-inferior-request exp inferior)
221 (read-inferior-response inferior))
225 ;;; Inferior packages.
228 (define-record-type <inferior-package>
229 (inferior-package inferior name version id)
231 (inferior inferior-package-inferior)
232 (name inferior-package-name)
233 (version inferior-package-version)
234 (id inferior-package-id))
236 (define (write-inferior-package package port)
238 (($ <inferior-package> _ name version)
239 (format port "#<inferior-package ~a@~a ~a>"
241 (number->string (object-address package) 16)))))
243 (set-record-type-printer! <inferior-package> write-inferior-package)
245 (define (%inferior-packages inferior)
246 "Compute the list of inferior packages from INFERIOR."
247 (let ((result (inferior-eval
248 '(fold-packages (lambda (package result)
249 (let ((id (object-address package)))
250 (hashv-set! %package-table id package)
251 (cons (list (package-name package)
252 (package-version package)
259 (inferior-package inferior name version id)))
262 (define (inferior-packages inferior)
263 "Return the list of packages known to INFERIOR."
264 (force (inferior-package-promise inferior)))
266 (define (%inferior-package-table inferior)
267 "Compute a package lookup table for INFERIOR."
268 (fold (lambda (package table)
269 (vhash-cons (inferior-package-name package) package
272 (inferior-packages inferior)))
274 (define (inferior-available-packages inferior)
275 "Return the list of name/version pairs corresponding to the set of packages
276 available in INFERIOR.
278 This is faster and requires less resource-intensive than calling
279 'inferior-packages'."
280 (if (inferior-eval '(defined? 'fold-available-packages)
282 (inferior-eval '(fold-available-packages
283 (lambda* (name version result
284 #:key supported? deprecated?
286 (if (and supported? (not deprecated?))
287 (acons name version result)
292 ;; As a last resort, if INFERIOR is old and lacks
293 ;; 'fold-available-packages', fall back to 'inferior-packages'.
294 (map (lambda (package)
295 (cons (inferior-package-name package)
296 (inferior-package-version package)))
297 (inferior-packages inferior))))
299 (define* (lookup-inferior-packages inferior name #:optional version)
300 "Return the sorted list of inferior packages matching NAME in INFERIOR, with
301 highest version numbers first. If VERSION is true, return only packages with
302 a version number prefixed by VERSION."
303 ;; This is the counterpart of 'find-packages-by-name'.
304 (sort (filter (lambda (package)
306 (version-prefix? version
307 (inferior-package-version package))))
308 (vhash-fold* cons '() name
309 (force (inferior-package-table inferior))))
311 (version>? (inferior-package-version p1)
312 (inferior-package-version p2)))))
314 (define (inferior-package-field package getter)
315 "Return the field of PACKAGE, an inferior package, accessed with GETTER."
316 (let ((inferior (inferior-package-inferior package))
317 (id (inferior-package-id package)))
318 (inferior-eval `(,getter (hashv-ref %package-table ,id))
321 (define* (inferior-package-synopsis package #:key (translate? #t))
322 "Return the Texinfo synopsis of PACKAGE, an inferior package. When
323 TRANSLATE? is true, translate it to the current locale's language."
324 (inferior-package-field package
326 '(compose (@ (guix ui) P_) package-synopsis)
329 (define* (inferior-package-description package #:key (translate? #t))
330 "Return the Texinfo description of PACKAGE, an inferior package. When
331 TRANSLATE? is true, translate it to the current locale's language."
332 (inferior-package-field package
334 '(compose (@ (guix ui) P_) package-description)
335 'package-description)))
337 (define (inferior-package-home-page package)
338 "Return the home page of PACKAGE."
339 (inferior-package-field package 'package-home-page))
341 (define (inferior-package-location package)
342 "Return the source code location of PACKAGE, either #f or a <location>
344 (source-properties->location
345 (inferior-package-field package
346 '(compose (lambda (loc)
348 (location->source-properties
352 (define (inferior-package-input-field package field)
353 "Return the input field FIELD (e.g., 'native-inputs') of PACKAGE, an
356 `(compose (lambda (inputs)
358 ;; XXX: Origins are not handled.
359 ((label (? package? package) rest ...)
360 (let ((id (object-address package)))
361 (hashv-set! %package-table id package)
362 `(,label (package ,id
363 ,(package-name package)
364 ,(package-version package))
372 (inferior-package-field package field*))
375 (inferior-package-inferior package))
378 ((label ('package id name version) . rest)
379 ;; XXX: eq?-ness of inferior packages is not preserved here.
380 `(,label ,(inferior-package inferior name version id)
385 (define inferior-package-inputs
386 (cut inferior-package-input-field <> 'package-inputs))
388 (define inferior-package-native-inputs
389 (cut inferior-package-input-field <> 'package-native-inputs))
391 (define inferior-package-propagated-inputs
392 (cut inferior-package-input-field <> 'package-propagated-inputs))
394 (define inferior-package-transitive-propagated-inputs
395 (cut inferior-package-input-field <> 'package-transitive-propagated-inputs))
397 (define (%inferior-package-search-paths package field)
398 "Return the list of search path specifications of PACKAGE, an inferior
401 (inferior-package-field package
402 `(compose (lambda (paths)
403 (map (@ (guix search-paths)
404 search-path-specification->sexp)
408 (map sexp->search-path-specification paths))
410 (define inferior-package-native-search-paths
411 (cut %inferior-package-search-paths <> 'package-native-search-paths))
413 (define inferior-package-search-paths
414 (cut %inferior-package-search-paths <> 'package-search-paths))
416 (define inferior-package-transitive-native-search-paths
417 (cut %inferior-package-search-paths <> 'package-transitive-native-search-paths))
419 (define (proxy client backend) ;adapted from (guix ssh)
420 "Proxy communication between CLIENT and BACKEND until CLIENT closes the
421 connection, at which point CLIENT is closed (both CLIENT and BACKEND must be
422 input/output ports.)"
423 (define (select* read write except)
424 ;; This is a workaround for <https://bugs.gnu.org/30365> in Guile < 2.2.4:
425 ;; since 'select' sometimes returns non-empty sets for no good reason,
426 ;; call 'select' a second time with a zero timeout to filter out incorrect
428 (match (select read write except)
430 (select read write except 0))))
432 ;; Use buffered ports so that 'get-bytevector-some' returns up to the
433 ;; whole buffer like read(2) would--see <https://bugs.gnu.org/30066>.
434 (setvbuf client 'block 65536)
435 (setvbuf backend 'block 65536)
438 (match (select* (list client backend) '() '())
440 (when (memq client reads)
441 (match (get-bytevector-some client)
445 (put-bytevector backend bv)
446 (force-output backend))))
447 (when (memq backend reads)
448 (match (get-bytevector-some backend)
450 (put-bytevector client bv)
451 (force-output client))))
452 (unless (port-closed? client)
455 (define (inferior-eval-with-store inferior store code)
456 "Evaluate CODE in INFERIOR, passing it STORE as its argument. CODE must
457 thus be the code of a one-argument procedure that accepts a store."
458 ;; Create a named socket in /tmp and let INFERIOR connect to it and use it
459 ;; as its store. This ensures the inferior uses the same store, with the
460 ;; same options, the same per-session GC roots, etc.
461 ;; FIXME: This strategy doesn't work for remote inferiors (SSH).
462 (call-with-temporary-directory
464 (chmod directory #o700)
465 (let* ((name (string-append directory "/inferior"))
466 (socket (socket AF_UNIX SOCK_STREAM 0))
467 (major (store-connection-major-version store))
468 (minor (store-connection-minor-version store))
469 (proto (logior major minor)))
470 (bind socket AF_UNIX name)
472 (send-inferior-request
474 (socket (socket AF_UNIX SOCK_STREAM 0))
475 (error? (if (defined? 'store-protocol-error?)
476 store-protocol-error?
477 nix-protocol-error?))
478 (error-message (if (defined? 'store-protocol-error-message)
479 store-protocol-error-message
480 nix-protocol-error-message)))
481 (connect socket AF_UNIX ,name)
483 ;; 'port->connection' appeared in June 2018 and we can hardly
484 ;; emulate it on older versions. Thus fall back to
485 ;; 'open-connection', at the risk of talking to the wrong daemon or
486 ;; having our build result reclaimed (XXX).
487 (let ((store (if (defined? 'port->connection)
488 (port->connection socket #:version ,proto)
493 ;; Serialize '&store-protocol-error' conditions. The
494 ;; exception serialization mechanism that
495 ;; 'read-repl-response' expects is unsuitable for SRFI-35
496 ;; error conditions, hence this special case.
497 (guard (c ((error? c)
498 `(store-protocol-error ,(error-message c))))
499 `(result ,(proc store))))
501 (close-connection store)
502 (close-port socket)))))
504 (match (accept socket)
506 (proxy client (store-connection-socket store))))
509 (match (read-inferior-response inferior)
510 (('store-protocol-error message)
512 (&store-protocol-error (message message)
517 (define* (inferior-package-derivation store package
519 (system (%current-system))
521 "Return the derivation for PACKAGE, an inferior package, built for SYSTEM
522 and cross-built for TARGET if TARGET is true. The inferior corresponding to
523 PACKAGE must be live."
526 (let* ((package (hashv-ref %package-table
527 ,(inferior-package-id package)))
529 `(package-cross-derivation store package
532 `(package-derivation store package
534 (derivation-file-name drv))))
536 (and=> (inferior-eval-with-store (inferior-package-inferior package) store
538 read-derivation-from-file))
540 (define inferior-package->derivation
541 (store-lift inferior-package-derivation))
543 (define-gexp-compiler (package-compiler (package <inferior-package>) system
545 ;; Compile PACKAGE for SYSTEM, optionally cross-building for TARGET.
546 (inferior-package->derivation package system #:target target))
548 (define* (gexp->derivation-in-inferior name exp guix
549 #:key silent-failure?
552 "Return a derivation that evaluates EXP with GUIX, an instance of Guix as
553 returned for example by 'channel-instances->derivation'. Other arguments are
554 passed as-is to 'gexp->derivation'.
556 When SILENT-FAILURE? is true, create an empty output directory instead of
557 failing when GUIX is too old and lacks the 'guix repl' command."
559 ;; EXP wrapped with a proper (set! %load-path …) prologue.
560 (scheme-file "inferior-script.scm" exp))
563 ;; This is a crude way to run EXP on GUIX. TODO: use 'raw-derivation' and
564 ;; make 'guix repl' the "builder"; this will require "opening up" the
565 ;; mechanisms behind 'gexp->derivation', and adding '-l' to 'guix repl'.
567 (use-modules (ice-9 popen))
569 (let ((pipe (open-pipe* OPEN_WRITE
570 #+(file-append guix "/bin/guix")
571 "repl" "-t" "machine")))
573 ;; XXX: EXP presumably refers to #$output but that reference is lost
574 ;; so explicitly reference it here.
577 (write `(primitive-load #$script) pipe)
579 (unless (zero? (close-pipe pipe))
580 (if #$silent-failure?
582 (error "inferior failed" #+guix))))))
584 (define (drop-extra-keyword lst)
590 ((#:silent-failure? _ . rest)
593 (loop tail (cons* value kw result))))))
595 (apply gexp->derivation name trampoline
596 (drop-extra-keyword rest)))
600 ;;; Manifest entries.
603 (define* (inferior-package->manifest-entry package
604 #:optional (output "out")
605 #:key (parent (delay #f))
607 "Return a manifest entry for the OUTPUT of package PACKAGE."
608 ;; For each dependency, keep a promise pointing to its "parent" entry.
609 (letrec* ((deps (map (match-lambda
611 (inferior-package->manifest-entry package
612 #:parent (delay entry)))
613 ((label package output)
614 (inferior-package->manifest-entry package output
615 #:parent (delay entry))))
616 (inferior-package-propagated-inputs package)))
617 (entry (manifest-entry
618 (name (inferior-package-name package))
619 (version (inferior-package-version package))
622 (dependencies (delete-duplicates deps))
624 (inferior-package-transitive-native-search-paths package))
626 (properties properties))))
631 ;;; Cached inferiors.
634 (define %inferior-cache-directory
635 ;; Directory for cached inferiors (GC roots).
636 (make-parameter (string-append (cache-directory #:ensure? #f)
639 (define* (cached-channel-instance store
642 (cache-directory (%inferior-cache-directory))
643 (ttl (* 3600 24 30)))
644 "Return a directory containing a guix filetree defined by CHANNELS, a list of channels.
645 The directory is a subdirectory of CACHE-DIRECTORY, where entries can be reclaimed after TTL seconds.
646 This procedure opens a new connection to the build daemon."
648 (latest-channel-instances store channels))
651 (bytevector->base32-string
654 (string-concatenate (map channel-instance-commit instances))))))
657 (string-append cache-directory "/" key))
659 (define (base32-encoded-sha256? str)
660 (= (string-length str) 52))
662 (define (cache-entries directory)
664 (string-append directory "/" file))
665 (scandir directory base32-encoded-sha256?)))
668 (lift2 symlink %store-monad))
670 (define add-indirect-root*
671 (store-lift add-indirect-root))
673 (mkdir-p cache-directory)
674 (maybe-remove-expired-cache-entries cache-directory
677 (file-expiration-time ttl))
679 (if (file-exists? cached)
681 (run-with-store store
682 (mlet %store-monad ((profile
683 (channel-instances->derivation instances)))
685 (show-what-to-build* (list profile))
686 (built-derivations (list profile))
687 (symlink* (derivation->output-path profile) cached)
688 (add-indirect-root* cached)
691 (define* (inferior-for-channels channels
693 (cache-directory (%inferior-cache-directory))
694 (ttl (* 3600 24 30)))
695 "Return an inferior for CHANNELS, a list of channels. Use the cache at
696 CACHE-DIRECTORY, where entries can be reclaimed after TTL seconds. This
697 procedure opens a new connection to the build daemon.
699 This is a convenience procedure that people may use in manifests passed to
700 'guix package -m', for instance."
703 (cached-channel-instance store
705 #:cache-directory cache-directory
707 (open-inferior cached))