inferior.scm 37 KB

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