inferior.scm 33 KB

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