gnu: gcc-5.1: Add specific libvtv patch.
[jackhill/guix/guix.git] / guix / store.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
e87f0591 2;;; Copyright © 2012, 2013, 2014, 2015 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)
0f41c26f 22 #:use-module (guix serialization)
e87f0591 23 #:use-module (guix monads)
a9d2a105 24 #:autoload (guix base32) (bytevector->base32-string)
77d3cf08
LC
25 #:use-module (rnrs bytevectors)
26 #:use-module (rnrs io ports)
27 #:use-module (srfi srfi-1)
28 #:use-module (srfi srfi-9)
bf8e7fc5 29 #:use-module (srfi srfi-9 gnu)
77d3cf08 30 #:use-module (srfi srfi-26)
e87088c9
LC
31 #:use-module (srfi srfi-34)
32 #:use-module (srfi srfi-35)
26bbbb95 33 #:use-module (srfi srfi-39)
77d3cf08 34 #:use-module (ice-9 match)
e3d74106 35 #:use-module (ice-9 regex)
3f1e6939 36 #:use-module (ice-9 vlist)
6bfec3ed 37 #:use-module (ice-9 popen)
9fd72fb1 38 #:export (%daemon-socket-file
a9d2a105 39 %gc-roots-directory
9fd72fb1
LC
40
41 nix-server?
77d3cf08
LC
42 nix-server-major-version
43 nix-server-minor-version
44 nix-server-socket
45
e87088c9 46 &nix-error nix-error?
ef86c39f
LC
47 &nix-connection-error nix-connection-error?
48 nix-connection-error-file
49 nix-connection-error-code
e87088c9
LC
50 &nix-protocol-error nix-protocol-error?
51 nix-protocol-error-message
52 nix-protocol-error-status
53
26bbbb95
LC
54 hash-algo
55
77d3cf08 56 open-connection
3abaf0c4 57 close-connection
ce4a4829 58 with-store
77d3cf08 59 set-build-options
31ef99a8 60 valid-path?
82058eff 61 query-path-hash
11e7a6cf 62 hash-part->path
533d1768 63 query-path-info
77d3cf08
LC
64 add-text-to-store
65 add-to-store
abac874b
LC
66 build-things
67 build
3259877d 68 add-temp-root
34811f02 69 add-indirect-root
a9d2a105
LC
70 add-permanent-root
71 remove-permanent-root
26bbbb95 72
0f3d2504
LC
73 substitutable?
74 substitutable-path
75 substitutable-deriver
76 substitutable-references
77 substitutable-download-size
78 substitutable-nar-size
79 has-substitutes?
80 substitutable-paths
81 substitutable-path-info
82
533d1768
DT
83 path-info?
84 path-info-deriver
85 path-info-hash
86 path-info-references
87 path-info-registration-time
88 path-info-nar-size
89
fae31edc 90 references
3f1e6939 91 requisites
fae31edc 92 referrers
50add477 93 topologically-sorted
fae31edc
LC
94 valid-derivers
95 query-derivation-outputs
3259877d
LC
96 live-paths
97 dead-paths
98 collect-garbage
99 delete-paths
526382ff
LC
100 import-paths
101 export-paths
3259877d 102
dcee50c1
LC
103 current-build-output-port
104
6bfec3ed
LC
105 register-path
106
e87f0591
LC
107 %store-monad
108 store-bind
109 store-return
110 store-lift
023d9892 111 store-lower
e87f0591
LC
112 run-with-store
113 %guile-for-build
114 text-file
115 interned-file
116
26bbbb95
LC
117 %store-prefix
118 store-path?
9336e5b5 119 direct-store-path?
e3d74106 120 derivation-path?
2c6ab6cc 121 store-path-package-name
eddd4077
LC
122 store-path-hash-part
123 log-file))
77d3cf08 124
63193ebf 125(define %protocol-version #x10c)
77d3cf08 126
d66b704b
LC
127(define %worker-magic-1 #x6e697863) ; "nixc"
128(define %worker-magic-2 #x6478696f) ; "dxio"
77d3cf08
LC
129
130(define (protocol-major magic)
131 (logand magic #xff00))
132(define (protocol-minor magic)
133 (logand magic #x00ff))
134
135(define-syntax define-enumerate-type
136 (syntax-rules ()
137 ((_ name->int (name id) ...)
138 (define-syntax name->int
139 (syntax-rules (name ...)
140 ((_ name) id) ...)))))
141
142(define-enumerate-type operation-id
143 ;; operation numbers from worker-protocol.hh
144 (quit 0)
145 (valid-path? 1)
146 (has-substitutes? 3)
147 (query-path-hash 4)
148 (query-references 5)
149 (query-referrers 6)
150 (add-to-store 7)
151 (add-text-to-store 8)
abac874b 152 (build-things 9)
77d3cf08
LC
153 (ensure-path 10)
154 (add-temp-root 11)
155 (add-indirect-root 12)
156 (sync-with-gc 13)
157 (find-roots 14)
158 (export-path 16)
159 (query-deriver 18)
160 (set-options 19)
161 (collect-garbage 20)
63193ebf 162 ;;(query-substitutable-path-info 21) ; obsolete as of #x10c
77d3cf08 163 (query-derivation-outputs 22)
63193ebf 164 (query-all-valid-paths 23)
77d3cf08
LC
165 (query-failed-paths 24)
166 (clear-failed-paths 25)
167 (query-path-info 26)
168 (import-paths 27)
63193ebf
LC
169 (query-derivation-output-names 28)
170 (query-path-from-hash-part 29)
171 (query-substitutable-path-infos 30)
172 (query-valid-paths 31)
fae31edc
LC
173 (query-substitutable-paths 32)
174 (query-valid-derivers 33))
77d3cf08
LC
175
176(define-enumerate-type hash-algo
177 ;; hash.hh
178 (md5 1)
179 (sha1 2)
180 (sha256 3))
181
3259877d
LC
182(define-enumerate-type gc-action
183 ;; store-api.hh
184 (return-live 0)
185 (return-dead 1)
186 (delete-dead 2)
187 (delete-specific 3))
188
77d3cf08 189(define %default-socket-path
80d0447c 190 (string-append %state-directory "/daemon-socket/socket"))
77d3cf08 191
9fd72fb1
LC
192(define %daemon-socket-file
193 ;; File name of the socket the daemon listens too.
194 (make-parameter (or (getenv "GUIX_DAEMON_SOCKET")
195 %default-socket-path)))
196
197
77d3cf08 198\f
0f3d2504
LC
199;; Information about a substitutable store path.
200(define-record-type <substitutable>
201 (substitutable path deriver refs dl-size nar-size)
202 substitutable?
203 (path substitutable-path)
204 (deriver substitutable-deriver)
205 (refs substitutable-references)
206 (dl-size substitutable-download-size)
207 (nar-size substitutable-nar-size))
208
209(define (read-substitutable-path-list p)
210 (let loop ((len (read-int p))
211 (result '()))
212 (if (zero? len)
213 (reverse result)
214 (let ((path (read-store-path p))
215 (deriver (read-store-path p))
216 (refs (read-store-path-list p))
217 (dl-size (read-long-long p))
218 (nar-size (read-long-long p)))
219 (loop (- len 1)
220 (cons (substitutable path deriver refs dl-size nar-size)
221 result))))))
222
533d1768
DT
223;; Information about a store path.
224(define-record-type <path-info>
225 (path-info deriver hash references registration-time nar-size)
226 path-info?
227 (deriver path-info-deriver)
228 (hash path-info-hash)
229 (references path-info-references)
230 (registration-time path-info-registration-time)
231 (nar-size path-info-nar-size))
232
233(define (read-path-info p)
234 (let ((deriver (read-store-path p))
235 (hash (base16-string->bytevector (read-string p)))
236 (refs (read-store-path-list p))
237 (registration-time (read-int p))
238 (nar-size (read-long-long p)))
239 (path-info deriver hash refs registration-time nar-size)))
240
77d3cf08 241(define-syntax write-arg
6c20d1d0 242 (syntax-rules (integer boolean file string string-list string-pairs
3259877d 243 store-path store-path-list base16)
77d3cf08
LC
244 ((_ integer arg p)
245 (write-int arg p))
246 ((_ boolean arg p)
247 (write-int (if arg 1 0) p))
248 ((_ file arg p)
249 (write-file arg p))
250 ((_ string arg p)
251 (write-string arg p))
252 ((_ string-list arg p)
82058eff 253 (write-string-list arg p))
6c20d1d0
LC
254 ((_ string-pairs arg p)
255 (write-string-pairs arg p))
3259877d
LC
256 ((_ store-path arg p)
257 (write-store-path arg p))
258 ((_ store-path-list arg p)
259 (write-store-path-list arg p))
82058eff
LC
260 ((_ base16 arg p)
261 (write-string (bytevector->base16-string arg) p))))
77d3cf08
LC
262
263(define-syntax read-arg
0f3d2504 264 (syntax-rules (integer boolean string store-path store-path-list
533d1768 265 substitutable-path-list path-info base16)
77d3cf08
LC
266 ((_ integer p)
267 (read-int p))
268 ((_ boolean p)
269 (not (zero? (read-int p))))
270 ((_ string p)
271 (read-string p))
272 ((_ store-path p)
82058eff 273 (read-store-path p))
3259877d
LC
274 ((_ store-path-list p)
275 (read-store-path-list p))
0f3d2504
LC
276 ((_ substitutable-path-list p)
277 (read-substitutable-path-list p))
533d1768
DT
278 ((_ path-info p)
279 (read-path-info p))
0f3d2504 280 ((_ base16 p)
82058eff 281 (base16-string->bytevector (read-string p)))))
77d3cf08
LC
282
283\f
284;; remote-store.cc
285
286(define-record-type <nix-server>
2c3f47ee 287 (%make-nix-server socket major minor
bdcf35a6 288 ats-cache atts-cache)
77d3cf08
LC
289 nix-server?
290 (socket nix-server-socket)
291 (major nix-server-major-version)
2c3f47ee
LC
292 (minor nix-server-minor-version)
293
294 ;; Caches. We keep them per-connection, because store paths build
295 ;; during the session are temporary GC roots kept for the duration of
296 ;; the session.
bdcf35a6
LC
297 (ats-cache nix-server-add-to-store-cache)
298 (atts-cache nix-server-add-text-to-store-cache))
77d3cf08 299
bf8e7fc5
LC
300(set-record-type-printer! <nix-server>
301 (lambda (obj port)
302 (format port "#<build-daemon ~a.~a ~a>"
303 (nix-server-major-version obj)
304 (nix-server-minor-version obj)
305 (number->string (object-address obj)
306 16))))
307
e87088c9
LC
308(define-condition-type &nix-error &error
309 nix-error?)
310
ef86c39f
LC
311(define-condition-type &nix-connection-error &nix-error
312 nix-connection-error?
313 (file nix-connection-error-file)
314 (errno nix-connection-error-code))
315
e87088c9
LC
316(define-condition-type &nix-protocol-error &nix-error
317 nix-protocol-error?
318 (message nix-protocol-error-message)
319 (status nix-protocol-error-status))
320
9fd72fb1 321(define* (open-connection #:optional (file (%daemon-socket-file))
e36a7172 322 #:key (reserve-space? #t))
e531ac2a
LC
323 "Connect to the daemon over the Unix-domain socket at FILE. When
324RESERVE-SPACE? is true, instruct it to reserve a little bit of extra
325space on the file system so that the garbage collector can still
326operate, should the disk become full. Return a server object."
77d3cf08
LC
327 (let ((s (with-fluids ((%default-port-encoding #f))
328 ;; This trick allows use of the `scm_c_read' optimization.
329 (socket PF_UNIX SOCK_STREAM 0)))
330 (a (make-socket-address PF_UNIX file)))
df1fab58 331
ef86c39f
LC
332 (catch 'system-error
333 (cut connect s a)
334 (lambda args
335 ;; Translate the error to something user-friendly.
336 (let ((errno (system-error-errno args)))
337 (raise (condition (&nix-connection-error
338 (file file)
339 (errno errno)))))))
340
77d3cf08
LC
341 (write-int %worker-magic-1 s)
342 (let ((r (read-int s)))
343 (and (eqv? r %worker-magic-2)
344 (let ((v (read-int s)))
345 (and (eqv? (protocol-major %protocol-version)
346 (protocol-major v))
347 (begin
348 (write-int %protocol-version s)
e36a7172
LC
349 (if (>= (protocol-minor v) 11)
350 (write-int (if reserve-space? 1 0) s))
77d3cf08
LC
351 (let ((s (%make-nix-server s
352 (protocol-major v)
2c3f47ee 353 (protocol-minor v)
fce2394e
LC
354 (make-hash-table 100)
355 (make-hash-table 100))))
34fcbe3a
LC
356 (let loop ((done? (process-stderr s)))
357 (or done? (process-stderr s)))
77d3cf08
LC
358 s))))))))
359
3abaf0c4
LC
360(define (close-connection server)
361 "Close the connection to SERVER."
362 (close (nix-server-socket server)))
363
ce4a4829
LC
364(define-syntax-rule (with-store store exp ...)
365 "Bind STORE to an open connection to the store and evaluate EXPs;
366automatically close the store when the dynamic extent of EXP is left."
367 (let ((store (open-connection)))
368 (dynamic-wind
369 (const #f)
370 (lambda ()
371 exp ...)
372 (lambda ()
373 (false-if-exception (close-connection store))))))
374
dcee50c1
LC
375(define current-build-output-port
376 ;; The port where build output is sent.
377 (make-parameter (current-error-port)))
378
526382ff
LC
379(define* (dump-port in out
380 #:optional len
381 #:key (buffer-size 16384))
382 "Read LEN bytes from IN (or as much as possible if LEN is #f) and write it
383to OUT, using chunks of BUFFER-SIZE bytes."
384 (define buffer
385 (make-bytevector buffer-size))
386
387 (let loop ((total 0)
388 (bytes (get-bytevector-n! in buffer 0
389 (if len
390 (min len buffer-size)
391 buffer-size))))
392 (or (eof-object? bytes)
393 (and len (= total len))
394 (let ((total (+ total bytes)))
395 (put-bytevector out buffer 0 bytes)
396 (loop total
397 (get-bytevector-n! in buffer 0
398 (if len
399 (min (- len total) buffer-size)
400 buffer-size)))))))
401
d28869af
LC
402(define %newlines
403 ;; Newline characters triggering a flush of 'current-build-output-port'.
404 ;; Unlike Guile's _IOLBF, we flush upon #\return so that progress reports
405 ;; that use that trick are correctly displayed.
406 (char-set #\newline #\return))
407
526382ff 408(define* (process-stderr server #:optional user-port)
dcee50c1
LC
409 "Read standard output and standard error from SERVER, writing it to
410CURRENT-BUILD-OUTPUT-PORT. Return #t when SERVER is done sending data, and
411#f otherwise; in the latter case, the caller should call `process-stderr'
bbdb3ffa
LC
412again until #t is returned or an error is raised.
413
414Since the build process's output cannot be assumed to be UTF-8, we
415conservatively consider it to be Latin-1, thereby avoiding possible
416encoding conversion errors."
77d3cf08
LC
417 (define p
418 (nix-server-socket server))
419
420 ;; magic cookies from worker-protocol.hh
5674a3fd
LC
421 (define %stderr-next #x6f6c6d67) ; "olmg", build log
422 (define %stderr-read #x64617461) ; "data", data needed from source
423 (define %stderr-write #x64617416) ; "dat\x16", data for sink
424 (define %stderr-last #x616c7473) ; "alts", we're done
425 (define %stderr-error #x63787470) ; "cxtp", error reporting
77d3cf08
LC
426
427 (let ((k (read-int p)))
428 (cond ((= k %stderr-write)
526382ff
LC
429 ;; Write a byte stream to USER-PORT.
430 (let* ((len (read-int p))
431 (m (modulo len 8)))
432 (dump-port p user-port len)
433 (unless (zero? m)
434 ;; Consume padding, as for strings.
435 (get-bytevector-n p (- 8 m))))
dcee50c1 436 #f)
77d3cf08 437 ((= k %stderr-read)
526382ff 438 ;; Read a byte stream from USER-PORT.
5895f244
LC
439 ;; Note: Avoid 'get-bytevector-n' to work around
440 ;; <http://bugs.gnu.org/17591> in Guile up to 2.0.11.
526382ff 441 (let* ((max-len (read-int p))
5895f244
LC
442 (data (make-bytevector max-len))
443 (len (get-bytevector-n! user-port data 0 max-len)))
526382ff 444 (write-int len p)
5895f244 445 (put-bytevector p data 0 len)
526382ff 446 (write-padding len p)
dcee50c1 447 #f))
77d3cf08 448 ((= k %stderr-next)
ce72c780
LC
449 ;; Log a string. Build logs are usually UTF-8-encoded, but they
450 ;; may also contain arbitrary byte sequences that should not cause
451 ;; this to fail. Thus, use the permissive
452 ;; 'read-maybe-utf8-string'.
453 (let ((s (read-maybe-utf8-string p)))
dcee50c1 454 (display s (current-build-output-port))
d28869af
LC
455 (when (string-any %newlines s)
456 (flush-output-port (current-build-output-port)))
dcee50c1 457 #f))
77d3cf08 458 ((= k %stderr-error)
526382ff 459 ;; Report an error.
ce72c780 460 (let ((error (read-maybe-utf8-string p))
0ff3e3aa
LC
461 ;; Currently the daemon fails to send a status code for early
462 ;; errors like DB schema version mismatches, so check for EOF.
463 (status (if (and (>= (nix-server-minor-version server) 8)
464 (not (eof-object? (lookahead-u8 p))))
77d3cf08
LC
465 (read-int p)
466 1)))
e87088c9
LC
467 (raise (condition (&nix-protocol-error
468 (message error)
469 (status status))))))
77d3cf08 470 ((= k %stderr-last)
dcee50c1 471 ;; The daemon is done (see `stopWork' in `nix-worker.cc'.)
77d3cf08
LC
472 #t)
473 (else
e87088c9
LC
474 (raise (condition (&nix-protocol-error
475 (message "invalid error code")
476 (status k))))))))
77d3cf08 477
4d581220
LC
478(define %default-substitute-urls
479 ;; Default list of substituters.
480 '("http://hydra.gnu.org"))
481
77d3cf08 482(define* (set-build-options server
c3eb878f 483 #:key keep-failed? keep-going? fallback?
77d3cf08 484 (verbosity 0)
8b47758f 485 (max-build-jobs 1)
6c20d1d0 486 timeout
77d3cf08
LC
487 (max-silent-time 3600)
488 (use-build-hook? #t)
489 (build-verbosity 0)
490 (log-type 0)
e036c31b 491 (print-build-trace #t)
8b47758f 492 (build-cores (current-processor-count))
63193ebf 493 (use-substitutes? #t)
4d581220
LC
494
495 ;; Client-provided substitute URLs. For
496 ;; unprivileged clients, these are considered
497 ;; "untrusted"; for root, they override the
498 ;; daemon's settings.
499 (substitute-urls %default-substitute-urls))
77d3cf08
LC
500 ;; Must be called after `open-connection'.
501
502 (define socket
503 (nix-server-socket server))
504
505 (let-syntax ((send (syntax-rules ()
e036c31b
LC
506 ((_ (type option) ...)
507 (begin
508 (write-arg type option socket)
509 ...)))))
510 (write-int (operation-id set-options) socket)
511 (send (boolean keep-failed?) (boolean keep-going?)
c3eb878f 512 (boolean fallback?) (integer verbosity)
e036c31b 513 (integer max-build-jobs) (integer max-silent-time))
371e87d2
LC
514 (when (>= (nix-server-minor-version server) 2)
515 (send (boolean use-build-hook?)))
516 (when (>= (nix-server-minor-version server) 4)
517 (send (integer build-verbosity) (integer log-type)
518 (boolean print-build-trace)))
519 (when (>= (nix-server-minor-version server) 6)
520 (send (integer build-cores)))
521 (when (>= (nix-server-minor-version server) 10)
522 (send (boolean use-substitutes?)))
523 (when (>= (nix-server-minor-version server) 12)
41c45e78
LC
524 (let ((pairs `(,@(if timeout
525 `(("build-timeout" . ,(number->string timeout)))
526 '())
527 ("substitute-urls" . ,(string-join substitute-urls)))))
f401b1e9 528 (send (string-pairs pairs))))
dcee50c1
LC
529 (let loop ((done? (process-stderr server)))
530 (or done? (process-stderr server)))))
77d3cf08 531
fd060fd3 532(define-syntax operation
77d3cf08 533 (syntax-rules ()
fd060fd3 534 "Define a client-side RPC stub for the given operation."
3259877d 535 ((_ (name (type arg) ...) docstring return ...)
fd060fd3 536 (lambda (server arg ...)
77d3cf08
LC
537 docstring
538 (let ((s (nix-server-socket server)))
539 (write-int (operation-id name) s)
540 (write-arg type arg s)
541 ...
dcee50c1
LC
542 ;; Loop until the server is done sending error output.
543 (let loop ((done? (process-stderr server)))
544 (or done? (loop (process-stderr server))))
3259877d 545 (values (read-arg return s) ...))))))
77d3cf08 546
fd060fd3
LC
547(define-syntax-rule (define-operation (name args ...)
548 docstring return ...)
549 (define name
550 (operation (name args ...) docstring return ...)))
551
31ef99a8
LC
552(define-operation (valid-path? (string path))
553 "Return #t when PATH is a valid store path."
554 boolean)
555
63193ebf 556(define-operation (query-path-hash (store-path path))
82058eff
LC
557 "Return the SHA256 hash of PATH as a bytevector."
558 base16)
559
11e7a6cf
LC
560(define hash-part->path
561 (let ((query-path-from-hash-part
562 (operation (query-path-from-hash-part (string hash))
563 #f
564 store-path)))
565 (lambda (server hash-part)
566 "Return the store path whose hash part is HASH-PART (a nix-base32
567string). Raise an error if no such path exists."
568 ;; This RPC is primarily used by Hydra to reply to HTTP GETs of
569 ;; /HASH.narinfo.
570 (query-path-from-hash-part server hash-part))))
571
533d1768
DT
572(define-operation (query-path-info (store-path path))
573 "Return the info (hash, references, etc.) for PATH."
574 path-info)
575
fd060fd3
LC
576(define add-text-to-store
577 ;; A memoizing version of `add-to-store', to avoid repeated RPCs with
578 ;; the very same arguments during a given session.
579 (let ((add-text-to-store
580 (operation (add-text-to-store (string name) (string text)
581 (string-list references))
582 #f
583 store-path)))
cfbf9160 584 (lambda* (server name text #:optional (references '()))
bdcf35a6
LC
585 "Add TEXT under file NAME in the store, and return its store path.
586REFERENCES is the list of store paths referred to by the resulting store
587path."
fce2394e 588 (let ((args `(,text ,name ,references))
bdcf35a6
LC
589 (cache (nix-server-add-text-to-store-cache server)))
590 (or (hash-ref cache args)
fd060fd3 591 (let ((path (add-text-to-store server name text references)))
bdcf35a6
LC
592 (hash-set! cache args path)
593 path))))))
594
fd060fd3 595(define add-to-store
a7b6ffee
LC
596 ;; A memoizing version of `add-to-store'. This is important because
597 ;; `add-to-store' leads to huge data transfers to the server, and
598 ;; because it's often called many times with the very same argument.
fd060fd3
LC
599 (let ((add-to-store (operation (add-to-store (string basename)
600 (boolean fixed?) ; obsolete, must be #t
601 (boolean recursive?)
602 (string hash-algo)
603 (file file-name))
604 #f
605 store-path)))
a9ebd9ef
LC
606 (lambda (server basename recursive? hash-algo file-name)
607 "Add the contents of FILE-NAME under BASENAME to the store. When
608RECURSIVE? is true and FILE-NAME designates a directory, the contents of
609FILE-NAME are added recursively; if FILE-NAME designates a flat file and
610RECURSIVE? is true, its contents are added, and its permission bits are
611kept. HASH-ALGO must be a string such as \"sha256\"."
2c3f47ee 612 (let* ((st (stat file-name #f))
fce2394e 613 (args `(,st ,basename ,recursive? ,hash-algo))
2c3f47ee 614 (cache (nix-server-add-to-store-cache server)))
a7b6ffee 615 (or (and st (hash-ref cache args))
a9ebd9ef 616 (let ((path (add-to-store server basename #t recursive?
a7b6ffee
LC
617 hash-algo file-name)))
618 (hash-set! cache args path)
619 path))))))
620
abac874b
LC
621(define-operation (build-things (string-list things))
622 "Build THINGS, a list of store items which may be either '.drv' files or
623outputs, and return when the worker is done building them. Elements of THINGS
624that are not derivations can only be substituted and not built locally.
dcee50c1 625Return #t on success."
77d3cf08 626 boolean)
26bbbb95 627
d3648e01
LC
628(define-operation (add-temp-root (store-path path))
629 "Make PATH a temporary root for the duration of the current session.
630Return #t."
631 boolean)
632
34811f02 633(define-operation (add-indirect-root (string file-name))
a9d2a105
LC
634 "Make the symlink FILE-NAME an indirect root for the garbage collector:
635whatever store item FILE-NAME points to will not be collected. Return #t on
636success.
637
638FILE-NAME can be anywhere on the file system, but it must be an absolute file
639name--it is the caller's responsibility to ensure that it is an absolute file
640name."
34811f02
LC
641 boolean)
642
a9d2a105
LC
643(define %gc-roots-directory
644 ;; The place where garbage collector roots (symlinks) are kept.
645 (string-append %state-directory "/gcroots"))
646
647(define (add-permanent-root target)
648 "Add a garbage collector root pointing to TARGET, an element of the store,
649preventing TARGET from even being collected. This can also be used if TARGET
650does not exist yet.
651
652Raise an error if the caller does not have write access to the GC root
653directory."
654 (let* ((root (string-append %gc-roots-directory "/" (basename target))))
655 (catch 'system-error
656 (lambda ()
657 (symlink target root))
658 (lambda args
659 ;; If ROOT already exists, this is fine; otherwise, re-throw.
660 (unless (= EEXIST (system-error-errno args))
661 (apply throw args))))))
662
663(define (remove-permanent-root target)
664 "Remove the permanent garbage collector root pointing to TARGET. Raise an
665error if there is no such root."
666 (delete-file (string-append %gc-roots-directory "/" (basename target))))
667
fae31edc
LC
668(define references
669 (operation (query-references (store-path path))
670 "Return the list of references of PATH."
671 store-path-list))
672
3f1e6939
LC
673(define* (fold-path store proc seed path
674 #:optional (relatives (cut references store <>)))
675 "Call PROC for each of the RELATIVES of PATH, exactly once, and return the
676result formed from the successive calls to PROC, the first of which is passed
677SEED."
678 (let loop ((paths (list path))
679 (result seed)
680 (seen vlist-null))
681 (match paths
682 ((path rest ...)
683 (if (vhash-assoc path seen)
684 (loop rest result seen)
685 (let ((seen (vhash-cons path #t seen))
686 (rest (append rest (relatives path)))
687 (result (proc path result)))
688 (loop rest result seen))))
689 (()
690 result))))
691
692(define (requisites store path)
693 "Return the requisites of PATH, including PATH---i.e., its closure (all its
694references, recursively)."
695 (fold-path store cons '() path))
696
50add477
LC
697(define (topologically-sorted store paths)
698 "Return a list containing PATHS and all their references sorted in
699topological order."
700 (define (traverse)
701 ;; Do a simple depth-first traversal of all of PATHS.
702 (let loop ((paths paths)
703 (visited vlist-null)
704 (result '()))
705 (define (visit n)
706 (vhash-cons n #t visited))
707
708 (define (visited? n)
709 (vhash-assoc n visited))
710
711 (match paths
712 ((head tail ...)
713 (if (visited? head)
714 (loop tail visited result)
715 (call-with-values
716 (lambda ()
717 (loop (references store head)
718 (visit head)
719 result))
720 (lambda (visited result)
721 (loop tail
722 visited
723 (cons head result))))))
724 (()
725 (values visited result)))))
726
727 (call-with-values traverse
728 (lambda (_ result)
729 (reverse result))))
730
fae31edc
LC
731(define referrers
732 (operation (query-referrers (store-path path))
733 "Return the list of path that refer to PATH."
734 store-path-list))
735
736(define valid-derivers
737 (operation (query-valid-derivers (store-path path))
738 "Return the list of valid \"derivers\" of PATH---i.e., all the
739.drv present in the store that have PATH among their outputs."
740 store-path-list))
741
742(define query-derivation-outputs ; avoid name clash with `derivation-outputs'
743 (operation (query-derivation-outputs (store-path path))
744 "Return the list of outputs of PATH, a .drv file."
745 store-path-list))
746
0f3d2504
LC
747(define-operation (has-substitutes? (store-path path))
748 "Return #t if binary substitutes are available for PATH, and #f otherwise."
749 boolean)
750
751(define substitutable-paths
752 (operation (query-substitutable-paths (store-path-list paths))
753 "Return the subset of PATHS that is substitutable."
754 store-path-list))
755
756(define substitutable-path-info
f65cf81a 757 (operation (query-substitutable-path-infos (store-path-list paths))
0f3d2504
LC
758 "Return information about the subset of PATHS that is
759substitutable. For each substitutable path, a `substitutable?' object is
760returned."
761 substitutable-path-list))
762
3259877d
LC
763(define (run-gc server action to-delete min-freed)
764 "Perform the garbage-collector operation ACTION, one of the
765`gc-action' values. When ACTION is `delete-specific', the TO-DELETE is
766the list of store paths to delete. IGNORE-LIVENESS? should always be
767#f. MIN-FREED is the minimum amount of disk space to be freed, in
768bytes, before the GC can stop. Return the list of store paths delete,
769and the number of bytes freed."
770 (let ((s (nix-server-socket server)))
771 (write-int (operation-id collect-garbage) s)
772 (write-int action s)
773 (write-store-path-list to-delete s)
774 (write-arg boolean #f s) ; ignore-liveness?
775 (write-long-long min-freed s)
776 (write-int 0 s) ; obsolete
777 (when (>= (nix-server-minor-version server) 5)
778 ;; Obsolete `use-atime' and `max-atime' parameters.
779 (write-int 0 s)
780 (write-int 0 s))
781
782 ;; Loop until the server is done sending error output.
783 (let loop ((done? (process-stderr server)))
784 (or done? (loop (process-stderr server))))
785
786 (let ((paths (read-store-path-list s))
787 (freed (read-long-long s))
788 (obsolete (read-long-long s)))
000c59b6
LC
789 (unless (null? paths)
790 ;; To be on the safe side, completely invalidate both caches.
791 ;; Otherwise we could end up returning store paths that are no longer
792 ;; valid.
793 (hash-clear! (nix-server-add-to-store-cache server))
794 (hash-clear! (nix-server-add-text-to-store-cache server)))
795
3259877d
LC
796 (values paths freed))))
797
798(define-syntax-rule (%long-long-max)
799 ;; Maximum unsigned 64-bit integer.
800 (- (expt 2 64) 1))
801
802(define (live-paths server)
803 "Return the list of live store paths---i.e., store paths still
804referenced, and thus not subject to being garbage-collected."
805 (run-gc server (gc-action return-live) '() (%long-long-max)))
806
807(define (dead-paths server)
808 "Return the list of dead store paths---i.e., store paths no longer
809referenced, and thus subject to being garbage-collected."
810 (run-gc server (gc-action return-dead) '() (%long-long-max)))
811
812(define* (collect-garbage server #:optional (min-freed (%long-long-max)))
813 "Collect garbage from the store at SERVER. If MIN-FREED is non-zero,
814then collect at least MIN-FREED bytes. Return the paths that were
815collected, and the number of bytes freed."
816 (run-gc server (gc-action delete-dead) '() min-freed))
817
818(define* (delete-paths server paths #:optional (min-freed (%long-long-max)))
819 "Delete PATHS from the store at SERVER, if they are no longer
820referenced. If MIN-FREED is non-zero, then stop after at least
821MIN-FREED bytes have been collected. Return the paths that were
822collected, and the number of bytes freed."
823 (run-gc server (gc-action delete-specific) paths min-freed))
824
526382ff
LC
825(define (import-paths server port)
826 "Import the set of store paths read from PORT into SERVER's store. An error
827is raised if the set of paths read from PORT is not signed (as per
828'export-path #:sign? #t'.) Return the list of store paths imported."
829 (let ((s (nix-server-socket server)))
830 (write-int (operation-id import-paths) s)
831 (let loop ((done? (process-stderr server port)))
832 (or done? (loop (process-stderr server port))))
833 (read-store-path-list s)))
834
835(define* (export-path server path port #:key (sign? #t))
836 "Export PATH to PORT. When SIGN? is true, sign it."
837 (let ((s (nix-server-socket server)))
838 (write-int (operation-id export-path) s)
839 (write-store-path path s)
840 (write-arg boolean sign? s)
841 (let loop ((done? (process-stderr server port)))
842 (or done? (loop (process-stderr server port))))
843 (= 1 (read-int s))))
844
5b3d863f 845(define* (export-paths server paths port #:key (sign? #t) recursive?)
99fbddf9 846 "Export the store paths listed in PATHS to PORT, in topological order,
5b3d863f
LC
847signing them if SIGN? is true. When RECURSIVE? is true, export the closure of
848PATHS---i.e., PATHS and all their dependencies."
cafb92d8 849 (define ordered
5b3d863f
LC
850 (let ((sorted (topologically-sorted server paths)))
851 ;; When RECURSIVE? is #f, filter out the references of PATHS.
852 (if recursive?
853 sorted
854 (filter (cut member <> paths) sorted))))
cafb92d8 855
1d506993
LC
856 (let loop ((paths ordered))
857 (match paths
858 (()
859 (write-int 0 port))
860 ((head tail ...)
861 (write-int 1 port)
862 (and (export-path server head port #:sign? sign?)
863 (loop tail))))))
526382ff 864
6bfec3ed 865(define* (register-path path
689142cd
LC
866 #:key (references '()) deriver prefix
867 state-directory)
6bfec3ed 868 "Register PATH as a valid store file, with REFERENCES as its list of
bb31e0a3
LC
869references, and DERIVER as its deriver (.drv that led to it.) If PREFIX is
870not #f, it must be the name of the directory containing the new store to
689142cd
LC
871initialize; if STATE-DIRECTORY is not #f, it must be a string containing the
872absolute file name to the state directory of the store being initialized.
873Return #t on success.
6bfec3ed
LC
874
875Use with care as it directly modifies the store! This is primarily meant to
876be used internally by the daemon's build hook."
877 ;; Currently this is implemented by calling out to the fine C++ blob.
878 (catch 'system-error
879 (lambda ()
e901ef29 880 (let ((pipe (apply open-pipe* OPEN_WRITE %guix-register-program
689142cd
LC
881 `(,@(if prefix
882 `("--prefix" ,prefix)
883 '())
884 ,@(if state-directory
885 `("--state-directory" ,state-directory)
886 '())))))
6bfec3ed
LC
887 (and pipe
888 (begin
889 (format pipe "~a~%~a~%~a~%"
890 path (or deriver "") (length references))
891 (for-each (cut format pipe "~a~%" <>) references)
892 (zero? (close-pipe pipe))))))
893 (lambda args
894 ;; Failed to run %GUIX-REGISTER-PROGRAM.
895 #f)))
896
26bbbb95 897\f
e87f0591
LC
898;;;
899;;; Store monad.
900;;;
901
4e190c28
LC
902(define-syntax-rule (define-alias new old)
903 (define-syntax new (identifier-syntax old)))
e87f0591 904
4e190c28
LC
905;; The store monad allows us to (1) build sequences of operations in the
906;; store, and (2) make the store an implicit part of the execution context,
907;; rather than a parameter of every single function.
908(define-alias %store-monad %state-monad)
909(define-alias store-return state-return)
910(define-alias store-bind state-bind)
e87f0591 911
5808dcc2
LC
912(define (preserve-documentation original proc)
913 "Return PROC with documentation taken from ORIGINAL."
914 (set-object-property! proc 'documentation
915 (procedure-property original 'documentation))
916 proc)
917
e87f0591
LC
918(define (store-lift proc)
919 "Lift PROC, a procedure whose first argument is a connection to the store,
920in the store monad."
5808dcc2
LC
921 (preserve-documentation proc
922 (lambda args
923 (lambda (store)
924 (values (apply proc store args) store)))))
e87f0591 925
023d9892
LC
926(define (store-lower proc)
927 "Lower PROC, a monadic procedure in %STORE-MONAD, to a \"normal\" procedure
928taking the store as its first argument."
5808dcc2
LC
929 (preserve-documentation proc
930 (lambda (store . args)
931 (run-with-store store (apply proc args)))))
023d9892 932
e87f0591
LC
933;;
934;; Store monad operators.
935;;
936
ad372953
LC
937(define* (text-file name text
938 #:optional (references '()))
e87f0591 939 "Return as a monadic value the absolute file name in the store of the file
ad372953
LC
940containing TEXT, a string. REFERENCES is a list of store items that the
941resulting text file refers to; it defaults to the empty list."
e87f0591 942 (lambda (store)
ad372953 943 (values (add-text-to-store store name text references)
4e190c28 944 store)))
e87f0591
LC
945
946(define* (interned-file file #:optional name
947 #:key (recursive? #t))
948 "Return the name of FILE once interned in the store. Use NAME as its store
949name, or the basename of FILE if NAME is omitted.
950
951When RECURSIVE? is true, the contents of FILE are added recursively; if FILE
952designates a flat file and RECURSIVE? is true, its contents are added, and its
953permission bits are kept."
954 (lambda (store)
4e190c28
LC
955 (values (add-to-store store (or name (basename file))
956 recursive? "sha256" file)
957 store)))
e87f0591 958
abac874b
LC
959(define build
960 ;; Monadic variant of 'build-things'.
961 (store-lift build-things))
962
e87f0591
LC
963(define %guile-for-build
964 ;; The derivation of the Guile to be used within the build environment,
965 ;; when using 'gexp->derivation' and co.
966 (make-parameter #f))
967
968(define* (run-with-store store mval
969 #:key
970 (guile-for-build (%guile-for-build))
971 (system (%current-system)))
972 "Run MVAL, a monadic value in the store monad, in STORE, an open store
3698f524 973connection, and return the result."
e87f0591
LC
974 (parameterize ((%guile-for-build guile-for-build)
975 (%current-system system))
3698f524
LC
976 (call-with-values (lambda ()
977 (run-with-state mval store))
978 (lambda (result store)
979 ;; Discard the state.
980 result))))
e87f0591
LC
981
982\f
26bbbb95
LC
983;;;
984;;; Store paths.
985;;;
986
987(define %store-prefix
988 ;; Absolute path to the Nix store.
1d6816f9 989 (make-parameter %store-directory))
26bbbb95 990
f39bd08a
LC
991(define (store-path? path)
992 "Return #t if PATH is a store path."
993 ;; This is a lightweight check, compared to using a regexp, but this has to
994 ;; be fast as it's called often in `derivation', for instance.
995 ;; `isStorePath' in Nix does something similar.
996 (string-prefix? (%store-prefix) path))
26bbbb95 997
9336e5b5
LC
998(define (direct-store-path? path)
999 "Return #t if PATH is a store path, and not a sub-directory of a store path.
1000This predicate is sometimes needed because files *under* a store path are not
1001valid inputs."
1002 (and (store-path? path)
eee21271 1003 (not (string=? path (%store-prefix)))
9336e5b5
LC
1004 (let ((len (+ 1 (string-length (%store-prefix)))))
1005 (not (string-index (substring path len) #\/)))))
1006
26bbbb95
LC
1007(define (derivation-path? path)
1008 "Return #t if PATH is a derivation path."
1009 (and (store-path? path) (string-suffix? ".drv" path)))
e3d74106 1010
5c0f1845
LC
1011(define store-regexp*
1012 ;; The substituter makes repeated calls to 'store-path-hash-part', hence
1013 ;; this optimization.
1014 (memoize
1015 (lambda (store)
1016 "Return a regexp matching a file in STORE."
1017 (make-regexp (string-append "^" (regexp-quote store)
1018 "/([0-9a-df-np-sv-z]{32})-([^/]+)$")))))
1019
e3d74106
LC
1020(define (store-path-package-name path)
1021 "Return the package name part of PATH, a file name in the store."
5c0f1845
LC
1022 (let ((path-rx (store-regexp* (%store-prefix))))
1023 (and=> (regexp-exec path-rx path)
1024 (cut match:substring <> 2))))
2c6ab6cc
LC
1025
1026(define (store-path-hash-part path)
1027 "Return the hash part of PATH as a base32 string, or #f if PATH is not a
1028syntactically valid store path."
5c0f1845 1029 (let ((path-rx (store-regexp* (%store-prefix))))
2c6ab6cc
LC
1030 (and=> (regexp-exec path-rx path)
1031 (cut match:substring <> 1))))
eddd4077
LC
1032
1033(define (log-file store file)
1034 "Return the build log file for FILE, or #f if none could be found. FILE
1035must be an absolute store file name, or a derivation file name."
eddd4077 1036 (cond ((derivation-path? file)
021a201f 1037 (let* ((base (basename file))
80d0447c 1038 (log (string-append (dirname %state-directory) ; XXX
f5768afa 1039 "/log/guix/drvs/"
021a201f
LC
1040 (string-take base 2) "/"
1041 (string-drop base 2)))
1042 (log.bz2 (string-append log ".bz2")))
1043 (cond ((file-exists? log.bz2) log.bz2)
1044 ((file-exists? log) log)
1045 (else #f))))
eddd4077
LC
1046 (else
1047 (match (valid-derivers store file)
1048 ((derivers ...)
1049 ;; Return the first that works.
1050 (any (cut log-file store <>) derivers))
1051 (_ #f)))))