inferior.scm 30 KB

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