inferior.scm 39 KB

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