inferior.scm 28 KB

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