Commit | Line | Data |
---|---|---|
2ca299ca LC |
1 | ;;; GNU Guix --- Functional package management for GNU |
2 | ;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org> | |
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) | |
9daf046c LC |
22 | #:use-module ((guix utils) |
23 | #:select (%current-system | |
24 | source-properties->location | |
e1a4ffda | 25 | call-with-temporary-directory |
2dad0313 LC |
26 | version>? version-prefix? |
27 | cache-directory)) | |
9daf046c LC |
28 | #:use-module ((guix store) |
29 | #:select (nix-server-socket | |
30 | nix-server-major-version | |
31 | nix-server-minor-version | |
32 | store-lift)) | |
33 | #:use-module ((guix derivations) | |
34 | #:select (read-derivation-from-file)) | |
35 | #:use-module (guix gexp) | |
eee8b303 | 36 | #:use-module (guix search-paths) |
2e6d64e1 | 37 | #:use-module (guix profiles) |
2dad0313 LC |
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) | |
e1a4ffda | 47 | #:use-module (srfi srfi-1) |
6030396a | 48 | #:use-module (srfi srfi-26) |
2dad0313 | 49 | #:autoload (ice-9 ftw) (scandir) |
2ca299ca LC |
50 | #:use-module (ice-9 match) |
51 | #:use-module (ice-9 popen) | |
e1a4ffda | 52 | #:use-module (ice-9 vlist) |
9daf046c | 53 | #:use-module (ice-9 binary-ports) |
2dad0313 | 54 | #:use-module ((rnrs bytevectors) #:select (string->utf8)) |
2ca299ca LC |
55 | #:export (inferior? |
56 | open-inferior | |
af15fe13 | 57 | port->inferior |
2ca299ca LC |
58 | close-inferior |
59 | inferior-eval | |
94c0e61f | 60 | inferior-eval-with-store |
2ca299ca LC |
61 | inferior-object? |
62 | ||
2e6d64e1 LC |
63 | inferior-packages |
64 | lookup-inferior-packages | |
65 | ||
2ca299ca LC |
66 | inferior-package? |
67 | inferior-package-name | |
68 | inferior-package-version | |
2ca299ca | 69 | inferior-package-synopsis |
7e1d2290 LC |
70 | inferior-package-description |
71 | inferior-package-home-page | |
9daf046c | 72 | inferior-package-location |
6030396a LC |
73 | inferior-package-inputs |
74 | inferior-package-native-inputs | |
75 | inferior-package-propagated-inputs | |
76 | inferior-package-transitive-propagated-inputs | |
eee8b303 LC |
77 | inferior-package-native-search-paths |
78 | inferior-package-transitive-native-search-paths | |
79 | inferior-package-search-paths | |
2e6d64e1 LC |
80 | inferior-package-derivation |
81 | ||
2dad0313 LC |
82 | inferior-package->manifest-entry |
83 | ||
84 | %inferior-cache-directory | |
85 | inferior-for-channels)) | |
2ca299ca LC |
86 | |
87 | ;;; Commentary: | |
88 | ;;; | |
89 | ;;; This module provides a way to spawn Guix "inferior" processes and to talk | |
90 | ;;; to them. It allows us, from one instance of Guix, to interact with | |
91 | ;;; another instance of Guix coming from a different commit. | |
92 | ;;; | |
93 | ;;; Code: | |
94 | ||
95 | ;; Inferior Guix process. | |
96 | (define-record-type <inferior> | |
af15fe13 | 97 | (inferior pid socket close version packages table) |
2ca299ca LC |
98 | inferior? |
99 | (pid inferior-pid) | |
100 | (socket inferior-socket) | |
af15fe13 | 101 | (close inferior-close-socket) ;procedure |
e1a4ffda LC |
102 | (version inferior-version) ;REPL protocol version |
103 | (packages inferior-package-promise) ;promise of inferior packages | |
104 | (table inferior-package-table)) ;promise of vhash | |
2ca299ca LC |
105 | |
106 | (define (inferior-pipe directory command) | |
107 | "Return an input/output pipe on the Guix instance in DIRECTORY. This runs | |
108 | 'DIRECTORY/COMMAND repl' if it exists, or falls back to some other method if | |
109 | it's an old Guix." | |
110 | (let ((pipe (with-error-to-port (%make-void-port "w") | |
111 | (lambda () | |
112 | (open-pipe* OPEN_BOTH | |
113 | (string-append directory "/" command) | |
114 | "repl" "-t" "machine"))))) | |
115 | (if (eof-object? (peek-char pipe)) | |
116 | (begin | |
117 | (close-pipe pipe) | |
118 | ||
119 | ;; Older versions of Guix didn't have a 'guix repl' command, so | |
120 | ;; emulate it. | |
121 | (open-pipe* OPEN_BOTH "guile" | |
122 | "-L" (string-append directory "/share/guile/site/" | |
123 | (effective-version)) | |
124 | "-C" (string-append directory "/share/guile/site/" | |
125 | (effective-version)) | |
126 | "-C" (string-append directory "/lib/guile/" | |
127 | (effective-version) "/site-ccache") | |
128 | "-c" | |
129 | (object->string | |
130 | `(begin | |
131 | (primitive-load ,(search-path %load-path | |
132 | "guix/scripts/repl.scm")) | |
133 | ((@ (guix scripts repl) machine-repl)))))) | |
134 | pipe))) | |
135 | ||
af15fe13 LC |
136 | (define* (port->inferior pipe #:optional (close close-port)) |
137 | "Given PIPE, an input/output port, return an inferior that talks over PIPE. | |
138 | PIPE is closed with CLOSE when 'close-inferior' is called on the returned | |
139 | inferior." | |
a81b59b1 LC |
140 | (cond-expand |
141 | ((and guile-2 (not guile-2.2)) #t) | |
142 | (else (setvbuf pipe 'line))) | |
143 | ||
2ca299ca LC |
144 | (match (read pipe) |
145 | (('repl-version 0 rest ...) | |
af15fe13 | 146 | (letrec ((result (inferior 'pipe pipe close (cons 0 rest) |
e1a4ffda LC |
147 | (delay (%inferior-packages result)) |
148 | (delay (%inferior-package-table result))))) | |
2ca299ca LC |
149 | (inferior-eval '(use-modules (guix)) result) |
150 | (inferior-eval '(use-modules (gnu)) result) | |
6030396a | 151 | (inferior-eval '(use-modules (ice-9 match)) result) |
2ca299ca LC |
152 | (inferior-eval '(define %package-table (make-hash-table)) |
153 | result) | |
154 | result)) | |
155 | (_ | |
156 | #f))) | |
157 | ||
af15fe13 LC |
158 | (define* (open-inferior directory #:key (command "bin/guix")) |
159 | "Open the inferior Guix in DIRECTORY, running 'DIRECTORY/COMMAND repl' or | |
160 | equivalent. Return #f if the inferior could not be launched." | |
161 | (define pipe | |
162 | (inferior-pipe directory command)) | |
163 | ||
164 | (port->inferior pipe close-pipe)) | |
165 | ||
2ca299ca LC |
166 | (define (close-inferior inferior) |
167 | "Close INFERIOR." | |
af15fe13 LC |
168 | (let ((close (inferior-close-socket inferior))) |
169 | (close (inferior-socket inferior)))) | |
2ca299ca LC |
170 | |
171 | ;; Non-self-quoting object of the inferior. | |
172 | (define-record-type <inferior-object> | |
173 | (inferior-object address appearance) | |
174 | inferior-object? | |
175 | (address inferior-object-address) | |
176 | (appearance inferior-object-appearance)) | |
177 | ||
178 | (define (write-inferior-object object port) | |
179 | (match object | |
180 | (($ <inferior-object> _ appearance) | |
181 | (format port "#<inferior-object ~a>" appearance)))) | |
182 | ||
183 | (set-record-type-printer! <inferior-object> write-inferior-object) | |
184 | ||
9daf046c | 185 | (define (read-inferior-response inferior) |
2ca299ca LC |
186 | (define sexp->object |
187 | (match-lambda | |
188 | (('value value) | |
189 | value) | |
190 | (('non-self-quoting address string) | |
191 | (inferior-object address string)))) | |
192 | ||
2ca299ca LC |
193 | (match (read (inferior-socket inferior)) |
194 | (('values objects ...) | |
195 | (apply values (map sexp->object objects))) | |
196 | (('exception key objects ...) | |
197 | (apply throw key (map sexp->object objects))))) | |
198 | ||
9daf046c LC |
199 | (define (send-inferior-request exp inferior) |
200 | (write exp (inferior-socket inferior)) | |
201 | (newline (inferior-socket inferior))) | |
202 | ||
203 | (define (inferior-eval exp inferior) | |
204 | "Evaluate EXP in INFERIOR." | |
205 | (send-inferior-request exp inferior) | |
206 | (read-inferior-response inferior)) | |
207 | ||
2ca299ca LC |
208 | \f |
209 | ;;; | |
210 | ;;; Inferior packages. | |
211 | ;;; | |
212 | ||
213 | (define-record-type <inferior-package> | |
214 | (inferior-package inferior name version id) | |
215 | inferior-package? | |
216 | (inferior inferior-package-inferior) | |
217 | (name inferior-package-name) | |
218 | (version inferior-package-version) | |
219 | (id inferior-package-id)) | |
220 | ||
221 | (define (write-inferior-package package port) | |
222 | (match package | |
223 | (($ <inferior-package> _ name version) | |
224 | (format port "#<inferior-package ~a@~a ~a>" | |
225 | name version | |
226 | (number->string (object-address package) 16))))) | |
227 | ||
228 | (set-record-type-printer! <inferior-package> write-inferior-package) | |
229 | ||
e1a4ffda LC |
230 | (define (%inferior-packages inferior) |
231 | "Compute the list of inferior packages from INFERIOR." | |
2ca299ca LC |
232 | (let ((result (inferior-eval |
233 | '(fold-packages (lambda (package result) | |
234 | (let ((id (object-address package))) | |
235 | (hashv-set! %package-table id package) | |
236 | (cons (list (package-name package) | |
237 | (package-version package) | |
238 | id) | |
239 | result))) | |
240 | '()) | |
241 | inferior))) | |
242 | (map (match-lambda | |
243 | ((name version id) | |
244 | (inferior-package inferior name version id))) | |
245 | result))) | |
246 | ||
e1a4ffda LC |
247 | (define (inferior-packages inferior) |
248 | "Return the list of packages known to INFERIOR." | |
249 | (force (inferior-package-promise inferior))) | |
250 | ||
251 | (define (%inferior-package-table inferior) | |
252 | "Compute a package lookup table for INFERIOR." | |
253 | (fold (lambda (package table) | |
254 | (vhash-cons (inferior-package-name package) package | |
255 | table)) | |
256 | vlist-null | |
257 | (inferior-packages inferior))) | |
258 | ||
259 | (define* (lookup-inferior-packages inferior name #:optional version) | |
260 | "Return the sorted list of inferior packages matching NAME in INFERIOR, with | |
261 | highest version numbers first. If VERSION is true, return only packages with | |
262 | a version number prefixed by VERSION." | |
263 | ;; This is the counterpart of 'find-packages-by-name'. | |
264 | (sort (filter (lambda (package) | |
265 | (or (not version) | |
266 | (version-prefix? version | |
267 | (inferior-package-version package)))) | |
268 | (vhash-fold* cons '() name | |
269 | (force (inferior-package-table inferior)))) | |
270 | (lambda (p1 p2) | |
271 | (version>? (inferior-package-version p1) | |
272 | (inferior-package-version p2))))) | |
273 | ||
2ca299ca LC |
274 | (define (inferior-package-field package getter) |
275 | "Return the field of PACKAGE, an inferior package, accessed with GETTER." | |
276 | (let ((inferior (inferior-package-inferior package)) | |
277 | (id (inferior-package-id package))) | |
278 | (inferior-eval `(,getter (hashv-ref %package-table ,id)) | |
279 | inferior))) | |
280 | ||
281 | (define* (inferior-package-synopsis package #:key (translate? #t)) | |
282 | "Return the Texinfo synopsis of PACKAGE, an inferior package. When | |
283 | TRANSLATE? is true, translate it to the current locale's language." | |
284 | (inferior-package-field package | |
285 | (if translate? | |
286 | '(compose (@ (guix ui) P_) package-synopsis) | |
287 | 'package-synopsis))) | |
288 | ||
289 | (define* (inferior-package-description package #:key (translate? #t)) | |
290 | "Return the Texinfo description of PACKAGE, an inferior package. When | |
291 | TRANSLATE? is true, translate it to the current locale's language." | |
292 | (inferior-package-field package | |
293 | (if translate? | |
294 | '(compose (@ (guix ui) P_) package-description) | |
295 | 'package-description))) | |
7e1d2290 LC |
296 | |
297 | (define (inferior-package-home-page package) | |
298 | "Return the home page of PACKAGE." | |
299 | (inferior-package-field package 'package-home-page)) | |
300 | ||
301 | (define (inferior-package-location package) | |
302 | "Return the source code location of PACKAGE, either #f or a <location> | |
303 | record." | |
304 | (source-properties->location | |
305 | (inferior-package-field package | |
306 | '(compose (lambda (loc) | |
307 | (and loc | |
308 | (location->source-properties | |
309 | loc))) | |
310 | package-location)))) | |
9daf046c | 311 | |
6030396a LC |
312 | (define (inferior-package-input-field package field) |
313 | "Return the input field FIELD (e.g., 'native-inputs') of PACKAGE, an | |
314 | inferior package." | |
315 | (define field* | |
316 | `(compose (lambda (inputs) | |
317 | (map (match-lambda | |
318 | ;; XXX: Origins are not handled. | |
319 | ((label (? package? package) rest ...) | |
320 | (let ((id (object-address package))) | |
321 | (hashv-set! %package-table id package) | |
322 | `(,label (package ,id | |
323 | ,(package-name package) | |
324 | ,(package-version package)) | |
325 | ,@rest))) | |
326 | (x | |
327 | x)) | |
328 | inputs)) | |
329 | ,field)) | |
330 | ||
331 | (define inputs | |
332 | (inferior-package-field package field*)) | |
333 | ||
334 | (define inferior | |
335 | (inferior-package-inferior package)) | |
336 | ||
337 | (map (match-lambda | |
338 | ((label ('package id name version) . rest) | |
339 | ;; XXX: eq?-ness of inferior packages is not preserved here. | |
340 | `(,label ,(inferior-package inferior name version id) | |
341 | ,@rest)) | |
342 | (x x)) | |
343 | inputs)) | |
344 | ||
345 | (define inferior-package-inputs | |
346 | (cut inferior-package-input-field <> 'package-inputs)) | |
347 | ||
348 | (define inferior-package-native-inputs | |
349 | (cut inferior-package-input-field <> 'package-native-inputs)) | |
350 | ||
351 | (define inferior-package-propagated-inputs | |
352 | (cut inferior-package-input-field <> 'package-propagated-inputs)) | |
353 | ||
354 | (define inferior-package-transitive-propagated-inputs | |
355 | (cut inferior-package-input-field <> 'package-transitive-propagated-inputs)) | |
356 | ||
eee8b303 LC |
357 | (define (%inferior-package-search-paths package field) |
358 | "Return the list of search path specificiations of PACKAGE, an inferior | |
359 | package." | |
360 | (define paths | |
361 | (inferior-package-field package | |
362 | `(compose (lambda (paths) | |
363 | (map (@ (guix search-paths) | |
364 | search-path-specification->sexp) | |
365 | paths)) | |
366 | ,field))) | |
367 | ||
368 | (map sexp->search-path-specification paths)) | |
369 | ||
370 | (define inferior-package-native-search-paths | |
371 | (cut %inferior-package-search-paths <> 'package-native-search-paths)) | |
372 | ||
373 | (define inferior-package-search-paths | |
374 | (cut %inferior-package-search-paths <> 'package-search-paths)) | |
375 | ||
376 | (define inferior-package-transitive-native-search-paths | |
377 | (cut %inferior-package-search-paths <> 'package-transitive-native-search-paths)) | |
378 | ||
9daf046c LC |
379 | (define (proxy client backend) ;adapted from (guix ssh) |
380 | "Proxy communication between CLIENT and BACKEND until CLIENT closes the | |
381 | connection, at which point CLIENT is closed (both CLIENT and BACKEND must be | |
382 | input/output ports.)" | |
383 | (define (select* read write except) | |
384 | ;; This is a workaround for <https://bugs.gnu.org/30365> in Guile < 2.2.4: | |
385 | ;; since 'select' sometimes returns non-empty sets for no good reason, | |
386 | ;; call 'select' a second time with a zero timeout to filter out incorrect | |
387 | ;; replies. | |
388 | (match (select read write except) | |
389 | ((read write except) | |
390 | (select read write except 0)))) | |
391 | ||
392 | ;; Use buffered ports so that 'get-bytevector-some' returns up to the | |
393 | ;; whole buffer like read(2) would--see <https://bugs.gnu.org/30066>. | |
394 | (setvbuf client _IOFBF 65536) | |
395 | (setvbuf backend _IOFBF 65536) | |
396 | ||
397 | (let loop () | |
398 | (match (select* (list client backend) '() '()) | |
399 | ((reads () ()) | |
400 | (when (memq client reads) | |
401 | (match (get-bytevector-some client) | |
402 | ((? eof-object?) | |
403 | (close-port client)) | |
404 | (bv | |
405 | (put-bytevector backend bv) | |
406 | (force-output backend)))) | |
407 | (when (memq backend reads) | |
408 | (match (get-bytevector-some backend) | |
409 | (bv | |
410 | (put-bytevector client bv) | |
411 | (force-output client)))) | |
412 | (unless (port-closed? client) | |
413 | (loop)))))) | |
414 | ||
94c0e61f LC |
415 | (define (inferior-eval-with-store inferior store code) |
416 | "Evaluate CODE in INFERIOR, passing it STORE as its argument. CODE must | |
417 | thus be the code of a one-argument procedure that accepts a store." | |
418 | ;; Create a named socket in /tmp and let INFERIOR connect to it and use it | |
419 | ;; as its store. This ensures the inferior uses the same store, with the | |
420 | ;; same options, the same per-session GC roots, etc. | |
af15fe13 | 421 | ;; FIXME: This strategy doesn't work for remote inferiors (SSH). |
9daf046c LC |
422 | (call-with-temporary-directory |
423 | (lambda (directory) | |
424 | (chmod directory #o700) | |
425 | (let* ((name (string-append directory "/inferior")) | |
426 | (socket (socket AF_UNIX SOCK_STREAM 0)) | |
9daf046c LC |
427 | (major (nix-server-major-version store)) |
428 | (minor (nix-server-minor-version store)) | |
429 | (proto (logior major minor))) | |
430 | (bind socket AF_UNIX name) | |
431 | (listen socket 1024) | |
432 | (send-inferior-request | |
94c0e61f LC |
433 | `(let ((proc ,code) |
434 | (socket (socket AF_UNIX SOCK_STREAM 0))) | |
9daf046c LC |
435 | (connect socket AF_UNIX ,name) |
436 | ||
437 | ;; 'port->connection' appeared in June 2018 and we can hardly | |
438 | ;; emulate it on older versions. Thus fall back to | |
439 | ;; 'open-connection', at the risk of talking to the wrong daemon or | |
440 | ;; having our build result reclaimed (XXX). | |
94c0e61f LC |
441 | (let ((store (if (defined? 'port->connection) |
442 | (port->connection socket #:version ,proto) | |
443 | (open-connection)))) | |
444 | (dynamic-wind | |
445 | (const #t) | |
446 | (lambda () | |
447 | (proc store)) | |
448 | (lambda () | |
449 | (close-connection store) | |
450 | (close-port socket))))) | |
9daf046c LC |
451 | inferior) |
452 | (match (accept socket) | |
453 | ((client . address) | |
454 | (proxy client (nix-server-socket store)))) | |
455 | (close-port socket) | |
94c0e61f LC |
456 | (read-inferior-response inferior))))) |
457 | ||
458 | (define* (inferior-package-derivation store package | |
459 | #:optional | |
460 | (system (%current-system)) | |
461 | #:key target) | |
462 | "Return the derivation for PACKAGE, an inferior package, built for SYSTEM | |
463 | and cross-built for TARGET if TARGET is true. The inferior corresponding to | |
464 | PACKAGE must be live." | |
465 | (define proc | |
466 | `(lambda (store) | |
467 | (let* ((package (hashv-ref %package-table | |
468 | ,(inferior-package-id package))) | |
469 | (drv ,(if target | |
470 | `(package-cross-derivation store package | |
471 | ,target | |
472 | ,system) | |
473 | `(package-derivation store package | |
474 | ,system)))) | |
475 | (derivation-file-name drv)))) | |
476 | ||
477 | (and=> (inferior-eval-with-store (inferior-package-inferior package) store | |
478 | proc) | |
479 | read-derivation-from-file)) | |
9daf046c LC |
480 | |
481 | (define inferior-package->derivation | |
482 | (store-lift inferior-package-derivation)) | |
483 | ||
484 | (define-gexp-compiler (package-compiler (package <inferior-package>) system | |
485 | target) | |
486 | ;; Compile PACKAGE for SYSTEM, optionally cross-building for TARGET. | |
487 | (inferior-package->derivation package system #:target target)) | |
2e6d64e1 LC |
488 | |
489 | \f | |
490 | ;;; | |
491 | ;;; Manifest entries. | |
492 | ;;; | |
493 | ||
494 | (define* (inferior-package->manifest-entry package | |
495 | #:optional (output "out") | |
496 | #:key (parent (delay #f)) | |
497 | (properties '())) | |
498 | "Return a manifest entry for the OUTPUT of package PACKAGE." | |
499 | ;; For each dependency, keep a promise pointing to its "parent" entry. | |
500 | (letrec* ((deps (map (match-lambda | |
501 | ((label package) | |
502 | (inferior-package->manifest-entry package | |
503 | #:parent (delay entry))) | |
504 | ((label package output) | |
505 | (inferior-package->manifest-entry package output | |
506 | #:parent (delay entry)))) | |
507 | (inferior-package-propagated-inputs package))) | |
508 | (entry (manifest-entry | |
509 | (name (inferior-package-name package)) | |
510 | (version (inferior-package-version package)) | |
511 | (output output) | |
512 | (item package) | |
513 | (dependencies (delete-duplicates deps)) | |
514 | (search-paths | |
515 | (inferior-package-transitive-native-search-paths package)) | |
516 | (parent parent) | |
517 | (properties properties)))) | |
518 | entry)) | |
2dad0313 LC |
519 | |
520 | \f | |
521 | ;;; | |
522 | ;;; Cached inferiors. | |
523 | ;;; | |
524 | ||
525 | (define %inferior-cache-directory | |
526 | ;; Directory for cached inferiors (GC roots). | |
527 | (make-parameter (string-append (cache-directory #:ensure? #f) | |
528 | "/inferiors"))) | |
529 | ||
530 | (define* (inferior-for-channels channels | |
531 | #:key | |
532 | (cache-directory (%inferior-cache-directory)) | |
533 | (ttl (* 3600 24 30))) | |
534 | "Return an inferior for CHANNELS, a list of channels. Use the cache at | |
535 | CACHE-DIRECTORY, where entries can be reclaimed after TTL seconds. This | |
536 | procedure opens a new connection to the build daemon. | |
537 | ||
538 | This is a convenience procedure that people may use in manifests passed to | |
539 | 'guix package -m', for instance." | |
540 | (with-store store | |
541 | (let () | |
542 | (define instances | |
543 | (latest-channel-instances store channels)) | |
544 | ||
545 | (define key | |
546 | (bytevector->base32-string | |
547 | (sha256 | |
548 | (string->utf8 | |
549 | (string-concatenate (map channel-instance-commit instances)))))) | |
550 | ||
551 | (define cached | |
552 | (string-append cache-directory "/" key)) | |
553 | ||
554 | (define (base32-encoded-sha256? str) | |
555 | (= (string-length str) 52)) | |
556 | ||
557 | (define (cache-entries directory) | |
558 | (map (lambda (file) | |
559 | (string-append directory "/" file)) | |
560 | (scandir directory base32-encoded-sha256?))) | |
561 | ||
562 | (define symlink* | |
563 | (lift2 symlink %store-monad)) | |
564 | ||
565 | (define add-indirect-root* | |
566 | (store-lift add-indirect-root)) | |
567 | ||
568 | (mkdir-p cache-directory) | |
569 | (maybe-remove-expired-cache-entries cache-directory | |
570 | cache-entries | |
571 | #:entry-expiration | |
572 | (file-expiration-time ttl)) | |
573 | ||
574 | (if (file-exists? cached) | |
575 | (open-inferior cached) | |
576 | (run-with-store store | |
577 | (mlet %store-monad ((profile | |
578 | (channel-instances->derivation instances))) | |
579 | (mbegin %store-monad | |
580 | (show-what-to-build* (list profile)) | |
581 | (built-derivations (list profile)) | |
582 | (symlink* (derivation->output-path profile) cached) | |
583 | (add-indirect-root* cached) | |
584 | (return (open-inferior cached))))))))) |