You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2050 lines
79 KiB

  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2018 Jan Nieuwenhuizen <janneke@gnu.org>
  4. ;;; Copyright © 2019, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
  5. ;;; Copyright © 2020 Florian Pelz <pelzflorian@pelzflorian.de>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (guix store)
  22. #:use-module (guix utils)
  23. #:use-module (guix config)
  24. #:use-module (guix deprecation)
  25. #:use-module (guix memoization)
  26. #:use-module (guix serialization)
  27. #:use-module (guix monads)
  28. #:use-module (guix records)
  29. #:use-module (guix base16)
  30. #:use-module (guix base32)
  31. #:use-module (gcrypt hash)
  32. #:use-module (guix profiling)
  33. #:autoload (guix build syscalls) (terminal-columns)
  34. #:use-module (rnrs bytevectors)
  35. #:use-module (ice-9 binary-ports)
  36. #:use-module ((ice-9 control) #:select (let/ec))
  37. #:use-module (srfi srfi-1)
  38. #:use-module (srfi srfi-9)
  39. #:use-module (srfi srfi-9 gnu)
  40. #:use-module (srfi srfi-11)
  41. #:use-module (srfi srfi-26)
  42. #:use-module (srfi srfi-34)
  43. #:use-module (srfi srfi-35)
  44. #:use-module (srfi srfi-39)
  45. #:use-module (ice-9 match)
  46. #:use-module (ice-9 vlist)
  47. #:use-module (ice-9 popen)
  48. #:use-module (ice-9 threads)
  49. #:use-module (ice-9 format)
  50. #:use-module (web uri)
  51. #:export (%daemon-socket-uri
  52. %gc-roots-directory
  53. %default-substitute-urls
  54. store-connection?
  55. store-connection-version
  56. store-connection-major-version
  57. store-connection-minor-version
  58. store-connection-socket
  59. ;; Deprecated forms for 'store-connection'.
  60. nix-server?
  61. nix-server-version
  62. nix-server-major-version
  63. nix-server-minor-version
  64. nix-server-socket
  65. current-store-protocol-version ;for internal use
  66. mcached
  67. &store-error store-error?
  68. &store-connection-error store-connection-error?
  69. store-connection-error-file
  70. store-connection-error-code
  71. &store-protocol-error store-protocol-error?
  72. store-protocol-error-message
  73. store-protocol-error-status
  74. ;; Deprecated forms for '&store-error' et al.
  75. &nix-error nix-error?
  76. &nix-connection-error nix-connection-error?
  77. nix-connection-error-file
  78. nix-connection-error-code
  79. &nix-protocol-error nix-protocol-error?
  80. nix-protocol-error-message
  81. nix-protocol-error-status
  82. hash-algo
  83. build-mode
  84. open-connection
  85. port->connection
  86. close-connection
  87. with-store
  88. set-build-options
  89. set-build-options*
  90. valid-path?
  91. query-path-hash
  92. hash-part->path
  93. query-path-info
  94. add-data-to-store
  95. add-text-to-store
  96. add-to-store
  97. add-file-tree-to-store
  98. binary-file
  99. with-build-handler
  100. build-things
  101. build
  102. query-failed-paths
  103. clear-failed-paths
  104. add-temp-root
  105. add-indirect-root
  106. add-permanent-root
  107. remove-permanent-root
  108. substitutable?
  109. substitutable-path
  110. substitutable-deriver
  111. substitutable-references
  112. substitutable-download-size
  113. substitutable-nar-size
  114. has-substitutes?
  115. substitutable-paths
  116. substitutable-path-info
  117. path-info?
  118. path-info-deriver
  119. path-info-hash
  120. path-info-references
  121. path-info-registration-time
  122. path-info-nar-size
  123. built-in-builders
  124. references
  125. references/substitutes
  126. references*
  127. query-path-info*
  128. requisites
  129. referrers
  130. optimize-store
  131. verify-store
  132. topologically-sorted
  133. valid-derivers
  134. query-derivation-outputs
  135. live-paths
  136. dead-paths
  137. collect-garbage
  138. delete-paths
  139. import-paths
  140. export-paths
  141. current-build-output-port
  142. %store-monad
  143. store-bind
  144. store-return
  145. store-lift
  146. store-lower
  147. run-with-store
  148. %guile-for-build
  149. current-system
  150. set-current-system
  151. current-target-system
  152. set-current-target
  153. text-file
  154. interned-file
  155. interned-file-tree
  156. %store-prefix
  157. store-path
  158. output-path
  159. fixed-output-path
  160. store-path?
  161. direct-store-path?
  162. derivation-path?
  163. store-path-base
  164. store-path-package-name
  165. store-path-hash-part
  166. direct-store-path
  167. derivation-log-file
  168. log-file))
  169. (define %protocol-version #x163)
  170. (define %worker-magic-1 #x6e697863) ; "nixc"
  171. (define %worker-magic-2 #x6478696f) ; "dxio"
  172. (define (protocol-major magic)
  173. (logand magic #xff00))
  174. (define (protocol-minor magic)
  175. (logand magic #x00ff))
  176. (define (protocol-version major minor)
  177. (logior major minor))
  178. (define-syntax define-enumerate-type
  179. (syntax-rules ()
  180. ((_ name->int (name id) ...)
  181. (define-syntax name->int
  182. (syntax-rules (name ...)
  183. ((_ name) id) ...)))))
  184. (define-enumerate-type operation-id
  185. ;; operation numbers from worker-protocol.hh
  186. (quit 0)
  187. (valid-path? 1)
  188. (has-substitutes? 3)
  189. (query-path-hash 4)
  190. (query-references 5)
  191. (query-referrers 6)
  192. (add-to-store 7)
  193. (add-text-to-store 8)
  194. (build-things 9)
  195. (ensure-path 10)
  196. (add-temp-root 11)
  197. (add-indirect-root 12)
  198. (sync-with-gc 13)
  199. (find-roots 14)
  200. (export-path 16)
  201. (query-deriver 18)
  202. (set-options 19)
  203. (collect-garbage 20)
  204. ;;(query-substitutable-path-info 21) ; obsolete as of #x10c
  205. (query-derivation-outputs 22)
  206. (query-all-valid-paths 23)
  207. (query-failed-paths 24)
  208. (clear-failed-paths 25)
  209. (query-path-info 26)
  210. (import-paths 27)
  211. (query-derivation-output-names 28)
  212. (query-path-from-hash-part 29)
  213. (query-substitutable-path-infos 30)
  214. (query-valid-paths 31)
  215. (query-substitutable-paths 32)
  216. (query-valid-derivers 33)
  217. (optimize-store 34)
  218. (verify-store 35)
  219. (built-in-builders 80))
  220. (define-enumerate-type hash-algo
  221. ;; hash.hh
  222. (md5 1)
  223. (sha1 2)
  224. (sha256 3))
  225. (define-enumerate-type build-mode
  226. ;; store-api.hh
  227. (normal 0)
  228. (repair 1)
  229. (check 2))
  230. (define-enumerate-type gc-action
  231. ;; store-api.hh
  232. (return-live 0)
  233. (return-dead 1)
  234. (delete-dead 2)
  235. (delete-specific 3))
  236. (define %default-socket-path
  237. (string-append %state-directory "/daemon-socket/socket"))
  238. (define %daemon-socket-uri
  239. ;; URI or file name of the socket the daemon listens too.
  240. (make-parameter (or (getenv "GUIX_DAEMON_SOCKET")
  241. %default-socket-path)))
  242. ;; Information about a substitutable store path.
  243. (define-record-type <substitutable>
  244. (substitutable path deriver refs dl-size nar-size)
  245. substitutable?
  246. (path substitutable-path)
  247. (deriver substitutable-deriver)
  248. (refs substitutable-references)
  249. (dl-size substitutable-download-size)
  250. (nar-size substitutable-nar-size))
  251. (define (read-substitutable-path-list p)
  252. (let loop ((len (read-int p))
  253. (result '()))
  254. (if (zero? len)
  255. (reverse result)
  256. (let ((path (read-store-path p))
  257. (deriver (read-store-path p))
  258. (refs (read-store-path-list p))
  259. (dl-size (read-long-long p))
  260. (nar-size (read-long-long p)))
  261. (loop (- len 1)
  262. (cons (substitutable path deriver refs dl-size nar-size)
  263. result))))))
  264. ;; Information about a store path.
  265. (define-record-type <path-info>
  266. (path-info deriver hash references registration-time nar-size)
  267. path-info?
  268. (deriver path-info-deriver) ;string | #f
  269. (hash path-info-hash)
  270. (references path-info-references)
  271. (registration-time path-info-registration-time)
  272. (nar-size path-info-nar-size))
  273. (define (read-path-info p)
  274. (let ((deriver (match (read-store-path p)
  275. ("" #f)
  276. (x x)))
  277. (hash (base16-string->bytevector (read-string p)))
  278. (refs (read-store-path-list p))
  279. (registration-time (read-int p))
  280. (nar-size (read-long-long p)))
  281. (path-info deriver hash refs registration-time nar-size)))
  282. (define-syntax write-arg
  283. (syntax-rules (integer boolean bytevector
  284. string string-list string-pairs
  285. store-path store-path-list base16)
  286. ((_ integer arg p)
  287. (write-int arg p))
  288. ((_ boolean arg p)
  289. (write-int (if arg 1 0) p))
  290. ((_ bytevector arg p)
  291. (write-bytevector arg p))
  292. ((_ string arg p)
  293. (write-string arg p))
  294. ((_ string-list arg p)
  295. (write-string-list arg p))
  296. ((_ string-pairs arg p)
  297. (write-string-pairs arg p))
  298. ((_ store-path arg p)
  299. (write-store-path arg p))
  300. ((_ store-path-list arg p)
  301. (write-store-path-list arg p))
  302. ((_ base16 arg p)
  303. (write-string (bytevector->base16-string arg) p))))
  304. (define-syntax read-arg
  305. (syntax-rules (integer boolean string store-path store-path-list string-list
  306. substitutable-path-list path-info base16)
  307. ((_ integer p)
  308. (read-int p))
  309. ((_ boolean p)
  310. (not (zero? (read-int p))))
  311. ((_ string p)
  312. (read-string p))
  313. ((_ store-path p)
  314. (read-store-path p))
  315. ((_ store-path-list p)
  316. (read-store-path-list p))
  317. ((_ string-list p)
  318. (read-string-list p))
  319. ((_ substitutable-path-list p)
  320. (read-substitutable-path-list p))
  321. ((_ path-info p)
  322. (read-path-info p))
  323. ((_ base16 p)
  324. (base16-string->bytevector (read-string p)))))
  325. ;; remote-store.cc
  326. (define-record-type* <store-connection> store-connection %make-store-connection
  327. store-connection?
  328. (socket store-connection-socket)
  329. (major store-connection-major-version)
  330. (minor store-connection-minor-version)
  331. (buffer store-connection-output-port) ;output port
  332. (flush store-connection-flush-output) ;thunk
  333. ;; Caches. We keep them per-connection, because store paths build
  334. ;; during the session are temporary GC roots kept for the duration of
  335. ;; the session.
  336. (ats-cache store-connection-add-to-store-cache)
  337. (atts-cache store-connection-add-text-to-store-cache)
  338. (object-cache store-connection-object-cache
  339. (default vlist-null)) ;vhash
  340. (built-in-builders store-connection-built-in-builders
  341. (default (delay '())))) ;promise
  342. (set-record-type-printer! <store-connection>
  343. (lambda (obj port)
  344. (format port "#<store-connection ~a.~a ~a>"
  345. (store-connection-major-version obj)
  346. (store-connection-minor-version obj)
  347. (number->string (object-address obj)
  348. 16))))
  349. (define-deprecated/alias nix-server? store-connection?)
  350. (define-deprecated/alias nix-server-major-version
  351. store-connection-major-version)
  352. (define-deprecated/alias nix-server-minor-version
  353. store-connection-minor-version)
  354. (define-deprecated/alias nix-server-socket store-connection-socket)
  355. (define-condition-type &store-error &error
  356. store-error?)
  357. (define-condition-type &store-connection-error &store-error
  358. store-connection-error?
  359. (file store-connection-error-file)
  360. (errno store-connection-error-code))
  361. (define-condition-type &store-protocol-error &store-error
  362. store-protocol-error?
  363. (message store-protocol-error-message)
  364. (status store-protocol-error-status))
  365. (define-deprecated/alias &nix-error &store-error)
  366. (define-deprecated/alias nix-error? store-error?)
  367. (define-deprecated/alias &nix-connection-error &store-connection-error)
  368. (define-deprecated/alias nix-connection-error? store-connection-error?)
  369. (define-deprecated/alias nix-connection-error-file
  370. store-connection-error-file)
  371. (define-deprecated/alias nix-connection-error-code
  372. store-connection-error-code)
  373. (define-deprecated/alias &nix-protocol-error &store-protocol-error)
  374. (define-deprecated/alias nix-protocol-error? store-protocol-error?)
  375. (define-deprecated/alias nix-protocol-error-message
  376. store-protocol-error-message)
  377. (define-deprecated/alias nix-protocol-error-status
  378. store-protocol-error-status)
  379. (define-syntax-rule (system-error-to-connection-error file exp ...)
  380. "Catch 'system-error' exceptions and translate them to
  381. '&store-connection-error'."
  382. (catch 'system-error
  383. (lambda ()
  384. exp ...)
  385. (lambda args
  386. (let ((errno (system-error-errno args)))
  387. (raise (condition (&store-connection-error
  388. (file file)
  389. (errno errno))))))))
  390. (define (open-unix-domain-socket file)
  391. "Connect to the Unix-domain socket at FILE and return it. Raise a
  392. '&store-connection-error' upon error."
  393. (let ((s (with-fluids ((%default-port-encoding #f))
  394. ;; This trick allows use of the `scm_c_read' optimization.
  395. (socket PF_UNIX SOCK_STREAM 0)))
  396. (a (make-socket-address PF_UNIX file)))
  397. (system-error-to-connection-error file
  398. (connect s a)
  399. s)))
  400. (define %default-guix-port
  401. ;; Default port when connecting to a daemon over TCP/IP.
  402. 44146)
  403. (define (open-inet-socket host port)
  404. "Connect to the Unix-domain socket at HOST:PORT and return it. Raise a
  405. '&store-connection-error' upon error."
  406. (let ((sock (with-fluids ((%default-port-encoding #f))
  407. ;; This trick allows use of the `scm_c_read' optimization.
  408. (socket PF_UNIX SOCK_STREAM 0))))
  409. (define addresses
  410. (getaddrinfo host
  411. (if (number? port) (number->string port) port)
  412. (if (number? port)
  413. (logior AI_ADDRCONFIG AI_NUMERICSERV)
  414. AI_ADDRCONFIG)
  415. 0 ;any address family
  416. SOCK_STREAM)) ;TCP only
  417. (let loop ((addresses addresses))
  418. (match addresses
  419. ((ai rest ...)
  420. (let ((s (socket (addrinfo:fam ai)
  421. ;; TCP/IP only
  422. SOCK_STREAM IPPROTO_IP)))
  423. (catch 'system-error
  424. (lambda ()
  425. (connect s (addrinfo:addr ai))
  426. ;; Setting this option makes a dramatic difference because it
  427. ;; avoids the "ACK delay" on our RPC messages.
  428. (setsockopt s IPPROTO_TCP TCP_NODELAY 1)
  429. s)
  430. (lambda args
  431. ;; Connection failed, so try one of the other addresses.
  432. (close s)
  433. (if (null? rest)
  434. (raise (condition (&store-connection-error
  435. (file host)
  436. (errno (system-error-errno args)))))
  437. (loop rest))))))))))
  438. (define (connect-to-daemon uri)
  439. "Connect to the daemon at URI, a string that may be an actual URI or a file
  440. name."
  441. (define (not-supported)
  442. (raise (condition (&store-connection-error
  443. (file uri)
  444. (errno ENOTSUP)))))
  445. (define connect
  446. (match (string->uri uri)
  447. (#f ;URI is a file name
  448. open-unix-domain-socket)
  449. ((? uri? uri)
  450. (match (uri-scheme uri)
  451. ((or #f 'file 'unix)
  452. (lambda (_)
  453. (open-unix-domain-socket (uri-path uri))))
  454. ('guix
  455. (lambda (_)
  456. (open-inet-socket (uri-host uri)
  457. (or (uri-port uri) %default-guix-port))))
  458. ((? symbol? scheme)
  459. ;; Try to dynamically load a module for SCHEME.
  460. ;; XXX: Errors are swallowed.
  461. (match (false-if-exception
  462. (resolve-interface `(guix store ,scheme)))
  463. ((? module? module)
  464. (match (false-if-exception
  465. (module-ref module 'connect-to-daemon))
  466. ((? procedure? connect)
  467. (lambda (_)
  468. (connect uri)))
  469. (x (not-supported))))
  470. (#f (not-supported))))
  471. (x
  472. (not-supported))))))
  473. (connect uri))
  474. (define* (open-connection #:optional (uri (%daemon-socket-uri))
  475. #:key port (reserve-space? #t) cpu-affinity)
  476. "Connect to the daemon at URI (a string), or, if PORT is not #f, use it as
  477. the I/O port over which to communicate to a build daemon.
  478. When RESERVE-SPACE? is true, instruct it to reserve a little bit of extra
  479. space on the file system so that the garbage collector can still operate,
  480. should the disk become full. When CPU-AFFINITY is true, it must be an integer
  481. corresponding to an OS-level CPU number to which the daemon's worker process
  482. for this connection will be pinned. Return a server object."
  483. (guard (c ((nar-error? c)
  484. ;; One of the 'write-' or 'read-' calls below failed, but this is
  485. ;; really a connection error.
  486. (raise (condition
  487. (&store-connection-error (file (or port uri))
  488. (errno EPROTO))
  489. (&message (message "build daemon handshake failed"))))))
  490. (let*-values (((port)
  491. (or port (connect-to-daemon uri)))
  492. ((output flush)
  493. (buffering-output-port port
  494. (make-bytevector 8192))))
  495. (write-int %worker-magic-1 port)
  496. (let ((r (read-int port)))
  497. (and (eqv? r %worker-magic-2)
  498. (let ((v (read-int port)))
  499. (and (eqv? (protocol-major %protocol-version)
  500. (protocol-major v))
  501. (begin
  502. (write-int %protocol-version port)
  503. (when (>= (protocol-minor v) 14)
  504. (write-int (if cpu-affinity 1 0) port)
  505. (when cpu-affinity
  506. (write-int cpu-affinity port)))
  507. (when (>= (protocol-minor v) 11)
  508. (write-int (if reserve-space? 1 0) port))
  509. (letrec* ((built-in-builders
  510. (delay (%built-in-builders conn)))
  511. (conn
  512. (%make-store-connection port
  513. (protocol-major v)
  514. (protocol-minor v)
  515. output flush
  516. (make-hash-table 100)
  517. (make-hash-table 100)
  518. vlist-null
  519. built-in-builders)))
  520. (let loop ((done? (process-stderr conn)))
  521. (or done? (process-stderr conn)))
  522. conn)))))))))
  523. (define* (port->connection port
  524. #:key (version %protocol-version))
  525. "Assimilate PORT, an input/output port, and return a connection to the
  526. daemon, assuming the given protocol VERSION.
  527. Warning: this procedure assumes that the initial handshake with the daemon has
  528. already taken place on PORT and that we're just continuing on this established
  529. connection. Use with care."
  530. (let-values (((output flush)
  531. (buffering-output-port port (make-bytevector 8192))))
  532. (define connection
  533. (%make-store-connection port
  534. (protocol-major version)
  535. (protocol-minor version)
  536. output flush
  537. (make-hash-table 100)
  538. (make-hash-table 100)
  539. vlist-null
  540. (delay (%built-in-builders connection))))
  541. connection))
  542. (define (store-connection-version store)
  543. "Return the protocol version of STORE as an integer."
  544. (protocol-version (store-connection-major-version store)
  545. (store-connection-minor-version store)))
  546. (define-deprecated/alias nix-server-version store-connection-version)
  547. (define (write-buffered-output server)
  548. "Flush SERVER's output port."
  549. (force-output (store-connection-output-port server))
  550. ((store-connection-flush-output server)))
  551. (define (close-connection server)
  552. "Close the connection to SERVER."
  553. (close (store-connection-socket server)))
  554. (define (call-with-store proc)
  555. "Call PROC with an open store connection."
  556. (let ((store (open-connection)))
  557. (dynamic-wind
  558. (const #f)
  559. (lambda ()
  560. (parameterize ((current-store-protocol-version
  561. (store-connection-version store)))
  562. (proc store)))
  563. (lambda ()
  564. (false-if-exception (close-connection store))))))
  565. (define-syntax-rule (with-store store exp ...)
  566. "Bind STORE to an open connection to the store and evaluate EXPs;
  567. automatically close the store when the dynamic extent of EXP is left."
  568. (call-with-store (lambda (store) exp ...)))
  569. (define current-store-protocol-version
  570. ;; Protocol version of the store currently used. XXX: This is a hack to
  571. ;; communicate the protocol version to the build output port. It's a hack
  572. ;; because it could be inaccurrate, for instance if there's code that
  573. ;; manipulates several store connections at once; it works well for the
  574. ;; purposes of (guix status) though.
  575. (make-parameter #f))
  576. (define current-build-output-port
  577. ;; The port where build output is sent.
  578. (make-parameter (current-error-port)))
  579. (define* (dump-port in out
  580. #:optional len
  581. #:key (buffer-size 16384))
  582. "Read LEN bytes from IN (or as much as possible if LEN is #f) and write it
  583. to OUT, using chunks of BUFFER-SIZE bytes."
  584. (define buffer
  585. (make-bytevector buffer-size))
  586. (let loop ((total 0)
  587. (bytes (get-bytevector-n! in buffer 0
  588. (if len
  589. (min len buffer-size)
  590. buffer-size))))
  591. (or (eof-object? bytes)
  592. (and len (= total len))
  593. (let ((total (+ total bytes)))
  594. (put-bytevector out buffer 0 bytes)
  595. (loop total
  596. (get-bytevector-n! in buffer 0
  597. (if len
  598. (min (- len total) buffer-size)
  599. buffer-size)))))))
  600. (define %newlines
  601. ;; Newline characters triggering a flush of 'current-build-output-port'.
  602. ;; Unlike Guile's 'line, we flush upon #\return so that progress reports
  603. ;; that use that trick are correctly displayed.
  604. (char-set #\newline #\return))
  605. (define* (process-stderr server #:optional user-port)
  606. "Read standard output and standard error from SERVER, writing it to
  607. CURRENT-BUILD-OUTPUT-PORT. Return #t when SERVER is done sending data, and
  608. #f otherwise; in the latter case, the caller should call `process-stderr'
  609. again until #t is returned or an error is raised.
  610. Since the build process's output cannot be assumed to be UTF-8, we
  611. conservatively consider it to be Latin-1, thereby avoiding possible
  612. encoding conversion errors."
  613. (define p
  614. (store-connection-socket server))
  615. ;; magic cookies from worker-protocol.hh
  616. (define %stderr-next #x6f6c6d67) ; "olmg", build log
  617. (define %stderr-read #x64617461) ; "data", data needed from source
  618. (define %stderr-write #x64617416) ; "dat\x16", data for sink
  619. (define %stderr-last #x616c7473) ; "alts", we're done
  620. (define %stderr-error #x63787470) ; "cxtp", error reporting
  621. (let ((k (read-int p)))
  622. (cond ((= k %stderr-write)
  623. ;; Write a byte stream to USER-PORT.
  624. (let* ((len (read-int p))
  625. (m (modulo len 8)))
  626. (dump-port p user-port len
  627. #:buffer-size (if (<= len 16384) 16384 65536))
  628. (unless (zero? m)
  629. ;; Consume padding, as for strings.
  630. (get-bytevector-n p (- 8 m))))
  631. #f)
  632. ((= k %stderr-read)
  633. ;; Read a byte stream from USER-PORT.
  634. ;; Note: Avoid 'get-bytevector-n' to work around
  635. ;; <http://bugs.gnu.org/17591> in Guile up to 2.0.11.
  636. (let* ((max-len (read-int p))
  637. (data (make-bytevector max-len))
  638. (len (get-bytevector-n! user-port data 0 max-len)))
  639. (write-bytevector data p len)
  640. #f))
  641. ((= k %stderr-next)
  642. ;; Log a string. Build logs are usually UTF-8-encoded, but they
  643. ;; may also contain arbitrary byte sequences that should not cause
  644. ;; this to fail. Thus, use the permissive
  645. ;; 'read-maybe-utf8-string'.
  646. (let ((s (read-maybe-utf8-string p)))
  647. (display s (current-build-output-port))
  648. (when (string-any %newlines s)
  649. (force-output (current-build-output-port)))
  650. #f))
  651. ((= k %stderr-error)
  652. ;; Report an error.
  653. (let ((error (read-maybe-utf8-string p))
  654. ;; Currently the daemon fails to send a status code for early
  655. ;; errors like DB schema version mismatches, so check for EOF.
  656. (status (if (and (>= (store-connection-minor-version server) 8)
  657. (not (eof-object? (lookahead-u8 p))))
  658. (read-int p)
  659. 1)))
  660. (raise (condition (&store-protocol-error
  661. (message error)
  662. (status status))))))
  663. ((= k %stderr-last)
  664. ;; The daemon is done (see `stopWork' in `nix-worker.cc'.)
  665. #t)
  666. (else
  667. (raise (condition (&store-protocol-error
  668. (message "invalid error code")
  669. (status k))))))))
  670. (define %default-substitute-urls
  671. ;; Default list of substituters. This is *not* the list baked in
  672. ;; 'guix-daemon', but it is used by 'guix-service-type' and and a couple of
  673. ;; clients ('guix build --log-file' uses it.)
  674. (map (if (false-if-exception (resolve-interface '(gnutls)))
  675. (cut string-append "https://" <>)
  676. (cut string-append "http://" <>))
  677. '("ci.guix.gnu.org")))
  678. (define (current-user-name)
  679. "Return the name of the calling user."
  680. (catch #t
  681. (lambda ()
  682. (passwd:name (getpwuid (getuid))))
  683. (lambda _
  684. (getenv "USER"))))
  685. (define* (set-build-options server
  686. #:key keep-failed? keep-going? fallback?
  687. (verbosity 0)
  688. rounds ;number of build rounds
  689. max-build-jobs
  690. timeout
  691. max-silent-time
  692. (offload? #t)
  693. (use-build-hook? *unspecified*) ;deprecated
  694. (build-verbosity 0)
  695. (log-type 0)
  696. (print-build-trace #t)
  697. (user-name (current-user-name))
  698. ;; When true, provide machine-readable "build
  699. ;; traces" for use by (guix status). Old clients
  700. ;; are unable to make sense, which is why it's
  701. ;; disabled by default.
  702. print-extended-build-trace?
  703. ;; When true, the daemon prefixes builder output
  704. ;; with "@ build-log" traces so we can
  705. ;; distinguish it from daemon output, and we can
  706. ;; distinguish each builder's output
  707. ;; (PRINT-BUILD-TRACE must be true as well.) The
  708. ;; latter is particularly useful when
  709. ;; MAX-BUILD-JOBS > 1.
  710. multiplexed-build-output?
  711. build-cores
  712. (use-substitutes? #t)
  713. ;; Client-provided substitute URLs. If it is #f,
  714. ;; the daemon's settings are used. Otherwise, it
  715. ;; overrides the daemons settings; see 'guix
  716. ;; substitute'.
  717. (substitute-urls #f)
  718. ;; Number of columns in the client's terminal.
  719. (terminal-columns (terminal-columns))
  720. ;; Locale of the client.
  721. (locale (false-if-exception (setlocale LC_ALL))))
  722. ;; Must be called after `open-connection'.
  723. (define socket
  724. (store-connection-socket server))
  725. (unless (unspecified? use-build-hook?)
  726. (warn-about-deprecation #:use-build-hook? #f
  727. #:replacement #:offload?))
  728. (let-syntax ((send (syntax-rules ()
  729. ((_ (type option) ...)
  730. (begin
  731. (write-arg type option socket)
  732. ...)))))
  733. (write-int (operation-id set-options) socket)
  734. (send (boolean keep-failed?) (boolean keep-going?)
  735. (boolean fallback?) (integer verbosity))
  736. (when (< (store-connection-minor-version server) #x61)
  737. (let ((max-build-jobs (or max-build-jobs 1))
  738. (max-silent-time (or max-silent-time 3600)))
  739. (send (integer max-build-jobs) (integer max-silent-time))))
  740. (when (>= (store-connection-minor-version server) 2)
  741. (send (boolean (if (unspecified? use-build-hook?)
  742. offload?
  743. use-build-hook?))))
  744. (when (>= (store-connection-minor-version server) 4)
  745. (send (integer build-verbosity) (integer log-type)
  746. (boolean print-build-trace)))
  747. (when (and (>= (store-connection-minor-version server) 6)
  748. (< (store-connection-minor-version server) #x61))
  749. (let ((build-cores (or build-cores (current-processor-count))))
  750. (send (integer build-cores))))
  751. (when (>= (store-connection-minor-version server) 10)
  752. (send (boolean use-substitutes?)))
  753. (when (>= (store-connection-minor-version server) 12)
  754. (let ((pairs `(;; This option is honored by 'guix substitute' et al.
  755. ,@(if print-build-trace
  756. `(("print-extended-build-trace"
  757. . ,(if print-extended-build-trace? "1" "0")))
  758. '())
  759. ,@(if multiplexed-build-output?
  760. `(("multiplexed-build-output"
  761. . ,(if multiplexed-build-output? "true" "false")))
  762. '())
  763. ,@(if timeout
  764. `(("build-timeout" . ,(number->string timeout)))
  765. '())
  766. ,@(if max-silent-time
  767. `(("build-max-silent-time"
  768. . ,(number->string max-silent-time)))
  769. '())
  770. ,@(if max-build-jobs
  771. `(("build-max-jobs"
  772. . ,(number->string max-build-jobs)))
  773. '())
  774. ,@(if build-cores
  775. `(("build-cores" . ,(number->string build-cores)))
  776. '())
  777. ,@(if substitute-urls
  778. `(("substitute-urls"
  779. . ,(string-join substitute-urls)))
  780. '())
  781. ,@(if rounds
  782. `(("build-repeat"
  783. . ,(number->string (max 0 (1- rounds)))))
  784. '())
  785. ,@(if user-name
  786. `(("user-name" . ,user-name))
  787. '())
  788. ,@(if terminal-columns
  789. `(("terminal-columns"
  790. . ,(number->string terminal-columns)))
  791. '())
  792. ,@(if locale
  793. `(("locale" . ,locale))
  794. '()))))
  795. (send (string-pairs pairs))))
  796. (let loop ((done? (process-stderr server)))
  797. (or done? (process-stderr server)))))
  798. (define (buffering-output-port port buffer)
  799. "Return two value: an output port wrapped around PORT that uses BUFFER (a
  800. bytevector) as its internal buffer, and a thunk to flush this output port."
  801. ;; Note: In Guile 2.2.2, custom binary output ports already have their own
  802. ;; 4K internal buffer.
  803. (define size
  804. (bytevector-length buffer))
  805. (define total 0)
  806. (define (flush)
  807. (put-bytevector port buffer 0 total)
  808. (force-output port)
  809. (set! total 0))
  810. (define (write bv offset count)
  811. (if (zero? count) ;end of file
  812. (flush)
  813. (let loop ((offset offset)
  814. (count count)
  815. (written 0))
  816. (cond ((= total size)
  817. (flush)
  818. (loop offset count written))
  819. ((zero? count)
  820. written)
  821. (else
  822. (let ((to-copy (min count (- size total))))
  823. (bytevector-copy! bv offset buffer total to-copy)
  824. (set! total (+ total to-copy))
  825. (loop (+ offset to-copy) (- count to-copy)
  826. (+ written to-copy))))))))
  827. ;; Note: We need to return FLUSH because the custom binary port has no way
  828. ;; to be notified of a 'force-output' call on itself.
  829. (values (make-custom-binary-output-port "buffering-output-port"
  830. write #f #f flush)
  831. flush))
  832. (define profiled?
  833. (let ((profiled
  834. (or (and=> (getenv "GUIX_PROFILING") string-tokenize)
  835. '())))
  836. (lambda (component)
  837. "Return true if COMPONENT profiling is active."
  838. (member component profiled))))
  839. (define %rpc-calls
  840. ;; Mapping from RPC names (symbols) to invocation counts.
  841. (make-hash-table))
  842. (define* (show-rpc-profile #:optional (port (current-error-port)))
  843. "Write to PORT a summary of the RPCs that have been made."
  844. (let ((profile (sort (hash-fold alist-cons '() %rpc-calls)
  845. (lambda (rpc1 rpc2)
  846. (< (cdr rpc1) (cdr rpc2))))))
  847. (format port "Remote procedure call summary: ~a RPCs~%"
  848. (match profile
  849. (((names . counts) ...)
  850. (reduce + 0 counts))))
  851. (for-each (match-lambda
  852. ((rpc . count)
  853. (format port " ~30a ... ~5@a~%" rpc count)))
  854. profile)))
  855. (define record-operation
  856. ;; Optionally, increment the number of calls of the given RPC.
  857. (if (profiled? "rpc")
  858. (begin
  859. (register-profiling-hook! "rpc" show-rpc-profile)
  860. (lambda (name)
  861. (let ((count (or (hashq-ref %rpc-calls name) 0)))
  862. (hashq-set! %rpc-calls name (+ count 1)))))
  863. (lambda (_)
  864. #t)))
  865. (define-syntax operation
  866. (syntax-rules ()
  867. "Define a client-side RPC stub for the given operation."
  868. ((_ (name (type arg) ...) docstring return ...)
  869. (lambda (server arg ...)
  870. docstring
  871. (let* ((s (store-connection-socket server))
  872. (buffered (store-connection-output-port server)))
  873. (record-operation 'name)
  874. (write-int (operation-id name) buffered)
  875. (write-arg type arg buffered)
  876. ...
  877. (write-buffered-output server)
  878. ;; Loop until the server is done sending error output.
  879. (let loop ((done? (process-stderr server)))
  880. (or done? (loop (process-stderr server))))
  881. (values (read-arg return s) ...))))))
  882. (define-syntax-rule (define-operation (name args ...)
  883. docstring return ...)
  884. (define name
  885. (operation (name args ...) docstring return ...)))
  886. (define-operation (valid-path? (string path))
  887. "Return #t when PATH designates a valid store item and #f otherwise (an
  888. invalid item may exist on disk but still be invalid, for instance because it
  889. is the result of an aborted or failed build.)
  890. A '&store-protocol-error' condition is raised if PATH is not prefixed by the
  891. store directory (/gnu/store)."
  892. boolean)
  893. (define-operation (query-path-hash (store-path path))
  894. "Return the SHA256 hash of the nar serialization of PATH as a bytevector."
  895. base16)
  896. (define hash-part->path
  897. (let ((query-path-from-hash-part
  898. (operation (query-path-from-hash-part (string hash))
  899. #f
  900. store-path)))
  901. (lambda (server hash-part)
  902. "Return the store path whose hash part is HASH-PART (a nix-base32
  903. string). Return the empty string if no such path exists."
  904. ;; This RPC is primarily used by Hydra to reply to HTTP GETs of
  905. ;; /HASH.narinfo.
  906. (query-path-from-hash-part server hash-part))))
  907. (define-operation (query-path-info (store-path path))
  908. "Return the info (hash, references, etc.) for PATH."
  909. path-info)
  910. (define add-data-to-store
  911. ;; A memoizing version of `add-to-store', to avoid repeated RPCs with
  912. ;; the very same arguments during a given session.
  913. (let ((add-text-to-store
  914. (operation (add-text-to-store (string name) (bytevector text)
  915. (string-list references))
  916. #f
  917. store-path))
  918. (lookup (if (profiled? "add-data-to-store-cache")
  919. (let ((lookups 0)
  920. (hits 0)
  921. (drv 0)
  922. (scheme 0))
  923. (define (show-stats)
  924. (define (% n)
  925. (if (zero? lookups)
  926. 100.
  927. (* 100. (/ n lookups))))
  928. (format (current-error-port) "
  929. 'add-data-to-store' cache:
  930. lookups: ~5@a
  931. hits: ~5@a (~,1f%)
  932. .drv files: ~5@a (~,1f%)
  933. Scheme files: ~5@a (~,1f%)~%"
  934. lookups hits (% hits)
  935. drv (% drv)
  936. scheme (% scheme)))
  937. (register-profiling-hook! "add-data-to-store-cache"
  938. show-stats)
  939. (lambda (cache args)
  940. (let ((result (hash-ref cache args)))
  941. (set! lookups (+ 1 lookups))
  942. (when result
  943. (set! hits (+ 1 hits)))
  944. (match args
  945. ((_ name _)
  946. (cond ((string-suffix? ".drv" name)
  947. (set! drv (+ drv 1)))
  948. ((string-suffix? "-builder" name)
  949. (set! scheme (+ scheme 1)))
  950. ((string-suffix? ".scm" name)
  951. (set! scheme (+ scheme 1))))))
  952. result)))
  953. hash-ref)))
  954. (lambda* (server name bytes #:optional (references '()))
  955. "Add BYTES under file NAME in the store, and return its store path.
  956. REFERENCES is the list of store paths referred to by the resulting store
  957. path."
  958. (let* ((args `(,bytes ,name ,references))
  959. (cache (store-connection-add-text-to-store-cache server)))
  960. (or (lookup cache args)
  961. (let ((path (add-text-to-store server name bytes references)))
  962. (hash-set! cache args path)
  963. path))))))
  964. (define* (add-text-to-store store name text #:optional (references '()))
  965. "Add TEXT under file NAME in the store, and return its store path.
  966. REFERENCES is the list of store paths referred to by the resulting store
  967. path."
  968. (add-data-to-store store name (string->utf8 text) references))
  969. (define true
  970. ;; Define it once and for all since we use it as a default value for
  971. ;; 'add-to-store' and want to make sure two default values are 'eq?' for the
  972. ;; purposes or memoization.
  973. (lambda (file stat)
  974. #t))
  975. (define add-to-store
  976. ;; A memoizing version of `add-to-store'. This is important because
  977. ;; `add-to-store' leads to huge data transfers to the server, and
  978. ;; because it's often called many times with the very same argument.
  979. (let ((add-to-store
  980. (lambda* (server basename recursive? hash-algo file-name
  981. #:key (select? true))
  982. ;; We don't use the 'operation' macro so we can pass SELECT? to
  983. ;; 'write-file'.
  984. (record-operation 'add-to-store)
  985. (let ((port (store-connection-socket server)))
  986. (write-int (operation-id add-to-store) port)
  987. (write-string basename port)
  988. (write-int 1 port) ;obsolete, must be #t
  989. (write-int (if recursive? 1 0) port)
  990. (write-string hash-algo port)
  991. (write-file file-name port #:select? select?)
  992. (write-buffered-output server)
  993. (let loop ((done? (process-stderr server)))
  994. (or done? (loop (process-stderr server))))
  995. (read-store-path port)))))
  996. (lambda* (server basename recursive? hash-algo file-name
  997. #:key (select? true))
  998. "Add the contents of FILE-NAME under BASENAME to the store. When
  999. RECURSIVE? is false, FILE-NAME must designate a regular file--not a directory
  1000. nor a symlink. When RECURSIVE? is true and FILE-NAME designates a directory,
  1001. the contents of FILE-NAME are added recursively; if FILE-NAME designates a
  1002. flat file and RECURSIVE? is true, its contents are added, and its permission
  1003. bits are kept. HASH-ALGO must be a string such as \"sha256\".
  1004. When RECURSIVE? is true, call (SELECT? FILE STAT) for each directory entry,
  1005. where FILE is the entry's absolute file name and STAT is the result of
  1006. 'lstat'; exclude entries for which SELECT? does not return true."
  1007. ;; Note: We don't stat FILE-NAME at each call, and thus we assume that
  1008. ;; the file remains unchanged for the lifetime of SERVER.
  1009. (let* ((args `(,file-name ,basename ,recursive? ,hash-algo ,select?))
  1010. (cache (store-connection-add-to-store-cache server)))
  1011. (or (hash-ref cache args)
  1012. (let ((path (add-to-store server basename recursive?
  1013. hash-algo file-name
  1014. #:select? select?)))
  1015. (hash-set! cache args path)
  1016. path))))))
  1017. (define %not-slash
  1018. (char-set-complement (char-set #\/)))
  1019. (define* (add-file-tree-to-store server tree
  1020. #:key
  1021. (hash-algo "sha256")
  1022. (recursive? #t))
  1023. "Add the given TREE to the store on SERVER. TREE must be an entry such as:
  1024. (\"my-tree\" directory
  1025. (\"a\" regular (data \"hello\"))
  1026. (\"b\" symlink \"a\")
  1027. (\"c\" directory
  1028. (\"d\" executable (file \"/bin/sh\"))))
  1029. This is a generalized version of 'add-to-store'. It allows you to reproduce
  1030. an arbitrary directory layout in the store without creating a derivation."
  1031. ;; Note: The format of TREE was chosen to allow trees to be compared with
  1032. ;; 'equal?', which in turn allows us to memoize things.
  1033. (define root
  1034. ;; TREE is a single entry.
  1035. (list tree))
  1036. (define basename
  1037. (match tree
  1038. ((name . _) name)))
  1039. (define (lookup file)
  1040. (let loop ((components (string-tokenize file %not-slash))
  1041. (tree root))
  1042. (match components
  1043. ((basename)
  1044. (assoc basename tree))
  1045. ((head . rest)
  1046. (loop rest
  1047. (match (assoc-ref tree head)
  1048. (('directory . entries) entries)))))))
  1049. (define (file-type+size file)
  1050. (match (lookup file)
  1051. ((_ (and type (or 'directory 'symlink)) . _)
  1052. (values type 0))
  1053. ((_ type ('file file))
  1054. (values type (stat:size (stat file))))
  1055. ((_ type ('data (? string? data)))
  1056. (values type (string-length data)))
  1057. ((_ type ('data (? bytevector? data)))
  1058. (values type (bytevector-length data)))))
  1059. (define (file-port file)
  1060. (match (lookup file)
  1061. ((_ (or 'regular 'executable) content)
  1062. (match content
  1063. (('file (? string? file))
  1064. (open-file file "r0b"))
  1065. (('data (? string? str))
  1066. (open-input-string str))
  1067. (('data (? bytevector? bv))
  1068. (open-bytevector-input-port bv))))))
  1069. (define (symlink-target file)
  1070. (match (lookup file)
  1071. ((_ 'symlink target) target)))
  1072. (define (directory-entries directory)
  1073. (match (lookup directory)
  1074. ((_ 'directory (names . _) ...) names)))
  1075. (define cache
  1076. (store-connection-add-to-store-cache server))
  1077. (or (hash-ref cache tree)
  1078. (begin
  1079. ;; We don't use the 'operation' macro so we can use 'write-file-tree'
  1080. ;; instead of 'write-file'.
  1081. (record-operation 'add-to-store/tree)
  1082. (let ((port (store-connection-socket server)))
  1083. (write-int (operation-id add-to-store) port)
  1084. (write-string basename port)
  1085. (write-int 1 port) ;obsolete, must be #t
  1086. (write-int (if recursive? 1 0) port)
  1087. (write-string hash-algo port)
  1088. (write-file-tree basename port
  1089. #:file-type+size file-type+size
  1090. #:file-port file-port
  1091. #:symlink-target symlink-target
  1092. #:directory-entries directory-entries)
  1093. (write-buffered-output server)
  1094. (let loop ((done? (process-stderr server)))
  1095. (or done? (loop (process-stderr server))))
  1096. (let ((result (read-store-path port)))
  1097. (hash-set! cache tree result)
  1098. result)))))
  1099. (define current-build-prompt
  1100. ;; When true, this is the prompt to abort to when 'build-things' is called.
  1101. (make-parameter #f))
  1102. (define (call-with-build-handler handler thunk)
  1103. "Register HANDLER as a \"build handler\" and invoke THUNK."
  1104. (define tag
  1105. (make-prompt-tag "build handler"))
  1106. (parameterize ((current-build-prompt tag))
  1107. (call-with-prompt tag
  1108. thunk
  1109. (lambda (k . args)
  1110. ;; Since HANDLER may call K, which in turn may call 'build-things'
  1111. ;; again, reinstate a prompt (thus, it's not a tail call.)
  1112. (call-with-build-handler handler
  1113. (lambda ()
  1114. (apply handler k args)))))))
  1115. (define (invoke-build-handler store things mode)
  1116. "Abort to 'current-build-prompt' if it is set."
  1117. (or (not (current-build-prompt))
  1118. (abort-to-prompt (current-build-prompt) store things mode)))
  1119. (define-syntax-rule (with-build-handler handler exp ...)
  1120. "Register HANDLER as a \"build handler\" and invoke THUNK. When
  1121. 'build-things' is called within the dynamic extent of the call to THUNK,
  1122. HANDLER is invoked like so:
  1123. (HANDLER CONTINUE STORE THINGS MODE)
  1124. where CONTINUE is the continuation, and the remaining arguments are those that
  1125. were passed to 'build-things'.
  1126. Build handlers are useful to announce a build plan with 'show-what-to-build'
  1127. and to implement dry runs (by not invoking CONTINUE) in a way that gracefully
  1128. deals with \"dynamic dependencies\" such as grafts---derivations that depend
  1129. on the build output of a previous derivation."
  1130. (call-with-build-handler handler (lambda () exp ...)))
  1131. (define build-things
  1132. (let ((build (operation (build-things (string-list things)
  1133. (integer mode))
  1134. "Do it!"
  1135. boolean))
  1136. (build/old (operation (build-things (string-list things))
  1137. "Do it!"
  1138. boolean)))
  1139. (lambda* (store things #:optional (mode (build-mode normal)))
  1140. "Build THINGS, a list of store items which may be either '.drv' files or
  1141. outputs, and return when the worker is done building them. Elements of THINGS
  1142. that are not derivations can only be substituted and not built locally.
  1143. Alternately, an element of THING can be a derivation/output name pair, in
  1144. which case the daemon will attempt to substitute just the requested output of
  1145. the derivation. Return #t on success.
  1146. When a handler is installed with 'with-build-handler', it is called any time
  1147. 'build-things' is called."
  1148. (or (not (invoke-build-handler store things mode))
  1149. (let ((things (map (match-lambda
  1150. ((drv . output) (string-append drv "!" output))
  1151. (thing thing))
  1152. things)))
  1153. (parameterize ((current-store-protocol-version
  1154. (store-connection-version store)))
  1155. (if (>= (store-connection-minor-version store) 15)
  1156. (build store things mode)
  1157. (if (= mode (build-mode normal))
  1158. (build/old store things)
  1159. (raise (condition (&store-protocol-error
  1160. (message "unsupported build mode")
  1161. (status 1))))))))))))
  1162. (define-operation (add-temp-root (store-path path))
  1163. "Make PATH a temporary root for the duration of the current session.
  1164. Return #t."
  1165. boolean)
  1166. (define-operation (add-indirect-root (string file-name))
  1167. "Make the symlink FILE-NAME an indirect root for the garbage collector:
  1168. whatever store item FILE-NAME points to will not be collected. Return #t on
  1169. success.
  1170. FILE-NAME can be anywhere on the file system, but it must be an absolute file
  1171. name--it is the caller's responsibility to ensure that it is an absolute file
  1172. name."
  1173. boolean)
  1174. (define %gc-roots-directory
  1175. ;; The place where garbage collector roots (symlinks) are kept.
  1176. (string-append %state-directory "/gcroots"))
  1177. (define (add-permanent-root target)
  1178. "Add a garbage collector root pointing to TARGET, an element of the store,
  1179. preventing TARGET from even being collected. This can also be used if TARGET
  1180. does not exist yet.
  1181. Raise an error if the caller does not have write access to the GC root
  1182. directory."
  1183. (let* ((root (string-append %gc-roots-directory "/" (basename target))))
  1184. (catch 'system-error
  1185. (lambda ()
  1186. (symlink target root))
  1187. (lambda args
  1188. ;; If ROOT already exists, this is fine; otherwise, re-throw.
  1189. (unless (= EEXIST (system-error-errno args))
  1190. (apply throw args))))))
  1191. (define (remove-permanent-root target)
  1192. "Remove the permanent garbage collector root pointing to TARGET. Raise an
  1193. error if there is no such root."
  1194. (delete-file (string-append %gc-roots-directory "/" (basename target))))
  1195. (define references
  1196. (operation (query-references (store-path path))
  1197. "Return the list of references of PATH."
  1198. store-path-list))
  1199. (define %reference-cache
  1200. ;; Brute-force cache mapping store items to their list of references.
  1201. ;; Caching matters because when building a profile in the presence of
  1202. ;; grafts, we keep calling 'graft-derivation', which in turn calls
  1203. ;; 'references/substitutes' many times with the same arguments. Ideally we
  1204. ;; would use a cache associated with the daemon connection instead (XXX).
  1205. (make-hash-table 100))
  1206. (define (references/substitutes store items)
  1207. "Return the list of list of references of ITEMS; the result has the same
  1208. length as ITEMS. Query substitute information for any item missing from the
  1209. store at once. Raise a '&store-protocol-error' exception if reference
  1210. information for one of ITEMS is missing."
  1211. (let* ((requested items)
  1212. (local-refs (map (lambda (item)
  1213. (or (hash-ref %reference-cache item)
  1214. (guard (c ((store-protocol-error? c) #f))
  1215. (references store item))))
  1216. items))
  1217. (missing (fold-right (lambda (item local-ref result)
  1218. (if local-ref
  1219. result
  1220. (cons item result)))
  1221. '()
  1222. items local-refs))
  1223. ;; Query all the substitutes at once to minimize the cost of
  1224. ;; launching 'guix substitute' and making HTTP requests.
  1225. (substs (if (null? missing)
  1226. '()
  1227. (substitutable-path-info store missing))))
  1228. (when (< (length substs) (length missing))
  1229. (raise (condition (&store-protocol-error
  1230. (message "cannot determine \
  1231. the list of references")
  1232. (status 1)))))
  1233. ;; Intersperse SUBSTS and LOCAL-REFS.
  1234. (let loop ((items items)
  1235. (local-refs local-refs)
  1236. (result '()))
  1237. (match items
  1238. (()
  1239. (let ((result (reverse result)))
  1240. (for-each (cut hash-set! %reference-cache <> <>)
  1241. requested result)
  1242. result))
  1243. ((item items ...)
  1244. (match local-refs
  1245. ((#f tail ...)
  1246. (loop items tail
  1247. (cons (any (lambda (subst)
  1248. (and (string=? (substitutable-path subst) item)
  1249. (substitutable-references subst)))
  1250. substs)
  1251. result)))
  1252. ((head tail ...)
  1253. (loop items tail
  1254. (cons head result)))))))))
  1255. (define* (fold-path store proc seed paths
  1256. #:optional (relatives (cut references store <>)))
  1257. "Call PROC for each of the RELATIVES of PATHS, exactly once, and return the
  1258. result formed from the successive calls to PROC, the first of which is passed
  1259. SEED."
  1260. (let loop ((paths paths)
  1261. (result seed)
  1262. (seen vlist-null))
  1263. (match paths
  1264. ((path rest ...)
  1265. (if (vhash-assoc path seen)
  1266. (loop rest result seen)
  1267. (let ((seen (vhash-cons path #t seen))
  1268. (rest (append rest (relatives path)))
  1269. (result (proc path result)))
  1270. (loop rest result seen))))
  1271. (()
  1272. result))))
  1273. (define (requisites store paths)
  1274. "Return the requisites of PATHS, including PATHS---i.e., their closures (all
  1275. its references, recursively)."
  1276. (fold-path store cons '() paths))
  1277. (define (topologically-sorted store paths)
  1278. "Return a list containing PATHS and all their references sorted in
  1279. topological order."
  1280. (define (traverse)
  1281. ;; Do a simple depth-first traversal of all of PATHS.
  1282. (let loop ((paths paths)
  1283. (visited vlist-null)
  1284. (result '()))
  1285. (define (visit n)
  1286. (vhash-cons n #t visited))
  1287. (define (visited? n)
  1288. (vhash-assoc n visited))
  1289. (match paths
  1290. ((head tail ...)
  1291. (if (visited? head)
  1292. (loop tail visited result)
  1293. (call-with-values
  1294. (lambda ()
  1295. (loop (references store head)
  1296. (visit head)
  1297. result))
  1298. (lambda (visited result)
  1299. (loop tail
  1300. visited
  1301. (cons head result))))))
  1302. (()
  1303. (values visited result)))))
  1304. (call-with-values traverse
  1305. (lambda (_ result)
  1306. (reverse result))))
  1307. (define referrers
  1308. (operation (query-referrers (store-path path))
  1309. "Return the list of path that refer to PATH."
  1310. store-path-list))
  1311. (define valid-derivers
  1312. (operation (query-valid-derivers (store-path path))
  1313. "Return the list of valid \"derivers\" of PATH---i.e., all the
  1314. .drv present in the store that have PATH among their outputs."
  1315. store-path-list))
  1316. (define query-derivation-outputs ; avoid name clash with `derivation-outputs'
  1317. (operation (query-derivation-outputs (store-path path))
  1318. "Return the list of outputs of PATH, a .drv file."
  1319. store-path-list))
  1320. (define-operation (has-substitutes? (store-path path))
  1321. "Return #t if binary substitutes are available for PATH, and #f otherwise."
  1322. boolean)
  1323. (define substitutable-paths
  1324. (operation (query-substitutable-paths (store-path-list paths))
  1325. "Return the subset of PATHS that is substitutable."
  1326. store-path-list))
  1327. (define substitutable-path-info
  1328. (operation (query-substitutable-path-infos (store-path-list paths))
  1329. "Return information about the subset of PATHS that is
  1330. substitutable. For each substitutable path, a `substitutable?' object is
  1331. returned; thus, the resulting list can be shorter than PATHS. Furthermore,
  1332. that there is no guarantee that the order of the resulting list matches the
  1333. order of PATHS."
  1334. substitutable-path-list))
  1335. (define %built-in-builders
  1336. (let ((builders (operation (built-in-builders)
  1337. "Return the built-in builders."
  1338. string-list)))
  1339. (lambda (store)
  1340. "Return the names of the supported built-in derivation builders
  1341. supported by STORE. The result is memoized for STORE."
  1342. ;; Check whether STORE's version supports this RPC and built-in
  1343. ;; derivation builders in general, which appeared in Guix > 0.11.0.
  1344. ;; Return the empty list if it doesn't. Note that this RPC does not
  1345. ;; exist in 'nix-daemon'.
  1346. (if (or (> (store-connection-major-version store) #x100)
  1347. (and (= (store-connection-major-version store) #x100)
  1348. (>= (store-connection-minor-version store) #x60)))
  1349. (builders store)
  1350. '()))))
  1351. (define (built-in-builders store)
  1352. "Return the names of the supported built-in derivation builders
  1353. supported by STORE."
  1354. (force (store-connection-built-in-builders store)))
  1355. (define-operation (optimize-store)
  1356. "Optimize the store by hard-linking identical files (\"deduplication\".)
  1357. Return #t on success."
  1358. ;; Note: the daemon in Guix <= 0.8.2 does not implement this RPC.
  1359. boolean)
  1360. (define verify-store
  1361. (let ((verify (operation (verify-store (boolean check-contents?)
  1362. (boolean repair?))
  1363. "Verify the store."
  1364. boolean)))
  1365. (lambda* (store #:key check-contents? repair?)
  1366. "Verify the integrity of the store and return false if errors remain,
  1367. and true otherwise. When REPAIR? is true, repair any missing or altered store
  1368. items by substituting them (this typically requires root privileges because it
  1369. is not an atomic operation.) When CHECK-CONTENTS? is true, check the contents
  1370. of store items; this can take a lot of time."
  1371. (not (verify store check-contents? repair?)))))
  1372. (define (run-gc server action to-delete min-freed)
  1373. "Perform the garbage-collector operation ACTION, one of the
  1374. `gc-action' values. When ACTION is `delete-specific', the TO-DELETE is
  1375. the list of store paths to delete. IGNORE-LIVENESS? should always be
  1376. #f. MIN-FREED is the minimum amount of disk space to be freed, in
  1377. bytes, before the GC can stop. Return the list of store paths delete,
  1378. and the number of bytes freed."
  1379. (let ((s (store-connection-socket server)))
  1380. (write-int (operation-id collect-garbage) s)
  1381. (write-int action s)
  1382. (write-store-path-list to-delete s)
  1383. (write-arg boolean #f s) ; ignore-liveness?
  1384. (write-long-long min-freed s)
  1385. (write-int 0 s) ; obsolete
  1386. (when (>= (store-connection-minor-version server) 5)
  1387. ;; Obsolete `use-atime' and `max-atime' parameters.
  1388. (write-int 0 s)
  1389. (write-int 0 s))
  1390. ;; Loop until the server is done sending error output.
  1391. (let loop ((done? (process-stderr server)))
  1392. (or done? (loop (process-stderr server))))
  1393. (let ((paths (read-store-path-list s))
  1394. (freed (read-long-long s))
  1395. (obsolete (read-long-long s)))
  1396. (unless (null? paths)
  1397. ;; To be on the safe side, completely invalidate both caches.
  1398. ;; Otherwise we could end up returning store paths that are no longer
  1399. ;; valid.
  1400. (hash-clear! (store-connection-add-to-store-cache server))
  1401. (hash-clear! (store-connection-add-text-to-store-cache server)))
  1402. (values paths freed))))
  1403. (define-syntax-rule (%long-long-max)
  1404. ;; Maximum unsigned 64-bit integer.
  1405. (- (expt 2 64) 1))
  1406. (define (live-paths server)
  1407. "Return the list of live store paths---i.e., store paths still
  1408. referenced, and thus not subject to being garbage-collected."
  1409. (run-gc server (gc-action return-live) '() (%long-long-max)))
  1410. (define (dead-paths server)
  1411. "Return the list of dead store paths---i.e., store paths no longer
  1412. referenced, and thus subject to being garbage-collected."
  1413. (run-gc server (gc-action return-dead) '() (%long-long-max)))
  1414. (define* (collect-garbage server #:optional (min-freed (%long-long-max)))
  1415. "Collect garbage from the store at SERVER. If MIN-FREED is non-zero,
  1416. then collect at least MIN-FREED bytes. Return the paths that were
  1417. collected, and the number of bytes freed."
  1418. (run-gc server (gc-action delete-dead) '() min-freed))
  1419. (define* (delete-paths server paths #:optional (min-freed (%long-long-max)))
  1420. "Delete PATHS from the store at SERVER, if they are no longer
  1421. referenced. If MIN-FREED is non-zero, then stop after at least
  1422. MIN-FREED bytes have been collected. Return the paths that were
  1423. collected, and the number of bytes freed."
  1424. (run-gc server (gc-action delete-specific) paths min-freed))
  1425. (define (import-paths server port)
  1426. "Import the set of store paths read from PORT into SERVER's store. An error
  1427. is raised if the set of paths read from PORT is not signed (as per
  1428. 'export-path #:sign? #t'.) Return the list of store paths imported."
  1429. (let ((s (store-connection-socket server)))
  1430. (write-int (operation-id import-paths) s)
  1431. (let loop ((done? (process-stderr server port)))
  1432. (or done? (loop (process-stderr server port))))
  1433. (read-store-path-list s)))
  1434. (define* (export-path server path port #:key (sign? #t))
  1435. "Export PATH to PORT. When SIGN? is true, sign it."
  1436. (let ((s (store-connection-socket server)))
  1437. (write-int (operation-id export-path) s)
  1438. (write-store-path path s)
  1439. (write-arg boolean sign? s)
  1440. (let loop ((done? (process-stderr server port)))
  1441. (or done? (loop (process-stderr server port))))
  1442. (= 1 (read-int s))))
  1443. (define* (export-paths server paths port #:key (sign? #t) recursive?)
  1444. "Export the store paths listed in PATHS to PORT, in topological order,
  1445. signing them if SIGN? is true. When RECURSIVE? is true, export the closure of
  1446. PATHS---i.e., PATHS and all their dependencies."
  1447. (define ordered
  1448. (let ((sorted (topologically-sorted server paths)))
  1449. ;; When RECURSIVE? is #f, filter out the references of PATHS.
  1450. (if recursive?
  1451. sorted
  1452. (filter (cut member <> paths) sorted))))
  1453. (let loop ((paths ordered))
  1454. (match paths
  1455. (()
  1456. (write-int 0 port))
  1457. ((head tail ...)
  1458. (write-int 1 port)
  1459. (and (export-path server head port #:sign? sign?)
  1460. (loop tail))))))
  1461. (define-operation (query-failed-paths)
  1462. "Return the list of store items for which a build failure is cached.
  1463. The result is always the empty list unless the daemon was started with
  1464. '--cache-failures'."
  1465. store-path-list)
  1466. (define-operation (clear-failed-paths (store-path-list items))
  1467. "Remove ITEMS from the list of cached build failures.
  1468. This makes sense only when the daemon was started with '--cache-failures'."
  1469. boolean)
  1470. ;;;
  1471. ;;; Store monad.
  1472. ;;;
  1473. (define-syntax-rule (define-alias new old)
  1474. (define-syntax new (identifier-syntax old)))
  1475. ;; The store monad allows us to (1) build sequences of operations in the
  1476. ;; store, and (2) make the store an implicit part of the execution context,
  1477. ;; rather than a parameter of every single function.
  1478. (define-alias %store-monad %state-monad)
  1479. (define-alias store-return state-return)
  1480. (define-alias store-bind state-bind)
  1481. ;; Instantiate templates for %STORE-MONAD since it's syntactically different
  1482. ;; from %STATE-MONAD.
  1483. (template-directory instantiations %store-monad)
  1484. (define* (cache-object-mapping object keys result
  1485. #:key (vhash-cons vhash-consq))
  1486. "Augment the store's object cache with a mapping from OBJECT/KEYS to RESULT.
  1487. KEYS is a list of additional keys to match against, for instance a (SYSTEM
  1488. TARGET) tuple. Use VHASH-CONS to insert OBJECT into the cache.
  1489. OBJECT is typically a high-level object such as a <package> or an <origin>,
  1490. and RESULT is typically its derivation."
  1491. (lambda (store)
  1492. (values result
  1493. (store-connection
  1494. (inherit store)
  1495. (object-cache (vhash-cons object (cons result keys)
  1496. (store-connection-object-cache store)))))))
  1497. (define record-cache-lookup!
  1498. (if (profiled? "object-cache")
  1499. (let ((fresh 0)
  1500. (lookups 0)
  1501. (hits 0))
  1502. (register-profiling-hook!
  1503. "object-cache"
  1504. (lambda ()
  1505. (format (current-error-port) "Store object cache:
  1506. fresh caches: ~5@a
  1507. lookups: ~5@a
  1508. hits: ~5@a (~,1f%)~%"
  1509. fresh lookups hits
  1510. (if (zero? lookups)
  1511. 100.
  1512. (* 100. (/ hits lookups))))))
  1513. (lambda (hit? cache)
  1514. (set! fresh
  1515. (if (eq? cache vlist-null)
  1516. (+ 1 fresh)
  1517. fresh))
  1518. (set! lookups (+ 1 lookups))
  1519. (set! hits (if hit? (+ hits 1) hits))))
  1520. (lambda (x y)
  1521. #t)))
  1522. (define* (lookup-cached-object object #:optional (keys '())
  1523. #:key (vhash-fold* vhash-foldq*))
  1524. "Return the cached object in the store connection corresponding to OBJECT
  1525. and KEYS; use VHASH-FOLD* to look for OBJECT in the cache. KEYS is a list of
  1526. additional keys to match against, and which are compared with 'equal?'.
  1527. Return #f on failure and the cached result otherwise."
  1528. (lambda (store)
  1529. (let* ((cache (store-connection-object-cache store))
  1530. ;; Escape as soon as we find the result. This avoids traversing
  1531. ;; the whole vlist chain and significantly reduces the number of
  1532. ;; 'hashq' calls.
  1533. (value (let/ec return
  1534. (vhash-fold* (lambda (item result)
  1535. (match item
  1536. ((value . keys*)
  1537. (if (equal? keys keys*)
  1538. (return value)
  1539. result))))
  1540. #f object
  1541. cache))))
  1542. (record-cache-lookup! value cache)
  1543. (values value store))))
  1544. (define* (%mcached mthunk object #:optional (keys '())
  1545. #:key
  1546. (vhash-cons vhash-consq)
  1547. (vhash-fold* vhash-foldq*))
  1548. "Bind the monadic value returned by MTHUNK, which supposedly corresponds to
  1549. OBJECT/KEYS, or return its cached value. Use VHASH-CONS to insert OBJECT into
  1550. the cache, and VHASH-FOLD* to look it up."
  1551. (mlet %store-monad ((cached (lookup-cached-object object keys
  1552. #:vhash-fold* vhash-fold*)))
  1553. (if cached
  1554. (return cached)
  1555. (>>= (mthunk)
  1556. (lambda (result)
  1557. (cache-object-mapping object keys result
  1558. #:vhash-cons vhash-cons))))))
  1559. (define-syntax mcached
  1560. (syntax-rules (eq? equal?)
  1561. "Run MVALUE, which corresponds to OBJECT/KEYS, and cache it; or return the
  1562. value associated with OBJECT/KEYS in the store's object cache if there is
  1563. one."
  1564. ((_ eq? mvalue object keys ...)
  1565. (%mcached (lambda () mvalue)
  1566. object (list keys ...)
  1567. #:vhash-cons vhash-consq
  1568. #:vhash-fold* vhash-foldq*))
  1569. ((_ equal? mvalue object keys ...)
  1570. (%mcached (lambda () mvalue)
  1571. object (list keys ...)
  1572. #:vhash-cons vhash-cons
  1573. #:vhash-fold* vhash-fold*))
  1574. ((_ mvalue object keys ...)
  1575. (mcached eq? mvalue object keys ...))))
  1576. (define (preserve-documentation original proc)
  1577. "Return PROC with documentation taken from ORIGINAL."
  1578. (set-object-property! proc 'documentation
  1579. (procedure-property original 'documentation))
  1580. proc)
  1581. (define (store-lift proc)
  1582. "Lift PROC, a procedure whose first argument is a connection to the store,
  1583. in the store monad."
  1584. (preserve-documentation proc
  1585. (lambda args
  1586. (lambda (store)
  1587. (values (apply proc store args) store)))))
  1588. (define (store-lower proc)
  1589. "Lower PROC, a monadic procedure in %STORE-MONAD, to a \"normal\" procedure
  1590. taking the store as its first argument."
  1591. (preserve-documentation proc
  1592. (lambda (store . args)
  1593. (run-with-store store (apply proc args)))))
  1594. ;;
  1595. ;; Store monad operators.
  1596. ;;
  1597. (define* (binary-file name
  1598. data ;bytevector
  1599. #:optional (references '()))
  1600. "Return as a monadic value the absolute file name in the store of the file
  1601. containing DATA, a bytevector. REFERENCES is a list of store items that the
  1602. resulting text file refers to; it defaults to the empty list."
  1603. (lambda (store)
  1604. (values (add-data-to-store store name data references)
  1605. store)))
  1606. (define* (text-file name
  1607. text ;string
  1608. #:optional (references '()))
  1609. "Return as a monadic value the absolute file name in the store of the file
  1610. containing TEXT, a string. REFERENCES is a list of store items that the
  1611. resulting text file refers to; it defaults to the empty list."
  1612. (lambda (store)
  1613. (values (add-text-to-store store name text references)
  1614. store)))
  1615. (define* (interned-file file #:optional name
  1616. #:key (recursive? #t) (select? true))
  1617. "Return the name of FILE once interned in the store. Use NAME as its store
  1618. name, or the basename of FILE if NAME is omitted.
  1619. When RECURSIVE? is true, the contents of FILE are added recursively; if FILE
  1620. designates a flat file and RECURSIVE? is true, its contents are added, and its
  1621. permission bits are kept.
  1622. When RECURSIVE? is true, call (SELECT? FILE STAT) for each directory entry,
  1623. where FILE is the entry's absolute file name and STAT is the result of
  1624. 'lstat'; exclude entries for which SELECT? does not return true."
  1625. (lambda (store)
  1626. (values (add-to-store store (or name (basename file))
  1627. recursive? "sha256" file
  1628. #:select? select?)
  1629. store)))
  1630. (define interned-file-tree
  1631. (store-lift add-file-tree-to-store))
  1632. (define build
  1633. ;; Monadic variant of 'build-things'.
  1634. (store-lift build-things))
  1635. (define set-build-options*
  1636. (store-lift set-build-options))
  1637. (define references*
  1638. (store-lift references))
  1639. (define (query-path-info* item)
  1640. "Monadic version of 'query-path-info' that returns #f when ITEM is not in
  1641. the store."
  1642. (lambda (store)
  1643. (guard (c ((store-protocol-error? c)
  1644. ;; ITEM is not in the store; return #f.
  1645. (values #f store)))
  1646. (values (query-path-info store item) store))))
  1647. (define-inlinable (current-system)
  1648. ;; Consult the %CURRENT-SYSTEM fluid at bind time. This is equivalent to
  1649. ;; (lift0 %current-system %store-monad), but inlinable, thus avoiding
  1650. ;; closure allocation in some cases.
  1651. (lambda (state)
  1652. (values (%current-system) state)))
  1653. (define-inlinable (set-current-system system)
  1654. ;; Set the %CURRENT-SYSTEM fluid at bind time.
  1655. (lambda (state)
  1656. (values (%current-system system) state)))
  1657. (define-inlinable (current-target-system)
  1658. ;; Consult the %CURRENT-TARGET-SYSTEM fluid at bind time.
  1659. (lambda (state)
  1660. (values (%current-target-system) state)))
  1661. (define-inlinable (set-current-target target)
  1662. ;; Set the %CURRENT-TARGET-SYSTEM fluid at bind time.
  1663. (lambda (state)
  1664. (values (%current-target-system target) state)))
  1665. (define %guile-for-build
  1666. ;; The derivation of the Guile to be used within the build environment,
  1667. ;; when using 'gexp->derivation' and co.
  1668. (make-parameter #f))
  1669. (define set-store-connection-object-cache!
  1670. (record-modifier <store-connection> 'object-cache))
  1671. (define* (run-with-store store mval
  1672. #:key
  1673. (guile-for-build (%guile-for-build))
  1674. (system (%current-system))
  1675. (target #f))
  1676. "Run MVAL, a monadic value in the store monad, in STORE, an open store
  1677. connection, and return the result."
  1678. ;; Initialize the dynamic bindings here to avoid bad surprises. The
  1679. ;; difficulty lies in the fact that dynamic bindings are resolved at
  1680. ;; bind-time and not at call time, which can be disconcerting.
  1681. (parameterize ((%guile-for-build guile-for-build)
  1682. (%current-system system)
  1683. (%current-target-system target))
  1684. (call-with-values (lambda ()
  1685. (run-with-state mval store))
  1686. (lambda (result new-store)
  1687. (when (and store new-store)
  1688. ;; Copy the object cache from NEW-STORE so we don't fully discard
  1689. ;; the state.
  1690. (let ((cache (store-connection-object-cache new-store)))
  1691. (set-store-connection-object-cache! store cache)))
  1692. result))))
  1693. ;;;
  1694. ;;; Store paths.
  1695. ;;;
  1696. (define %store-prefix
  1697. ;; Absolute path to the Nix store.
  1698. (make-parameter %store-directory))
  1699. (define (compressed-hash bv size) ; `compressHash'
  1700. "Given the hash stored in BV, return a compressed version thereof that fits
  1701. in SIZE bytes."
  1702. (define new (make-bytevector size 0))
  1703. (define old-size (bytevector-length bv))
  1704. (let loop ((i 0))
  1705. (if (= i old-size)
  1706. new
  1707. (let* ((j (modulo i size))
  1708. (o (bytevector-u8-ref new j)))
  1709. (bytevector-u8-set! new j
  1710. (logxor o (bytevector-u8-ref bv i)))
  1711. (loop (+ 1 i))))))
  1712. (define (store-path type hash name) ; makeStorePath
  1713. "Return the store path for NAME/HASH/TYPE."
  1714. (let* ((s (string-append type ":sha256:"
  1715. (bytevector->base16-string hash) ":"
  1716. (%store-prefix) ":" name))
  1717. (h (sha256 (string->utf8 s)))
  1718. (c (compressed-hash h 20)))
  1719. (string-append (%store-prefix) "/"
  1720. (bytevector->nix-base32-string c) "-"
  1721. name)))
  1722. (define (output-path output hash name) ; makeOutputPath
  1723. "Return an output path for OUTPUT (the name of the output as a string) of
  1724. the derivation called NAME with hash HASH."
  1725. (store-path (string-append "output:" output) hash
  1726. (if (string=? output "out")
  1727. name
  1728. (string-append name "-" output))))
  1729. (define* (fixed-output-path name hash
  1730. #:key
  1731. (output "out")
  1732. (hash-algo 'sha256)
  1733. (recursive? #t))
  1734. "Return an output path for the fixed output OUTPUT defined by HASH of type
  1735. HASH-ALGO, of the derivation NAME. RECURSIVE? has the same meaning as for
  1736. 'add-to-store'."
  1737. (if (and recursive? (eq? hash-algo 'sha256))
  1738. (store-path "source" hash name)
  1739. (let ((tag (string-append "fixed:" output ":"
  1740. (if recursive? "r:" "")
  1741. (symbol->string hash-algo) ":"
  1742. (bytevector->base16-string hash) ":")))
  1743. (store-path (string-append "output:" output)
  1744. (sha256 (string->utf8 tag))
  1745. name))))
  1746. (define (store-path? path)
  1747. "Return #t if PATH is a store path."
  1748. ;; This is a lightweight check, compared to using a regexp, but this has to
  1749. ;; be fast as it's called often in `derivation', for instance.
  1750. ;; `isStorePath' in Nix does something similar.
  1751. (string-prefix? (%store-prefix) path))
  1752. (define (direct-store-path? path)
  1753. "Return #t if PATH is a store path, and not a sub-directory of a store path.
  1754. This predicate is sometimes needed because files *under* a store path are not
  1755. valid inputs."
  1756. (and (store-path? path)
  1757. (not (string=? path (%store-prefix)))
  1758. (let ((len (+ 1 (string-length (%store-prefix)))))
  1759. (not (string-index (substring path len) #\/)))))
  1760. (define (direct-store-path path)
  1761. "Return the direct store path part of PATH, stripping components after
  1762. '/gnu/store/xxxx-foo'."
  1763. (let ((prefix-length (+ (string-length (%store-prefix)) 35)))
  1764. (if (> (string-length path) prefix-length)
  1765. (let ((slash (string-index path #\/ prefix-length)))
  1766. (if slash (string-take path slash) path))
  1767. path)))
  1768. (define (derivation-path? path)
  1769. "Return #t if PATH is a derivation path."
  1770. (and (store-path? path) (string-suffix? ".drv" path)))
  1771. (define (store-path-base path)
  1772. "Return the base path of a path in the store."
  1773. (and (string-prefix? (%store-prefix) path)
  1774. (let ((base (string-drop path (+ 1 (string-length (%store-prefix))))))
  1775. (and (> (string-length base) 33)
  1776. (not (string-index base #\/))
  1777. base))))
  1778. (define (store-path-package-name path)
  1779. "Return the package name part of PATH, a file name in the store."
  1780. (let ((base (store-path-base path)))
  1781. (string-drop base (+ 32 1)))) ;32 hash part + 1 hyphen
  1782. (define (store-path-hash-part path)
  1783. "Return the hash part of PATH as a base32 string, or #f if PATH is not a
  1784. syntactically valid store path."
  1785. (let* ((base (store-path-base path))
  1786. (hash (string-take base 32)))
  1787. (and (string-every %nix-base32-charset hash)
  1788. hash)))
  1789. (define (derivation-log-file drv)
  1790. "Return the build log file for DRV, a derivation file name, or #f if it
  1791. could not be found."
  1792. (let* ((base (basename drv))
  1793. (log (string-append (or (getenv "GUIX_LOG_DIRECTORY")
  1794. (string-append %localstatedir "/log/guix"))
  1795. "/drvs/"
  1796. (string-take base 2) "/"
  1797. (string-drop base 2)))
  1798. (log.gz (string-append log ".gz"))
  1799. (log.bz2 (string-append log ".bz2")))
  1800. (cond ((file-exists? log.gz) log.gz)
  1801. ((file-exists? log.bz2) log.bz2)
  1802. ((file-exists? log) log)
  1803. (else #f))))
  1804. (define (log-file store file)
  1805. "Return the build log file for FILE, or #f if none could be found. FILE
  1806. must be an absolute store file name, or a derivation file name."
  1807. (cond ((derivation-path? file)
  1808. (derivation-log-file file))
  1809. (else
  1810. (match (valid-derivers store file)
  1811. ((derivers ...)
  1812. ;; Return the first that works.
  1813. (any (cut log-file store <>) derivers))
  1814. (_ #f)))))
  1815. ;;; Local Variables:
  1816. ;;; eval: (put 'system-error-to-connection-error 'scheme-indent-function 1)
  1817. ;;; End: