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