1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 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 (guix memoization)
23 #:use-module (guix serialization)
24 #:use-module (guix monads)
25 #:use-module (guix base16)
26 #:use-module (guix base32)
27 #:use-module (guix hash)
28 #:autoload (guix build syscalls) (terminal-columns)
29 #:use-module (rnrs bytevectors)
30 #:use-module (ice-9 binary-ports)
31 #:use-module (srfi srfi-1)
32 #:use-module (srfi srfi-9)
33 #:use-module (srfi srfi-9 gnu)
34 #:use-module (srfi srfi-11)
35 #:use-module (srfi srfi-26)
36 #:use-module (srfi srfi-34)
37 #:use-module (srfi srfi-35)
38 #:use-module (srfi srfi-39)
39 #:use-module (ice-9 match)
40 #:use-module (ice-9 regex)
41 #:use-module (ice-9 vlist)
42 #:use-module (ice-9 popen)
43 #:use-module (ice-9 threads)
44 #:use-module (ice-9 format)
45 #:use-module (web uri)
46 #:export (%daemon-socket-uri
48 %default-substitute-urls
51 nix-server-major-version
52 nix-server-minor-version
56 &nix-connection-error nix-connection-error?
57 nix-connection-error-file
58 nix-connection-error-code
59 &nix-protocol-error nix-protocol-error?
60 nix-protocol-error-message
61 nix-protocol-error-status
90 substitutable-references
91 substitutable-download-size
92 substitutable-nar-size
95 substitutable-path-info
101 path-info-registration-time
106 references/substitutes
114 query-derivation-outputs
122 current-build-output-port
145 store-path-package-name
150 (define %protocol-version #x161)
152 (define %worker-magic-1 #x6e697863) ; "nixc"
153 (define %worker-magic-2 #x6478696f) ; "dxio"
155 (define (protocol-major magic)
156 (logand magic #xff00))
157 (define (protocol-minor magic)
158 (logand magic #x00ff))
160 (define-syntax define-enumerate-type
162 ((_ name->int (name id) ...)
163 (define-syntax name->int
164 (syntax-rules (name ...)
165 ((_ name) id) ...)))))
167 (define-enumerate-type operation-id
168 ;; operation numbers from worker-protocol.hh
176 (add-text-to-store 8)
180 (add-indirect-root 12)
187 ;;(query-substitutable-path-info 21) ; obsolete as of #x10c
188 (query-derivation-outputs 22)
189 (query-all-valid-paths 23)
190 (query-failed-paths 24)
191 (clear-failed-paths 25)
194 (query-derivation-output-names 28)
195 (query-path-from-hash-part 29)
196 (query-substitutable-path-infos 30)
197 (query-valid-paths 31)
198 (query-substitutable-paths 32)
199 (query-valid-derivers 33)
202 (built-in-builders 80))
204 (define-enumerate-type hash-algo
210 (define-enumerate-type build-mode
216 (define-enumerate-type gc-action
223 (define %default-socket-path
224 (string-append %state-directory "/daemon-socket/socket"))
226 (define %daemon-socket-uri
227 ;; URI or file name of the socket the daemon listens too.
228 (make-parameter (or (getenv "GUIX_DAEMON_SOCKET")
229 %default-socket-path)))
233 ;; Information about a substitutable store path.
234 (define-record-type <substitutable>
235 (substitutable path deriver refs dl-size nar-size)
237 (path substitutable-path)
238 (deriver substitutable-deriver)
239 (refs substitutable-references)
240 (dl-size substitutable-download-size)
241 (nar-size substitutable-nar-size))
243 (define (read-substitutable-path-list p)
244 (let loop ((len (read-int p))
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)))
254 (cons (substitutable path deriver refs dl-size nar-size)
257 ;; Information about a store path.
258 (define-record-type <path-info>
259 (path-info deriver hash references registration-time nar-size)
261 (deriver path-info-deriver) ;string | #f
262 (hash path-info-hash)
263 (references path-info-references)
264 (registration-time path-info-registration-time)
265 (nar-size path-info-nar-size))
267 (define (read-path-info p)
268 (let ((deriver (match (read-store-path p)
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)))
277 (define-syntax write-arg
278 (syntax-rules (integer boolean bytevector
279 string string-list string-pairs
280 store-path store-path-list base16)
284 (write-int (if arg 1 0) p))
285 ((_ bytevector arg p)
286 (write-bytevector arg p))
288 (write-string arg p))
289 ((_ string-list arg p)
290 (write-string-list arg p))
291 ((_ string-pairs arg p)
292 (write-string-pairs arg p))
293 ((_ store-path arg p)
294 (write-store-path arg p))
295 ((_ store-path-list arg p)
296 (write-store-path-list arg p))
298 (write-string (bytevector->base16-string arg) p))))
300 (define-syntax read-arg
301 (syntax-rules (integer boolean string store-path store-path-list string-list
302 substitutable-path-list path-info base16)
306 (not (zero? (read-int p))))
311 ((_ store-path-list p)
312 (read-store-path-list p))
314 (read-string-list p))
315 ((_ substitutable-path-list p)
316 (read-substitutable-path-list p))
320 (base16-string->bytevector (read-string p)))))
325 (define-record-type <nix-server>
326 (%make-nix-server socket major minor
328 ats-cache atts-cache)
330 (socket nix-server-socket)
331 (major nix-server-major-version)
332 (minor nix-server-minor-version)
334 (buffer nix-server-output-port) ;output port
335 (flush nix-server-flush-output) ;thunk
337 ;; Caches. We keep them per-connection, because store paths build
338 ;; during the session are temporary GC roots kept for the duration of
340 (ats-cache nix-server-add-to-store-cache)
341 (atts-cache nix-server-add-text-to-store-cache))
343 (set-record-type-printer! <nix-server>
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)
351 (define-condition-type &nix-error &error
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))
359 (define-condition-type &nix-protocol-error &nix-error
361 (message nix-protocol-error-message)
362 (status nix-protocol-error-status))
364 (define-syntax-rule (system-error-to-connection-error file exp ...)
365 "Catch 'system-error' exceptions and translate them to
366 '&nix-connection-error'."
371 (let ((errno (system-error-errno args)))
372 (raise (condition (&nix-connection-error
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."
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)))
384 (system-error-to-connection-error file
388 (define %default-guix-port
389 ;; Default port when connecting to a daemon over TCP/IP.
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."
395 ;; Define 'TCP_NODELAY' on Guile 2.0. The value is the same on all GNU
397 (cond-expand (guile-2.2 #t)
398 (else (define TCP_NODELAY 1)))
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))))
405 (if (number? port) (number->string port) port)
407 (logior AI_ADDRCONFIG AI_NUMERICSERV)
409 0 ;any address family
410 SOCK_STREAM)) ;TCP only
412 (let loop ((addresses addresses))
415 (let ((s (socket (addrinfo:fam ai)
417 SOCK_STREAM IPPROTO_IP)))
421 (connect s (addrinfo:addr ai))
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)
428 ;; Connection failed, so try one of the other addresses.
431 (raise (condition (&nix-connection-error
433 (errno (system-error-errno args)))))
436 (define (connect-to-daemon uri)
437 "Connect to the daemon at URI, a string that may be an actual URI or a file
439 (define (not-supported)
440 (raise (condition (&nix-connection-error
445 (match (string->uri uri)
446 (#f ;URI is a file name
447 open-unix-domain-socket)
449 (match (uri-scheme uri)
452 (open-unix-domain-socket (uri-path uri))))
455 (open-inet-socket (uri-host uri)
456 (or (uri-port uri) %default-guix-port))))
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)))
463 (match (false-if-exception
464 (module-ref module 'connect-to-daemon))
465 ((? procedure? connect)
468 (x (not-supported))))
469 (#f (not-supported))))
475 (define* (open-connection #:optional (uri (%daemon-socket-uri))
476 #:key port (reserve-space? #t) cpu-affinity)
477 "Connect to the daemon at URI (a string), or, if PORT is not #f, use it as
478 the I/O port over which to communicate to a build daemon.
480 When RESERVE-SPACE? is true, instruct it to reserve a little bit of extra
481 space on the file system so that the garbage collector can still operate,
482 should the disk become full. When CPU-AFFINITY is true, it must be an integer
483 corresponding to an OS-level CPU number to which the daemon's worker process
484 for this connection will be pinned. Return a server object."
485 (guard (c ((nar-error? c)
486 ;; One of the 'write-' or 'read-' calls below failed, but this is
487 ;; really a connection error.
489 (&nix-connection-error (file (or port uri))
491 (&message (message "build daemon handshake failed"))))))
492 (let*-values (((port)
493 (or port (connect-to-daemon uri)))
495 (buffering-output-port port
496 (make-bytevector 8192))))
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)
504 (write-int %protocol-version port)
505 (when (>= (protocol-minor v) 14)
506 (write-int (if cpu-affinity 1 0) port)
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
515 (make-hash-table 100)
516 (make-hash-table 100))))
517 (let loop ((done? (process-stderr conn)))
518 (or done? (process-stderr conn)))
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)))
526 (define (close-connection server)
527 "Close the connection to SERVER."
528 (close (nix-server-socket server)))
530 (define-syntax-rule (with-store store exp ...)
531 "Bind STORE to an open connection to the store and evaluate EXPs;
532 automatically close the store when the dynamic extent of EXP is left."
533 (let ((store (open-connection)))
539 (false-if-exception (close-connection store))))))
541 (define current-build-output-port
542 ;; The port where build output is sent.
543 (make-parameter (current-error-port)))
545 (define* (dump-port in out
547 #:key (buffer-size 16384))
548 "Read LEN bytes from IN (or as much as possible if LEN is #f) and write it
549 to OUT, using chunks of BUFFER-SIZE bytes."
551 (make-bytevector buffer-size))
554 (bytes (get-bytevector-n! in buffer 0
556 (min len buffer-size)
558 (or (eof-object? bytes)
559 (and len (= total len))
560 (let ((total (+ total bytes)))
561 (put-bytevector out buffer 0 bytes)
563 (get-bytevector-n! in buffer 0
565 (min (- len total) buffer-size)
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))
574 (define* (process-stderr server #:optional user-port)
575 "Read standard output and standard error from SERVER, writing it to
576 CURRENT-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'
578 again until #t is returned or an error is raised.
580 Since the build process's output cannot be assumed to be UTF-8, we
581 conservatively consider it to be Latin-1, thereby avoiding possible
582 encoding conversion errors."
584 (nix-server-socket server))
586 ;; magic cookies from worker-protocol.hh
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
593 (let ((k (read-int p)))
594 (cond ((= k %stderr-write)
595 ;; Write a byte stream to USER-PORT.
596 (let* ((len (read-int p))
598 (dump-port p user-port len
599 #:buffer-size (if (<= len 16384) 16384 65536))
601 ;; Consume padding, as for strings.
602 (get-bytevector-n p (- 8 m))))
605 ;; Read a byte stream from USER-PORT.
606 ;; Note: Avoid 'get-bytevector-n' to work around
607 ;; <http://bugs.gnu.org/17591> in Guile up to 2.0.11.
608 (let* ((max-len (read-int p))
609 (data (make-bytevector max-len))
610 (len (get-bytevector-n! user-port data 0 max-len)))
611 (write-bytevector data p)
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)))
619 (display s (current-build-output-port))
620 (when (string-any %newlines s)
621 (force-output (current-build-output-port)))
625 (let ((error (read-maybe-utf8-string p))
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))))
632 (raise (condition (&nix-protocol-error
636 ;; The daemon is done (see `stopWork' in `nix-worker.cc'.)
639 (raise (condition (&nix-protocol-error
640 (message "invalid error code")
643 (define %default-substitute-urls
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.)
647 (map (if (false-if-exception (resolve-interface '(gnutls)))
648 (cut string-append "https://" <>)
649 (cut string-append "http://" <>))
650 '("mirror.hydra.gnu.org")))
652 (define* (set-build-options server
653 #:key keep-failed? keep-going? fallback?
655 rounds ;number of build rounds
662 (print-build-trace #t)
664 (use-substitutes? #t)
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
672 ;; Number of columns in the client's terminal.
673 (terminal-columns (terminal-columns))
675 ;; Locale of the client.
676 (locale (false-if-exception (setlocale LC_ALL))))
677 ;; Must be called after `open-connection'.
680 (nix-server-socket server))
682 (let-syntax ((send (syntax-rules ()
683 ((_ (type option) ...)
685 (write-arg type option socket)
687 (write-int (operation-id set-options) socket)
688 (send (boolean keep-failed?) (boolean keep-going?)
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))))
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)))
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))))
703 (when (>= (nix-server-minor-version server) 10)
704 (send (boolean use-substitutes?)))
705 (when (>= (nix-server-minor-version server) 12)
706 (let ((pairs `(,@(if timeout
707 `(("build-timeout" . ,(number->string timeout)))
709 ,@(if max-silent-time
710 `(("build-max-silent-time"
711 . ,(number->string max-silent-time)))
715 . ,(number->string max-build-jobs)))
718 `(("build-cores" . ,(number->string build-cores)))
720 ,@(if substitute-urls
722 . ,(string-join substitute-urls)))
726 . ,(number->string (max 0 (1- rounds)))))
728 ,@(if terminal-columns
729 `(("terminal-columns"
730 . ,(number->string terminal-columns)))
733 `(("locale" . ,locale))
735 (send (string-pairs pairs))))
736 (let loop ((done? (process-stderr server)))
737 (or done? (process-stderr server)))))
739 (define (buffering-output-port port buffer)
740 "Return two value: an output port wrapped around PORT that uses BUFFER (a
741 bytevector) 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.
745 (bytevector-length buffer))
750 (put-bytevector port buffer 0 total)
753 (define (write bv offset count)
754 (if (zero? count) ;end of file
756 (let loop ((offset offset)
759 (cond ((= total size)
761 (loop offset count written))
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))))))))
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"
778 ;; Mapping from RPC names (symbols) to invocation counts.
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)
785 (< (cdr rpc1) (cdr rpc2))))))
786 (format port "Remote procedure call summary: ~a RPCs~%"
788 (((names . counts) ...)
789 (reduce + 0 counts))))
790 (for-each (match-lambda
792 (format port " ~30a ... ~5@a~%" rpc count)))
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)
799 (if (member "rpc" profiled)
801 (add-hook! exit-hook show-rpc-profile)
803 (let ((count (or (hashq-ref %rpc-calls name) 0)))
804 (hashq-set! %rpc-calls name (+ count 1)))))
808 (define-syntax operation
810 "Define a client-side RPC stub for the given operation."
811 ((_ (name (type arg) ...) docstring return ...)
812 (lambda (server arg ...)
814 (let* ((s (nix-server-socket server))
815 (buffered (nix-server-output-port server)))
816 (record-operation 'name)
817 (write-int (operation-id name) buffered)
818 (write-arg type arg buffered)
820 (write-buffered-output server)
822 ;; Loop until the server is done sending error output.
823 (let loop ((done? (process-stderr server)))
824 (or done? (loop (process-stderr server))))
825 (values (read-arg return s) ...))))))
827 (define-syntax-rule (define-operation (name args ...)
828 docstring return ...)
830 (operation (name args ...) docstring return ...)))
832 (define-operation (valid-path? (string path))
833 "Return #t when PATH designates a valid store item and #f otherwise (an
834 invalid item may exist on disk but still be invalid, for instance because it
835 is the result of an aborted or failed build.)
837 A '&nix-protocol-error' condition is raised if PATH is not prefixed by the
838 store directory (/gnu/store)."
841 (define-operation (query-path-hash (store-path path))
842 "Return the SHA256 hash of the nar serialization of PATH as a bytevector."
845 (define hash-part->path
846 (let ((query-path-from-hash-part
847 (operation (query-path-from-hash-part (string hash))
850 (lambda (server hash-part)
851 "Return the store path whose hash part is HASH-PART (a nix-base32
852 string). Raise an error if no such path exists."
853 ;; This RPC is primarily used by Hydra to reply to HTTP GETs of
855 (query-path-from-hash-part server hash-part))))
857 (define-operation (query-path-info (store-path path))
858 "Return the info (hash, references, etc.) for PATH."
861 (define add-data-to-store
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
865 (operation (add-text-to-store (string name) (bytevector text)
866 (string-list references))
869 (lambda* (server name bytes #:optional (references '()))
870 "Add BYTES under file NAME in the store, and return its store path.
871 REFERENCES is the list of store paths referred to by the resulting store
873 (let* ((args `(,bytes ,name ,references))
874 (cache (nix-server-add-text-to-store-cache server)))
875 (or (hash-ref cache args)
876 (let ((path (add-text-to-store server name bytes references)))
877 (hash-set! cache args path)
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.
882 REFERENCES is the list of store paths referred to by the resulting store
884 (add-data-to-store store name (string->utf8 text) references))
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.
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.
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
902 (record-operation 'add-to-store)
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))
915 "Add the contents of FILE-NAME under BASENAME to the store. When
916 RECURSIVE? is false, FILE-NAME must designate a regular file--not a directory
917 nor a symlink. When RECURSIVE? is true and FILE-NAME designates a directory,
918 the contents of FILE-NAME are added recursively; if FILE-NAME designates a
919 flat file and RECURSIVE? is true, its contents are added, and its permission
920 bits are kept. HASH-ALGO must be a string such as \"sha256\".
922 When RECURSIVE? is true, call (SELECT? FILE STAT) for each directory entry,
923 where 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."
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?))
928 (cache (nix-server-add-to-store-cache server)))
929 (or (hash-ref cache args)
930 (let ((path (add-to-store server basename recursive?
933 (hash-set! cache args path)
937 (let ((build (operation (build-things (string-list things)
941 (build/old (operation (build-things (string-list things))
944 (lambda* (store things #:optional (mode (build-mode normal)))
945 "Build THINGS, a list of store items which may be either '.drv' files or
946 outputs, and return when the worker is done building them. Elements of THINGS
947 that are not derivations can only be substituted and not built locally.
948 Return #t on success."
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")
957 (define-operation (add-temp-root (store-path path))
958 "Make PATH a temporary root for the duration of the current session.
962 (define-operation (add-indirect-root (string file-name))
963 "Make the symlink FILE-NAME an indirect root for the garbage collector:
964 whatever store item FILE-NAME points to will not be collected. Return #t on
967 FILE-NAME can be anywhere on the file system, but it must be an absolute file
968 name--it is the caller's responsibility to ensure that it is an absolute file
972 (define %gc-roots-directory
973 ;; The place where garbage collector roots (symlinks) are kept.
974 (string-append %state-directory "/gcroots"))
976 (define (add-permanent-root target)
977 "Add a garbage collector root pointing to TARGET, an element of the store,
978 preventing TARGET from even being collected. This can also be used if TARGET
981 Raise an error if the caller does not have write access to the GC root
983 (let* ((root (string-append %gc-roots-directory "/" (basename target))))
986 (symlink target root))
988 ;; If ROOT already exists, this is fine; otherwise, re-throw.
989 (unless (= EEXIST (system-error-errno args))
990 (apply throw args))))))
992 (define (remove-permanent-root target)
993 "Remove the permanent garbage collector root pointing to TARGET. Raise an
994 error if there is no such root."
995 (delete-file (string-append %gc-roots-directory "/" (basename target))))
998 (operation (query-references (store-path path))
999 "Return the list of references of PATH."
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))
1010 (define (references/substitutes store items)
1011 "Return the list of list of references of ITEMS; the result has the same
1012 length as ITEMS. Query substitute information for any item missing from the
1013 store at once. Raise a '&nix-protocol-error' exception if reference
1014 information for one of ITEMS is missing."
1015 (let* ((requested items)
1016 (local-refs (map (lambda (item)
1017 (or (hash-ref %reference-cache item)
1018 (guard (c ((nix-protocol-error? c) #f))
1019 (references store item))))
1021 (missing (fold-right (lambda (item local-ref result)
1024 (cons item result)))
1028 ;; Query all the substitutes at once to minimize the cost of
1029 ;; launching 'guix substitute' and making HTTP requests.
1030 (substs (if (null? missing)
1032 (substitutable-path-info store missing))))
1033 (when (< (length substs) (length missing))
1034 (raise (condition (&nix-protocol-error
1035 (message "cannot determine \
1036 the list of references")
1039 ;; Intersperse SUBSTS and LOCAL-REFS.
1040 (let loop ((items items)
1041 (local-refs local-refs)
1045 (let ((result (reverse result)))
1046 (for-each (cut hash-set! %reference-cache <> <>)
1053 (cons (any (lambda (subst)
1054 (and (string=? (substitutable-path subst) item)
1055 (substitutable-references subst)))
1060 (cons head result)))))))))
1062 (define* (fold-path store proc seed paths
1063 #:optional (relatives (cut references store <>)))
1064 "Call PROC for each of the RELATIVES of PATHS, exactly once, and return the
1065 result formed from the successive calls to PROC, the first of which is passed
1067 (let loop ((paths paths)
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))))
1081 (define (requisites store paths)
1082 "Return the requisites of PATHS, including PATHS---i.e., their closures (all
1083 its references, recursively)."
1084 (fold-path store cons '() paths))
1086 (define (topologically-sorted store paths)
1087 "Return a list containing PATHS and all their references sorted in
1090 ;; Do a simple depth-first traversal of all of PATHS.
1091 (let loop ((paths paths)
1092 (visited vlist-null)
1095 (vhash-cons n #t visited))
1097 (define (visited? n)
1098 (vhash-assoc n visited))
1103 (loop tail visited result)
1106 (loop (references store head)
1109 (lambda (visited result)
1112 (cons head result))))))
1114 (values visited result)))))
1116 (call-with-values traverse
1121 (operation (query-referrers (store-path path))
1122 "Return the list of path that refer to PATH."
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."
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."
1136 (define-operation (has-substitutes? (store-path path))
1137 "Return #t if binary substitutes are available for PATH, and #f otherwise."
1140 (define substitutable-paths
1141 (operation (query-substitutable-paths (store-path-list paths))
1142 "Return the subset of PATHS that is substitutable."
1145 (define substitutable-path-info
1146 (operation (query-substitutable-path-infos (store-path-list paths))
1147 "Return information about the subset of PATHS that is
1148 substitutable. For each substitutable path, a `substitutable?' object is
1149 returned; thus, the resulting list can be shorter than PATHS. Furthermore,
1150 that there is no guarantee that the order of the resulting list matches the
1152 substitutable-path-list))
1154 (define built-in-builders
1155 (let ((builders (operation (built-in-builders)
1156 "Return the built-in builders."
1159 "Return the names of the supported built-in derivation builders
1160 supported 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)))
1171 (define-operation (optimize-store)
1172 "Optimize the store by hard-linking identical files (\"deduplication\".)
1173 Return #t on success."
1174 ;; Note: the daemon in Guix <= 0.8.2 does not implement this RPC.
1177 (define verify-store
1178 (let ((verify (operation (verify-store (boolean check-contents?)
1182 (lambda* (store #:key check-contents? repair?)
1183 "Verify the integrity of the store and return false if errors remain,
1184 and true otherwise. When REPAIR? is true, repair any missing or altered store
1185 items by substituting them (this typically requires root privileges because it
1186 is not an atomic operation.) When CHECK-CONTENTS? is true, check the contents
1187 of store items; this can take a lot of time."
1188 (not (verify store check-contents? repair?)))))
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
1193 the 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
1195 bytes, before the GC can stop. Return the list of store paths delete,
1196 and 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.
1209 ;; Loop until the server is done sending error output.
1210 (let loop ((done? (process-stderr server)))
1211 (or done? (loop (process-stderr server))))
1213 (let ((paths (read-store-path-list s))
1214 (freed (read-long-long s))
1215 (obsolete (read-long-long s)))
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
1220 (hash-clear! (nix-server-add-to-store-cache server))
1221 (hash-clear! (nix-server-add-text-to-store-cache server)))
1223 (values paths freed))))
1225 (define-syntax-rule (%long-long-max)
1226 ;; Maximum unsigned 64-bit integer.
1229 (define (live-paths server)
1230 "Return the list of live store paths---i.e., store paths still
1231 referenced, and thus not subject to being garbage-collected."
1232 (run-gc server (gc-action return-live) '() (%long-long-max)))
1234 (define (dead-paths server)
1235 "Return the list of dead store paths---i.e., store paths no longer
1236 referenced, and thus subject to being garbage-collected."
1237 (run-gc server (gc-action return-dead) '() (%long-long-max)))
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,
1241 then collect at least MIN-FREED bytes. Return the paths that were
1242 collected, and the number of bytes freed."
1243 (run-gc server (gc-action delete-dead) '() min-freed))
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
1247 referenced. If MIN-FREED is non-zero, then stop after at least
1248 MIN-FREED bytes have been collected. Return the paths that were
1249 collected, and the number of bytes freed."
1250 (run-gc server (gc-action delete-specific) paths min-freed))
1252 (define (import-paths server port)
1253 "Import the set of store paths read from PORT into SERVER's store. An error
1254 is 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)))
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))))
1272 (define* (export-paths server paths port #:key (sign? #t) recursive?)
1273 "Export the store paths listed in PATHS to PORT, in topological order,
1274 signing them if SIGN? is true. When RECURSIVE? is true, export the closure of
1275 PATHS---i.e., PATHS and all their dependencies."
1277 (let ((sorted (topologically-sorted server paths)))
1278 ;; When RECURSIVE? is #f, filter out the references of PATHS.
1281 (filter (cut member <> paths) sorted))))
1283 (let loop ((paths ordered))
1289 (and (export-path server head port #:sign? sign?)
1292 (define-operation (query-failed-paths)
1293 "Return the list of store items for which a build failure is cached.
1295 The result is always the empty list unless the daemon was started with
1296 '--cache-failures'."
1299 (define-operation (clear-failed-paths (store-path-list items))
1300 "Remove ITEMS from the list of cached build failures.
1302 This makes sense only when the daemon was started with '--cache-failures'."
1305 (define* (register-path path
1306 #:key (references '()) deriver prefix
1308 "Register PATH as a valid store file, with REFERENCES as its list of
1309 references, and DERIVER as its deriver (.drv that led to it.) If PREFIX is
1310 not #f, it must be the name of the directory containing the new store to
1311 initialize; if STATE-DIRECTORY is not #f, it must be a string containing the
1312 absolute file name to the state directory of the store being initialized.
1313 Return #t on success.
1315 Use with care as it directly modifies the store! This is primarily meant to
1316 be used internally by the daemon's build hook."
1317 ;; Currently this is implemented by calling out to the fine C++ blob.
1318 (let ((pipe (apply open-pipe* OPEN_WRITE %guix-register-program
1320 `("--prefix" ,prefix)
1322 ,@(if state-directory
1323 `("--state-directory" ,state-directory)
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))))))
1337 (define-syntax-rule (define-alias new old)
1338 (define-syntax new (identifier-syntax old)))
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)
1347 ;; Instantiate templates for %STORE-MONAD since it's syntactically different
1348 ;; from %STATE-MONAD.
1349 (template-directory instantiations %store-monad)
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))
1357 (define (store-lift proc)
1358 "Lift PROC, a procedure whose first argument is a connection to the store,
1359 in the store monad."
1360 (preserve-documentation proc
1363 (values (apply proc store args) store)))))
1365 (define (store-lower proc)
1366 "Lower PROC, a monadic procedure in %STORE-MONAD, to a \"normal\" procedure
1367 taking the store as its first argument."
1368 (preserve-documentation proc
1369 (lambda (store . args)
1370 (run-with-store store (apply proc args)))))
1373 ;; Store monad operators.
1376 (define* (text-file name text
1377 #:optional (references '()))
1378 "Return as a monadic value the absolute file name in the store of the file
1379 containing TEXT, a string. REFERENCES is a list of store items that the
1380 resulting text file refers to; it defaults to the empty list."
1382 (values (add-text-to-store store name text references)
1385 (define* (interned-file file #:optional name
1386 #:key (recursive? #t) (select? true))
1387 "Return the name of FILE once interned in the store. Use NAME as its store
1388 name, or the basename of FILE if NAME is omitted.
1390 When RECURSIVE? is true, the contents of FILE are added recursively; if FILE
1391 designates a flat file and RECURSIVE? is true, its contents are added, and its
1392 permission bits are kept.
1394 When RECURSIVE? is true, call (SELECT? FILE STAT) for each directory entry,
1395 where 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."
1398 (values (add-to-store store (or name (basename file))
1399 recursive? "sha256" file
1404 ;; Monadic variant of 'build-things'.
1405 (store-lift build-things))
1407 (define set-build-options*
1408 (store-lift set-build-options))
1411 (store-lift references))
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.
1418 (values (%current-system) state)))
1420 (define-inlinable (set-current-system system)
1421 ;; Set the %CURRENT-SYSTEM fluid at bind time.
1423 (values (%current-system system) state)))
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))
1430 (define* (run-with-store store mval
1432 (guile-for-build (%guile-for-build))
1433 (system (%current-system))
1435 "Run MVAL, a monadic value in the store monad, in STORE, an open store
1436 connection, and return the result."
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.
1440 (parameterize ((%guile-for-build guile-for-build)
1441 (%current-system system)
1442 (%current-target-system target))
1443 (call-with-values (lambda ()
1444 (run-with-state mval store))
1445 (lambda (result store)
1446 ;; Discard the state.
1454 (define %store-prefix
1455 ;; Absolute path to the Nix store.
1456 (make-parameter %store-directory))
1458 (define (compressed-hash bv size) ; `compressHash'
1459 "Given the hash stored in BV, return a compressed version thereof that fits
1461 (define new (make-bytevector size 0))
1462 (define old-size (bytevector-length bv))
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)))
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) "-"
1483 (define (output-path output hash name) ; makeOutputPath
1484 "Return an output path for OUTPUT (the name of the output as a string) of
1485 the derivation called NAME with hash HASH."
1486 (store-path (string-append "output:" output) hash
1487 (if (string=? output "out")
1489 (string-append name "-" output))))
1491 (define* (fixed-output-path name hash
1496 "Return an output path for the fixed output OUTPUT defined by HASH of type
1497 HASH-ALGO, of the derivation NAME. RECURSIVE? has the same meaning as for
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))
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))
1516 (define (direct-store-path? path)
1517 "Return #t if PATH is a store path, and not a sub-directory of a store path.
1518 This predicate is sometimes needed because files *under* a store path are not
1520 (and (store-path? path)
1521 (not (string=? path (%store-prefix)))
1522 (let ((len (+ 1 (string-length (%store-prefix)))))
1523 (not (string-index (substring path len) #\/)))))
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))
1534 (define (derivation-path? path)
1535 "Return #t if PATH is a derivation path."
1536 (and (store-path? path) (string-suffix? ".drv" path)))
1538 (define store-regexp*
1539 ;; The substituter makes repeated calls to 'store-path-hash-part', hence
1540 ;; this optimization.
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})-([^/]+)$"))))
1546 (define (store-path-package-name path)
1547 "Return the package name part of PATH, a file name in the store."
1548 (let ((path-rx (store-regexp* (%store-prefix))))
1549 (and=> (regexp-exec path-rx path)
1550 (cut match:substring <> 2))))
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
1554 syntactically valid store path."
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)
1562 (define (log-file store file)
1563 "Return the build log file for FILE, or #f if none could be found. FILE
1564 must be an absolute store file name, or a derivation file name."
1565 (cond ((derivation-path? file)
1566 (let* ((base (basename file))
1567 (log (string-append (dirname %state-directory) ; XXX
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)
1576 (match (valid-derivers store file)
1578 ;; Return the first that works.
1579 (any (cut log-file store <>) derivers))
1582 ;;; Local Variables:
1583 ;;; eval: (put 'system-error-to-connection-error 'scheme-indent-function 1)