inferior.scm 31 KB

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