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 ((guix utils)
23 #:select (%current-system
24 source-properties->location
25 call-with-temporary-directory
26 version>? version-prefix?
28 #:use-module ((guix store)
29 #:select (store-connection-socket
30 store-connection-major-version
31 store-connection-minor-version
33 #:use-module ((guix derivations)
34 #:select (read-derivation-from-file))
35 #:use-module (guix gexp)
36 #:use-module (guix search-paths)
37 #:use-module (guix profiles)
38 #:use-module (guix channels)
39 #:use-module (guix monads)
40 #:use-module (guix store)
41 #:use-module (guix derivations)
42 #:use-module (guix base32)
43 #:use-module (gcrypt hash)
44 #:autoload (guix cache) (maybe-remove-expired-cache-entries)
45 #:autoload (guix ui) (show-what-to-build*)
46 #:autoload (guix build utils) (mkdir-p)
47 #:use-module (srfi srfi-1)
48 #:use-module (srfi srfi-26)
49 #:autoload (ice-9 ftw) (scandir)
50 #:use-module (ice-9 match)
51 #:use-module (ice-9 popen)
52 #:use-module (ice-9 vlist)
53 #:use-module (ice-9 binary-ports)
54 #:use-module ((rnrs bytevectors) #:select (string->utf8))
60 inferior-eval-with-store
64 inferior-available-packages
65 lookup-inferior-packages
69 inferior-package-version
70 inferior-package-synopsis
71 inferior-package-description
72 inferior-package-home-page
73 inferior-package-location
74 inferior-package-inputs
75 inferior-package-native-inputs
76 inferior-package-propagated-inputs
77 inferior-package-transitive-propagated-inputs
78 inferior-package-native-search-paths
79 inferior-package-transitive-native-search-paths
80 inferior-package-search-paths
81 inferior-package-derivation
83 inferior-package->manifest-entry
85 gexp->derivation-in-inferior
87 %inferior-cache-directory
88 inferior-for-channels))
92 ;;; This module provides a way to spawn Guix "inferior" processes and to talk
93 ;;; to them. It allows us, from one instance of Guix, to interact with
94 ;;; another instance of Guix coming from a different commit.
98 ;; Inferior Guix process.
99 (define-record-type <inferior>
100 (inferior pid socket close version packages table)
103 (socket inferior-socket)
104 (close inferior-close-socket) ;procedure
105 (version inferior-version) ;REPL protocol version
106 (packages inferior-package-promise) ;promise of inferior packages
107 (table inferior-package-table)) ;promise of vhash
109 (define (inferior-pipe directory command)
110 "Return an input/output pipe on the Guix instance in DIRECTORY. This runs
111 'DIRECTORY/COMMAND repl' if it exists, or falls back to some other method if
113 (let ((pipe (with-error-to-port (%make-void-port "w")
115 (open-pipe* OPEN_BOTH
116 (string-append directory "/" command)
117 "repl" "-t" "machine")))))
118 (if (eof-object? (peek-char pipe))
122 ;; Older versions of Guix didn't have a 'guix repl' command, so
124 (open-pipe* OPEN_BOTH "guile"
125 "-L" (string-append directory "/share/guile/site/"
127 "-C" (string-append directory "/share/guile/site/"
129 "-C" (string-append directory "/lib/guile/"
130 (effective-version) "/site-ccache")
134 (primitive-load ,(search-path %load-path
135 "guix/scripts/repl.scm"))
136 ((@ (guix scripts repl) machine-repl))))))
139 (define* (port->inferior pipe #:optional (close close-port))
140 "Given PIPE, an input/output port, return an inferior that talks over PIPE.
141 PIPE is closed with CLOSE when 'close-inferior' is called on the returned
146 (('repl-version 0 rest ...)
147 (letrec ((result (inferior 'pipe pipe close (cons 0 rest)
148 (delay (%inferior-packages result))
149 (delay (%inferior-package-table result)))))
150 (inferior-eval '(use-modules (guix)) result)
151 (inferior-eval '(use-modules (gnu)) result)
152 (inferior-eval '(use-modules (ice-9 match)) result)
153 (inferior-eval '(define %package-table (make-hash-table))
159 (define* (open-inferior directory #:key (command "bin/guix"))
160 "Open the inferior Guix in DIRECTORY, running 'DIRECTORY/COMMAND repl' or
161 equivalent. Return #f if the inferior could not be launched."
163 (inferior-pipe directory command))
165 (port->inferior pipe close-pipe))
167 (define (close-inferior inferior)
169 (let ((close (inferior-close-socket inferior)))
170 (close (inferior-socket inferior))))
172 ;; Non-self-quoting object of the inferior.
173 (define-record-type <inferior-object>
174 (inferior-object address appearance)
176 (address inferior-object-address)
177 (appearance inferior-object-appearance))
179 (define (write-inferior-object object port)
181 (($ <inferior-object> _ appearance)
182 (format port "#<inferior-object ~a>" appearance))))
184 (set-record-type-printer! <inferior-object> write-inferior-object)
186 (define (read-inferior-response inferior)
191 (('non-self-quoting address string)
192 (inferior-object address string))))
194 (match (read (inferior-socket inferior))
195 (('values objects ...)
196 (apply values (map sexp->object objects)))
197 (('exception key objects ...)
198 (apply throw key (map sexp->object objects)))))
200 (define (send-inferior-request exp inferior)
201 (write exp (inferior-socket inferior))
202 (newline (inferior-socket inferior)))
204 (define (inferior-eval exp inferior)
205 "Evaluate EXP in INFERIOR."
206 (send-inferior-request exp inferior)
207 (read-inferior-response inferior))
211 ;;; Inferior packages.
214 (define-record-type <inferior-package>
215 (inferior-package inferior name version id)
217 (inferior inferior-package-inferior)
218 (name inferior-package-name)
219 (version inferior-package-version)
220 (id inferior-package-id))
222 (define (write-inferior-package package port)
224 (($ <inferior-package> _ name version)
225 (format port "#<inferior-package ~a@~a ~a>"
227 (number->string (object-address package) 16)))))
229 (set-record-type-printer! <inferior-package> write-inferior-package)
231 (define (%inferior-packages inferior)
232 "Compute the list of inferior packages from INFERIOR."
233 (let ((result (inferior-eval
234 '(fold-packages (lambda (package result)
235 (let ((id (object-address package)))
236 (hashv-set! %package-table id package)
237 (cons (list (package-name package)
238 (package-version package)
245 (inferior-package inferior name version id)))
248 (define (inferior-packages inferior)
249 "Return the list of packages known to INFERIOR."
250 (force (inferior-package-promise inferior)))
252 (define (%inferior-package-table inferior)
253 "Compute a package lookup table for INFERIOR."
254 (fold (lambda (package table)
255 (vhash-cons (inferior-package-name package) package
258 (inferior-packages inferior)))
260 (define (inferior-available-packages inferior)
261 "Return the list of name/version pairs corresponding to the set of packages
262 available in INFERIOR.
264 This is faster and requires less resource-intensive than calling
265 'inferior-packages'."
266 (if (inferior-eval '(defined? 'fold-available-packages)
268 (inferior-eval '(fold-available-packages
269 (lambda* (name version result
270 #:key supported? deprecated?
272 (if (and supported? (not deprecated?))
273 (acons name version result)
278 ;; As a last resort, if INFERIOR is old and lacks
279 ;; 'fold-available-packages', fall back to 'inferior-packages'.
280 (map (lambda (package)
281 (cons (inferior-package-name package)
282 (inferior-package-version package)))
283 (inferior-packages inferior))))
285 (define* (lookup-inferior-packages inferior name #:optional version)
286 "Return the sorted list of inferior packages matching NAME in INFERIOR, with
287 highest version numbers first. If VERSION is true, return only packages with
288 a version number prefixed by VERSION."
289 ;; This is the counterpart of 'find-packages-by-name'.
290 (sort (filter (lambda (package)
292 (version-prefix? version
293 (inferior-package-version package))))
294 (vhash-fold* cons '() name
295 (force (inferior-package-table inferior))))
297 (version>? (inferior-package-version p1)
298 (inferior-package-version p2)))))
300 (define (inferior-package-field package getter)
301 "Return the field of PACKAGE, an inferior package, accessed with GETTER."
302 (let ((inferior (inferior-package-inferior package))
303 (id (inferior-package-id package)))
304 (inferior-eval `(,getter (hashv-ref %package-table ,id))
307 (define* (inferior-package-synopsis package #:key (translate? #t))
308 "Return the Texinfo synopsis of PACKAGE, an inferior package. When
309 TRANSLATE? is true, translate it to the current locale's language."
310 (inferior-package-field package
312 '(compose (@ (guix ui) P_) package-synopsis)
315 (define* (inferior-package-description package #:key (translate? #t))
316 "Return the Texinfo description of PACKAGE, an inferior package. When
317 TRANSLATE? is true, translate it to the current locale's language."
318 (inferior-package-field package
320 '(compose (@ (guix ui) P_) package-description)
321 'package-description)))
323 (define (inferior-package-home-page package)
324 "Return the home page of PACKAGE."
325 (inferior-package-field package 'package-home-page))
327 (define (inferior-package-location package)
328 "Return the source code location of PACKAGE, either #f or a <location>
330 (source-properties->location
331 (inferior-package-field package
332 '(compose (lambda (loc)
334 (location->source-properties
338 (define (inferior-package-input-field package field)
339 "Return the input field FIELD (e.g., 'native-inputs') of PACKAGE, an
342 `(compose (lambda (inputs)
344 ;; XXX: Origins are not handled.
345 ((label (? package? package) rest ...)
346 (let ((id (object-address package)))
347 (hashv-set! %package-table id package)
348 `(,label (package ,id
349 ,(package-name package)
350 ,(package-version package))
358 (inferior-package-field package field*))
361 (inferior-package-inferior package))
364 ((label ('package id name version) . rest)
365 ;; XXX: eq?-ness of inferior packages is not preserved here.
366 `(,label ,(inferior-package inferior name version id)
371 (define inferior-package-inputs
372 (cut inferior-package-input-field <> 'package-inputs))
374 (define inferior-package-native-inputs
375 (cut inferior-package-input-field <> 'package-native-inputs))
377 (define inferior-package-propagated-inputs
378 (cut inferior-package-input-field <> 'package-propagated-inputs))
380 (define inferior-package-transitive-propagated-inputs
381 (cut inferior-package-input-field <> 'package-transitive-propagated-inputs))
383 (define (%inferior-package-search-paths package field)
384 "Return the list of search path specificiations of PACKAGE, an inferior
387 (inferior-package-field package
388 `(compose (lambda (paths)
389 (map (@ (guix search-paths)
390 search-path-specification->sexp)
394 (map sexp->search-path-specification paths))
396 (define inferior-package-native-search-paths
397 (cut %inferior-package-search-paths <> 'package-native-search-paths))
399 (define inferior-package-search-paths
400 (cut %inferior-package-search-paths <> 'package-search-paths))
402 (define inferior-package-transitive-native-search-paths
403 (cut %inferior-package-search-paths <> 'package-transitive-native-search-paths))
405 (define (proxy client backend) ;adapted from (guix ssh)
406 "Proxy communication between CLIENT and BACKEND until CLIENT closes the
407 connection, at which point CLIENT is closed (both CLIENT and BACKEND must be
408 input/output ports.)"
409 (define (select* read write except)
410 ;; This is a workaround for <https://bugs.gnu.org/30365> in Guile < 2.2.4:
411 ;; since 'select' sometimes returns non-empty sets for no good reason,
412 ;; call 'select' a second time with a zero timeout to filter out incorrect
414 (match (select read write except)
416 (select read write except 0))))
418 ;; Use buffered ports so that 'get-bytevector-some' returns up to the
419 ;; whole buffer like read(2) would--see <https://bugs.gnu.org/30066>.
420 (setvbuf client 'block 65536)
421 (setvbuf backend 'block 65536)
424 (match (select* (list client backend) '() '())
426 (when (memq client reads)
427 (match (get-bytevector-some client)
431 (put-bytevector backend bv)
432 (force-output backend))))
433 (when (memq backend reads)
434 (match (get-bytevector-some backend)
436 (put-bytevector client bv)
437 (force-output client))))
438 (unless (port-closed? client)
441 (define (inferior-eval-with-store inferior store code)
442 "Evaluate CODE in INFERIOR, passing it STORE as its argument. CODE must
443 thus be the code of a one-argument procedure that accepts a store."
444 ;; Create a named socket in /tmp and let INFERIOR connect to it and use it
445 ;; as its store. This ensures the inferior uses the same store, with the
446 ;; same options, the same per-session GC roots, etc.
447 ;; FIXME: This strategy doesn't work for remote inferiors (SSH).
448 (call-with-temporary-directory
450 (chmod directory #o700)
451 (let* ((name (string-append directory "/inferior"))
452 (socket (socket AF_UNIX SOCK_STREAM 0))
453 (major (store-connection-major-version store))
454 (minor (store-connection-minor-version store))
455 (proto (logior major minor)))
456 (bind socket AF_UNIX name)
458 (send-inferior-request
460 (socket (socket AF_UNIX SOCK_STREAM 0)))
461 (connect socket AF_UNIX ,name)
463 ;; 'port->connection' appeared in June 2018 and we can hardly
464 ;; emulate it on older versions. Thus fall back to
465 ;; 'open-connection', at the risk of talking to the wrong daemon or
466 ;; having our build result reclaimed (XXX).
467 (let ((store (if (defined? 'port->connection)
468 (port->connection socket #:version ,proto)
475 (close-connection store)
476 (close-port socket)))))
478 (match (accept socket)
480 (proxy client (store-connection-socket store))))
482 (read-inferior-response inferior)))))
484 (define* (inferior-package-derivation store package
486 (system (%current-system))
488 "Return the derivation for PACKAGE, an inferior package, built for SYSTEM
489 and cross-built for TARGET if TARGET is true. The inferior corresponding to
490 PACKAGE must be live."
493 (let* ((package (hashv-ref %package-table
494 ,(inferior-package-id package)))
496 `(package-cross-derivation store package
499 `(package-derivation store package
501 (derivation-file-name drv))))
503 (and=> (inferior-eval-with-store (inferior-package-inferior package) store
505 read-derivation-from-file))
507 (define inferior-package->derivation
508 (store-lift inferior-package-derivation))
510 (define-gexp-compiler (package-compiler (package <inferior-package>) system
512 ;; Compile PACKAGE for SYSTEM, optionally cross-building for TARGET.
513 (inferior-package->derivation package system #:target target))
515 (define* (gexp->derivation-in-inferior name exp guix
516 #:key silent-failure?
519 "Return a derivation that evaluates EXP with GUIX, an instance of Guix as
520 returned for example by 'channel-instances->derivation'. Other arguments are
521 passed as-is to 'gexp->derivation'.
523 When SILENT-FAILURE? is true, create an empty output directory instead of
524 failing when GUIX is too old and lacks the 'guix repl' command."
526 ;; EXP wrapped with a proper (set! %load-path …) prologue.
527 (scheme-file "inferior-script.scm" exp))
530 ;; This is a crude way to run EXP on GUIX. TODO: use 'raw-derivation' and
531 ;; make 'guix repl' the "builder"; this will require "opening up" the
532 ;; mechanisms behind 'gexp->derivation', and adding '-l' to 'guix repl'.
534 (use-modules (ice-9 popen))
536 (let ((pipe (open-pipe* OPEN_WRITE
537 #+(file-append guix "/bin/guix")
538 "repl" "-t" "machine")))
540 ;; XXX: EXP presumably refers to #$output but that reference is lost
541 ;; so explicitly reference it here.
544 (write `(primitive-load #$script) pipe)
546 (unless (zero? (close-pipe pipe))
547 (if #$silent-failure?
549 (error "inferior failed" #+guix))))))
551 (define (drop-extra-keyword lst)
557 ((#:silent-failure? _ . rest)
560 (loop tail (cons* value kw result))))))
562 (apply gexp->derivation name trampoline
563 (drop-extra-keyword rest)))
567 ;;; Manifest entries.
570 (define* (inferior-package->manifest-entry package
571 #:optional (output "out")
572 #:key (parent (delay #f))
574 "Return a manifest entry for the OUTPUT of package PACKAGE."
575 ;; For each dependency, keep a promise pointing to its "parent" entry.
576 (letrec* ((deps (map (match-lambda
578 (inferior-package->manifest-entry package
579 #:parent (delay entry)))
580 ((label package output)
581 (inferior-package->manifest-entry package output
582 #:parent (delay entry))))
583 (inferior-package-propagated-inputs package)))
584 (entry (manifest-entry
585 (name (inferior-package-name package))
586 (version (inferior-package-version package))
589 (dependencies (delete-duplicates deps))
591 (inferior-package-transitive-native-search-paths package))
593 (properties properties))))
598 ;;; Cached inferiors.
601 (define %inferior-cache-directory
602 ;; Directory for cached inferiors (GC roots).
603 (make-parameter (string-append (cache-directory #:ensure? #f)
606 (define* (inferior-for-channels channels
608 (cache-directory (%inferior-cache-directory))
609 (ttl (* 3600 24 30)))
610 "Return an inferior for CHANNELS, a list of channels. Use the cache at
611 CACHE-DIRECTORY, where entries can be reclaimed after TTL seconds. This
612 procedure opens a new connection to the build daemon.
614 This is a convenience procedure that people may use in manifests passed to
615 'guix package -m', for instance."
619 (latest-channel-instances store channels))
622 (bytevector->base32-string
625 (string-concatenate (map channel-instance-commit instances))))))
628 (string-append cache-directory "/" key))
630 (define (base32-encoded-sha256? str)
631 (= (string-length str) 52))
633 (define (cache-entries directory)
635 (string-append directory "/" file))
636 (scandir directory base32-encoded-sha256?)))
639 (lift2 symlink %store-monad))
641 (define add-indirect-root*
642 (store-lift add-indirect-root))
644 (mkdir-p cache-directory)
645 (maybe-remove-expired-cache-entries cache-directory
648 (file-expiration-time ttl))
650 (if (file-exists? cached)
651 (open-inferior cached)
652 (run-with-store store
653 (mlet %store-monad ((profile
654 (channel-instances->derivation instances)))
656 (show-what-to-build* (list profile))
657 (built-derivations (list profile))
658 (symlink* (derivation->output-path profile) cached)
659 (add-indirect-root* cached)
660 (return (open-inferior cached)))))))))