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