inferior.scm 37 KB

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