1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015 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 serialization)
23 #:use-module (guix monads)
24 #:autoload (guix base32) (bytevector->base32-string)
25 #:use-module (rnrs bytevectors)
26 #:use-module (rnrs io ports)
27 #:use-module (srfi srfi-1)
28 #:use-module (srfi srfi-9)
29 #:use-module (srfi srfi-9 gnu)
30 #:use-module (srfi srfi-26)
31 #:use-module (srfi srfi-34)
32 #:use-module (srfi srfi-35)
33 #:use-module (srfi srfi-39)
34 #:use-module (ice-9 match)
35 #:use-module (ice-9 regex)
36 #:use-module (ice-9 vlist)
37 #:use-module (ice-9 popen)
38 #:export (%daemon-socket-file
42 nix-server-major-version
43 nix-server-minor-version
47 &nix-connection-error nix-connection-error?
48 nix-connection-error-file
49 nix-connection-error-code
50 &nix-protocol-error nix-protocol-error?
51 nix-protocol-error-message
52 nix-protocol-error-status
76 substitutable-references
77 substitutable-download-size
78 substitutable-nar-size
81 substitutable-path-info
87 path-info-registration-time
95 query-derivation-outputs
103 current-build-output-port
121 store-path-package-name
125 (define %protocol-version #x10c)
127 (define %worker-magic-1 #x6e697863) ; "nixc"
128 (define %worker-magic-2 #x6478696f) ; "dxio"
130 (define (protocol-major magic)
131 (logand magic #xff00))
132 (define (protocol-minor magic)
133 (logand magic #x00ff))
135 (define-syntax define-enumerate-type
137 ((_ name->int (name id) ...)
138 (define-syntax name->int
139 (syntax-rules (name ...)
140 ((_ name) id) ...)))))
142 (define-enumerate-type operation-id
143 ;; operation numbers from worker-protocol.hh
151 (add-text-to-store 8)
155 (add-indirect-root 12)
162 ;;(query-substitutable-path-info 21) ; obsolete as of #x10c
163 (query-derivation-outputs 22)
164 (query-all-valid-paths 23)
165 (query-failed-paths 24)
166 (clear-failed-paths 25)
169 (query-derivation-output-names 28)
170 (query-path-from-hash-part 29)
171 (query-substitutable-path-infos 30)
172 (query-valid-paths 31)
173 (query-substitutable-paths 32)
174 (query-valid-derivers 33))
176 (define-enumerate-type hash-algo
182 (define-enumerate-type gc-action
189 (define %default-socket-path
190 (string-append %state-directory "/daemon-socket/socket"))
192 (define %daemon-socket-file
193 ;; File name of the socket the daemon listens too.
194 (make-parameter (or (getenv "GUIX_DAEMON_SOCKET")
195 %default-socket-path)))
199 ;; Information about a substitutable store path.
200 (define-record-type <substitutable>
201 (substitutable path deriver refs dl-size nar-size)
203 (path substitutable-path)
204 (deriver substitutable-deriver)
205 (refs substitutable-references)
206 (dl-size substitutable-download-size)
207 (nar-size substitutable-nar-size))
209 (define (read-substitutable-path-list p)
210 (let loop ((len (read-int p))
214 (let ((path (read-store-path p))
215 (deriver (read-store-path p))
216 (refs (read-store-path-list p))
217 (dl-size (read-long-long p))
218 (nar-size (read-long-long p)))
220 (cons (substitutable path deriver refs dl-size nar-size)
223 ;; Information about a store path.
224 (define-record-type <path-info>
225 (path-info deriver hash references registration-time nar-size)
227 (deriver path-info-deriver)
228 (hash path-info-hash)
229 (references path-info-references)
230 (registration-time path-info-registration-time)
231 (nar-size path-info-nar-size))
233 (define (read-path-info p)
234 (let ((deriver (read-store-path p))
235 (hash (base16-string->bytevector (read-string p)))
236 (refs (read-store-path-list p))
237 (registration-time (read-int p))
238 (nar-size (read-long-long p)))
239 (path-info deriver hash refs registration-time nar-size)))
241 (define-syntax write-arg
242 (syntax-rules (integer boolean file string string-list string-pairs
243 store-path store-path-list base16)
247 (write-int (if arg 1 0) p))
251 (write-string arg p))
252 ((_ string-list arg p)
253 (write-string-list arg p))
254 ((_ string-pairs arg p)
255 (write-string-pairs arg p))
256 ((_ store-path arg p)
257 (write-store-path arg p))
258 ((_ store-path-list arg p)
259 (write-store-path-list arg p))
261 (write-string (bytevector->base16-string arg) p))))
263 (define-syntax read-arg
264 (syntax-rules (integer boolean string store-path store-path-list
265 substitutable-path-list path-info base16)
269 (not (zero? (read-int p))))
274 ((_ store-path-list p)
275 (read-store-path-list p))
276 ((_ substitutable-path-list p)
277 (read-substitutable-path-list p))
281 (base16-string->bytevector (read-string p)))))
286 (define-record-type <nix-server>
287 (%make-nix-server socket major minor
288 ats-cache atts-cache)
290 (socket nix-server-socket)
291 (major nix-server-major-version)
292 (minor nix-server-minor-version)
294 ;; Caches. We keep them per-connection, because store paths build
295 ;; during the session are temporary GC roots kept for the duration of
297 (ats-cache nix-server-add-to-store-cache)
298 (atts-cache nix-server-add-text-to-store-cache))
300 (set-record-type-printer! <nix-server>
302 (format port "#<build-daemon ~a.~a ~a>"
303 (nix-server-major-version obj)
304 (nix-server-minor-version obj)
305 (number->string (object-address obj)
308 (define-condition-type &nix-error &error
311 (define-condition-type &nix-connection-error &nix-error
312 nix-connection-error?
313 (file nix-connection-error-file)
314 (errno nix-connection-error-code))
316 (define-condition-type &nix-protocol-error &nix-error
318 (message nix-protocol-error-message)
319 (status nix-protocol-error-status))
321 (define* (open-connection #:optional (file (%daemon-socket-file))
322 #:key (reserve-space? #t))
323 "Connect to the daemon over the Unix-domain socket at FILE. When
324 RESERVE-SPACE? is true, instruct it to reserve a little bit of extra
325 space on the file system so that the garbage collector can still
326 operate, should the disk become full. Return a server object."
327 (let ((s (with-fluids ((%default-port-encoding #f))
328 ;; This trick allows use of the `scm_c_read' optimization.
329 (socket PF_UNIX SOCK_STREAM 0)))
330 (a (make-socket-address PF_UNIX file)))
335 ;; Translate the error to something user-friendly.
336 (let ((errno (system-error-errno args)))
337 (raise (condition (&nix-connection-error
341 (write-int %worker-magic-1 s)
342 (let ((r (read-int s)))
343 (and (eqv? r %worker-magic-2)
344 (let ((v (read-int s)))
345 (and (eqv? (protocol-major %protocol-version)
348 (write-int %protocol-version s)
349 (if (>= (protocol-minor v) 11)
350 (write-int (if reserve-space? 1 0) s))
351 (let ((s (%make-nix-server s
354 (make-hash-table 100)
355 (make-hash-table 100))))
356 (let loop ((done? (process-stderr s)))
357 (or done? (process-stderr s)))
360 (define (close-connection server)
361 "Close the connection to SERVER."
362 (close (nix-server-socket server)))
364 (define-syntax-rule (with-store store exp ...)
365 "Bind STORE to an open connection to the store and evaluate EXPs;
366 automatically close the store when the dynamic extent of EXP is left."
367 (let ((store (open-connection)))
373 (false-if-exception (close-connection store))))))
375 (define current-build-output-port
376 ;; The port where build output is sent.
377 (make-parameter (current-error-port)))
379 (define* (dump-port in out
381 #:key (buffer-size 16384))
382 "Read LEN bytes from IN (or as much as possible if LEN is #f) and write it
383 to OUT, using chunks of BUFFER-SIZE bytes."
385 (make-bytevector buffer-size))
388 (bytes (get-bytevector-n! in buffer 0
390 (min len buffer-size)
392 (or (eof-object? bytes)
393 (and len (= total len))
394 (let ((total (+ total bytes)))
395 (put-bytevector out buffer 0 bytes)
397 (get-bytevector-n! in buffer 0
399 (min (- len total) buffer-size)
403 ;; Newline characters triggering a flush of 'current-build-output-port'.
404 ;; Unlike Guile's _IOLBF, we flush upon #\return so that progress reports
405 ;; that use that trick are correctly displayed.
406 (char-set #\newline #\return))
408 (define* (process-stderr server #:optional user-port)
409 "Read standard output and standard error from SERVER, writing it to
410 CURRENT-BUILD-OUTPUT-PORT. Return #t when SERVER is done sending data, and
411 #f otherwise; in the latter case, the caller should call `process-stderr'
412 again until #t is returned or an error is raised.
414 Since the build process's output cannot be assumed to be UTF-8, we
415 conservatively consider it to be Latin-1, thereby avoiding possible
416 encoding conversion errors."
418 (nix-server-socket server))
420 ;; magic cookies from worker-protocol.hh
421 (define %stderr-next #x6f6c6d67) ; "olmg", build log
422 (define %stderr-read #x64617461) ; "data", data needed from source
423 (define %stderr-write #x64617416) ; "dat\x16", data for sink
424 (define %stderr-last #x616c7473) ; "alts", we're done
425 (define %stderr-error #x63787470) ; "cxtp", error reporting
427 (let ((k (read-int p)))
428 (cond ((= k %stderr-write)
429 ;; Write a byte stream to USER-PORT.
430 (let* ((len (read-int p))
432 (dump-port p user-port len)
434 ;; Consume padding, as for strings.
435 (get-bytevector-n p (- 8 m))))
438 ;; Read a byte stream from USER-PORT.
439 ;; Note: Avoid 'get-bytevector-n' to work around
440 ;; <http://bugs.gnu.org/17591> in Guile up to 2.0.11.
441 (let* ((max-len (read-int p))
442 (data (make-bytevector max-len))
443 (len (get-bytevector-n! user-port data 0 max-len)))
445 (put-bytevector p data 0 len)
446 (write-padding len p)
449 ;; Log a string. Build logs are usually UTF-8-encoded, but they
450 ;; may also contain arbitrary byte sequences that should not cause
451 ;; this to fail. Thus, use the permissive
452 ;; 'read-maybe-utf8-string'.
453 (let ((s (read-maybe-utf8-string p)))
454 (display s (current-build-output-port))
455 (when (string-any %newlines s)
456 (flush-output-port (current-build-output-port)))
460 (let ((error (read-maybe-utf8-string p))
461 ;; Currently the daemon fails to send a status code for early
462 ;; errors like DB schema version mismatches, so check for EOF.
463 (status (if (and (>= (nix-server-minor-version server) 8)
464 (not (eof-object? (lookahead-u8 p))))
467 (raise (condition (&nix-protocol-error
471 ;; The daemon is done (see `stopWork' in `nix-worker.cc'.)
474 (raise (condition (&nix-protocol-error
475 (message "invalid error code")
478 (define %default-substitute-urls
479 ;; Default list of substituters.
480 '("http://hydra.gnu.org"))
482 (define* (set-build-options server
483 #:key keep-failed? keep-going? fallback?
487 (max-silent-time 3600)
491 (print-build-trace #t)
492 (build-cores (current-processor-count))
493 (use-substitutes? #t)
495 ;; Client-provided substitute URLs. For
496 ;; unprivileged clients, these are considered
497 ;; "untrusted"; for root, they override the
498 ;; daemon's settings.
499 (substitute-urls %default-substitute-urls))
500 ;; Must be called after `open-connection'.
503 (nix-server-socket server))
505 (let-syntax ((send (syntax-rules ()
506 ((_ (type option) ...)
508 (write-arg type option socket)
510 (write-int (operation-id set-options) socket)
511 (send (boolean keep-failed?) (boolean keep-going?)
512 (boolean fallback?) (integer verbosity)
513 (integer max-build-jobs) (integer max-silent-time))
514 (when (>= (nix-server-minor-version server) 2)
515 (send (boolean use-build-hook?)))
516 (when (>= (nix-server-minor-version server) 4)
517 (send (integer build-verbosity) (integer log-type)
518 (boolean print-build-trace)))
519 (when (>= (nix-server-minor-version server) 6)
520 (send (integer build-cores)))
521 (when (>= (nix-server-minor-version server) 10)
522 (send (boolean use-substitutes?)))
523 (when (>= (nix-server-minor-version server) 12)
524 (let ((pairs `(,@(if timeout
525 `(("build-timeout" . ,(number->string timeout)))
527 ("substitute-urls" . ,(string-join substitute-urls)))))
528 (send (string-pairs pairs))))
529 (let loop ((done? (process-stderr server)))
530 (or done? (process-stderr server)))))
532 (define-syntax operation
534 "Define a client-side RPC stub for the given operation."
535 ((_ (name (type arg) ...) docstring return ...)
536 (lambda (server arg ...)
538 (let ((s (nix-server-socket server)))
539 (write-int (operation-id name) s)
540 (write-arg type arg s)
542 ;; Loop until the server is done sending error output.
543 (let loop ((done? (process-stderr server)))
544 (or done? (loop (process-stderr server))))
545 (values (read-arg return s) ...))))))
547 (define-syntax-rule (define-operation (name args ...)
548 docstring return ...)
550 (operation (name args ...) docstring return ...)))
552 (define-operation (valid-path? (string path))
553 "Return #t when PATH is a valid store path."
556 (define-operation (query-path-hash (store-path path))
557 "Return the SHA256 hash of PATH as a bytevector."
560 (define hash-part->path
561 (let ((query-path-from-hash-part
562 (operation (query-path-from-hash-part (string hash))
565 (lambda (server hash-part)
566 "Return the store path whose hash part is HASH-PART (a nix-base32
567 string). Raise an error if no such path exists."
568 ;; This RPC is primarily used by Hydra to reply to HTTP GETs of
570 (query-path-from-hash-part server hash-part))))
572 (define-operation (query-path-info (store-path path))
573 "Return the info (hash, references, etc.) for PATH."
576 (define add-text-to-store
577 ;; A memoizing version of `add-to-store', to avoid repeated RPCs with
578 ;; the very same arguments during a given session.
579 (let ((add-text-to-store
580 (operation (add-text-to-store (string name) (string text)
581 (string-list references))
584 (lambda* (server name text #:optional (references '()))
585 "Add TEXT under file NAME in the store, and return its store path.
586 REFERENCES is the list of store paths referred to by the resulting store
588 (let ((args `(,text ,name ,references))
589 (cache (nix-server-add-text-to-store-cache server)))
590 (or (hash-ref cache args)
591 (let ((path (add-text-to-store server name text references)))
592 (hash-set! cache args path)
596 ;; A memoizing version of `add-to-store'. This is important because
597 ;; `add-to-store' leads to huge data transfers to the server, and
598 ;; because it's often called many times with the very same argument.
599 (let ((add-to-store (operation (add-to-store (string basename)
600 (boolean fixed?) ; obsolete, must be #t
606 (lambda (server basename recursive? hash-algo file-name)
607 "Add the contents of FILE-NAME under BASENAME to the store. When
608 RECURSIVE? is true and FILE-NAME designates a directory, the contents of
609 FILE-NAME are added recursively; if FILE-NAME designates a flat file and
610 RECURSIVE? is true, its contents are added, and its permission bits are
611 kept. HASH-ALGO must be a string such as \"sha256\"."
612 (let* ((st (stat file-name #f))
613 (args `(,st ,basename ,recursive? ,hash-algo))
614 (cache (nix-server-add-to-store-cache server)))
615 (or (and st (hash-ref cache args))
616 (let ((path (add-to-store server basename #t recursive?
617 hash-algo file-name)))
618 (hash-set! cache args path)
621 (define-operation (build-things (string-list things))
622 "Build THINGS, a list of store items which may be either '.drv' files or
623 outputs, and return when the worker is done building them. Elements of THINGS
624 that are not derivations can only be substituted and not built locally.
625 Return #t on success."
628 (define-operation (add-temp-root (store-path path))
629 "Make PATH a temporary root for the duration of the current session.
633 (define-operation (add-indirect-root (string file-name))
634 "Make the symlink FILE-NAME an indirect root for the garbage collector:
635 whatever store item FILE-NAME points to will not be collected. Return #t on
638 FILE-NAME can be anywhere on the file system, but it must be an absolute file
639 name--it is the caller's responsibility to ensure that it is an absolute file
643 (define %gc-roots-directory
644 ;; The place where garbage collector roots (symlinks) are kept.
645 (string-append %state-directory "/gcroots"))
647 (define (add-permanent-root target)
648 "Add a garbage collector root pointing to TARGET, an element of the store,
649 preventing TARGET from even being collected. This can also be used if TARGET
652 Raise an error if the caller does not have write access to the GC root
654 (let* ((root (string-append %gc-roots-directory "/" (basename target))))
657 (symlink target root))
659 ;; If ROOT already exists, this is fine; otherwise, re-throw.
660 (unless (= EEXIST (system-error-errno args))
661 (apply throw args))))))
663 (define (remove-permanent-root target)
664 "Remove the permanent garbage collector root pointing to TARGET. Raise an
665 error if there is no such root."
666 (delete-file (string-append %gc-roots-directory "/" (basename target))))
669 (operation (query-references (store-path path))
670 "Return the list of references of PATH."
673 (define* (fold-path store proc seed path
674 #:optional (relatives (cut references store <>)))
675 "Call PROC for each of the RELATIVES of PATH, exactly once, and return the
676 result formed from the successive calls to PROC, the first of which is passed
678 (let loop ((paths (list path))
683 (if (vhash-assoc path seen)
684 (loop rest result seen)
685 (let ((seen (vhash-cons path #t seen))
686 (rest (append rest (relatives path)))
687 (result (proc path result)))
688 (loop rest result seen))))
692 (define (requisites store path)
693 "Return the requisites of PATH, including PATH---i.e., its closure (all its
694 references, recursively)."
695 (fold-path store cons '() path))
697 (define (topologically-sorted store paths)
698 "Return a list containing PATHS and all their references sorted in
701 ;; Do a simple depth-first traversal of all of PATHS.
702 (let loop ((paths paths)
706 (vhash-cons n #t visited))
709 (vhash-assoc n visited))
714 (loop tail visited result)
717 (loop (references store head)
720 (lambda (visited result)
723 (cons head result))))))
725 (values visited result)))))
727 (call-with-values traverse
732 (operation (query-referrers (store-path path))
733 "Return the list of path that refer to PATH."
736 (define valid-derivers
737 (operation (query-valid-derivers (store-path path))
738 "Return the list of valid \"derivers\" of PATH---i.e., all the
739 .drv present in the store that have PATH among their outputs."
742 (define query-derivation-outputs ; avoid name clash with `derivation-outputs'
743 (operation (query-derivation-outputs (store-path path))
744 "Return the list of outputs of PATH, a .drv file."
747 (define-operation (has-substitutes? (store-path path))
748 "Return #t if binary substitutes are available for PATH, and #f otherwise."
751 (define substitutable-paths
752 (operation (query-substitutable-paths (store-path-list paths))
753 "Return the subset of PATHS that is substitutable."
756 (define substitutable-path-info
757 (operation (query-substitutable-path-infos (store-path-list paths))
758 "Return information about the subset of PATHS that is
759 substitutable. For each substitutable path, a `substitutable?' object is
761 substitutable-path-list))
763 (define (run-gc server action to-delete min-freed)
764 "Perform the garbage-collector operation ACTION, one of the
765 `gc-action' values. When ACTION is `delete-specific', the TO-DELETE is
766 the list of store paths to delete. IGNORE-LIVENESS? should always be
767 #f. MIN-FREED is the minimum amount of disk space to be freed, in
768 bytes, before the GC can stop. Return the list of store paths delete,
769 and the number of bytes freed."
770 (let ((s (nix-server-socket server)))
771 (write-int (operation-id collect-garbage) s)
773 (write-store-path-list to-delete s)
774 (write-arg boolean #f s) ; ignore-liveness?
775 (write-long-long min-freed s)
776 (write-int 0 s) ; obsolete
777 (when (>= (nix-server-minor-version server) 5)
778 ;; Obsolete `use-atime' and `max-atime' parameters.
782 ;; Loop until the server is done sending error output.
783 (let loop ((done? (process-stderr server)))
784 (or done? (loop (process-stderr server))))
786 (let ((paths (read-store-path-list s))
787 (freed (read-long-long s))
788 (obsolete (read-long-long s)))
789 (unless (null? paths)
790 ;; To be on the safe side, completely invalidate both caches.
791 ;; Otherwise we could end up returning store paths that are no longer
793 (hash-clear! (nix-server-add-to-store-cache server))
794 (hash-clear! (nix-server-add-text-to-store-cache server)))
796 (values paths freed))))
798 (define-syntax-rule (%long-long-max)
799 ;; Maximum unsigned 64-bit integer.
802 (define (live-paths server)
803 "Return the list of live store paths---i.e., store paths still
804 referenced, and thus not subject to being garbage-collected."
805 (run-gc server (gc-action return-live) '() (%long-long-max)))
807 (define (dead-paths server)
808 "Return the list of dead store paths---i.e., store paths no longer
809 referenced, and thus subject to being garbage-collected."
810 (run-gc server (gc-action return-dead) '() (%long-long-max)))
812 (define* (collect-garbage server #:optional (min-freed (%long-long-max)))
813 "Collect garbage from the store at SERVER. If MIN-FREED is non-zero,
814 then collect at least MIN-FREED bytes. Return the paths that were
815 collected, and the number of bytes freed."
816 (run-gc server (gc-action delete-dead) '() min-freed))
818 (define* (delete-paths server paths #:optional (min-freed (%long-long-max)))
819 "Delete PATHS from the store at SERVER, if they are no longer
820 referenced. If MIN-FREED is non-zero, then stop after at least
821 MIN-FREED bytes have been collected. Return the paths that were
822 collected, and the number of bytes freed."
823 (run-gc server (gc-action delete-specific) paths min-freed))
825 (define (import-paths server port)
826 "Import the set of store paths read from PORT into SERVER's store. An error
827 is raised if the set of paths read from PORT is not signed (as per
828 'export-path #:sign? #t'.) Return the list of store paths imported."
829 (let ((s (nix-server-socket server)))
830 (write-int (operation-id import-paths) s)
831 (let loop ((done? (process-stderr server port)))
832 (or done? (loop (process-stderr server port))))
833 (read-store-path-list s)))
835 (define* (export-path server path port #:key (sign? #t))
836 "Export PATH to PORT. When SIGN? is true, sign it."
837 (let ((s (nix-server-socket server)))
838 (write-int (operation-id export-path) s)
839 (write-store-path path s)
840 (write-arg boolean sign? s)
841 (let loop ((done? (process-stderr server port)))
842 (or done? (loop (process-stderr server port))))
845 (define* (export-paths server paths port #:key (sign? #t) recursive?)
846 "Export the store paths listed in PATHS to PORT, in topological order,
847 signing them if SIGN? is true. When RECURSIVE? is true, export the closure of
848 PATHS---i.e., PATHS and all their dependencies."
850 (let ((sorted (topologically-sorted server paths)))
851 ;; When RECURSIVE? is #f, filter out the references of PATHS.
854 (filter (cut member <> paths) sorted))))
856 (let loop ((paths ordered))
862 (and (export-path server head port #:sign? sign?)
865 (define* (register-path path
866 #:key (references '()) deriver prefix
868 "Register PATH as a valid store file, with REFERENCES as its list of
869 references, and DERIVER as its deriver (.drv that led to it.) If PREFIX is
870 not #f, it must be the name of the directory containing the new store to
871 initialize; if STATE-DIRECTORY is not #f, it must be a string containing the
872 absolute file name to the state directory of the store being initialized.
873 Return #t on success.
875 Use with care as it directly modifies the store! This is primarily meant to
876 be used internally by the daemon's build hook."
877 ;; Currently this is implemented by calling out to the fine C++ blob.
880 (let ((pipe (apply open-pipe* OPEN_WRITE %guix-register-program
882 `("--prefix" ,prefix)
884 ,@(if state-directory
885 `("--state-directory" ,state-directory)
889 (format pipe "~a~%~a~%~a~%"
890 path (or deriver "") (length references))
891 (for-each (cut format pipe "~a~%" <>) references)
892 (zero? (close-pipe pipe))))))
894 ;; Failed to run %GUIX-REGISTER-PROGRAM.
902 (define-syntax-rule (define-alias new old)
903 (define-syntax new (identifier-syntax old)))
905 ;; The store monad allows us to (1) build sequences of operations in the
906 ;; store, and (2) make the store an implicit part of the execution context,
907 ;; rather than a parameter of every single function.
908 (define-alias %store-monad %state-monad)
909 (define-alias store-return state-return)
910 (define-alias store-bind state-bind)
912 (define (preserve-documentation original proc)
913 "Return PROC with documentation taken from ORIGINAL."
914 (set-object-property! proc 'documentation
915 (procedure-property original 'documentation))
918 (define (store-lift proc)
919 "Lift PROC, a procedure whose first argument is a connection to the store,
921 (preserve-documentation proc
924 (values (apply proc store args) store)))))
926 (define (store-lower proc)
927 "Lower PROC, a monadic procedure in %STORE-MONAD, to a \"normal\" procedure
928 taking the store as its first argument."
929 (preserve-documentation proc
930 (lambda (store . args)
931 (run-with-store store (apply proc args)))))
934 ;; Store monad operators.
937 (define* (text-file name text
938 #:optional (references '()))
939 "Return as a monadic value the absolute file name in the store of the file
940 containing TEXT, a string. REFERENCES is a list of store items that the
941 resulting text file refers to; it defaults to the empty list."
943 (values (add-text-to-store store name text references)
946 (define* (interned-file file #:optional name
947 #:key (recursive? #t))
948 "Return the name of FILE once interned in the store. Use NAME as its store
949 name, or the basename of FILE if NAME is omitted.
951 When RECURSIVE? is true, the contents of FILE are added recursively; if FILE
952 designates a flat file and RECURSIVE? is true, its contents are added, and its
953 permission bits are kept."
955 (values (add-to-store store (or name (basename file))
956 recursive? "sha256" file)
960 ;; Monadic variant of 'build-things'.
961 (store-lift build-things))
963 (define %guile-for-build
964 ;; The derivation of the Guile to be used within the build environment,
965 ;; when using 'gexp->derivation' and co.
968 (define* (run-with-store store mval
970 (guile-for-build (%guile-for-build))
971 (system (%current-system)))
972 "Run MVAL, a monadic value in the store monad, in STORE, an open store
973 connection, and return the result."
974 (parameterize ((%guile-for-build guile-for-build)
975 (%current-system system))
976 (call-with-values (lambda ()
977 (run-with-state mval store))
978 (lambda (result store)
979 ;; Discard the state.
987 (define %store-prefix
988 ;; Absolute path to the Nix store.
989 (make-parameter %store-directory))
991 (define (store-path? path)
992 "Return #t if PATH is a store path."
993 ;; This is a lightweight check, compared to using a regexp, but this has to
994 ;; be fast as it's called often in `derivation', for instance.
995 ;; `isStorePath' in Nix does something similar.
996 (string-prefix? (%store-prefix) path))
998 (define (direct-store-path? path)
999 "Return #t if PATH is a store path, and not a sub-directory of a store path.
1000 This predicate is sometimes needed because files *under* a store path are not
1002 (and (store-path? path)
1003 (not (string=? path (%store-prefix)))
1004 (let ((len (+ 1 (string-length (%store-prefix)))))
1005 (not (string-index (substring path len) #\/)))))
1007 (define (derivation-path? path)
1008 "Return #t if PATH is a derivation path."
1009 (and (store-path? path) (string-suffix? ".drv" path)))
1011 (define store-regexp*
1012 ;; The substituter makes repeated calls to 'store-path-hash-part', hence
1013 ;; this optimization.
1016 "Return a regexp matching a file in STORE."
1017 (make-regexp (string-append "^" (regexp-quote store)
1018 "/([0-9a-df-np-sv-z]{32})-([^/]+)$")))))
1020 (define (store-path-package-name path)
1021 "Return the package name part of PATH, a file name in the store."
1022 (let ((path-rx (store-regexp* (%store-prefix))))
1023 (and=> (regexp-exec path-rx path)
1024 (cut match:substring <> 2))))
1026 (define (store-path-hash-part path)
1027 "Return the hash part of PATH as a base32 string, or #f if PATH is not a
1028 syntactically valid store path."
1029 (let ((path-rx (store-regexp* (%store-prefix))))
1030 (and=> (regexp-exec path-rx path)
1031 (cut match:substring <> 1))))
1033 (define (log-file store file)
1034 "Return the build log file for FILE, or #f if none could be found. FILE
1035 must be an absolute store file name, or a derivation file name."
1036 (cond ((derivation-path? file)
1037 (let* ((base (basename file))
1038 (log (string-append (dirname %state-directory) ; XXX
1040 (string-take base 2) "/"
1041 (string-drop base 2)))
1042 (log.bz2 (string-append log ".bz2")))
1043 (cond ((file-exists? log.bz2) log.bz2)
1044 ((file-exists? log) log)
1047 (match (valid-derivers store file)
1049 ;; Return the first that works.
1050 (any (cut log-file store <>) derivers))