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