inferior.scm 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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 (write-inferior inferior port)
  115. (match inferior
  116. (($ <inferior> pid _ _ version)
  117. (format port "#<inferior ~a ~a ~a>"
  118. pid version
  119. (number->string (object-address inferior) 16)))))
  120. (set-record-type-printer! <inferior> write-inferior)
  121. (define* (inferior-pipe directory command error-port)
  122. "Return an input/output pipe on the Guix instance in DIRECTORY. This runs
  123. 'DIRECTORY/COMMAND repl' if it exists, or falls back to some other method if
  124. it's an old Guix."
  125. (let ((pipe (with-error-to-port error-port
  126. (lambda ()
  127. (open-pipe* OPEN_BOTH
  128. (string-append directory "/" command)
  129. "repl" "-t" "machine")))))
  130. (if (eof-object? (peek-char pipe))
  131. (begin
  132. (close-pipe pipe)
  133. ;; Older versions of Guix didn't have a 'guix repl' command, so
  134. ;; emulate it.
  135. (with-error-to-port error-port
  136. (lambda ()
  137. (open-pipe* OPEN_BOTH "guile"
  138. "-L" (string-append directory "/share/guile/site/"
  139. (effective-version))
  140. "-C" (string-append directory "/share/guile/site/"
  141. (effective-version))
  142. "-C" (string-append directory "/lib/guile/"
  143. (effective-version) "/site-ccache")
  144. "-c"
  145. (object->string
  146. `(begin
  147. (primitive-load ,(search-path %load-path
  148. "guix/repl.scm"))
  149. ((@ (guix repl) machine-repl))))))))
  150. pipe)))
  151. (define* (port->inferior pipe #:optional (close close-port))
  152. "Given PIPE, an input/output port, return an inferior that talks over PIPE.
  153. PIPE is closed with CLOSE when 'close-inferior' is called on the returned
  154. inferior."
  155. (setvbuf pipe 'line)
  156. (match (read pipe)
  157. (('repl-version 0 rest ...)
  158. (letrec ((result (inferior 'pipe pipe close (cons 0 rest)
  159. (delay (%inferior-packages result))
  160. (delay (%inferior-package-table result)))))
  161. ;; For protocol (0 1) and later, send the protocol version we support.
  162. (match rest
  163. ((n _ ...)
  164. (when (>= n 1)
  165. (send-inferior-request '(() repl-version 0 1 1) result)))
  166. (_
  167. #t))
  168. (inferior-eval '(use-modules (guix)) result)
  169. (inferior-eval '(use-modules (gnu)) result)
  170. (inferior-eval '(use-modules (ice-9 match)) result)
  171. (inferior-eval '(use-modules (srfi srfi-34)) result)
  172. (inferior-eval '(define %package-table (make-hash-table))
  173. result)
  174. result))
  175. (_
  176. #f)))
  177. (define* (open-inferior directory
  178. #:key (command "bin/guix")
  179. (error-port (%make-void-port "w")))
  180. "Open the inferior Guix in DIRECTORY, running 'DIRECTORY/COMMAND repl' or
  181. equivalent. Return #f if the inferior could not be launched."
  182. (define pipe
  183. (inferior-pipe directory command error-port))
  184. (port->inferior pipe close-pipe))
  185. (define (close-inferior inferior)
  186. "Close INFERIOR."
  187. (let ((close (inferior-close-socket inferior)))
  188. (close (inferior-socket inferior))))
  189. ;; Non-self-quoting object of the inferior.
  190. (define-record-type <inferior-object>
  191. (inferior-object address appearance)
  192. inferior-object?
  193. (address inferior-object-address)
  194. (appearance inferior-object-appearance))
  195. (define (write-inferior-object object port)
  196. (match object
  197. (($ <inferior-object> _ appearance)
  198. (format port "#<inferior-object ~a>" appearance))))
  199. (set-record-type-printer! <inferior-object> write-inferior-object)
  200. ;; Reified exception thrown by an inferior.
  201. (define-condition-type &inferior-exception &error
  202. inferior-exception?
  203. (arguments inferior-exception-arguments) ;key + arguments
  204. (inferior inferior-exception-inferior) ;<inferior> | #f
  205. (stack inferior-exception-stack)) ;list of (FILE COLUMN LINE)
  206. (define* (read-repl-response port #:optional inferior)
  207. "Read a (guix repl) response from PORT and return it as a Scheme object.
  208. Raise '&inferior-exception' when an exception is read from PORT."
  209. (define sexp->object
  210. (match-lambda
  211. (('value value)
  212. value)
  213. (('non-self-quoting address string)
  214. (inferior-object address string))))
  215. (match (read port)
  216. (('values objects ...)
  217. (apply values (map sexp->object objects)))
  218. (('exception ('arguments key objects ...)
  219. ('stack frames ...))
  220. ;; Protocol (0 1 1) and later.
  221. (raise (condition (&inferior-exception
  222. (arguments (cons key (map sexp->object objects)))
  223. (inferior inferior)
  224. (stack frames)))))
  225. (('exception key objects ...)
  226. ;; Protocol (0 0).
  227. (raise (condition (&inferior-exception
  228. (arguments (cons key (map sexp->object objects)))
  229. (inferior inferior)
  230. (stack '())))))))
  231. (define (read-inferior-response inferior)
  232. (read-repl-response (inferior-socket inferior)
  233. inferior))
  234. (define (send-inferior-request exp inferior)
  235. (write exp (inferior-socket inferior))
  236. (newline (inferior-socket inferior)))
  237. (define (inferior-eval exp inferior)
  238. "Evaluate EXP in INFERIOR."
  239. (send-inferior-request exp inferior)
  240. (read-inferior-response inferior))
  241. ;;;
  242. ;;; Inferior packages.
  243. ;;;
  244. (define-record-type <inferior-package>
  245. (inferior-package inferior name version id)
  246. inferior-package?
  247. (inferior inferior-package-inferior)
  248. (name inferior-package-name)
  249. (version inferior-package-version)
  250. (id inferior-package-id))
  251. (define (write-inferior-package package port)
  252. (match package
  253. (($ <inferior-package> _ name version)
  254. (format port "#<inferior-package ~a@~a ~a>"
  255. name version
  256. (number->string (object-address package) 16)))))
  257. (set-record-type-printer! <inferior-package> write-inferior-package)
  258. (define (%inferior-packages inferior)
  259. "Compute the list of inferior packages from INFERIOR."
  260. (let ((result (inferior-eval
  261. '(fold-packages (lambda (package result)
  262. (let ((id (object-address package)))
  263. (hashv-set! %package-table id package)
  264. (cons (list (package-name package)
  265. (package-version package)
  266. id)
  267. result)))
  268. '())
  269. inferior)))
  270. (map (match-lambda
  271. ((name version id)
  272. (inferior-package inferior name version id)))
  273. result)))
  274. (define (inferior-packages inferior)
  275. "Return the list of packages known to INFERIOR."
  276. (force (inferior-package-promise inferior)))
  277. (define (%inferior-package-table inferior)
  278. "Compute a package lookup table for INFERIOR."
  279. (fold (lambda (package table)
  280. (vhash-cons (inferior-package-name package) package
  281. table))
  282. vlist-null
  283. (inferior-packages inferior)))
  284. (define (inferior-available-packages inferior)
  285. "Return the list of name/version pairs corresponding to the set of packages
  286. available in INFERIOR.
  287. This is faster and less resource-intensive than calling 'inferior-packages'."
  288. (if (inferior-eval '(defined? 'fold-available-packages)
  289. inferior)
  290. (inferior-eval '(fold-available-packages
  291. (lambda* (name version result
  292. #:key supported? deprecated?
  293. #:allow-other-keys)
  294. (if (and supported? (not deprecated?))
  295. (acons name version result)
  296. result))
  297. '())
  298. inferior)
  299. ;; As a last resort, if INFERIOR is old and lacks
  300. ;; 'fold-available-packages', fall back to 'inferior-packages'.
  301. (map (lambda (package)
  302. (cons (inferior-package-name package)
  303. (inferior-package-version package)))
  304. (inferior-packages inferior))))
  305. (define* (lookup-inferior-packages inferior name #:optional version)
  306. "Return the sorted list of inferior packages matching NAME in INFERIOR, with
  307. highest version numbers first. If VERSION is true, return only packages with
  308. a version number prefixed by VERSION."
  309. ;; This is the counterpart of 'find-packages-by-name'.
  310. (sort (filter (lambda (package)
  311. (or (not version)
  312. (version-prefix? version
  313. (inferior-package-version package))))
  314. (vhash-fold* cons '() name
  315. (force (inferior-package-table inferior))))
  316. (lambda (p1 p2)
  317. (version>? (inferior-package-version p1)
  318. (inferior-package-version p2)))))
  319. (define (inferior-package-field package getter)
  320. "Return the field of PACKAGE, an inferior package, accessed with GETTER."
  321. (let ((inferior (inferior-package-inferior package))
  322. (id (inferior-package-id package)))
  323. (inferior-eval `(,getter (hashv-ref %package-table ,id))
  324. inferior)))
  325. (define* (inferior-package-synopsis package #:key (translate? #t))
  326. "Return the Texinfo synopsis 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-synopsis)
  331. 'package-synopsis)))
  332. (define* (inferior-package-description package #:key (translate? #t))
  333. "Return the Texinfo description of PACKAGE, an inferior package. When
  334. TRANSLATE? is true, translate it to the current locale's language."
  335. (inferior-package-field package
  336. (if translate?
  337. '(compose (@ (guix ui) P_) package-description)
  338. 'package-description)))
  339. (define (inferior-package-home-page package)
  340. "Return the home page of PACKAGE."
  341. (inferior-package-field package 'package-home-page))
  342. (define (inferior-package-location package)
  343. "Return the source code location of PACKAGE, either #f or a <location>
  344. record."
  345. (source-properties->location
  346. (inferior-package-field package
  347. '(compose (lambda (loc)
  348. (and loc
  349. (location->source-properties
  350. loc)))
  351. package-location))))
  352. (define (inferior-package-input-field package field)
  353. "Return the input field FIELD (e.g., 'native-inputs') of PACKAGE, an
  354. inferior package."
  355. (define field*
  356. `(compose (lambda (inputs)
  357. (map (match-lambda
  358. ;; XXX: Origins are not handled.
  359. ((label (? package? package) rest ...)
  360. (let ((id (object-address package)))
  361. (hashv-set! %package-table id package)
  362. `(,label (package ,id
  363. ,(package-name package)
  364. ,(package-version package))
  365. ,@rest)))
  366. (x
  367. x))
  368. inputs))
  369. ,field))
  370. (define inputs
  371. (inferior-package-field package field*))
  372. (define inferior
  373. (inferior-package-inferior package))
  374. (map (match-lambda
  375. ((label ('package id name version) . rest)
  376. ;; XXX: eq?-ness of inferior packages is not preserved here.
  377. `(,label ,(inferior-package inferior name version id)
  378. ,@rest))
  379. (x x))
  380. inputs))
  381. (define inferior-package-inputs
  382. (cut inferior-package-input-field <> 'package-inputs))
  383. (define inferior-package-native-inputs
  384. (cut inferior-package-input-field <> 'package-native-inputs))
  385. (define inferior-package-propagated-inputs
  386. (cut inferior-package-input-field <> 'package-propagated-inputs))
  387. (define inferior-package-transitive-propagated-inputs
  388. (cut inferior-package-input-field <> 'package-transitive-propagated-inputs))
  389. (define (%inferior-package-search-paths package field)
  390. "Return the list of search path specifications of PACKAGE, an inferior
  391. package."
  392. (define paths
  393. (inferior-package-field package
  394. `(compose (lambda (paths)
  395. (map (@ (guix search-paths)
  396. search-path-specification->sexp)
  397. paths))
  398. ,field)))
  399. (map sexp->search-path-specification paths))
  400. (define inferior-package-native-search-paths
  401. (cut %inferior-package-search-paths <> 'package-native-search-paths))
  402. (define inferior-package-search-paths
  403. (cut %inferior-package-search-paths <> 'package-search-paths))
  404. (define inferior-package-transitive-native-search-paths
  405. (cut %inferior-package-search-paths <> 'package-transitive-native-search-paths))
  406. (define (inferior-package-provenance package)
  407. "Return a \"provenance sexp\" for PACKAGE, an inferior package. The result
  408. is similar to the sexp returned by 'package-provenance' for regular packages."
  409. (inferior-package-field package
  410. '(let* ((describe
  411. (false-if-exception
  412. (resolve-interface '(guix describe))))
  413. (provenance
  414. (false-if-exception
  415. (module-ref describe
  416. 'package-provenance))))
  417. (or provenance (const #f)))))
  418. (define (proxy client backend) ;adapted from (guix ssh)
  419. "Proxy communication between CLIENT and BACKEND until CLIENT closes the
  420. connection, at which point CLIENT is closed (both CLIENT and BACKEND must be
  421. input/output ports.)"
  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 (properties '()))
  576. "Return a manifest entry for the OUTPUT of package PACKAGE."
  577. (define cache
  578. (make-hash-table))
  579. (define-syntax-rule (memoized package output exp)
  580. ;; Memoize the entry returned by EXP for PACKAGE/OUTPUT. This is
  581. ;; important as the same package may be traversed many times through
  582. ;; propagated inputs, and querying the inferior is costly. Use
  583. ;; 'hash'/'equal?', which is okay since <inferior-package> is simple.
  584. (let ((compute (lambda () exp))
  585. (key (cons package output)))
  586. (or (hash-ref cache key)
  587. (let ((result (compute)))
  588. (hash-set! cache key result)
  589. result))))
  590. (let loop ((package package)
  591. (output output)
  592. (parent (delay #f)))
  593. (memoized package output
  594. ;; For each dependency, keep a promise pointing to its "parent" entry.
  595. (letrec* ((deps (map (match-lambda
  596. ((label package)
  597. (loop package "out" (delay entry)))
  598. ((label package output)
  599. (loop package output (delay entry))))
  600. (inferior-package-propagated-inputs package)))
  601. (entry (manifest-entry
  602. (name (inferior-package-name package))
  603. (version (inferior-package-version package))
  604. (output output)
  605. (item package)
  606. (dependencies (delete-duplicates deps))
  607. (search-paths
  608. (inferior-package-transitive-native-search-paths package))
  609. (parent parent)
  610. (properties properties))))
  611. entry))))
  612. ;;;
  613. ;;; Cached inferiors.
  614. ;;;
  615. (define %inferior-cache-directory
  616. ;; Directory for cached inferiors (GC roots).
  617. (make-parameter (string-append (cache-directory #:ensure? #f)
  618. "/inferiors")))
  619. (define (channel-full-commit channel)
  620. "Return the commit designated by CHANNEL as quickly as possible. If
  621. CHANNEL's 'commit' field is a full SHA1, return it as-is; if it's a SHA1
  622. prefix, resolve it; and if 'commit' is unset, fetch CHANNEL's branch tip."
  623. (let ((commit (channel-commit channel))
  624. (branch (channel-branch channel)))
  625. (if (and commit (= (string-length commit) 40))
  626. commit
  627. (let* ((ref (if commit `(commit . ,commit) `(branch . ,branch)))
  628. (cache commit relation
  629. (update-cached-checkout (channel-url channel)
  630. #:ref ref
  631. #:check-out? #f)))
  632. commit))))
  633. (define* (cached-channel-instance store
  634. channels
  635. #:key
  636. (authenticate? #t)
  637. (cache-directory (%inferior-cache-directory))
  638. (ttl (* 3600 24 30)))
  639. "Return a directory containing a guix filetree defined by CHANNELS, a list of channels.
  640. The directory is a subdirectory of CACHE-DIRECTORY, where entries can be reclaimed after TTL seconds.
  641. This procedure opens a new connection to the build daemon. AUTHENTICATE?
  642. determines whether CHANNELS are authenticated."
  643. (define commits
  644. ;; Since computing the instances of CHANNELS is I/O-intensive, use a
  645. ;; cheaper way to get the commit list of CHANNELS. This limits overhead
  646. ;; to the minimum in case of a cache hit.
  647. (map channel-full-commit channels))
  648. (define key
  649. (bytevector->base32-string
  650. (sha256
  651. (string->utf8 (string-concatenate commits)))))
  652. (define cached
  653. (string-append cache-directory "/" key))
  654. (define (base32-encoded-sha256? str)
  655. (= (string-length str) 52))
  656. (define (cache-entries directory)
  657. (map (lambda (file)
  658. (string-append directory "/" file))
  659. (scandir directory base32-encoded-sha256?)))
  660. (define (symlink/safe old new)
  661. (catch 'system-error
  662. (lambda ()
  663. (symlink old new))
  664. (lambda args
  665. (unless (= EEXIST (system-error-errno args))
  666. (apply throw args)))))
  667. (define symlink*
  668. (lift2 symlink/safe %store-monad))
  669. (define add-indirect-root*
  670. (store-lift add-indirect-root))
  671. (mkdir-p cache-directory)
  672. (maybe-remove-expired-cache-entries cache-directory
  673. cache-entries
  674. #:entry-expiration
  675. (file-expiration-time ttl))
  676. (if (file-exists? cached)
  677. cached
  678. (run-with-store store
  679. (mlet* %store-monad ((instances
  680. -> (latest-channel-instances store channels
  681. #:authenticate?
  682. authenticate?))
  683. (profile
  684. (channel-instances->derivation instances)))
  685. (mbegin %store-monad
  686. (show-what-to-build* (list profile))
  687. (built-derivations (list profile))
  688. ;; Note: Caching is fine even when AUTHENTICATE? is false because
  689. ;; we always call 'latest-channel-instances?'.
  690. (symlink* (derivation->output-path profile) cached)
  691. (add-indirect-root* cached)
  692. (return cached))))))
  693. (define* (inferior-for-channels channels
  694. #:key
  695. (cache-directory (%inferior-cache-directory))
  696. (ttl (* 3600 24 30)))
  697. "Return an inferior for CHANNELS, a list of channels. Use the cache at
  698. CACHE-DIRECTORY, where entries can be reclaimed after TTL seconds. This
  699. procedure opens a new connection to the build daemon.
  700. This is a convenience procedure that people may use in manifests passed to
  701. 'guix package -m', for instance."
  702. (define cached
  703. (with-store store
  704. (cached-channel-instance store
  705. channels
  706. #:cache-directory cache-directory
  707. #:ttl ttl)))
  708. (open-inferior cached))
  709. ;;; Local Variables:
  710. ;;; eval: (put 'memoized 'scheme-indent-function 1)
  711. ;;; End: