Commit | Line | Data |
---|---|---|
2ca299ca | 1 | ;;; GNU Guix --- Functional package management for GNU |
ea6d962b | 2 | ;;; Copyright © 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org> |
2ca299ca LC |
3 | ;;; |
4 | ;;; This file is part of GNU Guix. | |
5 | ;;; | |
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. | |
10 | ;;; | |
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. | |
15 | ;;; | |
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/>. | |
18 | ||
19 | (define-module (guix inferior) | |
20 | #:use-module (srfi srfi-9) | |
21 | #:use-module (srfi srfi-9 gnu) | |
71507435 LC |
22 | #:use-module (srfi srfi-34) |
23 | #:use-module (srfi srfi-35) | |
9daf046c LC |
24 | #:use-module ((guix utils) |
25 | #:select (%current-system | |
26 | source-properties->location | |
e1a4ffda | 27 | call-with-temporary-directory |
2dad0313 LC |
28 | version>? version-prefix? |
29 | cache-directory)) | |
9daf046c | 30 | #:use-module ((guix store) |
de9fbe9c LC |
31 | #:select (store-connection-socket |
32 | store-connection-major-version | |
33 | store-connection-minor-version | |
71507435 LC |
34 | store-lift |
35 | &store-protocol-error)) | |
9daf046c LC |
36 | #:use-module ((guix derivations) |
37 | #:select (read-derivation-from-file)) | |
38 | #:use-module (guix gexp) | |
eee8b303 | 39 | #:use-module (guix search-paths) |
2e6d64e1 | 40 | #:use-module (guix profiles) |
2dad0313 LC |
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) | |
ea6d962b LC |
47 | #:autoload (guix cache) (maybe-remove-expired-cache-entries |
48 | file-expiration-time) | |
2dad0313 LC |
49 | #:autoload (guix ui) (show-what-to-build*) |
50 | #:autoload (guix build utils) (mkdir-p) | |
e1a4ffda | 51 | #:use-module (srfi srfi-1) |
6030396a | 52 | #:use-module (srfi srfi-26) |
2dad0313 | 53 | #:autoload (ice-9 ftw) (scandir) |
2ca299ca LC |
54 | #:use-module (ice-9 match) |
55 | #:use-module (ice-9 popen) | |
e1a4ffda | 56 | #:use-module (ice-9 vlist) |
9daf046c | 57 | #:use-module (ice-9 binary-ports) |
2dad0313 | 58 | #:use-module ((rnrs bytevectors) #:select (string->utf8)) |
2ca299ca LC |
59 | #:export (inferior? |
60 | open-inferior | |
af15fe13 | 61 | port->inferior |
2ca299ca LC |
62 | close-inferior |
63 | inferior-eval | |
94c0e61f | 64 | inferior-eval-with-store |
2ca299ca | 65 | inferior-object? |
f7537e30 LC |
66 | inferior-exception? |
67 | inferior-exception-arguments | |
68 | inferior-exception-inferior | |
d0ffa321 | 69 | read-repl-response |
2ca299ca | 70 | |
2e6d64e1 | 71 | inferior-packages |
73938054 | 72 | inferior-available-packages |
2e6d64e1 LC |
73 | lookup-inferior-packages |
74 | ||
2ca299ca LC |
75 | inferior-package? |
76 | inferior-package-name | |
77 | inferior-package-version | |
2ca299ca | 78 | inferior-package-synopsis |
7e1d2290 LC |
79 | inferior-package-description |
80 | inferior-package-home-page | |
9daf046c | 81 | inferior-package-location |
6030396a LC |
82 | inferior-package-inputs |
83 | inferior-package-native-inputs | |
84 | inferior-package-propagated-inputs | |
85 | inferior-package-transitive-propagated-inputs | |
eee8b303 LC |
86 | inferior-package-native-search-paths |
87 | inferior-package-transitive-native-search-paths | |
88 | inferior-package-search-paths | |
7a241c63 | 89 | inferior-package-provenance |
2e6d64e1 LC |
90 | inferior-package-derivation |
91 | ||
2dad0313 LC |
92 | inferior-package->manifest-entry |
93 | ||
ae927822 LC |
94 | gexp->derivation-in-inferior |
95 | ||
2dad0313 | 96 | %inferior-cache-directory |
f675f8de | 97 | cached-channel-instance |
2dad0313 | 98 | inferior-for-channels)) |
2ca299ca LC |
99 | |
100 | ;;; Commentary: | |
101 | ;;; | |
102 | ;;; This module provides a way to spawn Guix "inferior" processes and to talk | |
103 | ;;; to them. It allows us, from one instance of Guix, to interact with | |
104 | ;;; another instance of Guix coming from a different commit. | |
105 | ;;; | |
106 | ;;; Code: | |
107 | ||
108 | ;; Inferior Guix process. | |
109 | (define-record-type <inferior> | |
af15fe13 | 110 | (inferior pid socket close version packages table) |
2ca299ca LC |
111 | inferior? |
112 | (pid inferior-pid) | |
113 | (socket inferior-socket) | |
af15fe13 | 114 | (close inferior-close-socket) ;procedure |
e1a4ffda LC |
115 | (version inferior-version) ;REPL protocol version |
116 | (packages inferior-package-promise) ;promise of inferior packages | |
117 | (table inferior-package-table)) ;promise of vhash | |
2ca299ca | 118 | |
f0428c18 | 119 | (define* (inferior-pipe directory command error-port) |
2ca299ca LC |
120 | "Return an input/output pipe on the Guix instance in DIRECTORY. This runs |
121 | 'DIRECTORY/COMMAND repl' if it exists, or falls back to some other method if | |
122 | it's an old Guix." | |
f0428c18 | 123 | (let ((pipe (with-error-to-port error-port |
2ca299ca LC |
124 | (lambda () |
125 | (open-pipe* OPEN_BOTH | |
126 | (string-append directory "/" command) | |
127 | "repl" "-t" "machine"))))) | |
128 | (if (eof-object? (peek-char pipe)) | |
129 | (begin | |
130 | (close-pipe pipe) | |
131 | ||
132 | ;; Older versions of Guix didn't have a 'guix repl' command, so | |
133 | ;; emulate it. | |
ef0c2654 CB |
134 | (with-error-to-port error-port |
135 | (lambda () | |
136 | (open-pipe* OPEN_BOTH "guile" | |
137 | "-L" (string-append directory "/share/guile/site/" | |
138 | (effective-version)) | |
139 | "-C" (string-append directory "/share/guile/site/" | |
140 | (effective-version)) | |
141 | "-C" (string-append directory "/lib/guile/" | |
142 | (effective-version) "/site-ccache") | |
143 | "-c" | |
144 | (object->string | |
145 | `(begin | |
146 | (primitive-load ,(search-path %load-path | |
147 | "guix/repl.scm")) | |
148 | ((@ (guix repl) machine-repl)))))))) | |
2ca299ca LC |
149 | pipe))) |
150 | ||
af15fe13 LC |
151 | (define* (port->inferior pipe #:optional (close close-port)) |
152 | "Given PIPE, an input/output port, return an inferior that talks over PIPE. | |
153 | PIPE is closed with CLOSE when 'close-inferior' is called on the returned | |
154 | inferior." | |
a65177a6 | 155 | (setvbuf pipe 'line) |
a81b59b1 | 156 | |
2ca299ca LC |
157 | (match (read pipe) |
158 | (('repl-version 0 rest ...) | |
af15fe13 | 159 | (letrec ((result (inferior 'pipe pipe close (cons 0 rest) |
e1a4ffda LC |
160 | (delay (%inferior-packages result)) |
161 | (delay (%inferior-package-table result))))) | |
ec0a8661 LC |
162 | |
163 | ;; For protocol (0 1) and later, send the protocol version we support. | |
164 | (match rest | |
165 | ((n _ ...) | |
166 | (when (>= n 1) | |
167 | (send-inferior-request '(() repl-version 0 1) result))) | |
168 | (_ | |
169 | #t)) | |
170 | ||
2ca299ca LC |
171 | (inferior-eval '(use-modules (guix)) result) |
172 | (inferior-eval '(use-modules (gnu)) result) | |
6030396a | 173 | (inferior-eval '(use-modules (ice-9 match)) result) |
71507435 | 174 | (inferior-eval '(use-modules (srfi srfi-34)) result) |
2ca299ca LC |
175 | (inferior-eval '(define %package-table (make-hash-table)) |
176 | result) | |
177 | result)) | |
178 | (_ | |
179 | #f))) | |
180 | ||
f0428c18 CB |
181 | (define* (open-inferior directory |
182 | #:key (command "bin/guix") | |
183 | (error-port (%make-void-port "w"))) | |
af15fe13 LC |
184 | "Open the inferior Guix in DIRECTORY, running 'DIRECTORY/COMMAND repl' or |
185 | equivalent. Return #f if the inferior could not be launched." | |
186 | (define pipe | |
f0428c18 | 187 | (inferior-pipe directory command error-port)) |
af15fe13 LC |
188 | |
189 | (port->inferior pipe close-pipe)) | |
190 | ||
2ca299ca LC |
191 | (define (close-inferior inferior) |
192 | "Close INFERIOR." | |
af15fe13 LC |
193 | (let ((close (inferior-close-socket inferior))) |
194 | (close (inferior-socket inferior)))) | |
2ca299ca LC |
195 | |
196 | ;; Non-self-quoting object of the inferior. | |
197 | (define-record-type <inferior-object> | |
198 | (inferior-object address appearance) | |
199 | inferior-object? | |
200 | (address inferior-object-address) | |
201 | (appearance inferior-object-appearance)) | |
202 | ||
203 | (define (write-inferior-object object port) | |
204 | (match object | |
205 | (($ <inferior-object> _ appearance) | |
206 | (format port "#<inferior-object ~a>" appearance)))) | |
207 | ||
208 | (set-record-type-printer! <inferior-object> write-inferior-object) | |
209 | ||
f7537e30 LC |
210 | ;; Reified exception thrown by an inferior. |
211 | (define-condition-type &inferior-exception &error | |
212 | inferior-exception? | |
213 | (arguments inferior-exception-arguments) ;key + arguments | |
214 | (inferior inferior-exception-inferior)) ;<inferior> | #f | |
215 | ||
216 | (define* (read-repl-response port #:optional inferior) | |
217 | "Read a (guix repl) response from PORT and return it as a Scheme object. | |
218 | Raise '&inferior-exception' when an exception is read from PORT." | |
2ca299ca LC |
219 | (define sexp->object |
220 | (match-lambda | |
221 | (('value value) | |
222 | value) | |
223 | (('non-self-quoting address string) | |
224 | (inferior-object address string)))) | |
225 | ||
d0ffa321 | 226 | (match (read port) |
2ca299ca LC |
227 | (('values objects ...) |
228 | (apply values (map sexp->object objects))) | |
229 | (('exception key objects ...) | |
f7537e30 LC |
230 | (raise (condition (&inferior-exception |
231 | (arguments (cons key (map sexp->object objects))) | |
232 | (inferior inferior))))))) | |
2ca299ca | 233 | |
d0ffa321 | 234 | (define (read-inferior-response inferior) |
f7537e30 LC |
235 | (read-repl-response (inferior-socket inferior) |
236 | inferior)) | |
d0ffa321 | 237 | |
9daf046c LC |
238 | (define (send-inferior-request exp inferior) |
239 | (write exp (inferior-socket inferior)) | |
240 | (newline (inferior-socket inferior))) | |
241 | ||
242 | (define (inferior-eval exp inferior) | |
243 | "Evaluate EXP in INFERIOR." | |
244 | (send-inferior-request exp inferior) | |
245 | (read-inferior-response inferior)) | |
246 | ||
2ca299ca LC |
247 | \f |
248 | ;;; | |
249 | ;;; Inferior packages. | |
250 | ;;; | |
251 | ||
252 | (define-record-type <inferior-package> | |
253 | (inferior-package inferior name version id) | |
254 | inferior-package? | |
255 | (inferior inferior-package-inferior) | |
256 | (name inferior-package-name) | |
257 | (version inferior-package-version) | |
258 | (id inferior-package-id)) | |
259 | ||
260 | (define (write-inferior-package package port) | |
261 | (match package | |
262 | (($ <inferior-package> _ name version) | |
263 | (format port "#<inferior-package ~a@~a ~a>" | |
264 | name version | |
265 | (number->string (object-address package) 16))))) | |
266 | ||
267 | (set-record-type-printer! <inferior-package> write-inferior-package) | |
268 | ||
e1a4ffda LC |
269 | (define (%inferior-packages inferior) |
270 | "Compute the list of inferior packages from INFERIOR." | |
2ca299ca LC |
271 | (let ((result (inferior-eval |
272 | '(fold-packages (lambda (package result) | |
273 | (let ((id (object-address package))) | |
274 | (hashv-set! %package-table id package) | |
275 | (cons (list (package-name package) | |
276 | (package-version package) | |
277 | id) | |
278 | result))) | |
279 | '()) | |
280 | inferior))) | |
281 | (map (match-lambda | |
282 | ((name version id) | |
283 | (inferior-package inferior name version id))) | |
284 | result))) | |
285 | ||
e1a4ffda LC |
286 | (define (inferior-packages inferior) |
287 | "Return the list of packages known to INFERIOR." | |
288 | (force (inferior-package-promise inferior))) | |
289 | ||
290 | (define (%inferior-package-table inferior) | |
291 | "Compute a package lookup table for INFERIOR." | |
292 | (fold (lambda (package table) | |
293 | (vhash-cons (inferior-package-name package) package | |
294 | table)) | |
295 | vlist-null | |
296 | (inferior-packages inferior))) | |
297 | ||
73938054 LC |
298 | (define (inferior-available-packages inferior) |
299 | "Return the list of name/version pairs corresponding to the set of packages | |
300 | available in INFERIOR. | |
301 | ||
302 | This is faster and requires less resource-intensive than calling | |
303 | 'inferior-packages'." | |
304 | (if (inferior-eval '(defined? 'fold-available-packages) | |
305 | inferior) | |
306 | (inferior-eval '(fold-available-packages | |
307 | (lambda* (name version result | |
308 | #:key supported? deprecated? | |
309 | #:allow-other-keys) | |
310 | (if (and supported? (not deprecated?)) | |
311 | (acons name version result) | |
312 | result)) | |
313 | '()) | |
314 | inferior) | |
315 | ||
316 | ;; As a last resort, if INFERIOR is old and lacks | |
317 | ;; 'fold-available-packages', fall back to 'inferior-packages'. | |
318 | (map (lambda (package) | |
319 | (cons (inferior-package-name package) | |
320 | (inferior-package-version package))) | |
321 | (inferior-packages inferior)))) | |
322 | ||
e1a4ffda LC |
323 | (define* (lookup-inferior-packages inferior name #:optional version) |
324 | "Return the sorted list of inferior packages matching NAME in INFERIOR, with | |
325 | highest version numbers first. If VERSION is true, return only packages with | |
326 | a version number prefixed by VERSION." | |
327 | ;; This is the counterpart of 'find-packages-by-name'. | |
328 | (sort (filter (lambda (package) | |
329 | (or (not version) | |
330 | (version-prefix? version | |
331 | (inferior-package-version package)))) | |
332 | (vhash-fold* cons '() name | |
333 | (force (inferior-package-table inferior)))) | |
334 | (lambda (p1 p2) | |
335 | (version>? (inferior-package-version p1) | |
336 | (inferior-package-version p2))))) | |
337 | ||
2ca299ca LC |
338 | (define (inferior-package-field package getter) |
339 | "Return the field of PACKAGE, an inferior package, accessed with GETTER." | |
340 | (let ((inferior (inferior-package-inferior package)) | |
341 | (id (inferior-package-id package))) | |
342 | (inferior-eval `(,getter (hashv-ref %package-table ,id)) | |
343 | inferior))) | |
344 | ||
345 | (define* (inferior-package-synopsis package #:key (translate? #t)) | |
346 | "Return the Texinfo synopsis of PACKAGE, an inferior package. When | |
347 | TRANSLATE? is true, translate it to the current locale's language." | |
348 | (inferior-package-field package | |
349 | (if translate? | |
350 | '(compose (@ (guix ui) P_) package-synopsis) | |
351 | 'package-synopsis))) | |
352 | ||
353 | (define* (inferior-package-description package #:key (translate? #t)) | |
354 | "Return the Texinfo description of PACKAGE, an inferior package. When | |
355 | TRANSLATE? is true, translate it to the current locale's language." | |
356 | (inferior-package-field package | |
357 | (if translate? | |
358 | '(compose (@ (guix ui) P_) package-description) | |
359 | 'package-description))) | |
7e1d2290 LC |
360 | |
361 | (define (inferior-package-home-page package) | |
362 | "Return the home page of PACKAGE." | |
363 | (inferior-package-field package 'package-home-page)) | |
364 | ||
365 | (define (inferior-package-location package) | |
366 | "Return the source code location of PACKAGE, either #f or a <location> | |
367 | record." | |
368 | (source-properties->location | |
369 | (inferior-package-field package | |
370 | '(compose (lambda (loc) | |
371 | (and loc | |
372 | (location->source-properties | |
373 | loc))) | |
374 | package-location)))) | |
9daf046c | 375 | |
6030396a LC |
376 | (define (inferior-package-input-field package field) |
377 | "Return the input field FIELD (e.g., 'native-inputs') of PACKAGE, an | |
378 | inferior package." | |
379 | (define field* | |
380 | `(compose (lambda (inputs) | |
381 | (map (match-lambda | |
382 | ;; XXX: Origins are not handled. | |
383 | ((label (? package? package) rest ...) | |
384 | (let ((id (object-address package))) | |
385 | (hashv-set! %package-table id package) | |
386 | `(,label (package ,id | |
387 | ,(package-name package) | |
388 | ,(package-version package)) | |
389 | ,@rest))) | |
390 | (x | |
391 | x)) | |
392 | inputs)) | |
393 | ,field)) | |
394 | ||
395 | (define inputs | |
396 | (inferior-package-field package field*)) | |
397 | ||
398 | (define inferior | |
399 | (inferior-package-inferior package)) | |
400 | ||
401 | (map (match-lambda | |
402 | ((label ('package id name version) . rest) | |
403 | ;; XXX: eq?-ness of inferior packages is not preserved here. | |
404 | `(,label ,(inferior-package inferior name version id) | |
405 | ,@rest)) | |
406 | (x x)) | |
407 | inputs)) | |
408 | ||
409 | (define inferior-package-inputs | |
410 | (cut inferior-package-input-field <> 'package-inputs)) | |
411 | ||
412 | (define inferior-package-native-inputs | |
413 | (cut inferior-package-input-field <> 'package-native-inputs)) | |
414 | ||
415 | (define inferior-package-propagated-inputs | |
416 | (cut inferior-package-input-field <> 'package-propagated-inputs)) | |
417 | ||
418 | (define inferior-package-transitive-propagated-inputs | |
419 | (cut inferior-package-input-field <> 'package-transitive-propagated-inputs)) | |
420 | ||
eee8b303 | 421 | (define (%inferior-package-search-paths package field) |
a130544d | 422 | "Return the list of search path specifications of PACKAGE, an inferior |
eee8b303 LC |
423 | package." |
424 | (define paths | |
425 | (inferior-package-field package | |
426 | `(compose (lambda (paths) | |
427 | (map (@ (guix search-paths) | |
428 | search-path-specification->sexp) | |
429 | paths)) | |
430 | ,field))) | |
431 | ||
432 | (map sexp->search-path-specification paths)) | |
433 | ||
434 | (define inferior-package-native-search-paths | |
435 | (cut %inferior-package-search-paths <> 'package-native-search-paths)) | |
436 | ||
437 | (define inferior-package-search-paths | |
438 | (cut %inferior-package-search-paths <> 'package-search-paths)) | |
439 | ||
440 | (define inferior-package-transitive-native-search-paths | |
441 | (cut %inferior-package-search-paths <> 'package-transitive-native-search-paths)) | |
442 | ||
7a241c63 LC |
443 | (define (inferior-package-provenance package) |
444 | "Return a \"provenance sexp\" for PACKAGE, an inferior package. The result | |
445 | is similar to the sexp returned by 'package-provenance' for regular packages." | |
446 | (inferior-package-field package | |
447 | '(let* ((describe | |
448 | (false-if-exception | |
449 | (resolve-interface '(guix describe)))) | |
450 | (provenance | |
451 | (false-if-exception | |
452 | (module-ref describe | |
453 | 'package-provenance)))) | |
454 | (or provenance (const #f))))) | |
455 | ||
9daf046c LC |
456 | (define (proxy client backend) ;adapted from (guix ssh) |
457 | "Proxy communication between CLIENT and BACKEND until CLIENT closes the | |
458 | connection, at which point CLIENT is closed (both CLIENT and BACKEND must be | |
459 | input/output ports.)" | |
460 | (define (select* read write except) | |
461 | ;; This is a workaround for <https://bugs.gnu.org/30365> in Guile < 2.2.4: | |
462 | ;; since 'select' sometimes returns non-empty sets for no good reason, | |
463 | ;; call 'select' a second time with a zero timeout to filter out incorrect | |
464 | ;; replies. | |
465 | (match (select read write except) | |
466 | ((read write except) | |
467 | (select read write except 0)))) | |
468 | ||
469 | ;; Use buffered ports so that 'get-bytevector-some' returns up to the | |
470 | ;; whole buffer like read(2) would--see <https://bugs.gnu.org/30066>. | |
76832d34 LC |
471 | (setvbuf client 'block 65536) |
472 | (setvbuf backend 'block 65536) | |
9daf046c LC |
473 | |
474 | (let loop () | |
475 | (match (select* (list client backend) '() '()) | |
476 | ((reads () ()) | |
477 | (when (memq client reads) | |
478 | (match (get-bytevector-some client) | |
479 | ((? eof-object?) | |
480 | (close-port client)) | |
481 | (bv | |
482 | (put-bytevector backend bv) | |
483 | (force-output backend)))) | |
484 | (when (memq backend reads) | |
485 | (match (get-bytevector-some backend) | |
486 | (bv | |
487 | (put-bytevector client bv) | |
488 | (force-output client)))) | |
489 | (unless (port-closed? client) | |
490 | (loop)))))) | |
491 | ||
94c0e61f LC |
492 | (define (inferior-eval-with-store inferior store code) |
493 | "Evaluate CODE in INFERIOR, passing it STORE as its argument. CODE must | |
494 | thus be the code of a one-argument procedure that accepts a store." | |
495 | ;; Create a named socket in /tmp and let INFERIOR connect to it and use it | |
496 | ;; as its store. This ensures the inferior uses the same store, with the | |
497 | ;; same options, the same per-session GC roots, etc. | |
af15fe13 | 498 | ;; FIXME: This strategy doesn't work for remote inferiors (SSH). |
9daf046c LC |
499 | (call-with-temporary-directory |
500 | (lambda (directory) | |
501 | (chmod directory #o700) | |
502 | (let* ((name (string-append directory "/inferior")) | |
503 | (socket (socket AF_UNIX SOCK_STREAM 0)) | |
de9fbe9c LC |
504 | (major (store-connection-major-version store)) |
505 | (minor (store-connection-minor-version store)) | |
9daf046c LC |
506 | (proto (logior major minor))) |
507 | (bind socket AF_UNIX name) | |
508 | (listen socket 1024) | |
509 | (send-inferior-request | |
94c0e61f | 510 | `(let ((proc ,code) |
71507435 LC |
511 | (socket (socket AF_UNIX SOCK_STREAM 0)) |
512 | (error? (if (defined? 'store-protocol-error?) | |
513 | store-protocol-error? | |
514 | nix-protocol-error?)) | |
515 | (error-message (if (defined? 'store-protocol-error-message) | |
516 | store-protocol-error-message | |
517 | nix-protocol-error-message))) | |
9daf046c LC |
518 | (connect socket AF_UNIX ,name) |
519 | ||
520 | ;; 'port->connection' appeared in June 2018 and we can hardly | |
521 | ;; emulate it on older versions. Thus fall back to | |
522 | ;; 'open-connection', at the risk of talking to the wrong daemon or | |
523 | ;; having our build result reclaimed (XXX). | |
94c0e61f LC |
524 | (let ((store (if (defined? 'port->connection) |
525 | (port->connection socket #:version ,proto) | |
526 | (open-connection)))) | |
527 | (dynamic-wind | |
528 | (const #t) | |
529 | (lambda () | |
71507435 LC |
530 | ;; Serialize '&store-protocol-error' conditions. The |
531 | ;; exception serialization mechanism that | |
532 | ;; 'read-repl-response' expects is unsuitable for SRFI-35 | |
533 | ;; error conditions, hence this special case. | |
534 | (guard (c ((error? c) | |
535 | `(store-protocol-error ,(error-message c)))) | |
536 | `(result ,(proc store)))) | |
94c0e61f LC |
537 | (lambda () |
538 | (close-connection store) | |
539 | (close-port socket))))) | |
9daf046c LC |
540 | inferior) |
541 | (match (accept socket) | |
542 | ((client . address) | |
de9fbe9c | 543 | (proxy client (store-connection-socket store)))) |
9daf046c | 544 | (close-port socket) |
71507435 LC |
545 | |
546 | (match (read-inferior-response inferior) | |
547 | (('store-protocol-error message) | |
548 | (raise (condition | |
549 | (&store-protocol-error (message message) | |
550 | (status 1))))) | |
551 | (('result result) | |
552 | result)))))) | |
94c0e61f LC |
553 | |
554 | (define* (inferior-package-derivation store package | |
555 | #:optional | |
556 | (system (%current-system)) | |
557 | #:key target) | |
558 | "Return the derivation for PACKAGE, an inferior package, built for SYSTEM | |
559 | and cross-built for TARGET if TARGET is true. The inferior corresponding to | |
560 | PACKAGE must be live." | |
561 | (define proc | |
562 | `(lambda (store) | |
563 | (let* ((package (hashv-ref %package-table | |
564 | ,(inferior-package-id package))) | |
565 | (drv ,(if target | |
566 | `(package-cross-derivation store package | |
567 | ,target | |
568 | ,system) | |
569 | `(package-derivation store package | |
570 | ,system)))) | |
571 | (derivation-file-name drv)))) | |
572 | ||
573 | (and=> (inferior-eval-with-store (inferior-package-inferior package) store | |
574 | proc) | |
575 | read-derivation-from-file)) | |
9daf046c LC |
576 | |
577 | (define inferior-package->derivation | |
578 | (store-lift inferior-package-derivation)) | |
579 | ||
580 | (define-gexp-compiler (package-compiler (package <inferior-package>) system | |
581 | target) | |
582 | ;; Compile PACKAGE for SYSTEM, optionally cross-building for TARGET. | |
583 | (inferior-package->derivation package system #:target target)) | |
2e6d64e1 | 584 | |
ae927822 | 585 | (define* (gexp->derivation-in-inferior name exp guix |
4035fcba LC |
586 | #:key silent-failure? |
587 | #:allow-other-keys | |
ae927822 LC |
588 | #:rest rest) |
589 | "Return a derivation that evaluates EXP with GUIX, an instance of Guix as | |
590 | returned for example by 'channel-instances->derivation'. Other arguments are | |
4035fcba LC |
591 | passed as-is to 'gexp->derivation'. |
592 | ||
593 | When SILENT-FAILURE? is true, create an empty output directory instead of | |
594 | failing when GUIX is too old and lacks the 'guix repl' command." | |
1fafc383 LC |
595 | (define script |
596 | ;; EXP wrapped with a proper (set! %load-path …) prologue. | |
597 | (scheme-file "inferior-script.scm" exp)) | |
598 | ||
ae927822 LC |
599 | (define trampoline |
600 | ;; This is a crude way to run EXP on GUIX. TODO: use 'raw-derivation' and | |
601 | ;; make 'guix repl' the "builder"; this will require "opening up" the | |
602 | ;; mechanisms behind 'gexp->derivation', and adding '-l' to 'guix repl'. | |
603 | #~(begin | |
604 | (use-modules (ice-9 popen)) | |
605 | ||
606 | (let ((pipe (open-pipe* OPEN_WRITE | |
607 | #+(file-append guix "/bin/guix") | |
608 | "repl" "-t" "machine"))) | |
1fafc383 LC |
609 | |
610 | ;; XXX: EXP presumably refers to #$output but that reference is lost | |
611 | ;; so explicitly reference it here. | |
612 | #$output | |
613 | ||
614 | (write `(primitive-load #$script) pipe) | |
ae927822 LC |
615 | |
616 | (unless (zero? (close-pipe pipe)) | |
4035fcba LC |
617 | (if #$silent-failure? |
618 | (mkdir #$output) | |
619 | (error "inferior failed" #+guix)))))) | |
620 | ||
621 | (define (drop-extra-keyword lst) | |
622 | (let loop ((lst lst) | |
623 | (result '())) | |
624 | (match lst | |
625 | (() | |
626 | (reverse result)) | |
627 | ((#:silent-failure? _ . rest) | |
628 | (loop rest result)) | |
629 | ((kw value . tail) | |
630 | (loop tail (cons* value kw result)))))) | |
631 | ||
632 | (apply gexp->derivation name trampoline | |
633 | (drop-extra-keyword rest))) | |
ae927822 | 634 | |
2e6d64e1 LC |
635 | \f |
636 | ;;; | |
637 | ;;; Manifest entries. | |
638 | ;;; | |
639 | ||
640 | (define* (inferior-package->manifest-entry package | |
641 | #:optional (output "out") | |
642 | #:key (parent (delay #f)) | |
643 | (properties '())) | |
644 | "Return a manifest entry for the OUTPUT of package PACKAGE." | |
645 | ;; For each dependency, keep a promise pointing to its "parent" entry. | |
646 | (letrec* ((deps (map (match-lambda | |
647 | ((label package) | |
648 | (inferior-package->manifest-entry package | |
649 | #:parent (delay entry))) | |
650 | ((label package output) | |
651 | (inferior-package->manifest-entry package output | |
652 | #:parent (delay entry)))) | |
653 | (inferior-package-propagated-inputs package))) | |
654 | (entry (manifest-entry | |
655 | (name (inferior-package-name package)) | |
656 | (version (inferior-package-version package)) | |
657 | (output output) | |
658 | (item package) | |
659 | (dependencies (delete-duplicates deps)) | |
660 | (search-paths | |
661 | (inferior-package-transitive-native-search-paths package)) | |
662 | (parent parent) | |
663 | (properties properties)))) | |
664 | entry)) | |
2dad0313 LC |
665 | |
666 | \f | |
667 | ;;; | |
668 | ;;; Cached inferiors. | |
669 | ;;; | |
670 | ||
671 | (define %inferior-cache-directory | |
672 | ;; Directory for cached inferiors (GC roots). | |
673 | (make-parameter (string-append (cache-directory #:ensure? #f) | |
674 | "/inferiors"))) | |
675 | ||
1d548569 KH |
676 | (define* (cached-channel-instance store |
677 | channels | |
f675f8de KH |
678 | #:key |
679 | (cache-directory (%inferior-cache-directory)) | |
680 | (ttl (* 3600 24 30))) | |
681 | "Return a directory containing a guix filetree defined by CHANNELS, a list of channels. | |
682 | The directory is a subdirectory of CACHE-DIRECTORY, where entries can be reclaimed after TTL seconds. | |
683 | This procedure opens a new connection to the build daemon." | |
1d548569 KH |
684 | (define instances |
685 | (latest-channel-instances store channels)) | |
686 | ||
687 | (define key | |
688 | (bytevector->base32-string | |
689 | (sha256 | |
690 | (string->utf8 | |
691 | (string-concatenate (map channel-instance-commit instances)))))) | |
692 | ||
693 | (define cached | |
694 | (string-append cache-directory "/" key)) | |
695 | ||
696 | (define (base32-encoded-sha256? str) | |
697 | (= (string-length str) 52)) | |
698 | ||
699 | (define (cache-entries directory) | |
700 | (map (lambda (file) | |
701 | (string-append directory "/" file)) | |
702 | (scandir directory base32-encoded-sha256?))) | |
703 | ||
704 | (define symlink* | |
705 | (lift2 symlink %store-monad)) | |
706 | ||
707 | (define add-indirect-root* | |
708 | (store-lift add-indirect-root)) | |
709 | ||
710 | (mkdir-p cache-directory) | |
711 | (maybe-remove-expired-cache-entries cache-directory | |
712 | cache-entries | |
713 | #:entry-expiration | |
714 | (file-expiration-time ttl)) | |
715 | ||
716 | (if (file-exists? cached) | |
717 | cached | |
718 | (run-with-store store | |
719 | (mlet %store-monad ((profile | |
720 | (channel-instances->derivation instances))) | |
721 | (mbegin %store-monad | |
722 | (show-what-to-build* (list profile)) | |
723 | (built-derivations (list profile)) | |
724 | (symlink* (derivation->output-path profile) cached) | |
725 | (add-indirect-root* cached) | |
726 | (return cached)))))) | |
f675f8de KH |
727 | |
728 | (define* (inferior-for-channels channels | |
729 | #:key | |
730 | (cache-directory (%inferior-cache-directory)) | |
731 | (ttl (* 3600 24 30))) | |
732 | "Return an inferior for CHANNELS, a list of channels. Use the cache at | |
733 | CACHE-DIRECTORY, where entries can be reclaimed after TTL seconds. This | |
734 | procedure opens a new connection to the build daemon. | |
735 | ||
736 | This is a convenience procedure that people may use in manifests passed to | |
737 | 'guix package -m', for instance." | |
738 | (define cached | |
1d548569 KH |
739 | (with-store store |
740 | (cached-channel-instance store | |
741 | channels | |
742 | #:cache-directory cache-directory | |
743 | #:ttl ttl))) | |
f675f8de | 744 | (open-inferior cached)) |