store: Add 'with-store' convenience macro.
[jackhill/guix/guix.git] / guix / store.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
021a201f 2;;; Copyright © 2012, 2013, 2014 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)
0f41c26f 20 #:use-module (guix nar)
82058eff 21 #:use-module (guix utils)
d8eea3d2 22 #:use-module (guix config)
0f41c26f 23 #:use-module (guix serialization)
77d3cf08
LC
24 #:use-module (rnrs bytevectors)
25 #:use-module (rnrs io ports)
26 #:use-module (srfi srfi-1)
27 #:use-module (srfi srfi-9)
bf8e7fc5 28 #:use-module (srfi srfi-9 gnu)
77d3cf08 29 #:use-module (srfi srfi-26)
e87088c9
LC
30 #:use-module (srfi srfi-34)
31 #:use-module (srfi srfi-35)
26bbbb95 32 #:use-module (srfi srfi-39)
77d3cf08 33 #:use-module (ice-9 match)
e3d74106 34 #:use-module (ice-9 regex)
3f1e6939 35 #:use-module (ice-9 vlist)
6bfec3ed 36 #:use-module (ice-9 popen)
9fd72fb1
LC
37 #:export (%daemon-socket-file
38
39 nix-server?
77d3cf08
LC
40 nix-server-major-version
41 nix-server-minor-version
42 nix-server-socket
43
e87088c9 44 &nix-error nix-error?
ef86c39f
LC
45 &nix-connection-error nix-connection-error?
46 nix-connection-error-file
47 nix-connection-error-code
e87088c9
LC
48 &nix-protocol-error nix-protocol-error?
49 nix-protocol-error-message
50 nix-protocol-error-status
51
26bbbb95
LC
52 hash-algo
53
77d3cf08 54 open-connection
3abaf0c4 55 close-connection
ce4a4829 56 with-store
77d3cf08 57 set-build-options
31ef99a8 58 valid-path?
82058eff 59 query-path-hash
77d3cf08
LC
60 add-text-to-store
61 add-to-store
26bbbb95 62 build-derivations
3259877d 63 add-temp-root
34811f02 64 add-indirect-root
26bbbb95 65
0f3d2504
LC
66 substitutable?
67 substitutable-path
68 substitutable-deriver
69 substitutable-references
70 substitutable-download-size
71 substitutable-nar-size
72 has-substitutes?
73 substitutable-paths
74 substitutable-path-info
75
fae31edc 76 references
3f1e6939 77 requisites
fae31edc
LC
78 referrers
79 valid-derivers
80 query-derivation-outputs
3259877d
LC
81 live-paths
82 dead-paths
83 collect-garbage
84 delete-paths
526382ff
LC
85 import-paths
86 export-paths
3259877d 87
dcee50c1
LC
88 current-build-output-port
89
6bfec3ed
LC
90 register-path
91
26bbbb95
LC
92 %store-prefix
93 store-path?
9336e5b5 94 direct-store-path?
e3d74106 95 derivation-path?
2c6ab6cc 96 store-path-package-name
eddd4077
LC
97 store-path-hash-part
98 log-file))
77d3cf08 99
63193ebf 100(define %protocol-version #x10c)
77d3cf08
LC
101
102(define %worker-magic-1 #x6e697863)
103(define %worker-magic-2 #x6478696f)
104
105(define (protocol-major magic)
106 (logand magic #xff00))
107(define (protocol-minor magic)
108 (logand magic #x00ff))
109
110(define-syntax define-enumerate-type
111 (syntax-rules ()
112 ((_ name->int (name id) ...)
113 (define-syntax name->int
114 (syntax-rules (name ...)
115 ((_ name) id) ...)))))
116
117(define-enumerate-type operation-id
118 ;; operation numbers from worker-protocol.hh
119 (quit 0)
120 (valid-path? 1)
121 (has-substitutes? 3)
122 (query-path-hash 4)
123 (query-references 5)
124 (query-referrers 6)
125 (add-to-store 7)
126 (add-text-to-store 8)
127 (build-derivations 9)
128 (ensure-path 10)
129 (add-temp-root 11)
130 (add-indirect-root 12)
131 (sync-with-gc 13)
132 (find-roots 14)
133 (export-path 16)
134 (query-deriver 18)
135 (set-options 19)
136 (collect-garbage 20)
63193ebf 137 ;;(query-substitutable-path-info 21) ; obsolete as of #x10c
77d3cf08 138 (query-derivation-outputs 22)
63193ebf 139 (query-all-valid-paths 23)
77d3cf08
LC
140 (query-failed-paths 24)
141 (clear-failed-paths 25)
142 (query-path-info 26)
143 (import-paths 27)
63193ebf
LC
144 (query-derivation-output-names 28)
145 (query-path-from-hash-part 29)
146 (query-substitutable-path-infos 30)
147 (query-valid-paths 31)
fae31edc
LC
148 (query-substitutable-paths 32)
149 (query-valid-derivers 33))
77d3cf08
LC
150
151(define-enumerate-type hash-algo
152 ;; hash.hh
153 (md5 1)
154 (sha1 2)
155 (sha256 3))
156
3259877d
LC
157(define-enumerate-type gc-action
158 ;; store-api.hh
159 (return-live 0)
160 (return-dead 1)
161 (delete-dead 2)
162 (delete-specific 3))
163
77d3cf08 164(define %default-socket-path
80d0447c 165 (string-append %state-directory "/daemon-socket/socket"))
77d3cf08 166
9fd72fb1
LC
167(define %daemon-socket-file
168 ;; File name of the socket the daemon listens too.
169 (make-parameter (or (getenv "GUIX_DAEMON_SOCKET")
170 %default-socket-path)))
171
172
77d3cf08 173\f
0f3d2504
LC
174;; Information about a substitutable store path.
175(define-record-type <substitutable>
176 (substitutable path deriver refs dl-size nar-size)
177 substitutable?
178 (path substitutable-path)
179 (deriver substitutable-deriver)
180 (refs substitutable-references)
181 (dl-size substitutable-download-size)
182 (nar-size substitutable-nar-size))
183
184(define (read-substitutable-path-list p)
185 (let loop ((len (read-int p))
186 (result '()))
187 (if (zero? len)
188 (reverse result)
189 (let ((path (read-store-path p))
190 (deriver (read-store-path p))
191 (refs (read-store-path-list p))
192 (dl-size (read-long-long p))
193 (nar-size (read-long-long p)))
194 (loop (- len 1)
195 (cons (substitutable path deriver refs dl-size nar-size)
196 result))))))
197
77d3cf08 198(define-syntax write-arg
3259877d
LC
199 (syntax-rules (integer boolean file string string-list
200 store-path store-path-list base16)
77d3cf08
LC
201 ((_ integer arg p)
202 (write-int arg p))
203 ((_ boolean arg p)
204 (write-int (if arg 1 0) p))
205 ((_ file arg p)
206 (write-file arg p))
207 ((_ string arg p)
208 (write-string arg p))
209 ((_ string-list arg p)
82058eff 210 (write-string-list arg p))
3259877d
LC
211 ((_ store-path arg p)
212 (write-store-path arg p))
213 ((_ store-path-list arg p)
214 (write-store-path-list arg p))
82058eff
LC
215 ((_ base16 arg p)
216 (write-string (bytevector->base16-string arg) p))))
77d3cf08
LC
217
218(define-syntax read-arg
0f3d2504
LC
219 (syntax-rules (integer boolean string store-path store-path-list
220 substitutable-path-list base16)
77d3cf08
LC
221 ((_ integer p)
222 (read-int p))
223 ((_ boolean p)
224 (not (zero? (read-int p))))
225 ((_ string p)
226 (read-string p))
227 ((_ store-path p)
82058eff 228 (read-store-path p))
3259877d
LC
229 ((_ store-path-list p)
230 (read-store-path-list p))
0f3d2504
LC
231 ((_ substitutable-path-list p)
232 (read-substitutable-path-list p))
233 ((_ base16 p)
82058eff 234 (base16-string->bytevector (read-string p)))))
77d3cf08
LC
235
236\f
237;; remote-store.cc
238
239(define-record-type <nix-server>
2c3f47ee 240 (%make-nix-server socket major minor
bdcf35a6 241 ats-cache atts-cache)
77d3cf08
LC
242 nix-server?
243 (socket nix-server-socket)
244 (major nix-server-major-version)
2c3f47ee
LC
245 (minor nix-server-minor-version)
246
247 ;; Caches. We keep them per-connection, because store paths build
248 ;; during the session are temporary GC roots kept for the duration of
249 ;; the session.
bdcf35a6
LC
250 (ats-cache nix-server-add-to-store-cache)
251 (atts-cache nix-server-add-text-to-store-cache))
77d3cf08 252
bf8e7fc5
LC
253(set-record-type-printer! <nix-server>
254 (lambda (obj port)
255 (format port "#<build-daemon ~a.~a ~a>"
256 (nix-server-major-version obj)
257 (nix-server-minor-version obj)
258 (number->string (object-address obj)
259 16))))
260
e87088c9
LC
261(define-condition-type &nix-error &error
262 nix-error?)
263
ef86c39f
LC
264(define-condition-type &nix-connection-error &nix-error
265 nix-connection-error?
266 (file nix-connection-error-file)
267 (errno nix-connection-error-code))
268
e87088c9
LC
269(define-condition-type &nix-protocol-error &nix-error
270 nix-protocol-error?
271 (message nix-protocol-error-message)
272 (status nix-protocol-error-status))
273
9fd72fb1 274(define* (open-connection #:optional (file (%daemon-socket-file))
e36a7172 275 #:key (reserve-space? #t))
e531ac2a
LC
276 "Connect to the daemon over the Unix-domain socket at FILE. When
277RESERVE-SPACE? is true, instruct it to reserve a little bit of extra
278space on the file system so that the garbage collector can still
279operate, should the disk become full. Return a server object."
77d3cf08
LC
280 (let ((s (with-fluids ((%default-port-encoding #f))
281 ;; This trick allows use of the `scm_c_read' optimization.
282 (socket PF_UNIX SOCK_STREAM 0)))
283 (a (make-socket-address PF_UNIX file)))
df1fab58 284
8c95b27c
LC
285 (catch 'system-error
286 (lambda ()
287 ;; Enlarge the receive buffer.
288 (setsockopt s SOL_SOCKET SO_RCVBUF (* 12 1024)))
289 (lambda args
290 ;; On the Hurd, the pflocal server's implementation of `socket_setopt'
291 ;; always returns ENOPROTOOPT. Ignore it.
292 (unless (= (system-error-errno args) ENOPROTOOPT)
293 (apply throw args))))
df1fab58 294
ef86c39f
LC
295 (catch 'system-error
296 (cut connect s a)
297 (lambda args
298 ;; Translate the error to something user-friendly.
299 (let ((errno (system-error-errno args)))
300 (raise (condition (&nix-connection-error
301 (file file)
302 (errno errno)))))))
303
77d3cf08
LC
304 (write-int %worker-magic-1 s)
305 (let ((r (read-int s)))
306 (and (eqv? r %worker-magic-2)
307 (let ((v (read-int s)))
308 (and (eqv? (protocol-major %protocol-version)
309 (protocol-major v))
310 (begin
311 (write-int %protocol-version s)
e36a7172
LC
312 (if (>= (protocol-minor v) 11)
313 (write-int (if reserve-space? 1 0) s))
77d3cf08
LC
314 (let ((s (%make-nix-server s
315 (protocol-major v)
2c3f47ee 316 (protocol-minor v)
fce2394e
LC
317 (make-hash-table 100)
318 (make-hash-table 100))))
34fcbe3a
LC
319 (let loop ((done? (process-stderr s)))
320 (or done? (process-stderr s)))
77d3cf08
LC
321 s))))))))
322
3abaf0c4
LC
323(define (close-connection server)
324 "Close the connection to SERVER."
325 (close (nix-server-socket server)))
326
ce4a4829
LC
327(define-syntax-rule (with-store store exp ...)
328 "Bind STORE to an open connection to the store and evaluate EXPs;
329automatically close the store when the dynamic extent of EXP is left."
330 (let ((store (open-connection)))
331 (dynamic-wind
332 (const #f)
333 (lambda ()
334 exp ...)
335 (lambda ()
336 (false-if-exception (close-connection store))))))
337
dcee50c1
LC
338(define current-build-output-port
339 ;; The port where build output is sent.
340 (make-parameter (current-error-port)))
341
526382ff
LC
342(define* (dump-port in out
343 #:optional len
344 #:key (buffer-size 16384))
345 "Read LEN bytes from IN (or as much as possible if LEN is #f) and write it
346to OUT, using chunks of BUFFER-SIZE bytes."
347 (define buffer
348 (make-bytevector buffer-size))
349
350 (let loop ((total 0)
351 (bytes (get-bytevector-n! in buffer 0
352 (if len
353 (min len buffer-size)
354 buffer-size))))
355 (or (eof-object? bytes)
356 (and len (= total len))
357 (let ((total (+ total bytes)))
358 (put-bytevector out buffer 0 bytes)
359 (loop total
360 (get-bytevector-n! in buffer 0
361 (if len
362 (min (- len total) buffer-size)
363 buffer-size)))))))
364
365(define* (process-stderr server #:optional user-port)
dcee50c1
LC
366 "Read standard output and standard error from SERVER, writing it to
367CURRENT-BUILD-OUTPUT-PORT. Return #t when SERVER is done sending data, and
368#f otherwise; in the latter case, the caller should call `process-stderr'
bbdb3ffa
LC
369again until #t is returned or an error is raised.
370
371Since the build process's output cannot be assumed to be UTF-8, we
372conservatively consider it to be Latin-1, thereby avoiding possible
373encoding conversion errors."
77d3cf08
LC
374 (define p
375 (nix-server-socket server))
376
377 ;; magic cookies from worker-protocol.hh
5674a3fd
LC
378 (define %stderr-next #x6f6c6d67) ; "olmg", build log
379 (define %stderr-read #x64617461) ; "data", data needed from source
380 (define %stderr-write #x64617416) ; "dat\x16", data for sink
381 (define %stderr-last #x616c7473) ; "alts", we're done
382 (define %stderr-error #x63787470) ; "cxtp", error reporting
77d3cf08
LC
383
384 (let ((k (read-int p)))
385 (cond ((= k %stderr-write)
526382ff
LC
386 ;; Write a byte stream to USER-PORT.
387 (let* ((len (read-int p))
388 (m (modulo len 8)))
389 (dump-port p user-port len)
390 (unless (zero? m)
391 ;; Consume padding, as for strings.
392 (get-bytevector-n p (- 8 m))))
dcee50c1 393 #f)
77d3cf08 394 ((= k %stderr-read)
526382ff
LC
395 ;; Read a byte stream from USER-PORT.
396 (let* ((max-len (read-int p))
397 (data (get-bytevector-n user-port max-len))
398 (len (bytevector-length data)))
399 (write-int len p)
400 (put-bytevector p data)
401 (write-padding len p)
dcee50c1 402 #f))
77d3cf08 403 ((= k %stderr-next)
526382ff 404 ;; Log a string.
bbdb3ffa 405 (let ((s (read-latin1-string p)))
dcee50c1
LC
406 (display s (current-build-output-port))
407 #f))
77d3cf08 408 ((= k %stderr-error)
526382ff 409 ;; Report an error.
bbdb3ffa 410 (let ((error (read-latin1-string p))
0ff3e3aa
LC
411 ;; Currently the daemon fails to send a status code for early
412 ;; errors like DB schema version mismatches, so check for EOF.
413 (status (if (and (>= (nix-server-minor-version server) 8)
414 (not (eof-object? (lookahead-u8 p))))
77d3cf08
LC
415 (read-int p)
416 1)))
e87088c9
LC
417 (raise (condition (&nix-protocol-error
418 (message error)
419 (status status))))))
77d3cf08 420 ((= k %stderr-last)
dcee50c1 421 ;; The daemon is done (see `stopWork' in `nix-worker.cc'.)
77d3cf08
LC
422 #t)
423 (else
e87088c9
LC
424 (raise (condition (&nix-protocol-error
425 (message "invalid error code")
426 (status k))))))))
77d3cf08
LC
427
428(define* (set-build-options server
c3eb878f 429 #:key keep-failed? keep-going? fallback?
77d3cf08
LC
430 (verbosity 0)
431 (max-build-jobs (current-processor-count))
432 (max-silent-time 3600)
433 (use-build-hook? #t)
434 (build-verbosity 0)
435 (log-type 0)
e036c31b
LC
436 (print-build-trace #t)
437 (build-cores 1)
63193ebf
LC
438 (use-substitutes? #t)
439 (binary-caches '())) ; client "untrusted" cache URLs
77d3cf08
LC
440 ;; Must be called after `open-connection'.
441
442 (define socket
443 (nix-server-socket server))
444
445 (let-syntax ((send (syntax-rules ()
e036c31b
LC
446 ((_ (type option) ...)
447 (begin
448 (write-arg type option socket)
449 ...)))))
450 (write-int (operation-id set-options) socket)
451 (send (boolean keep-failed?) (boolean keep-going?)
c3eb878f 452 (boolean fallback?) (integer verbosity)
e036c31b 453 (integer max-build-jobs) (integer max-silent-time))
77d3cf08 454 (if (>= (nix-server-minor-version server) 2)
e036c31b 455 (send (boolean use-build-hook?)))
77d3cf08 456 (if (>= (nix-server-minor-version server) 4)
e036c31b
LC
457 (send (integer build-verbosity) (integer log-type)
458 (boolean print-build-trace)))
459 (if (>= (nix-server-minor-version server) 6)
460 (send (integer build-cores)))
461 (if (>= (nix-server-minor-version server) 10)
462 (send (boolean use-substitutes?)))
63193ebf
LC
463 (if (>= (nix-server-minor-version server) 12)
464 (send (string-list (fold-right (lambda (pair result)
465 (match pair
466 ((h . t)
467 (cons* h t result))))
468 '()
469 binary-caches))))
dcee50c1
LC
470 (let loop ((done? (process-stderr server)))
471 (or done? (process-stderr server)))))
77d3cf08 472
fd060fd3 473(define-syntax operation
77d3cf08 474 (syntax-rules ()
fd060fd3 475 "Define a client-side RPC stub for the given operation."
3259877d 476 ((_ (name (type arg) ...) docstring return ...)
fd060fd3 477 (lambda (server arg ...)
77d3cf08
LC
478 docstring
479 (let ((s (nix-server-socket server)))
480 (write-int (operation-id name) s)
481 (write-arg type arg s)
482 ...
dcee50c1
LC
483 ;; Loop until the server is done sending error output.
484 (let loop ((done? (process-stderr server)))
485 (or done? (loop (process-stderr server))))
3259877d 486 (values (read-arg return s) ...))))))
77d3cf08 487
fd060fd3
LC
488(define-syntax-rule (define-operation (name args ...)
489 docstring return ...)
490 (define name
491 (operation (name args ...) docstring return ...)))
492
31ef99a8
LC
493(define-operation (valid-path? (string path))
494 "Return #t when PATH is a valid store path."
495 boolean)
496
63193ebf 497(define-operation (query-path-hash (store-path path))
82058eff
LC
498 "Return the SHA256 hash of PATH as a bytevector."
499 base16)
500
fd060fd3
LC
501(define add-text-to-store
502 ;; A memoizing version of `add-to-store', to avoid repeated RPCs with
503 ;; the very same arguments during a given session.
504 (let ((add-text-to-store
505 (operation (add-text-to-store (string name) (string text)
506 (string-list references))
507 #f
508 store-path)))
cfbf9160 509 (lambda* (server name text #:optional (references '()))
bdcf35a6
LC
510 "Add TEXT under file NAME in the store, and return its store path.
511REFERENCES is the list of store paths referred to by the resulting store
512path."
fce2394e 513 (let ((args `(,text ,name ,references))
bdcf35a6
LC
514 (cache (nix-server-add-text-to-store-cache server)))
515 (or (hash-ref cache args)
fd060fd3 516 (let ((path (add-text-to-store server name text references)))
bdcf35a6
LC
517 (hash-set! cache args path)
518 path))))))
519
fd060fd3 520(define add-to-store
a7b6ffee
LC
521 ;; A memoizing version of `add-to-store'. This is important because
522 ;; `add-to-store' leads to huge data transfers to the server, and
523 ;; because it's often called many times with the very same argument.
fd060fd3
LC
524 (let ((add-to-store (operation (add-to-store (string basename)
525 (boolean fixed?) ; obsolete, must be #t
526 (boolean recursive?)
527 (string hash-algo)
528 (file file-name))
529 #f
530 store-path)))
a9ebd9ef
LC
531 (lambda (server basename recursive? hash-algo file-name)
532 "Add the contents of FILE-NAME under BASENAME to the store. When
533RECURSIVE? is true and FILE-NAME designates a directory, the contents of
534FILE-NAME are added recursively; if FILE-NAME designates a flat file and
535RECURSIVE? is true, its contents are added, and its permission bits are
536kept. HASH-ALGO must be a string such as \"sha256\"."
2c3f47ee 537 (let* ((st (stat file-name #f))
fce2394e 538 (args `(,st ,basename ,recursive? ,hash-algo))
2c3f47ee 539 (cache (nix-server-add-to-store-cache server)))
a7b6ffee 540 (or (and st (hash-ref cache args))
a9ebd9ef 541 (let ((path (add-to-store server basename #t recursive?
a7b6ffee
LC
542 hash-algo file-name)))
543 (hash-set! cache args path)
544 path))))))
545
77d3cf08 546(define-operation (build-derivations (string-list derivations))
dcee50c1
LC
547 "Build DERIVATIONS, and return when the worker is done building them.
548Return #t on success."
77d3cf08 549 boolean)
26bbbb95 550
d3648e01
LC
551(define-operation (add-temp-root (store-path path))
552 "Make PATH a temporary root for the duration of the current session.
553Return #t."
554 boolean)
555
34811f02
LC
556(define-operation (add-indirect-root (string file-name))
557 "Make FILE-NAME an indirect root for the garbage collector; FILE-NAME
558can be anywhere on the file system, but it must be an absolute file
559name--it is the caller's responsibility to ensure that it is an absolute
560file name. Return #t on success."
561 boolean)
562
fae31edc
LC
563(define references
564 (operation (query-references (store-path path))
565 "Return the list of references of PATH."
566 store-path-list))
567
3f1e6939
LC
568(define* (fold-path store proc seed path
569 #:optional (relatives (cut references store <>)))
570 "Call PROC for each of the RELATIVES of PATH, exactly once, and return the
571result formed from the successive calls to PROC, the first of which is passed
572SEED."
573 (let loop ((paths (list path))
574 (result seed)
575 (seen vlist-null))
576 (match paths
577 ((path rest ...)
578 (if (vhash-assoc path seen)
579 (loop rest result seen)
580 (let ((seen (vhash-cons path #t seen))
581 (rest (append rest (relatives path)))
582 (result (proc path result)))
583 (loop rest result seen))))
584 (()
585 result))))
586
587(define (requisites store path)
588 "Return the requisites of PATH, including PATH---i.e., its closure (all its
589references, recursively)."
590 (fold-path store cons '() path))
591
fae31edc
LC
592(define referrers
593 (operation (query-referrers (store-path path))
594 "Return the list of path that refer to PATH."
595 store-path-list))
596
597(define valid-derivers
598 (operation (query-valid-derivers (store-path path))
599 "Return the list of valid \"derivers\" of PATH---i.e., all the
600.drv present in the store that have PATH among their outputs."
601 store-path-list))
602
603(define query-derivation-outputs ; avoid name clash with `derivation-outputs'
604 (operation (query-derivation-outputs (store-path path))
605 "Return the list of outputs of PATH, a .drv file."
606 store-path-list))
607
0f3d2504
LC
608(define-operation (has-substitutes? (store-path path))
609 "Return #t if binary substitutes are available for PATH, and #f otherwise."
610 boolean)
611
612(define substitutable-paths
613 (operation (query-substitutable-paths (store-path-list paths))
614 "Return the subset of PATHS that is substitutable."
615 store-path-list))
616
617(define substitutable-path-info
f65cf81a 618 (operation (query-substitutable-path-infos (store-path-list paths))
0f3d2504
LC
619 "Return information about the subset of PATHS that is
620substitutable. For each substitutable path, a `substitutable?' object is
621returned."
622 substitutable-path-list))
623
3259877d
LC
624(define (run-gc server action to-delete min-freed)
625 "Perform the garbage-collector operation ACTION, one of the
626`gc-action' values. When ACTION is `delete-specific', the TO-DELETE is
627the list of store paths to delete. IGNORE-LIVENESS? should always be
628#f. MIN-FREED is the minimum amount of disk space to be freed, in
629bytes, before the GC can stop. Return the list of store paths delete,
630and the number of bytes freed."
631 (let ((s (nix-server-socket server)))
632 (write-int (operation-id collect-garbage) s)
633 (write-int action s)
634 (write-store-path-list to-delete s)
635 (write-arg boolean #f s) ; ignore-liveness?
636 (write-long-long min-freed s)
637 (write-int 0 s) ; obsolete
638 (when (>= (nix-server-minor-version server) 5)
639 ;; Obsolete `use-atime' and `max-atime' parameters.
640 (write-int 0 s)
641 (write-int 0 s))
642
643 ;; Loop until the server is done sending error output.
644 (let loop ((done? (process-stderr server)))
645 (or done? (loop (process-stderr server))))
646
647 (let ((paths (read-store-path-list s))
648 (freed (read-long-long s))
649 (obsolete (read-long-long s)))
650 (values paths freed))))
651
652(define-syntax-rule (%long-long-max)
653 ;; Maximum unsigned 64-bit integer.
654 (- (expt 2 64) 1))
655
656(define (live-paths server)
657 "Return the list of live store paths---i.e., store paths still
658referenced, and thus not subject to being garbage-collected."
659 (run-gc server (gc-action return-live) '() (%long-long-max)))
660
661(define (dead-paths server)
662 "Return the list of dead store paths---i.e., store paths no longer
663referenced, and thus subject to being garbage-collected."
664 (run-gc server (gc-action return-dead) '() (%long-long-max)))
665
666(define* (collect-garbage server #:optional (min-freed (%long-long-max)))
667 "Collect garbage from the store at SERVER. If MIN-FREED is non-zero,
668then collect at least MIN-FREED bytes. Return the paths that were
669collected, and the number of bytes freed."
670 (run-gc server (gc-action delete-dead) '() min-freed))
671
672(define* (delete-paths server paths #:optional (min-freed (%long-long-max)))
673 "Delete PATHS from the store at SERVER, if they are no longer
674referenced. If MIN-FREED is non-zero, then stop after at least
675MIN-FREED bytes have been collected. Return the paths that were
676collected, and the number of bytes freed."
677 (run-gc server (gc-action delete-specific) paths min-freed))
678
526382ff
LC
679(define (import-paths server port)
680 "Import the set of store paths read from PORT into SERVER's store. An error
681is raised if the set of paths read from PORT is not signed (as per
682'export-path #:sign? #t'.) Return the list of store paths imported."
683 (let ((s (nix-server-socket server)))
684 (write-int (operation-id import-paths) s)
685 (let loop ((done? (process-stderr server port)))
686 (or done? (loop (process-stderr server port))))
687 (read-store-path-list s)))
688
689(define* (export-path server path port #:key (sign? #t))
690 "Export PATH to PORT. When SIGN? is true, sign it."
691 (let ((s (nix-server-socket server)))
692 (write-int (operation-id export-path) s)
693 (write-store-path path s)
694 (write-arg boolean sign? s)
695 (let loop ((done? (process-stderr server port)))
696 (or done? (loop (process-stderr server port))))
697 (= 1 (read-int s))))
698
699(define* (export-paths server paths port #:key (sign? #t))
700 "Export the store paths listed in PATHS to PORT, signing them if SIGN?
701is true."
702 (let ((s (nix-server-socket server)))
703 (let loop ((paths paths))
704 (match paths
705 (()
706 (write-int 0 port))
707 ((head tail ...)
708 (write-int 1 port)
709 (and (export-path server head port #:sign? sign?)
710 (loop tail)))))))
711
6bfec3ed
LC
712(define* (register-path path
713 #:key (references '()) deriver)
714 "Register PATH as a valid store file, with REFERENCES as its list of
715references, and DERIVER as its deriver (.drv that led to it.) Return #t on
716success.
717
718Use with care as it directly modifies the store! This is primarily meant to
719be used internally by the daemon's build hook."
720 ;; Currently this is implemented by calling out to the fine C++ blob.
721 (catch 'system-error
722 (lambda ()
723 (let ((pipe (open-pipe* OPEN_WRITE %guix-register-program)))
724 (and pipe
725 (begin
726 (format pipe "~a~%~a~%~a~%"
727 path (or deriver "") (length references))
728 (for-each (cut format pipe "~a~%" <>) references)
729 (zero? (close-pipe pipe))))))
730 (lambda args
731 ;; Failed to run %GUIX-REGISTER-PROGRAM.
732 #f)))
733
26bbbb95
LC
734\f
735;;;
736;;; Store paths.
737;;;
738
739(define %store-prefix
740 ;; Absolute path to the Nix store.
1d6816f9 741 (make-parameter %store-directory))
26bbbb95 742
f39bd08a
LC
743(define (store-path? path)
744 "Return #t if PATH is a store path."
745 ;; This is a lightweight check, compared to using a regexp, but this has to
746 ;; be fast as it's called often in `derivation', for instance.
747 ;; `isStorePath' in Nix does something similar.
748 (string-prefix? (%store-prefix) path))
26bbbb95 749
9336e5b5
LC
750(define (direct-store-path? path)
751 "Return #t if PATH is a store path, and not a sub-directory of a store path.
752This predicate is sometimes needed because files *under* a store path are not
753valid inputs."
754 (and (store-path? path)
755 (let ((len (+ 1 (string-length (%store-prefix)))))
756 (not (string-index (substring path len) #\/)))))
757
26bbbb95
LC
758(define (derivation-path? path)
759 "Return #t if PATH is a derivation path."
760 (and (store-path? path) (string-suffix? ".drv" path)))
e3d74106 761
5c0f1845
LC
762(define store-regexp*
763 ;; The substituter makes repeated calls to 'store-path-hash-part', hence
764 ;; this optimization.
765 (memoize
766 (lambda (store)
767 "Return a regexp matching a file in STORE."
768 (make-regexp (string-append "^" (regexp-quote store)
769 "/([0-9a-df-np-sv-z]{32})-([^/]+)$")))))
770
e3d74106
LC
771(define (store-path-package-name path)
772 "Return the package name part of PATH, a file name in the store."
5c0f1845
LC
773 (let ((path-rx (store-regexp* (%store-prefix))))
774 (and=> (regexp-exec path-rx path)
775 (cut match:substring <> 2))))
2c6ab6cc
LC
776
777(define (store-path-hash-part path)
778 "Return the hash part of PATH as a base32 string, or #f if PATH is not a
779syntactically valid store path."
5c0f1845 780 (let ((path-rx (store-regexp* (%store-prefix))))
2c6ab6cc
LC
781 (and=> (regexp-exec path-rx path)
782 (cut match:substring <> 1))))
eddd4077
LC
783
784(define (log-file store file)
785 "Return the build log file for FILE, or #f if none could be found. FILE
786must be an absolute store file name, or a derivation file name."
eddd4077 787 (cond ((derivation-path? file)
021a201f 788 (let* ((base (basename file))
80d0447c 789 (log (string-append (dirname %state-directory) ; XXX
021a201f
LC
790 "/log/nix/drvs/"
791 (string-take base 2) "/"
792 (string-drop base 2)))
793 (log.bz2 (string-append log ".bz2")))
794 (cond ((file-exists? log.bz2) log.bz2)
795 ((file-exists? log) log)
796 (else #f))))
eddd4077
LC
797 (else
798 (match (valid-derivers store file)
799 ((derivers ...)
800 ;; Return the first that works.
801 (any (cut log-file store <>) derivers))
802 (_ #f)))))