gnu: crawl: Fix savegame upgrades.
[jackhill/guix/guix.git] / guix / store.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
deac976d 2;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
77d3cf08 3;;;
233e7676 4;;; This file is part of GNU Guix.
77d3cf08 5;;;
233e7676 6;;; GNU Guix is free software; you can redistribute it and/or modify it
77d3cf08
LC
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
233e7676 11;;; GNU Guix is distributed in the hope that it will be useful, but
77d3cf08
LC
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
233e7676 17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
77d3cf08
LC
18
19(define-module (guix store)
82058eff 20 #:use-module (guix utils)
d8eea3d2 21 #:use-module (guix config)
f9704f17 22 #:use-module (guix memoization)
0f41c26f 23 #:use-module (guix serialization)
e87f0591 24 #:use-module (guix monads)
4c0c4db0 25 #:use-module (guix base16)
cd041b26
LC
26 #:use-module (guix base32)
27 #:use-module (guix hash)
b0a6a971 28 #:autoload (guix build syscalls) (terminal-columns)
77d3cf08 29 #:use-module (rnrs bytevectors)
2535635f 30 #:use-module (ice-9 binary-ports)
77d3cf08
LC
31 #:use-module (srfi srfi-1)
32 #:use-module (srfi srfi-9)
bf8e7fc5 33 #:use-module (srfi srfi-9 gnu)
6581ec9a 34 #:use-module (srfi srfi-11)
77d3cf08 35 #:use-module (srfi srfi-26)
e87088c9
LC
36 #:use-module (srfi srfi-34)
37 #:use-module (srfi srfi-35)
26bbbb95 38 #:use-module (srfi srfi-39)
77d3cf08 39 #:use-module (ice-9 match)
e3d74106 40 #:use-module (ice-9 regex)
3f1e6939 41 #:use-module (ice-9 vlist)
6bfec3ed 42 #:use-module (ice-9 popen)
1397b422
LC
43 #:use-module (web uri)
44 #:export (%daemon-socket-uri
a9d2a105 45 %gc-roots-directory
f8a8e0fe 46 %default-substitute-urls
9fd72fb1
LC
47
48 nix-server?
77d3cf08
LC
49 nix-server-major-version
50 nix-server-minor-version
51 nix-server-socket
52
e87088c9 53 &nix-error nix-error?
ef86c39f
LC
54 &nix-connection-error nix-connection-error?
55 nix-connection-error-file
56 nix-connection-error-code
e87088c9
LC
57 &nix-protocol-error nix-protocol-error?
58 nix-protocol-error-message
59 nix-protocol-error-status
60
26bbbb95 61 hash-algo
07e70f48 62 build-mode
26bbbb95 63
77d3cf08 64 open-connection
3abaf0c4 65 close-connection
ce4a4829 66 with-store
77d3cf08 67 set-build-options
4f740b67 68 set-build-options*
31ef99a8 69 valid-path?
82058eff 70 query-path-hash
11e7a6cf 71 hash-part->path
533d1768 72 query-path-info
0d268c5d 73 add-data-to-store
77d3cf08
LC
74 add-text-to-store
75 add-to-store
abac874b
LC
76 build-things
77 build
16748d80
LC
78 query-failed-paths
79 clear-failed-paths
3259877d 80 add-temp-root
34811f02 81 add-indirect-root
a9d2a105
LC
82 add-permanent-root
83 remove-permanent-root
26bbbb95 84
0f3d2504
LC
85 substitutable?
86 substitutable-path
87 substitutable-deriver
88 substitutable-references
89 substitutable-download-size
90 substitutable-nar-size
91 has-substitutes?
92 substitutable-paths
93 substitutable-path-info
94
533d1768
DT
95 path-info?
96 path-info-deriver
97 path-info-hash
98 path-info-references
99 path-info-registration-time
100 path-info-nar-size
101
f9aefa2d 102 built-in-builders
fae31edc 103 references
6581ec9a 104 references/substitutes
e74f64b9 105 references*
3f1e6939 106 requisites
fae31edc 107 referrers
e3fd0ce6 108 optimize-store
c63d9403 109 verify-store
50add477 110 topologically-sorted
fae31edc
LC
111 valid-derivers
112 query-derivation-outputs
3259877d
LC
113 live-paths
114 dead-paths
115 collect-garbage
116 delete-paths
526382ff
LC
117 import-paths
118 export-paths
3259877d 119
dcee50c1
LC
120 current-build-output-port
121
6bfec3ed
LC
122 register-path
123
e87f0591
LC
124 %store-monad
125 store-bind
126 store-return
127 store-lift
023d9892 128 store-lower
e87f0591
LC
129 run-with-store
130 %guile-for-build
98a7b528
LC
131 current-system
132 set-current-system
e87f0591
LC
133 text-file
134 interned-file
135
26bbbb95 136 %store-prefix
cd041b26
LC
137 store-path
138 output-path
139 fixed-output-path
26bbbb95 140 store-path?
9336e5b5 141 direct-store-path?
e3d74106 142 derivation-path?
2c6ab6cc 143 store-path-package-name
eddd4077 144 store-path-hash-part
cdb5b075 145 direct-store-path
eddd4077 146 log-file))
77d3cf08 147
deac976d 148(define %protocol-version #x161)
77d3cf08 149
d66b704b
LC
150(define %worker-magic-1 #x6e697863) ; "nixc"
151(define %worker-magic-2 #x6478696f) ; "dxio"
77d3cf08
LC
152
153(define (protocol-major magic)
154 (logand magic #xff00))
155(define (protocol-minor magic)
156 (logand magic #x00ff))
157
158(define-syntax define-enumerate-type
159 (syntax-rules ()
160 ((_ name->int (name id) ...)
161 (define-syntax name->int
162 (syntax-rules (name ...)
163 ((_ name) id) ...)))))
164
165(define-enumerate-type operation-id
166 ;; operation numbers from worker-protocol.hh
167 (quit 0)
168 (valid-path? 1)
169 (has-substitutes? 3)
170 (query-path-hash 4)
171 (query-references 5)
172 (query-referrers 6)
173 (add-to-store 7)
174 (add-text-to-store 8)
abac874b 175 (build-things 9)
77d3cf08
LC
176 (ensure-path 10)
177 (add-temp-root 11)
178 (add-indirect-root 12)
179 (sync-with-gc 13)
180 (find-roots 14)
181 (export-path 16)
182 (query-deriver 18)
183 (set-options 19)
184 (collect-garbage 20)
63193ebf 185 ;;(query-substitutable-path-info 21) ; obsolete as of #x10c
77d3cf08 186 (query-derivation-outputs 22)
63193ebf 187 (query-all-valid-paths 23)
77d3cf08
LC
188 (query-failed-paths 24)
189 (clear-failed-paths 25)
190 (query-path-info 26)
191 (import-paths 27)
63193ebf
LC
192 (query-derivation-output-names 28)
193 (query-path-from-hash-part 29)
194 (query-substitutable-path-infos 30)
195 (query-valid-paths 31)
fae31edc 196 (query-substitutable-paths 32)
e3fd0ce6 197 (query-valid-derivers 33)
c63d9403 198 (optimize-store 34)
f9aefa2d
LC
199 (verify-store 35)
200 (built-in-builders 80))
77d3cf08
LC
201
202(define-enumerate-type hash-algo
203 ;; hash.hh
204 (md5 1)
205 (sha1 2)
206 (sha256 3))
207
07e70f48
LC
208(define-enumerate-type build-mode
209 ;; store-api.hh
210 (normal 0)
211 (repair 1)
212 (check 2))
213
3259877d
LC
214(define-enumerate-type gc-action
215 ;; store-api.hh
216 (return-live 0)
217 (return-dead 1)
218 (delete-dead 2)
219 (delete-specific 3))
220
77d3cf08 221(define %default-socket-path
80d0447c 222 (string-append %state-directory "/daemon-socket/socket"))
77d3cf08 223
1397b422
LC
224(define %daemon-socket-uri
225 ;; URI or file name of the socket the daemon listens too.
9fd72fb1
LC
226 (make-parameter (or (getenv "GUIX_DAEMON_SOCKET")
227 %default-socket-path)))
228
229
77d3cf08 230\f
0f3d2504
LC
231;; Information about a substitutable store path.
232(define-record-type <substitutable>
233 (substitutable path deriver refs dl-size nar-size)
234 substitutable?
235 (path substitutable-path)
236 (deriver substitutable-deriver)
237 (refs substitutable-references)
238 (dl-size substitutable-download-size)
239 (nar-size substitutable-nar-size))
240
241(define (read-substitutable-path-list p)
242 (let loop ((len (read-int p))
243 (result '()))
244 (if (zero? len)
245 (reverse result)
246 (let ((path (read-store-path p))
247 (deriver (read-store-path p))
248 (refs (read-store-path-list p))
249 (dl-size (read-long-long p))
250 (nar-size (read-long-long p)))
251 (loop (- len 1)
252 (cons (substitutable path deriver refs dl-size nar-size)
253 result))))))
254
533d1768
DT
255;; Information about a store path.
256(define-record-type <path-info>
257 (path-info deriver hash references registration-time nar-size)
258 path-info?
22572d56 259 (deriver path-info-deriver) ;string | #f
533d1768
DT
260 (hash path-info-hash)
261 (references path-info-references)
262 (registration-time path-info-registration-time)
263 (nar-size path-info-nar-size))
264
265(define (read-path-info p)
22572d56
LC
266 (let ((deriver (match (read-store-path p)
267 ("" #f)
268 (x x)))
533d1768
DT
269 (hash (base16-string->bytevector (read-string p)))
270 (refs (read-store-path-list p))
271 (registration-time (read-int p))
272 (nar-size (read-long-long p)))
273 (path-info deriver hash refs registration-time nar-size)))
274
77d3cf08 275(define-syntax write-arg
0d268c5d
LC
276 (syntax-rules (integer boolean bytevector
277 string string-list string-pairs
3259877d 278 store-path store-path-list base16)
77d3cf08
LC
279 ((_ integer arg p)
280 (write-int arg p))
281 ((_ boolean arg p)
282 (write-int (if arg 1 0) p))
0d268c5d
LC
283 ((_ bytevector arg p)
284 (write-bytevector arg p))
77d3cf08
LC
285 ((_ string arg p)
286 (write-string arg p))
287 ((_ string-list arg p)
82058eff 288 (write-string-list arg p))
6c20d1d0
LC
289 ((_ string-pairs arg p)
290 (write-string-pairs arg p))
3259877d
LC
291 ((_ store-path arg p)
292 (write-store-path arg p))
293 ((_ store-path-list arg p)
294 (write-store-path-list arg p))
82058eff
LC
295 ((_ base16 arg p)
296 (write-string (bytevector->base16-string arg) p))))
77d3cf08
LC
297
298(define-syntax read-arg
f9aefa2d 299 (syntax-rules (integer boolean string store-path store-path-list string-list
533d1768 300 substitutable-path-list path-info base16)
77d3cf08
LC
301 ((_ integer p)
302 (read-int p))
303 ((_ boolean p)
304 (not (zero? (read-int p))))
305 ((_ string p)
306 (read-string p))
307 ((_ store-path p)
82058eff 308 (read-store-path p))
3259877d
LC
309 ((_ store-path-list p)
310 (read-store-path-list p))
f9aefa2d
LC
311 ((_ string-list p)
312 (read-string-list p))
0f3d2504
LC
313 ((_ substitutable-path-list p)
314 (read-substitutable-path-list p))
533d1768
DT
315 ((_ path-info p)
316 (read-path-info p))
0f3d2504 317 ((_ base16 p)
82058eff 318 (base16-string->bytevector (read-string p)))))
77d3cf08
LC
319
320\f
321;; remote-store.cc
322
323(define-record-type <nix-server>
2c3f47ee 324 (%make-nix-server socket major minor
bdcf35a6 325 ats-cache atts-cache)
77d3cf08
LC
326 nix-server?
327 (socket nix-server-socket)
328 (major nix-server-major-version)
2c3f47ee
LC
329 (minor nix-server-minor-version)
330
331 ;; Caches. We keep them per-connection, because store paths build
332 ;; during the session are temporary GC roots kept for the duration of
333 ;; the session.
bdcf35a6
LC
334 (ats-cache nix-server-add-to-store-cache)
335 (atts-cache nix-server-add-text-to-store-cache))
77d3cf08 336
bf8e7fc5
LC
337(set-record-type-printer! <nix-server>
338 (lambda (obj port)
339 (format port "#<build-daemon ~a.~a ~a>"
340 (nix-server-major-version obj)
341 (nix-server-minor-version obj)
342 (number->string (object-address obj)
343 16))))
344
e87088c9
LC
345(define-condition-type &nix-error &error
346 nix-error?)
347
ef86c39f
LC
348(define-condition-type &nix-connection-error &nix-error
349 nix-connection-error?
350 (file nix-connection-error-file)
351 (errno nix-connection-error-code))
352
e87088c9
LC
353(define-condition-type &nix-protocol-error &nix-error
354 nix-protocol-error?
355 (message nix-protocol-error-message)
356 (status nix-protocol-error-status))
357
3b5cd17a
LC
358(define-syntax-rule (system-error-to-connection-error file exp ...)
359 "Catch 'system-error' exceptions and translate them to
360'&nix-connection-error'."
361 (catch 'system-error
362 (lambda ()
363 exp ...)
364 (lambda args
365 (let ((errno (system-error-errno args)))
366 (raise (condition (&nix-connection-error
367 (file file)
368 (errno errno))))))))
369
6230d6f0
LC
370(define (open-unix-domain-socket file)
371 "Connect to the Unix-domain socket at FILE and return it. Raise a
372'&nix-connection-error' upon error."
77d3cf08
LC
373 (let ((s (with-fluids ((%default-port-encoding #f))
374 ;; This trick allows use of the `scm_c_read' optimization.
375 (socket PF_UNIX SOCK_STREAM 0)))
376 (a (make-socket-address PF_UNIX file)))
df1fab58 377
3b5cd17a
LC
378 (system-error-to-connection-error file
379 (connect s a)
380 s)))
ef86c39f 381
3dff90ce
LC
382(define (open-inet-socket host port)
383 "Connect to the Unix-domain socket at HOST:PORT and return it. Raise a
384'&nix-connection-error' upon error."
950d51c9
LC
385 ;; Define 'TCP_NODELAY' on Guile 2.0. The value is the same on all GNU
386 ;; systems.
387 (cond-expand (guile-2.2 #t)
388 (else (define TCP_NODELAY 1)))
389
3dff90ce
LC
390 (let ((sock (with-fluids ((%default-port-encoding #f))
391 ;; This trick allows use of the `scm_c_read' optimization.
392 (socket PF_UNIX SOCK_STREAM 0))))
393 (define addresses
394 (getaddrinfo host
395 (if (number? port) (number->string port) port)
396 (if (number? port)
397 (logior AI_ADDRCONFIG AI_NUMERICSERV)
398 AI_ADDRCONFIG)))
399
400 (let loop ((addresses addresses))
401 (match addresses
402 ((ai rest ...)
403 (let ((s (socket (addrinfo:fam ai)
404 ;; TCP/IP only
405 SOCK_STREAM IPPROTO_IP)))
406
407 (catch 'system-error
408 (lambda ()
409 (connect s (addrinfo:addr ai))
950d51c9
LC
410
411 ;; Setting this option makes a dramatic difference because it
412 ;; avoids the "ACK delay" on our RPC messages.
413 (setsockopt s IPPROTO_TCP TCP_NODELAY 1)
3dff90ce
LC
414 s)
415 (lambda args
416 ;; Connection failed, so try one of the other addresses.
417 (close s)
418 (if (null? rest)
419 (raise (condition (&nix-connection-error
420 (file host)
421 (errno (system-error-errno args)))))
422 (loop rest))))))))))
423
1397b422
LC
424(define (connect-to-daemon uri)
425 "Connect to the daemon at URI, a string that may be an actual URI or a file
426name."
285f63e8
LC
427 (define (not-supported)
428 (raise (condition (&nix-connection-error
429 (file uri)
430 (errno ENOTSUP)))))
431
1397b422
LC
432 (define connect
433 (match (string->uri uri)
434 (#f ;URI is a file name
435 open-unix-domain-socket)
436 ((? uri? uri)
437 (match (uri-scheme uri)
438 ((or #f 'file 'unix)
439 (lambda (_)
440 (open-unix-domain-socket (uri-path uri))))
3dff90ce
LC
441 ('guix
442 (lambda (_)
443 (unless (uri-port uri)
444 (raise (condition (&nix-connection-error
445 (file (uri->string uri))
446 (errno EBADR))))) ;bah!
447
448 (open-inet-socket (uri-host uri) (uri-port uri))))
285f63e8
LC
449 ((? symbol? scheme)
450 ;; Try to dynamically load a module for SCHEME.
451 ;; XXX: Errors are swallowed.
452 (match (false-if-exception
453 (resolve-interface `(guix store ,scheme)))
454 ((? module? module)
455 (match (false-if-exception
456 (module-ref module 'connect-to-daemon))
457 ((? procedure? connect)
458 (lambda (_)
459 (connect uri)))
460 (x (not-supported))))
461 (#f (not-supported))))
1397b422 462 (x
285f63e8 463 (not-supported))))))
1397b422
LC
464
465 (connect uri))
466
467(define* (open-connection #:optional (uri (%daemon-socket-uri))
6230d6f0 468 #:key port (reserve-space? #t) cpu-affinity)
1397b422
LC
469 "Connect to the daemon at URI (a string), or, if PORT is not #f, use it as
470the I/O port over which to communicate to a build daemon.
6230d6f0
LC
471
472When RESERVE-SPACE? is true, instruct it to reserve a little bit of extra
473space on the file system so that the garbage collector can still operate,
474should the disk become full. When CPU-AFFINITY is true, it must be an integer
475corresponding to an OS-level CPU number to which the daemon's worker process
476for this connection will be pinned. Return a server object."
13d5e8da
LC
477 (guard (c ((nar-error? c)
478 ;; One of the 'write-' or 'read-' calls below failed, but this is
479 ;; really a connection error.
480 (raise (condition
1397b422 481 (&nix-connection-error (file (or port uri))
13d5e8da
LC
482 (errno EPROTO))
483 (&message (message "build daemon handshake failed"))))))
1397b422 484 (let ((port (or port (connect-to-daemon uri))))
13d5e8da
LC
485 (write-int %worker-magic-1 port)
486 (let ((r (read-int port)))
487 (and (eqv? r %worker-magic-2)
488 (let ((v (read-int port)))
489 (and (eqv? (protocol-major %protocol-version)
490 (protocol-major v))
491 (begin
492 (write-int %protocol-version port)
493 (when (>= (protocol-minor v) 14)
494 (write-int (if cpu-affinity 1 0) port)
495 (when cpu-affinity
496 (write-int cpu-affinity port)))
497 (when (>= (protocol-minor v) 11)
498 (write-int (if reserve-space? 1 0) port))
499 (let ((conn (%make-nix-server port
500 (protocol-major v)
501 (protocol-minor v)
502 (make-hash-table 100)
503 (make-hash-table 100))))
504 (let loop ((done? (process-stderr conn)))
505 (or done? (process-stderr conn)))
506 conn)))))))))
77d3cf08 507
3abaf0c4
LC
508(define (close-connection server)
509 "Close the connection to SERVER."
510 (close (nix-server-socket server)))
511
ce4a4829
LC
512(define-syntax-rule (with-store store exp ...)
513 "Bind STORE to an open connection to the store and evaluate EXPs;
514automatically close the store when the dynamic extent of EXP is left."
515 (let ((store (open-connection)))
516 (dynamic-wind
517 (const #f)
518 (lambda ()
519 exp ...)
520 (lambda ()
521 (false-if-exception (close-connection store))))))
522
dcee50c1
LC
523(define current-build-output-port
524 ;; The port where build output is sent.
525 (make-parameter (current-error-port)))
526
526382ff
LC
527(define* (dump-port in out
528 #:optional len
529 #:key (buffer-size 16384))
530 "Read LEN bytes from IN (or as much as possible if LEN is #f) and write it
531to OUT, using chunks of BUFFER-SIZE bytes."
532 (define buffer
533 (make-bytevector buffer-size))
534
535 (let loop ((total 0)
536 (bytes (get-bytevector-n! in buffer 0
537 (if len
538 (min len buffer-size)
539 buffer-size))))
540 (or (eof-object? bytes)
541 (and len (= total len))
542 (let ((total (+ total bytes)))
543 (put-bytevector out buffer 0 bytes)
544 (loop total
545 (get-bytevector-n! in buffer 0
546 (if len
547 (min (- len total) buffer-size)
548 buffer-size)))))))
549
d28869af
LC
550(define %newlines
551 ;; Newline characters triggering a flush of 'current-build-output-port'.
552 ;; Unlike Guile's _IOLBF, we flush upon #\return so that progress reports
553 ;; that use that trick are correctly displayed.
554 (char-set #\newline #\return))
555
526382ff 556(define* (process-stderr server #:optional user-port)
dcee50c1
LC
557 "Read standard output and standard error from SERVER, writing it to
558CURRENT-BUILD-OUTPUT-PORT. Return #t when SERVER is done sending data, and
559#f otherwise; in the latter case, the caller should call `process-stderr'
bbdb3ffa
LC
560again until #t is returned or an error is raised.
561
562Since the build process's output cannot be assumed to be UTF-8, we
563conservatively consider it to be Latin-1, thereby avoiding possible
564encoding conversion errors."
77d3cf08
LC
565 (define p
566 (nix-server-socket server))
567
568 ;; magic cookies from worker-protocol.hh
5674a3fd
LC
569 (define %stderr-next #x6f6c6d67) ; "olmg", build log
570 (define %stderr-read #x64617461) ; "data", data needed from source
571 (define %stderr-write #x64617416) ; "dat\x16", data for sink
572 (define %stderr-last #x616c7473) ; "alts", we're done
573 (define %stderr-error #x63787470) ; "cxtp", error reporting
77d3cf08
LC
574
575 (let ((k (read-int p)))
576 (cond ((= k %stderr-write)
526382ff
LC
577 ;; Write a byte stream to USER-PORT.
578 (let* ((len (read-int p))
579 (m (modulo len 8)))
6374633b
LC
580 (dump-port p user-port len
581 #:buffer-size (if (<= len 16384) 16384 65536))
526382ff
LC
582 (unless (zero? m)
583 ;; Consume padding, as for strings.
584 (get-bytevector-n p (- 8 m))))
dcee50c1 585 #f)
77d3cf08 586 ((= k %stderr-read)
526382ff 587 ;; Read a byte stream from USER-PORT.
5895f244
LC
588 ;; Note: Avoid 'get-bytevector-n' to work around
589 ;; <http://bugs.gnu.org/17591> in Guile up to 2.0.11.
526382ff 590 (let* ((max-len (read-int p))
5895f244
LC
591 (data (make-bytevector max-len))
592 (len (get-bytevector-n! user-port data 0 max-len)))
396d12fa 593 (write-bytevector data p)
dcee50c1 594 #f))
77d3cf08 595 ((= k %stderr-next)
ce72c780
LC
596 ;; Log a string. Build logs are usually UTF-8-encoded, but they
597 ;; may also contain arbitrary byte sequences that should not cause
598 ;; this to fail. Thus, use the permissive
599 ;; 'read-maybe-utf8-string'.
600 (let ((s (read-maybe-utf8-string p)))
dcee50c1 601 (display s (current-build-output-port))
d28869af 602 (when (string-any %newlines s)
2535635f 603 (force-output (current-build-output-port)))
dcee50c1 604 #f))
77d3cf08 605 ((= k %stderr-error)
526382ff 606 ;; Report an error.
ce72c780 607 (let ((error (read-maybe-utf8-string p))
0ff3e3aa
LC
608 ;; Currently the daemon fails to send a status code for early
609 ;; errors like DB schema version mismatches, so check for EOF.
610 (status (if (and (>= (nix-server-minor-version server) 8)
611 (not (eof-object? (lookahead-u8 p))))
77d3cf08
LC
612 (read-int p)
613 1)))
e87088c9
LC
614 (raise (condition (&nix-protocol-error
615 (message error)
616 (status status))))))
77d3cf08 617 ((= k %stderr-last)
dcee50c1 618 ;; The daemon is done (see `stopWork' in `nix-worker.cc'.)
77d3cf08
LC
619 #t)
620 (else
e87088c9
LC
621 (raise (condition (&nix-protocol-error
622 (message "invalid error code")
623 (status k))))))))
77d3cf08 624
4d581220 625(define %default-substitute-urls
d70533cb
LC
626 ;; Default list of substituters. This is *not* the list baked in
627 ;; 'guix-daemon', but it is used by 'guix-service-type' and and a couple of
628 ;; clients ('guix build --log-file' uses it.)
df061d07
LC
629 (map (if (false-if-exception (resolve-interface '(gnutls)))
630 (cut string-append "https://" <>)
631 (cut string-append "http://" <>))
f82ce8f6 632 '("mirror.hydra.gnu.org")))
4d581220 633
77d3cf08 634(define* (set-build-options server
c3eb878f 635 #:key keep-failed? keep-going? fallback?
77d3cf08 636 (verbosity 0)
2fba87ac 637 rounds ;number of build rounds
deac976d 638 max-build-jobs
6c20d1d0 639 timeout
deac976d 640 max-silent-time
77d3cf08
LC
641 (use-build-hook? #t)
642 (build-verbosity 0)
643 (log-type 0)
e036c31b 644 (print-build-trace #t)
deac976d 645 build-cores
63193ebf 646 (use-substitutes? #t)
4d581220 647
fb4bf72b
LC
648 ;; Client-provided substitute URLs. If it is #f,
649 ;; the daemon's settings are used. Otherwise, it
650 ;; overrides the daemons settings; see 'guix
651 ;; substitute'.
b0a6a971
LC
652 (substitute-urls #f)
653
654 ;; Number of columns in the client's terminal.
38f50f49
LC
655 (terminal-columns (terminal-columns))
656
657 ;; Locale of the client.
658 (locale (false-if-exception (setlocale LC_ALL))))
77d3cf08
LC
659 ;; Must be called after `open-connection'.
660
661 (define socket
662 (nix-server-socket server))
663
664 (let-syntax ((send (syntax-rules ()
e036c31b
LC
665 ((_ (type option) ...)
666 (begin
667 (write-arg type option socket)
668 ...)))))
669 (write-int (operation-id set-options) socket)
670 (send (boolean keep-failed?) (boolean keep-going?)
deac976d
LC
671 (boolean fallback?) (integer verbosity))
672 (when (< (nix-server-minor-version server) #x61)
673 (let ((max-build-jobs (or max-build-jobs 1))
674 (max-silent-time (or max-silent-time 3600)))
675 (send (integer max-build-jobs) (integer max-silent-time))))
371e87d2
LC
676 (when (>= (nix-server-minor-version server) 2)
677 (send (boolean use-build-hook?)))
678 (when (>= (nix-server-minor-version server) 4)
679 (send (integer build-verbosity) (integer log-type)
680 (boolean print-build-trace)))
deac976d
LC
681 (when (and (>= (nix-server-minor-version server) 6)
682 (< (nix-server-minor-version server) #x61))
683 (let ((build-cores (or build-cores (current-processor-count))))
684 (send (integer build-cores))))
371e87d2
LC
685 (when (>= (nix-server-minor-version server) 10)
686 (send (boolean use-substitutes?)))
687 (when (>= (nix-server-minor-version server) 12)
41c45e78
LC
688 (let ((pairs `(,@(if timeout
689 `(("build-timeout" . ,(number->string timeout)))
690 '())
deac976d
LC
691 ,@(if max-silent-time
692 `(("build-max-silent-time"
693 . ,(number->string max-silent-time)))
694 '())
695 ,@(if max-build-jobs
696 `(("build-max-jobs"
697 . ,(number->string max-build-jobs)))
698 '())
699 ,@(if build-cores
700 `(("build-cores" . ,(number->string build-cores)))
701 '())
fb4bf72b
LC
702 ,@(if substitute-urls
703 `(("substitute-urls"
704 . ,(string-join substitute-urls)))
2fba87ac
LC
705 '())
706 ,@(if rounds
707 `(("build-repeat"
708 . ,(number->string (max 0 (1- rounds)))))
b0a6a971
LC
709 '())
710 ,@(if terminal-columns
711 `(("terminal-columns"
712 . ,(number->string terminal-columns)))
38f50f49
LC
713 '())
714 ,@(if locale
715 `(("locale" . ,locale))
fb4bf72b 716 '()))))
f401b1e9 717 (send (string-pairs pairs))))
dcee50c1
LC
718 (let loop ((done? (process-stderr server)))
719 (or done? (process-stderr server)))))
77d3cf08 720
fd060fd3 721(define-syntax operation
77d3cf08 722 (syntax-rules ()
fd060fd3 723 "Define a client-side RPC stub for the given operation."
3259877d 724 ((_ (name (type arg) ...) docstring return ...)
fd060fd3 725 (lambda (server arg ...)
77d3cf08
LC
726 docstring
727 (let ((s (nix-server-socket server)))
728 (write-int (operation-id name) s)
729 (write-arg type arg s)
730 ...
dcee50c1
LC
731 ;; Loop until the server is done sending error output.
732 (let loop ((done? (process-stderr server)))
733 (or done? (loop (process-stderr server))))
3259877d 734 (values (read-arg return s) ...))))))
77d3cf08 735
fd060fd3
LC
736(define-syntax-rule (define-operation (name args ...)
737 docstring return ...)
738 (define name
739 (operation (name args ...) docstring return ...)))
740
31ef99a8 741(define-operation (valid-path? (string path))
06b76acc
LC
742 "Return #t when PATH designates a valid store item and #f otherwise (an
743invalid item may exist on disk but still be invalid, for instance because it
744is the result of an aborted or failed build.)
745
746A '&nix-protocol-error' condition is raised if PATH is not prefixed by the
747store directory (/gnu/store)."
31ef99a8
LC
748 boolean)
749
63193ebf 750(define-operation (query-path-hash (store-path path))
aa8fff0c 751 "Return the SHA256 hash of the nar serialization of PATH as a bytevector."
82058eff
LC
752 base16)
753
11e7a6cf
LC
754(define hash-part->path
755 (let ((query-path-from-hash-part
756 (operation (query-path-from-hash-part (string hash))
757 #f
758 store-path)))
759 (lambda (server hash-part)
760 "Return the store path whose hash part is HASH-PART (a nix-base32
761string). Raise an error if no such path exists."
762 ;; This RPC is primarily used by Hydra to reply to HTTP GETs of
763 ;; /HASH.narinfo.
764 (query-path-from-hash-part server hash-part))))
765
533d1768
DT
766(define-operation (query-path-info (store-path path))
767 "Return the info (hash, references, etc.) for PATH."
768 path-info)
769
0d268c5d 770(define add-data-to-store
fd060fd3
LC
771 ;; A memoizing version of `add-to-store', to avoid repeated RPCs with
772 ;; the very same arguments during a given session.
773 (let ((add-text-to-store
0d268c5d 774 (operation (add-text-to-store (string name) (bytevector text)
fd060fd3
LC
775 (string-list references))
776 #f
777 store-path)))
0d268c5d
LC
778 (lambda* (server name bytes #:optional (references '()))
779 "Add BYTES under file NAME in the store, and return its store path.
bdcf35a6
LC
780REFERENCES is the list of store paths referred to by the resulting store
781path."
0d268c5d
LC
782 (let* ((args `(,bytes ,name ,references))
783 (cache (nix-server-add-text-to-store-cache server)))
bdcf35a6 784 (or (hash-ref cache args)
0d268c5d 785 (let ((path (add-text-to-store server name bytes references)))
bdcf35a6
LC
786 (hash-set! cache args path)
787 path))))))
788
0d268c5d
LC
789(define* (add-text-to-store store name text #:optional (references '()))
790 "Add TEXT under file NAME in the store, and return its store path.
791REFERENCES is the list of store paths referred to by the resulting store
792path."
793 (add-data-to-store store name (string->utf8 text) references))
794
1ec32f4a
LC
795(define true
796 ;; Define it once and for all since we use it as a default value for
797 ;; 'add-to-store' and want to make sure two default values are 'eq?' for the
798 ;; purposes or memoization.
799 (lambda (file stat)
800 #t))
801
fd060fd3 802(define add-to-store
a7b6ffee
LC
803 ;; A memoizing version of `add-to-store'. This is important because
804 ;; `add-to-store' leads to huge data transfers to the server, and
805 ;; because it's often called many times with the very same argument.
1ec32f4a
LC
806 (let ((add-to-store
807 (lambda* (server basename recursive? hash-algo file-name
808 #:key (select? true))
809 ;; We don't use the 'operation' macro so we can pass SELECT? to
810 ;; 'write-file'.
811 (let ((port (nix-server-socket server)))
812 (write-int (operation-id add-to-store) port)
813 (write-string basename port)
814 (write-int 1 port) ;obsolete, must be #t
815 (write-int (if recursive? 1 0) port)
816 (write-string hash-algo port)
817 (write-file file-name port #:select? select?)
818 (let loop ((done? (process-stderr server)))
819 (or done? (loop (process-stderr server))))
820 (read-store-path port)))))
821 (lambda* (server basename recursive? hash-algo file-name
822 #:key (select? true))
a9ebd9ef 823 "Add the contents of FILE-NAME under BASENAME to the store. When
69792b28
LC
824RECURSIVE? is false, FILE-NAME must designate a regular file--not a directory
825nor a symlink. When RECURSIVE? is true and FILE-NAME designates a directory,
826the contents of FILE-NAME are added recursively; if FILE-NAME designates a
827flat file and RECURSIVE? is true, its contents are added, and its permission
1ec32f4a
LC
828bits are kept. HASH-ALGO must be a string such as \"sha256\".
829
830When RECURSIVE? is true, call (SELECT? FILE STAT) for each directory entry,
831where FILE is the entry's absolute file name and STAT is the result of
832'lstat'; exclude entries for which SELECT? does not return true."
69792b28 833 (let* ((st (false-if-exception (lstat file-name)))
1ec32f4a 834 (args `(,st ,basename ,recursive? ,hash-algo ,select?))
2c3f47ee 835 (cache (nix-server-add-to-store-cache server)))
a7b6ffee 836 (or (and st (hash-ref cache args))
1ec32f4a
LC
837 (let ((path (add-to-store server basename recursive?
838 hash-algo file-name
839 #:select? select?)))
a7b6ffee
LC
840 (hash-set! cache args path)
841 path))))))
842
07e70f48
LC
843(define build-things
844 (let ((build (operation (build-things (string-list things)
845 (integer mode))
846 "Do it!"
2734cbb8
LC
847 boolean))
848 (build/old (operation (build-things (string-list things))
849 "Do it!"
850 boolean)))
07e70f48
LC
851 (lambda* (store things #:optional (mode (build-mode normal)))
852 "Build THINGS, a list of store items which may be either '.drv' files or
abac874b
LC
853outputs, and return when the worker is done building them. Elements of THINGS
854that are not derivations can only be substituted and not built locally.
dcee50c1 855Return #t on success."
2734cbb8
LC
856 (if (>= (nix-server-minor-version store) 15)
857 (build store things mode)
858 (if (= mode (build-mode normal))
859 (build/old store things)
860 (raise (condition (&nix-protocol-error
861 (message "unsupported build mode")
862 (status 1)))))))))
26bbbb95 863
d3648e01
LC
864(define-operation (add-temp-root (store-path path))
865 "Make PATH a temporary root for the duration of the current session.
866Return #t."
867 boolean)
868
34811f02 869(define-operation (add-indirect-root (string file-name))
a9d2a105
LC
870 "Make the symlink FILE-NAME an indirect root for the garbage collector:
871whatever store item FILE-NAME points to will not be collected. Return #t on
872success.
873
874FILE-NAME can be anywhere on the file system, but it must be an absolute file
875name--it is the caller's responsibility to ensure that it is an absolute file
876name."
34811f02
LC
877 boolean)
878
a9d2a105
LC
879(define %gc-roots-directory
880 ;; The place where garbage collector roots (symlinks) are kept.
881 (string-append %state-directory "/gcroots"))
882
883(define (add-permanent-root target)
884 "Add a garbage collector root pointing to TARGET, an element of the store,
885preventing TARGET from even being collected. This can also be used if TARGET
886does not exist yet.
887
888Raise an error if the caller does not have write access to the GC root
889directory."
890 (let* ((root (string-append %gc-roots-directory "/" (basename target))))
891 (catch 'system-error
892 (lambda ()
893 (symlink target root))
894 (lambda args
895 ;; If ROOT already exists, this is fine; otherwise, re-throw.
896 (unless (= EEXIST (system-error-errno args))
897 (apply throw args))))))
898
899(define (remove-permanent-root target)
900 "Remove the permanent garbage collector root pointing to TARGET. Raise an
901error if there is no such root."
902 (delete-file (string-append %gc-roots-directory "/" (basename target))))
903
fae31edc
LC
904(define references
905 (operation (query-references (store-path path))
906 "Return the list of references of PATH."
907 store-path-list))
908
f09aea1b
LC
909(define %reference-cache
910 ;; Brute-force cache mapping store items to their list of references.
911 ;; Caching matters because when building a profile in the presence of
912 ;; grafts, we keep calling 'graft-derivation', which in turn calls
913 ;; 'references/substitutes' many times with the same arguments. Ideally we
914 ;; would use a cache associated with the daemon connection instead (XXX).
915 (make-hash-table 100))
916
6581ec9a
LC
917(define (references/substitutes store items)
918 "Return the list of list of references of ITEMS; the result has the same
919length as ITEMS. Query substitute information for any item missing from the
920store at once. Raise a '&nix-protocol-error' exception if reference
921information for one of ITEMS is missing."
922 (let* ((local-refs (map (lambda (item)
f09aea1b
LC
923 (or (hash-ref %reference-cache item)
924 (guard (c ((nix-protocol-error? c) #f))
925 (references store item))))
6581ec9a
LC
926 items))
927 (missing (fold-right (lambda (item local-ref result)
928 (if local-ref
929 result
930 (cons item result)))
931 '()
932 items local-refs))
933
934 ;; Query all the substitutes at once to minimize the cost of
935 ;; launching 'guix substitute' and making HTTP requests.
936 (substs (substitutable-path-info store missing)))
937 (when (< (length substs) (length missing))
938 (raise (condition (&nix-protocol-error
939 (message "cannot determine \
940the list of references")
941 (status 1)))))
942
943 ;; Intersperse SUBSTS and LOCAL-REFS.
dd78e90a
LC
944 (let loop ((items items)
945 (local-refs local-refs)
6581ec9a 946 (result '()))
dd78e90a 947 (match items
6581ec9a 948 (()
f09aea1b
LC
949 (let ((result (reverse result)))
950 (for-each (cut hash-set! %reference-cache <> <>)
951 items result)
952 result))
dd78e90a
LC
953 ((item items ...)
954 (match local-refs
955 ((#f tail ...)
956 (loop items tail
957 (cons (any (lambda (subst)
958 (and (string=? (substitutable-path subst) item)
959 (substitutable-references subst)))
960 substs)
961 result)))
962 ((head tail ...)
963 (loop items tail
964 (cons head result)))))))))
6581ec9a 965
f6fee16e 966(define* (fold-path store proc seed paths
3f1e6939 967 #:optional (relatives (cut references store <>)))
f6fee16e 968 "Call PROC for each of the RELATIVES of PATHS, exactly once, and return the
3f1e6939
LC
969result formed from the successive calls to PROC, the first of which is passed
970SEED."
f6fee16e 971 (let loop ((paths paths)
3f1e6939
LC
972 (result seed)
973 (seen vlist-null))
974 (match paths
975 ((path rest ...)
976 (if (vhash-assoc path seen)
977 (loop rest result seen)
978 (let ((seen (vhash-cons path #t seen))
979 (rest (append rest (relatives path)))
980 (result (proc path result)))
981 (loop rest result seen))))
982 (()
983 result))))
984
f6fee16e
LC
985(define (requisites store paths)
986 "Return the requisites of PATHS, including PATHS---i.e., their closures (all
987its references, recursively)."
988 (fold-path store cons '() paths))
3f1e6939 989
50add477
LC
990(define (topologically-sorted store paths)
991 "Return a list containing PATHS and all their references sorted in
992topological order."
993 (define (traverse)
994 ;; Do a simple depth-first traversal of all of PATHS.
995 (let loop ((paths paths)
996 (visited vlist-null)
997 (result '()))
998 (define (visit n)
999 (vhash-cons n #t visited))
1000
1001 (define (visited? n)
1002 (vhash-assoc n visited))
1003
1004 (match paths
1005 ((head tail ...)
1006 (if (visited? head)
1007 (loop tail visited result)
1008 (call-with-values
1009 (lambda ()
1010 (loop (references store head)
1011 (visit head)
1012 result))
1013 (lambda (visited result)
1014 (loop tail
1015 visited
1016 (cons head result))))))
1017 (()
1018 (values visited result)))))
1019
1020 (call-with-values traverse
1021 (lambda (_ result)
1022 (reverse result))))
1023
fae31edc
LC
1024(define referrers
1025 (operation (query-referrers (store-path path))
1026 "Return the list of path that refer to PATH."
1027 store-path-list))
1028
1029(define valid-derivers
1030 (operation (query-valid-derivers (store-path path))
1031 "Return the list of valid \"derivers\" of PATH---i.e., all the
1032.drv present in the store that have PATH among their outputs."
1033 store-path-list))
1034
1035(define query-derivation-outputs ; avoid name clash with `derivation-outputs'
1036 (operation (query-derivation-outputs (store-path path))
1037 "Return the list of outputs of PATH, a .drv file."
1038 store-path-list))
1039
0f3d2504
LC
1040(define-operation (has-substitutes? (store-path path))
1041 "Return #t if binary substitutes are available for PATH, and #f otherwise."
1042 boolean)
1043
1044(define substitutable-paths
1045 (operation (query-substitutable-paths (store-path-list paths))
1046 "Return the subset of PATHS that is substitutable."
1047 store-path-list))
1048
1049(define substitutable-path-info
f65cf81a 1050 (operation (query-substitutable-path-infos (store-path-list paths))
0f3d2504
LC
1051 "Return information about the subset of PATHS that is
1052substitutable. For each substitutable path, a `substitutable?' object is
dd78e90a
LC
1053returned; thus, the resulting list can be shorter than PATHS. Furthermore,
1054that there is no guarantee that the order of the resulting list matches the
1055order of PATHS."
0f3d2504
LC
1056 substitutable-path-list))
1057
f9aefa2d
LC
1058(define built-in-builders
1059 (let ((builders (operation (built-in-builders)
1060 "Return the built-in builders."
1061 string-list)))
1062 (lambda (store)
1063 "Return the names of the supported built-in derivation builders
1064supported by STORE."
1065 ;; Check whether STORE's version supports this RPC and built-in
1066 ;; derivation builders in general, which appeared in Guix > 0.11.0.
1067 ;; Return the empty list if it doesn't. Note that this RPC does not
1068 ;; exist in 'nix-daemon'.
1069 (if (or (> (nix-server-major-version store) #x100)
1070 (and (= (nix-server-major-version store) #x100)
1071 (>= (nix-server-minor-version store) #x60)))
1072 (builders store)
1073 '()))))
1074
e3fd0ce6
LC
1075(define-operation (optimize-store)
1076 "Optimize the store by hard-linking identical files (\"deduplication\".)
1077Return #t on success."
1078 ;; Note: the daemon in Guix <= 0.8.2 does not implement this RPC.
1079 boolean)
1080
c63d9403
LC
1081(define verify-store
1082 (let ((verify (operation (verify-store (boolean check-contents?)
1083 (boolean repair?))
1084 "Verify the store."
1085 boolean)))
1086 (lambda* (store #:key check-contents? repair?)
1087 "Verify the integrity of the store and return false if errors remain,
1088and true otherwise. When REPAIR? is true, repair any missing or altered store
1089items by substituting them (this typically requires root privileges because it
1090is not an atomic operation.) When CHECK-CONTENTS? is true, check the contents
1091of store items; this can take a lot of time."
1092 (not (verify store check-contents? repair?)))))
1093
3259877d
LC
1094(define (run-gc server action to-delete min-freed)
1095 "Perform the garbage-collector operation ACTION, one of the
1096`gc-action' values. When ACTION is `delete-specific', the TO-DELETE is
1097the list of store paths to delete. IGNORE-LIVENESS? should always be
1098#f. MIN-FREED is the minimum amount of disk space to be freed, in
1099bytes, before the GC can stop. Return the list of store paths delete,
1100and the number of bytes freed."
1101 (let ((s (nix-server-socket server)))
1102 (write-int (operation-id collect-garbage) s)
1103 (write-int action s)
1104 (write-store-path-list to-delete s)
1105 (write-arg boolean #f s) ; ignore-liveness?
1106 (write-long-long min-freed s)
1107 (write-int 0 s) ; obsolete
1108 (when (>= (nix-server-minor-version server) 5)
1109 ;; Obsolete `use-atime' and `max-atime' parameters.
1110 (write-int 0 s)
1111 (write-int 0 s))
1112
1113 ;; Loop until the server is done sending error output.
1114 (let loop ((done? (process-stderr server)))
1115 (or done? (loop (process-stderr server))))
1116
1117 (let ((paths (read-store-path-list s))
1118 (freed (read-long-long s))
1119 (obsolete (read-long-long s)))
000c59b6
LC
1120 (unless (null? paths)
1121 ;; To be on the safe side, completely invalidate both caches.
1122 ;; Otherwise we could end up returning store paths that are no longer
1123 ;; valid.
1124 (hash-clear! (nix-server-add-to-store-cache server))
1125 (hash-clear! (nix-server-add-text-to-store-cache server)))
1126
3259877d
LC
1127 (values paths freed))))
1128
1129(define-syntax-rule (%long-long-max)
1130 ;; Maximum unsigned 64-bit integer.
1131 (- (expt 2 64) 1))
1132
1133(define (live-paths server)
1134 "Return the list of live store paths---i.e., store paths still
1135referenced, and thus not subject to being garbage-collected."
1136 (run-gc server (gc-action return-live) '() (%long-long-max)))
1137
1138(define (dead-paths server)
1139 "Return the list of dead store paths---i.e., store paths no longer
1140referenced, and thus subject to being garbage-collected."
1141 (run-gc server (gc-action return-dead) '() (%long-long-max)))
1142
1143(define* (collect-garbage server #:optional (min-freed (%long-long-max)))
1144 "Collect garbage from the store at SERVER. If MIN-FREED is non-zero,
1145then collect at least MIN-FREED bytes. Return the paths that were
1146collected, and the number of bytes freed."
1147 (run-gc server (gc-action delete-dead) '() min-freed))
1148
1149(define* (delete-paths server paths #:optional (min-freed (%long-long-max)))
1150 "Delete PATHS from the store at SERVER, if they are no longer
1151referenced. If MIN-FREED is non-zero, then stop after at least
1152MIN-FREED bytes have been collected. Return the paths that were
1153collected, and the number of bytes freed."
1154 (run-gc server (gc-action delete-specific) paths min-freed))
1155
526382ff
LC
1156(define (import-paths server port)
1157 "Import the set of store paths read from PORT into SERVER's store. An error
1158is raised if the set of paths read from PORT is not signed (as per
1159'export-path #:sign? #t'.) Return the list of store paths imported."
1160 (let ((s (nix-server-socket server)))
1161 (write-int (operation-id import-paths) s)
1162 (let loop ((done? (process-stderr server port)))
1163 (or done? (loop (process-stderr server port))))
1164 (read-store-path-list s)))
1165
1166(define* (export-path server path port #:key (sign? #t))
1167 "Export PATH to PORT. When SIGN? is true, sign it."
1168 (let ((s (nix-server-socket server)))
1169 (write-int (operation-id export-path) s)
1170 (write-store-path path s)
1171 (write-arg boolean sign? s)
1172 (let loop ((done? (process-stderr server port)))
1173 (or done? (loop (process-stderr server port))))
1174 (= 1 (read-int s))))
1175
5b3d863f 1176(define* (export-paths server paths port #:key (sign? #t) recursive?)
99fbddf9 1177 "Export the store paths listed in PATHS to PORT, in topological order,
5b3d863f
LC
1178signing them if SIGN? is true. When RECURSIVE? is true, export the closure of
1179PATHS---i.e., PATHS and all their dependencies."
cafb92d8 1180 (define ordered
5b3d863f
LC
1181 (let ((sorted (topologically-sorted server paths)))
1182 ;; When RECURSIVE? is #f, filter out the references of PATHS.
1183 (if recursive?
1184 sorted
1185 (filter (cut member <> paths) sorted))))
cafb92d8 1186
1d506993
LC
1187 (let loop ((paths ordered))
1188 (match paths
1189 (()
1190 (write-int 0 port))
1191 ((head tail ...)
1192 (write-int 1 port)
1193 (and (export-path server head port #:sign? sign?)
1194 (loop tail))))))
526382ff 1195
16748d80
LC
1196(define-operation (query-failed-paths)
1197 "Return the list of store items for which a build failure is cached.
1198
1199The result is always the empty list unless the daemon was started with
1200'--cache-failures'."
1201 store-path-list)
1202
1203(define-operation (clear-failed-paths (store-path-list items))
1204 "Remove ITEMS from the list of cached build failures.
1205
1206This makes sense only when the daemon was started with '--cache-failures'."
1207 boolean)
1208
6bfec3ed 1209(define* (register-path path
689142cd
LC
1210 #:key (references '()) deriver prefix
1211 state-directory)
6bfec3ed 1212 "Register PATH as a valid store file, with REFERENCES as its list of
bb31e0a3
LC
1213references, and DERIVER as its deriver (.drv that led to it.) If PREFIX is
1214not #f, it must be the name of the directory containing the new store to
689142cd
LC
1215initialize; if STATE-DIRECTORY is not #f, it must be a string containing the
1216absolute file name to the state directory of the store being initialized.
1217Return #t on success.
6bfec3ed
LC
1218
1219Use with care as it directly modifies the store! This is primarily meant to
1220be used internally by the daemon's build hook."
1221 ;; Currently this is implemented by calling out to the fine C++ blob.
95fa173e
LC
1222 (let ((pipe (apply open-pipe* OPEN_WRITE %guix-register-program
1223 `(,@(if prefix
1224 `("--prefix" ,prefix)
1225 '())
1226 ,@(if state-directory
1227 `("--state-directory" ,state-directory)
1228 '())))))
1229 (and pipe
1230 (begin
1231 (format pipe "~a~%~a~%~a~%"
1232 path (or deriver "") (length references))
1233 (for-each (cut format pipe "~a~%" <>) references)
1234 (zero? (close-pipe pipe))))))
6bfec3ed 1235
26bbbb95 1236\f
e87f0591
LC
1237;;;
1238;;; Store monad.
1239;;;
1240
4e190c28
LC
1241(define-syntax-rule (define-alias new old)
1242 (define-syntax new (identifier-syntax old)))
e87f0591 1243
4e190c28
LC
1244;; The store monad allows us to (1) build sequences of operations in the
1245;; store, and (2) make the store an implicit part of the execution context,
1246;; rather than a parameter of every single function.
1247(define-alias %store-monad %state-monad)
1248(define-alias store-return state-return)
1249(define-alias store-bind state-bind)
e87f0591 1250
dcb95c1f
LC
1251;; Instantiate templates for %STORE-MONAD since it's syntactically different
1252;; from %STATE-MONAD.
1253(template-directory instantiations %store-monad)
1254
5808dcc2
LC
1255(define (preserve-documentation original proc)
1256 "Return PROC with documentation taken from ORIGINAL."
1257 (set-object-property! proc 'documentation
1258 (procedure-property original 'documentation))
1259 proc)
1260
e87f0591
LC
1261(define (store-lift proc)
1262 "Lift PROC, a procedure whose first argument is a connection to the store,
1263in the store monad."
5808dcc2
LC
1264 (preserve-documentation proc
1265 (lambda args
1266 (lambda (store)
1267 (values (apply proc store args) store)))))
e87f0591 1268
023d9892
LC
1269(define (store-lower proc)
1270 "Lower PROC, a monadic procedure in %STORE-MONAD, to a \"normal\" procedure
1271taking the store as its first argument."
5808dcc2
LC
1272 (preserve-documentation proc
1273 (lambda (store . args)
1274 (run-with-store store (apply proc args)))))
023d9892 1275
e87f0591
LC
1276;;
1277;; Store monad operators.
1278;;
1279
ad372953
LC
1280(define* (text-file name text
1281 #:optional (references '()))
e87f0591 1282 "Return as a monadic value the absolute file name in the store of the file
ad372953
LC
1283containing TEXT, a string. REFERENCES is a list of store items that the
1284resulting text file refers to; it defaults to the empty list."
e87f0591 1285 (lambda (store)
ad372953 1286 (values (add-text-to-store store name text references)
4e190c28 1287 store)))
e87f0591
LC
1288
1289(define* (interned-file file #:optional name
1ec32f4a 1290 #:key (recursive? #t) (select? true))
e87f0591
LC
1291 "Return the name of FILE once interned in the store. Use NAME as its store
1292name, or the basename of FILE if NAME is omitted.
1293
1294When RECURSIVE? is true, the contents of FILE are added recursively; if FILE
1295designates a flat file and RECURSIVE? is true, its contents are added, and its
1ec32f4a
LC
1296permission bits are kept.
1297
1298When RECURSIVE? is true, call (SELECT? FILE STAT) for each directory entry,
1299where FILE is the entry's absolute file name and STAT is the result of
1300'lstat'; exclude entries for which SELECT? does not return true."
e87f0591 1301 (lambda (store)
4e190c28 1302 (values (add-to-store store (or name (basename file))
1ec32f4a
LC
1303 recursive? "sha256" file
1304 #:select? select?)
4e190c28 1305 store)))
e87f0591 1306
abac874b
LC
1307(define build
1308 ;; Monadic variant of 'build-things'.
1309 (store-lift build-things))
1310
4f740b67
AK
1311(define set-build-options*
1312 (store-lift set-build-options))
1313
e74f64b9
LC
1314(define references*
1315 (store-lift references))
1316
98a7b528
LC
1317(define-inlinable (current-system)
1318 ;; Consult the %CURRENT-SYSTEM fluid at bind time. This is equivalent to
1319 ;; (lift0 %current-system %store-monad), but inlinable, thus avoiding
1320 ;; closure allocation in some cases.
1321 (lambda (state)
1322 (values (%current-system) state)))
1323
1324(define-inlinable (set-current-system system)
1325 ;; Set the %CURRENT-SYSTEM fluid at bind time.
1326 (lambda (state)
1327 (values (%current-system system) state)))
1328
e87f0591
LC
1329(define %guile-for-build
1330 ;; The derivation of the Guile to be used within the build environment,
1331 ;; when using 'gexp->derivation' and co.
1332 (make-parameter #f))
1333
1334(define* (run-with-store store mval
1335 #:key
1336 (guile-for-build (%guile-for-build))
1337 (system (%current-system)))
1338 "Run MVAL, a monadic value in the store monad, in STORE, an open store
3698f524 1339connection, and return the result."
a8afb9ae
LC
1340 ;; Initialize the dynamic bindings here to avoid bad surprises. The
1341 ;; difficulty lies in the fact that dynamic bindings are resolved at
1342 ;; bind-time and not at call time, which can be disconcerting.
e87f0591 1343 (parameterize ((%guile-for-build guile-for-build)
a8afb9ae
LC
1344 (%current-system system)
1345 (%current-target-system #f))
3698f524
LC
1346 (call-with-values (lambda ()
1347 (run-with-state mval store))
1348 (lambda (result store)
1349 ;; Discard the state.
1350 result))))
e87f0591
LC
1351
1352\f
26bbbb95
LC
1353;;;
1354;;; Store paths.
1355;;;
1356
1357(define %store-prefix
1358 ;; Absolute path to the Nix store.
1d6816f9 1359 (make-parameter %store-directory))
26bbbb95 1360
cd041b26
LC
1361(define (compressed-hash bv size) ; `compressHash'
1362 "Given the hash stored in BV, return a compressed version thereof that fits
1363in SIZE bytes."
1364 (define new (make-bytevector size 0))
1365 (define old-size (bytevector-length bv))
1366 (let loop ((i 0))
1367 (if (= i old-size)
1368 new
1369 (let* ((j (modulo i size))
1370 (o (bytevector-u8-ref new j)))
1371 (bytevector-u8-set! new j
1372 (logxor o (bytevector-u8-ref bv i)))
1373 (loop (+ 1 i))))))
1374
1375(define (store-path type hash name) ; makeStorePath
1376 "Return the store path for NAME/HASH/TYPE."
1377 (let* ((s (string-append type ":sha256:"
1378 (bytevector->base16-string hash) ":"
1379 (%store-prefix) ":" name))
1380 (h (sha256 (string->utf8 s)))
1381 (c (compressed-hash h 20)))
1382 (string-append (%store-prefix) "/"
1383 (bytevector->nix-base32-string c) "-"
1384 name)))
1385
1386(define (output-path output hash name) ; makeOutputPath
1387 "Return an output path for OUTPUT (the name of the output as a string) of
1388the derivation called NAME with hash HASH."
1389 (store-path (string-append "output:" output) hash
1390 (if (string=? output "out")
1391 name
1392 (string-append name "-" output))))
1393
1394(define* (fixed-output-path name hash
1395 #:key
1396 (output "out")
1397 (hash-algo 'sha256)
1398 (recursive? #t))
1399 "Return an output path for the fixed output OUTPUT defined by HASH of type
1400HASH-ALGO, of the derivation NAME. RECURSIVE? has the same meaning as for
1401'add-to-store'."
1402 (if (and recursive? (eq? hash-algo 'sha256))
1403 (store-path "source" hash name)
1404 (let ((tag (string-append "fixed:" output ":"
1405 (if recursive? "r:" "")
1406 (symbol->string hash-algo) ":"
1407 (bytevector->base16-string hash) ":")))
1408 (store-path (string-append "output:" output)
1409 (sha256 (string->utf8 tag))
1410 name))))
1411
f39bd08a
LC
1412(define (store-path? path)
1413 "Return #t if PATH is a store path."
1414 ;; This is a lightweight check, compared to using a regexp, but this has to
1415 ;; be fast as it's called often in `derivation', for instance.
1416 ;; `isStorePath' in Nix does something similar.
1417 (string-prefix? (%store-prefix) path))
26bbbb95 1418
9336e5b5
LC
1419(define (direct-store-path? path)
1420 "Return #t if PATH is a store path, and not a sub-directory of a store path.
1421This predicate is sometimes needed because files *under* a store path are not
1422valid inputs."
1423 (and (store-path? path)
eee21271 1424 (not (string=? path (%store-prefix)))
9336e5b5
LC
1425 (let ((len (+ 1 (string-length (%store-prefix)))))
1426 (not (string-index (substring path len) #\/)))))
1427
cdb5b075
CS
1428(define (direct-store-path path)
1429 "Return the direct store path part of PATH, stripping components after
1430'/gnu/store/xxxx-foo'."
1431 (let ((prefix-length (+ (string-length (%store-prefix)) 35)))
1432 (if (> (string-length path) prefix-length)
1433 (let ((slash (string-index path #\/ prefix-length)))
1434 (if slash (string-take path slash) path))
1435 path)))
1436
26bbbb95
LC
1437(define (derivation-path? path)
1438 "Return #t if PATH is a derivation path."
1439 (and (store-path? path) (string-suffix? ".drv" path)))
e3d74106 1440
5c0f1845
LC
1441(define store-regexp*
1442 ;; The substituter makes repeated calls to 'store-path-hash-part', hence
1443 ;; this optimization.
55b2d921
LC
1444 (mlambda (store)
1445 "Return a regexp matching a file in STORE."
1446 (make-regexp (string-append "^" (regexp-quote store)
1447 "/([0-9a-df-np-sv-z]{32})-([^/]+)$"))))
5c0f1845 1448
e3d74106
LC
1449(define (store-path-package-name path)
1450 "Return the package name part of PATH, a file name in the store."
5c0f1845
LC
1451 (let ((path-rx (store-regexp* (%store-prefix))))
1452 (and=> (regexp-exec path-rx path)
1453 (cut match:substring <> 2))))
2c6ab6cc
LC
1454
1455(define (store-path-hash-part path)
1456 "Return the hash part of PATH as a base32 string, or #f if PATH is not a
1457syntactically valid store path."
5c0f1845 1458 (let ((path-rx (store-regexp* (%store-prefix))))
2c6ab6cc
LC
1459 (and=> (regexp-exec path-rx path)
1460 (cut match:substring <> 1))))
eddd4077
LC
1461
1462(define (log-file store file)
1463 "Return the build log file for FILE, or #f if none could be found. FILE
1464must be an absolute store file name, or a derivation file name."
eddd4077 1465 (cond ((derivation-path? file)
021a201f 1466 (let* ((base (basename file))
80d0447c 1467 (log (string-append (dirname %state-directory) ; XXX
f5768afa 1468 "/log/guix/drvs/"
021a201f
LC
1469 (string-take base 2) "/"
1470 (string-drop base 2)))
1471 (log.bz2 (string-append log ".bz2")))
1472 (cond ((file-exists? log.bz2) log.bz2)
1473 ((file-exists? log) log)
1474 (else #f))))
eddd4077
LC
1475 (else
1476 (match (valid-derivers store file)
1477 ((derivers ...)
1478 ;; Return the first that works.
1479 (any (cut log-file store <>) derivers))
1480 (_ #f)))))
3b5cd17a
LC
1481
1482;;; Local Variables:
1483;;; eval: (put 'system-error-to-connection-error 'scheme-indent-function 1)
1484;;; End: