1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
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.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
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.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (guix store)
20 #:use-module (guix utils)
21 #:use-module (guix config)
22 #:use-module (rnrs bytevectors)
23 #:use-module (rnrs io ports)
24 #:use-module (srfi srfi-1)
25 #:use-module (srfi srfi-9)
26 #:use-module (srfi srfi-26)
27 #:use-module (srfi srfi-34)
28 #:use-module (srfi srfi-35)
29 #:use-module (srfi srfi-39)
30 #:use-module (ice-9 match)
31 #:use-module (ice-9 rdelim)
32 #:use-module (ice-9 ftw)
33 #:use-module (ice-9 regex)
35 nix-server-major-version
36 nix-server-minor-version
40 &nix-protocol-error nix-protocol-error?
41 nix-protocol-error-message
42 nix-protocol-error-status
60 substitutable-references
61 substitutable-download-size
62 substitutable-nar-size
65 substitutable-path-info
72 current-build-output-port
77 store-path-package-name))
79 (define %protocol-version #x10c)
81 (define %worker-magic-1 #x6e697863)
82 (define %worker-magic-2 #x6478696f)
84 (define (protocol-major magic)
85 (logand magic #xff00))
86 (define (protocol-minor magic)
87 (logand magic #x00ff))
89 (define-syntax define-enumerate-type
91 ((_ name->int (name id) ...)
92 (define-syntax name->int
93 (syntax-rules (name ...)
94 ((_ name) id) ...)))))
96 (define-enumerate-type operation-id
97 ;; operation numbers from worker-protocol.hh
105 (add-text-to-store 8)
106 (build-derivations 9)
109 (add-indirect-root 12)
116 ;;(query-substitutable-path-info 21) ; obsolete as of #x10c
117 (query-derivation-outputs 22)
118 (query-all-valid-paths 23)
119 (query-failed-paths 24)
120 (clear-failed-paths 25)
123 (query-derivation-output-names 28)
124 (query-path-from-hash-part 29)
125 (query-substitutable-path-infos 30)
126 (query-valid-paths 31)
127 (query-substitutable-paths 32))
129 (define-enumerate-type hash-algo
135 (define-enumerate-type gc-action
142 (define %default-socket-path
143 (string-append (or (getenv "NIX_STATE_DIR") %state-directory)
144 "/daemon-socket/socket"))
149 (define (write-int n p)
150 (let ((b (make-bytevector 8 0)))
151 (bytevector-u32-set! b 0 n (endianness little))
152 (put-bytevector p b)))
155 (let ((b (get-bytevector-n p 8)))
156 (bytevector-u32-ref b 0 (endianness little))))
158 (define (write-long-long n p)
159 (let ((b (make-bytevector 8 0)))
160 (bytevector-u64-set! b 0 n (endianness little))
161 (put-bytevector p b)))
163 (define (read-long-long p)
164 (let ((b (get-bytevector-n p 8)))
165 (bytevector-u64-ref b 0 (endianness little))))
167 (define write-padding
168 (let ((zero (make-bytevector 8 0)))
170 (let ((m (modulo n 8)))
172 (put-bytevector p zero 0 (- 8 m)))))))
174 (define (write-string s p)
175 (let* ((s (string->utf8 s))
176 (l (bytevector-length s))
178 (b (make-bytevector (+ 8 l (if (zero? m) 0 (- 8 m))))))
179 (bytevector-u32-set! b 0 l (endianness little))
180 (bytevector-copy! s 0 b 8 l)
181 (put-bytevector p b)))
183 (define (read-string p)
184 (let* ((len (read-int p))
186 (bv (get-bytevector-n p len))
187 (str (utf8->string bv)))
189 (get-bytevector-n p (- 8 m)))
192 (define (read-latin1-string p)
193 (let* ((len (read-int p))
195 (str (get-string-n p len)))
197 (get-bytevector-n p (- 8 m)))
200 (define (write-string-list l p)
201 (write-int (length l) p)
202 (for-each (cut write-string <> p) l))
204 (define (read-string-list p)
205 (let ((len (read-int p)))
206 (unfold (cut >= <> len)
212 (define (write-store-path f p)
213 (write-string f p)) ; TODO: assert path
215 (define (read-store-path p)
216 (read-string p)) ; TODO: assert path
218 (define write-store-path-list write-string-list)
219 (define read-store-path-list read-string-list)
221 (define (write-contents file p)
222 "Write the contents of FILE to output port P."
223 (define (dump in size)
224 (define buf-size 65536)
225 (define buf (make-bytevector buf-size))
227 (let loop ((left size))
230 (let ((read (get-bytevector-n! in buf 0 buf-size)))
231 (if (eof-object? read)
234 (put-bytevector p buf 0 read)
235 (loop (- left read))))))))
237 (let ((size (stat:size (lstat file))))
238 (write-string "contents" p)
239 (write-long-long size p)
240 (call-with-input-file file
243 (write-padding size p)))
245 (define (write-file f p)
246 (define %archive-version-1 "nix-archive-1")
248 (write-string %archive-version-1 p)
255 (write-string "type" p)
256 (write-string "regular" p)
257 (if (not (zero? (logand (stat:mode s) #o100)))
259 (write-string "executable" p)
260 (write-string "" p)))
261 (write-contents f p))
263 (write-string "type" p)
264 (write-string "directory" p)
265 (let ((entries (remove (cut member <> '("." ".."))
267 (for-each (lambda (e)
268 (let ((f (string-append f "/" e)))
269 (write-string "entry" p)
271 (write-string "name" p)
273 (write-string "node" p)
275 (write-string ")" p)))
279 (write-string ")" p))))
281 ;; Information about a substitutable store path.
282 (define-record-type <substitutable>
283 (substitutable path deriver refs dl-size nar-size)
285 (path substitutable-path)
286 (deriver substitutable-deriver)
287 (refs substitutable-references)
288 (dl-size substitutable-download-size)
289 (nar-size substitutable-nar-size))
291 (define (read-substitutable-path-list p)
292 (let loop ((len (read-int p))
296 (let ((path (read-store-path p))
297 (deriver (read-store-path p))
298 (refs (read-store-path-list p))
299 (dl-size (read-long-long p))
300 (nar-size (read-long-long p)))
302 (cons (substitutable path deriver refs dl-size nar-size)
305 (define-syntax write-arg
306 (syntax-rules (integer boolean file string string-list
307 store-path store-path-list base16)
311 (write-int (if arg 1 0) p))
315 (write-string arg p))
316 ((_ string-list arg p)
317 (write-string-list arg p))
318 ((_ store-path arg p)
319 (write-store-path arg p))
320 ((_ store-path-list arg p)
321 (write-store-path-list arg p))
323 (write-string (bytevector->base16-string arg) p))))
325 (define-syntax read-arg
326 (syntax-rules (integer boolean string store-path store-path-list
327 substitutable-path-list base16)
331 (not (zero? (read-int p))))
336 ((_ store-path-list p)
337 (read-store-path-list p))
338 ((_ substitutable-path-list p)
339 (read-substitutable-path-list p))
341 (base16-string->bytevector (read-string p)))))
346 (define-record-type <nix-server>
347 (%make-nix-server socket major minor
348 ats-cache atts-cache)
350 (socket nix-server-socket)
351 (major nix-server-major-version)
352 (minor nix-server-minor-version)
354 ;; Caches. We keep them per-connection, because store paths build
355 ;; during the session are temporary GC roots kept for the duration of
357 (ats-cache nix-server-add-to-store-cache)
358 (atts-cache nix-server-add-text-to-store-cache))
360 (define-condition-type &nix-error &error
363 (define-condition-type &nix-protocol-error &nix-error
365 (message nix-protocol-error-message)
366 (status nix-protocol-error-status))
368 (define* (open-connection #:optional (file %default-socket-path)
369 #:key (reserve-space? #t))
370 "Connect to the daemon over the Unix-domain socket at FILE. When
371 RESERVE-SPACE? is true, instruct it to reserve a little bit of extra
372 space on the file system so that the garbage collector can still
373 operate, should the disk become full. Return a server object."
374 (let ((s (with-fluids ((%default-port-encoding #f))
375 ;; This trick allows use of the `scm_c_read' optimization.
376 (socket PF_UNIX SOCK_STREAM 0)))
377 (a (make-socket-address PF_UNIX file)))
379 ;; Enlarge the receive buffer.
380 (setsockopt s SOL_SOCKET SO_RCVBUF (* 12 1024))
383 (write-int %worker-magic-1 s)
384 (let ((r (read-int s)))
385 (and (eqv? r %worker-magic-2)
386 (let ((v (read-int s)))
387 (and (eqv? (protocol-major %protocol-version)
390 (write-int %protocol-version s)
391 (if (>= (protocol-minor v) 11)
392 (write-int (if reserve-space? 1 0) s))
393 (let ((s (%make-nix-server s
396 (make-hash-table 100)
397 (make-hash-table 100))))
398 (let loop ((done? (process-stderr s)))
399 (or done? (process-stderr s)))
402 (define (close-connection server)
403 "Close the connection to SERVER."
404 (close (nix-server-socket server)))
406 (define current-build-output-port
407 ;; The port where build output is sent.
408 (make-parameter (current-error-port)))
410 (define (process-stderr server)
411 "Read standard output and standard error from SERVER, writing it to
412 CURRENT-BUILD-OUTPUT-PORT. Return #t when SERVER is done sending data, and
413 #f otherwise; in the latter case, the caller should call `process-stderr'
414 again until #t is returned or an error is raised.
416 Since the build process's output cannot be assumed to be UTF-8, we
417 conservatively consider it to be Latin-1, thereby avoiding possible
418 encoding conversion errors."
420 (nix-server-socket server))
422 ;; magic cookies from worker-protocol.hh
423 (define %stderr-next #x6f6c6d67)
424 (define %stderr-read #x64617461) ; data needed from source
425 (define %stderr-write #x64617416) ; data for sink
426 (define %stderr-last #x616c7473)
427 (define %stderr-error #x63787470)
429 (let ((k (read-int p)))
430 (cond ((= k %stderr-write)
431 (read-latin1-string p)
434 (let ((len (read-int p)))
435 (read-latin1-string p) ; FIXME: what to do?
438 (let ((s (read-latin1-string p)))
439 (display s (current-build-output-port))
442 (let ((error (read-latin1-string p))
443 (status (if (>= (nix-server-minor-version server) 8)
446 (raise (condition (&nix-protocol-error
450 ;; The daemon is done (see `stopWork' in `nix-worker.cc'.)
453 (raise (condition (&nix-protocol-error
454 (message "invalid error code")
457 (define* (set-build-options server
458 #:key keep-failed? keep-going? try-fallback?
460 (max-build-jobs (current-processor-count))
461 (max-silent-time 3600)
465 (print-build-trace #t)
467 (use-substitutes? #t)
468 (binary-caches '())) ; client "untrusted" cache URLs
469 ;; Must be called after `open-connection'.
472 (nix-server-socket server))
474 (let-syntax ((send (syntax-rules ()
475 ((_ (type option) ...)
477 (write-arg type option socket)
479 (write-int (operation-id set-options) socket)
480 (send (boolean keep-failed?) (boolean keep-going?)
481 (boolean try-fallback?) (integer verbosity)
482 (integer max-build-jobs) (integer max-silent-time))
483 (if (>= (nix-server-minor-version server) 2)
484 (send (boolean use-build-hook?)))
485 (if (>= (nix-server-minor-version server) 4)
486 (send (integer build-verbosity) (integer log-type)
487 (boolean print-build-trace)))
488 (if (>= (nix-server-minor-version server) 6)
489 (send (integer build-cores)))
490 (if (>= (nix-server-minor-version server) 10)
491 (send (boolean use-substitutes?)))
492 (if (>= (nix-server-minor-version server) 12)
493 (send (string-list (fold-right (lambda (pair result)
496 (cons* h t result))))
499 (let loop ((done? (process-stderr server)))
500 (or done? (process-stderr server)))))
502 (define-syntax operation
504 "Define a client-side RPC stub for the given operation."
505 ((_ (name (type arg) ...) docstring return ...)
506 (lambda (server arg ...)
508 (let ((s (nix-server-socket server)))
509 (write-int (operation-id name) s)
510 (write-arg type arg s)
512 ;; Loop until the server is done sending error output.
513 (let loop ((done? (process-stderr server)))
514 (or done? (loop (process-stderr server))))
515 (values (read-arg return s) ...))))))
517 (define-syntax-rule (define-operation (name args ...)
518 docstring return ...)
520 (operation (name args ...) docstring return ...)))
522 (define-operation (valid-path? (string path))
523 "Return #t when PATH is a valid store path."
526 (define-operation (query-path-hash (store-path path))
527 "Return the SHA256 hash of PATH as a bytevector."
530 (define add-text-to-store
531 ;; A memoizing version of `add-to-store', to avoid repeated RPCs with
532 ;; the very same arguments during a given session.
533 (let ((add-text-to-store
534 (operation (add-text-to-store (string name) (string text)
535 (string-list references))
538 (lambda (server name text references)
539 "Add TEXT under file NAME in the store, and return its store path.
540 REFERENCES is the list of store paths referred to by the resulting store
542 (let ((args `(,text ,name ,references))
543 (cache (nix-server-add-text-to-store-cache server)))
544 (or (hash-ref cache args)
545 (let ((path (add-text-to-store server name text references)))
546 (hash-set! cache args path)
550 ;; A memoizing version of `add-to-store'. This is important because
551 ;; `add-to-store' leads to huge data transfers to the server, and
552 ;; because it's often called many times with the very same argument.
553 (let ((add-to-store (operation (add-to-store (string basename)
554 (boolean fixed?) ; obsolete, must be #t
560 (lambda (server basename recursive? hash-algo file-name)
561 "Add the contents of FILE-NAME under BASENAME to the store. When
562 RECURSIVE? is true and FILE-NAME designates a directory, the contents of
563 FILE-NAME are added recursively; if FILE-NAME designates a flat file and
564 RECURSIVE? is true, its contents are added, and its permission bits are
565 kept. HASH-ALGO must be a string such as \"sha256\"."
566 (let* ((st (stat file-name #f))
567 (args `(,st ,basename ,recursive? ,hash-algo))
568 (cache (nix-server-add-to-store-cache server)))
569 (or (and st (hash-ref cache args))
570 (let ((path (add-to-store server basename #t recursive?
571 hash-algo file-name)))
572 (hash-set! cache args path)
575 (define-operation (build-derivations (string-list derivations))
576 "Build DERIVATIONS, and return when the worker is done building them.
577 Return #t on success."
580 (define-operation (add-temp-root (store-path path))
581 "Make PATH a temporary root for the duration of the current session.
585 (define-operation (add-indirect-root (string file-name))
586 "Make FILE-NAME an indirect root for the garbage collector; FILE-NAME
587 can be anywhere on the file system, but it must be an absolute file
588 name--it is the caller's responsibility to ensure that it is an absolute
589 file name. Return #t on success."
592 (define-operation (has-substitutes? (store-path path))
593 "Return #t if binary substitutes are available for PATH, and #f otherwise."
596 (define substitutable-paths
597 (operation (query-substitutable-paths (store-path-list paths))
598 "Return the subset of PATHS that is substitutable."
601 (define substitutable-path-info
602 (operation (query-substitutable-paths (store-path-list paths))
603 "Return information about the subset of PATHS that is
604 substitutable. For each substitutable path, a `substitutable?' object is
606 substitutable-path-list))
608 (define (run-gc server action to-delete min-freed)
609 "Perform the garbage-collector operation ACTION, one of the
610 `gc-action' values. When ACTION is `delete-specific', the TO-DELETE is
611 the list of store paths to delete. IGNORE-LIVENESS? should always be
612 #f. MIN-FREED is the minimum amount of disk space to be freed, in
613 bytes, before the GC can stop. Return the list of store paths delete,
614 and the number of bytes freed."
615 (let ((s (nix-server-socket server)))
616 (write-int (operation-id collect-garbage) s)
618 (write-store-path-list to-delete s)
619 (write-arg boolean #f s) ; ignore-liveness?
620 (write-long-long min-freed s)
621 (write-int 0 s) ; obsolete
622 (when (>= (nix-server-minor-version server) 5)
623 ;; Obsolete `use-atime' and `max-atime' parameters.
627 ;; Loop until the server is done sending error output.
628 (let loop ((done? (process-stderr server)))
629 (or done? (loop (process-stderr server))))
631 (let ((paths (read-store-path-list s))
632 (freed (read-long-long s))
633 (obsolete (read-long-long s)))
634 (values paths freed))))
636 (define-syntax-rule (%long-long-max)
637 ;; Maximum unsigned 64-bit integer.
640 (define (live-paths server)
641 "Return the list of live store paths---i.e., store paths still
642 referenced, and thus not subject to being garbage-collected."
643 (run-gc server (gc-action return-live) '() (%long-long-max)))
645 (define (dead-paths server)
646 "Return the list of dead store paths---i.e., store paths no longer
647 referenced, and thus subject to being garbage-collected."
648 (run-gc server (gc-action return-dead) '() (%long-long-max)))
650 (define* (collect-garbage server #:optional (min-freed (%long-long-max)))
651 "Collect garbage from the store at SERVER. If MIN-FREED is non-zero,
652 then collect at least MIN-FREED bytes. Return the paths that were
653 collected, and the number of bytes freed."
654 (run-gc server (gc-action delete-dead) '() min-freed))
656 (define* (delete-paths server paths #:optional (min-freed (%long-long-max)))
657 "Delete PATHS from the store at SERVER, if they are no longer
658 referenced. If MIN-FREED is non-zero, then stop after at least
659 MIN-FREED bytes have been collected. Return the paths that were
660 collected, and the number of bytes freed."
661 (run-gc server (gc-action delete-specific) paths min-freed))
668 (define %store-prefix
669 ;; Absolute path to the Nix store.
670 (make-parameter (or (and=> (getenv "NIX_STORE_DIR") canonicalize-path)
673 (define (store-path? path)
674 "Return #t if PATH is a store path."
675 ;; This is a lightweight check, compared to using a regexp, but this has to
676 ;; be fast as it's called often in `derivation', for instance.
677 ;; `isStorePath' in Nix does something similar.
678 (string-prefix? (%store-prefix) path))
680 (define (derivation-path? path)
681 "Return #t if PATH is a derivation path."
682 (and (store-path? path) (string-suffix? ".drv" path)))
684 (define (store-path-package-name path)
685 "Return the package name part of PATH, a file name in the store."
686 (define store-path-rx
687 (make-regexp (string-append "^.*" (regexp-quote (%store-prefix))
690 (and=> (regexp-exec store-path-rx path)
691 (cut match:substring <> 1)))