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
74 substitutable-references
75 substitutable-download-size
76 substitutable-nar-size
79 substitutable-path-info
86 query-derivation-outputs
94 current-build-output-port
111 store-path-package-name
115 (define %protocol-version #x10c)
117 (define %worker-magic-1 #x6e697863) ; "nixc"
118 (define %worker-magic-2 #x6478696f) ; "dxio"
120 (define (protocol-major magic)
121 (logand magic #xff00))
122 (define (protocol-minor magic)
123 (logand magic #x00ff))
125 (define-syntax define-enumerate-type
127 ((_ name->int (name id) ...)
128 (define-syntax name->int
129 (syntax-rules (name ...)
130 ((_ name) id) ...)))))
132 (define-enumerate-type operation-id
133 ;; operation numbers from worker-protocol.hh
141 (add-text-to-store 8)
142 (build-derivations 9)
145 (add-indirect-root 12)
152 ;;(query-substitutable-path-info 21) ; obsolete as of #x10c
153 (query-derivation-outputs 22)
154 (query-all-valid-paths 23)
155 (query-failed-paths 24)
156 (clear-failed-paths 25)
159 (query-derivation-output-names 28)
160 (query-path-from-hash-part 29)
161 (query-substitutable-path-infos 30)
162 (query-valid-paths 31)
163 (query-substitutable-paths 32)
164 (query-valid-derivers 33))
166 (define-enumerate-type hash-algo
172 (define-enumerate-type gc-action
179 (define %default-socket-path
180 (string-append %state-directory "/daemon-socket/socket"))
182 (define %daemon-socket-file
183 ;; File name of the socket the daemon listens too.
184 (make-parameter (or (getenv "GUIX_DAEMON_SOCKET")
185 %default-socket-path)))
189 ;; Information about a substitutable store path.
190 (define-record-type <substitutable>
191 (substitutable path deriver refs dl-size nar-size)
193 (path substitutable-path)
194 (deriver substitutable-deriver)
195 (refs substitutable-references)
196 (dl-size substitutable-download-size)
197 (nar-size substitutable-nar-size))
199 (define (read-substitutable-path-list p)
200 (let loop ((len (read-int p))
204 (let ((path (read-store-path p))
205 (deriver (read-store-path p))
206 (refs (read-store-path-list p))
207 (dl-size (read-long-long p))
208 (nar-size (read-long-long p)))
210 (cons (substitutable path deriver refs dl-size nar-size)
213 (define-syntax write-arg
214 (syntax-rules (integer boolean file string string-list string-pairs
215 store-path store-path-list base16)
219 (write-int (if arg 1 0) p))
223 (write-string arg p))
224 ((_ string-list arg p)
225 (write-string-list arg p))
226 ((_ string-pairs arg p)
227 (write-string-pairs arg p))
228 ((_ store-path arg p)
229 (write-store-path arg p))
230 ((_ store-path-list arg p)
231 (write-store-path-list arg p))
233 (write-string (bytevector->base16-string arg) p))))
235 (define-syntax read-arg
236 (syntax-rules (integer boolean string store-path store-path-list
237 substitutable-path-list base16)
241 (not (zero? (read-int p))))
246 ((_ store-path-list p)
247 (read-store-path-list p))
248 ((_ substitutable-path-list p)
249 (read-substitutable-path-list p))
251 (base16-string->bytevector (read-string p)))))
256 (define-record-type <nix-server>
257 (%make-nix-server socket major minor
258 ats-cache atts-cache)
260 (socket nix-server-socket)
261 (major nix-server-major-version)
262 (minor nix-server-minor-version)
264 ;; Caches. We keep them per-connection, because store paths build
265 ;; during the session are temporary GC roots kept for the duration of
267 (ats-cache nix-server-add-to-store-cache)
268 (atts-cache nix-server-add-text-to-store-cache))
270 (set-record-type-printer! <nix-server>
272 (format port "#<build-daemon ~a.~a ~a>"
273 (nix-server-major-version obj)
274 (nix-server-minor-version obj)
275 (number->string (object-address obj)
278 (define-condition-type &nix-error &error
281 (define-condition-type &nix-connection-error &nix-error
282 nix-connection-error?
283 (file nix-connection-error-file)
284 (errno nix-connection-error-code))
286 (define-condition-type &nix-protocol-error &nix-error
288 (message nix-protocol-error-message)
289 (status nix-protocol-error-status))
291 (define* (open-connection #:optional (file (%daemon-socket-file))
292 #:key (reserve-space? #t))
293 "Connect to the daemon over the Unix-domain socket at FILE. When
294 RESERVE-SPACE? is true, instruct it to reserve a little bit of extra
295 space on the file system so that the garbage collector can still
296 operate, should the disk become full. Return a server object."
297 (let ((s (with-fluids ((%default-port-encoding #f))
298 ;; This trick allows use of the `scm_c_read' optimization.
299 (socket PF_UNIX SOCK_STREAM 0)))
300 (a (make-socket-address PF_UNIX file)))
305 ;; Translate the error to something user-friendly.
306 (let ((errno (system-error-errno args)))
307 (raise (condition (&nix-connection-error
311 (write-int %worker-magic-1 s)
312 (let ((r (read-int s)))
313 (and (eqv? r %worker-magic-2)
314 (let ((v (read-int s)))
315 (and (eqv? (protocol-major %protocol-version)
318 (write-int %protocol-version s)
319 (if (>= (protocol-minor v) 11)
320 (write-int (if reserve-space? 1 0) s))
321 (let ((s (%make-nix-server s
324 (make-hash-table 100)
325 (make-hash-table 100))))
326 (let loop ((done? (process-stderr s)))
327 (or done? (process-stderr s)))
330 (define (close-connection server)
331 "Close the connection to SERVER."
332 (close (nix-server-socket server)))
334 (define-syntax-rule (with-store store exp ...)
335 "Bind STORE to an open connection to the store and evaluate EXPs;
336 automatically close the store when the dynamic extent of EXP is left."
337 (let ((store (open-connection)))
343 (false-if-exception (close-connection store))))))
345 (define current-build-output-port
346 ;; The port where build output is sent.
347 (make-parameter (current-error-port)))
349 (define* (dump-port in out
351 #:key (buffer-size 16384))
352 "Read LEN bytes from IN (or as much as possible if LEN is #f) and write it
353 to OUT, using chunks of BUFFER-SIZE bytes."
355 (make-bytevector buffer-size))
358 (bytes (get-bytevector-n! in buffer 0
360 (min len buffer-size)
362 (or (eof-object? bytes)
363 (and len (= total len))
364 (let ((total (+ total bytes)))
365 (put-bytevector out buffer 0 bytes)
367 (get-bytevector-n! in buffer 0
369 (min (- len total) buffer-size)
373 ;; Newline characters triggering a flush of 'current-build-output-port'.
374 ;; Unlike Guile's _IOLBF, we flush upon #\return so that progress reports
375 ;; that use that trick are correctly displayed.
376 (char-set #\newline #\return))
378 (define* (process-stderr server #:optional user-port)
379 "Read standard output and standard error from SERVER, writing it to
380 CURRENT-BUILD-OUTPUT-PORT. Return #t when SERVER is done sending data, and
381 #f otherwise; in the latter case, the caller should call `process-stderr'
382 again until #t is returned or an error is raised.
384 Since the build process's output cannot be assumed to be UTF-8, we
385 conservatively consider it to be Latin-1, thereby avoiding possible
386 encoding conversion errors."
388 (nix-server-socket server))
390 ;; magic cookies from worker-protocol.hh
391 (define %stderr-next #x6f6c6d67) ; "olmg", build log
392 (define %stderr-read #x64617461) ; "data", data needed from source
393 (define %stderr-write #x64617416) ; "dat\x16", data for sink
394 (define %stderr-last #x616c7473) ; "alts", we're done
395 (define %stderr-error #x63787470) ; "cxtp", error reporting
397 (let ((k (read-int p)))
398 (cond ((= k %stderr-write)
399 ;; Write a byte stream to USER-PORT.
400 (let* ((len (read-int p))
402 (dump-port p user-port len)
404 ;; Consume padding, as for strings.
405 (get-bytevector-n p (- 8 m))))
408 ;; Read a byte stream from USER-PORT.
409 ;; Note: Avoid 'get-bytevector-n' to work around
410 ;; <http://bugs.gnu.org/17591> in Guile up to 2.0.11.
411 (let* ((max-len (read-int p))
412 (data (make-bytevector max-len))
413 (len (get-bytevector-n! user-port data 0 max-len)))
415 (put-bytevector p data 0 len)
416 (write-padding len p)
420 (let ((s (read-latin1-string p)))
421 (display s (current-build-output-port))
422 (when (string-any %newlines s)
423 (flush-output-port (current-build-output-port)))
427 (let ((error (read-latin1-string p))
428 ;; Currently the daemon fails to send a status code for early
429 ;; errors like DB schema version mismatches, so check for EOF.
430 (status (if (and (>= (nix-server-minor-version server) 8)
431 (not (eof-object? (lookahead-u8 p))))
434 (raise (condition (&nix-protocol-error
438 ;; The daemon is done (see `stopWork' in `nix-worker.cc'.)
441 (raise (condition (&nix-protocol-error
442 (message "invalid error code")
445 (define* (set-build-options server
446 #:key keep-failed? keep-going? fallback?
450 (max-silent-time 3600)
454 (print-build-trace #t)
455 (build-cores (current-processor-count))
456 (use-substitutes? #t)
457 (binary-caches '())) ; client "untrusted" cache URLs
458 ;; Must be called after `open-connection'.
461 (nix-server-socket server))
463 (let-syntax ((send (syntax-rules ()
464 ((_ (type option) ...)
466 (write-arg type option socket)
468 (write-int (operation-id set-options) socket)
469 (send (boolean keep-failed?) (boolean keep-going?)
470 (boolean fallback?) (integer verbosity)
471 (integer max-build-jobs) (integer max-silent-time))
472 (when (>= (nix-server-minor-version server) 2)
473 (send (boolean use-build-hook?)))
474 (when (>= (nix-server-minor-version server) 4)
475 (send (integer build-verbosity) (integer log-type)
476 (boolean print-build-trace)))
477 (when (>= (nix-server-minor-version server) 6)
478 (send (integer build-cores)))
479 (when (>= (nix-server-minor-version server) 10)
480 (send (boolean use-substitutes?)))
481 (when (>= (nix-server-minor-version server) 12)
482 (let ((pairs (if timeout
483 `(("build-timeout" . ,(number->string timeout))
486 (send (string-pairs pairs))))
487 (let loop ((done? (process-stderr server)))
488 (or done? (process-stderr server)))))
490 (define-syntax operation
492 "Define a client-side RPC stub for the given operation."
493 ((_ (name (type arg) ...) docstring return ...)
494 (lambda (server arg ...)
496 (let ((s (nix-server-socket server)))
497 (write-int (operation-id name) s)
498 (write-arg type arg s)
500 ;; Loop until the server is done sending error output.
501 (let loop ((done? (process-stderr server)))
502 (or done? (loop (process-stderr server))))
503 (values (read-arg return s) ...))))))
505 (define-syntax-rule (define-operation (name args ...)
506 docstring return ...)
508 (operation (name args ...) docstring return ...)))
510 (define-operation (valid-path? (string path))
511 "Return #t when PATH is a valid store path."
514 (define-operation (query-path-hash (store-path path))
515 "Return the SHA256 hash of PATH as a bytevector."
518 (define hash-part->path
519 (let ((query-path-from-hash-part
520 (operation (query-path-from-hash-part (string hash))
523 (lambda (server hash-part)
524 "Return the store path whose hash part is HASH-PART (a nix-base32
525 string). Raise an error if no such path exists."
526 ;; This RPC is primarily used by Hydra to reply to HTTP GETs of
528 (query-path-from-hash-part server hash-part))))
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 #:optional (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 the symlink FILE-NAME an indirect root for the garbage collector:
587 whatever store item FILE-NAME points to will not be collected. Return #t on
590 FILE-NAME can be anywhere on the file system, but it must be an absolute file
591 name--it is the caller's responsibility to ensure that it is an absolute file
595 (define %gc-roots-directory
596 ;; The place where garbage collector roots (symlinks) are kept.
597 (string-append %state-directory "/gcroots"))
599 (define (add-permanent-root target)
600 "Add a garbage collector root pointing to TARGET, an element of the store,
601 preventing TARGET from even being collected. This can also be used if TARGET
604 Raise an error if the caller does not have write access to the GC root
606 (let* ((root (string-append %gc-roots-directory "/" (basename target))))
609 (symlink target root))
611 ;; If ROOT already exists, this is fine; otherwise, re-throw.
612 (unless (= EEXIST (system-error-errno args))
613 (apply throw args))))))
615 (define (remove-permanent-root target)
616 "Remove the permanent garbage collector root pointing to TARGET. Raise an
617 error if there is no such root."
618 (delete-file (string-append %gc-roots-directory "/" (basename target))))
621 (operation (query-references (store-path path))
622 "Return the list of references of PATH."
625 (define* (fold-path store proc seed path
626 #:optional (relatives (cut references store <>)))
627 "Call PROC for each of the RELATIVES of PATH, exactly once, and return the
628 result formed from the successive calls to PROC, the first of which is passed
630 (let loop ((paths (list path))
635 (if (vhash-assoc path seen)
636 (loop rest result seen)
637 (let ((seen (vhash-cons path #t seen))
638 (rest (append rest (relatives path)))
639 (result (proc path result)))
640 (loop rest result seen))))
644 (define (requisites store path)
645 "Return the requisites of PATH, including PATH---i.e., its closure (all its
646 references, recursively)."
647 (fold-path store cons '() path))
649 (define (topologically-sorted store paths)
650 "Return a list containing PATHS and all their references sorted in
653 ;; Do a simple depth-first traversal of all of PATHS.
654 (let loop ((paths paths)
658 (vhash-cons n #t visited))
661 (vhash-assoc n visited))
666 (loop tail visited result)
669 (loop (references store head)
672 (lambda (visited result)
675 (cons head result))))))
677 (values visited result)))))
679 (call-with-values traverse
684 (operation (query-referrers (store-path path))
685 "Return the list of path that refer to PATH."
688 (define valid-derivers
689 (operation (query-valid-derivers (store-path path))
690 "Return the list of valid \"derivers\" of PATH---i.e., all the
691 .drv present in the store that have PATH among their outputs."
694 (define query-derivation-outputs ; avoid name clash with `derivation-outputs'
695 (operation (query-derivation-outputs (store-path path))
696 "Return the list of outputs of PATH, a .drv file."
699 (define-operation (has-substitutes? (store-path path))
700 "Return #t if binary substitutes are available for PATH, and #f otherwise."
703 (define substitutable-paths
704 (operation (query-substitutable-paths (store-path-list paths))
705 "Return the subset of PATHS that is substitutable."
708 (define substitutable-path-info
709 (operation (query-substitutable-path-infos (store-path-list paths))
710 "Return information about the subset of PATHS that is
711 substitutable. For each substitutable path, a `substitutable?' object is
713 substitutable-path-list))
715 (define (run-gc server action to-delete min-freed)
716 "Perform the garbage-collector operation ACTION, one of the
717 `gc-action' values. When ACTION is `delete-specific', the TO-DELETE is
718 the list of store paths to delete. IGNORE-LIVENESS? should always be
719 #f. MIN-FREED is the minimum amount of disk space to be freed, in
720 bytes, before the GC can stop. Return the list of store paths delete,
721 and the number of bytes freed."
722 (let ((s (nix-server-socket server)))
723 (write-int (operation-id collect-garbage) s)
725 (write-store-path-list to-delete s)
726 (write-arg boolean #f s) ; ignore-liveness?
727 (write-long-long min-freed s)
728 (write-int 0 s) ; obsolete
729 (when (>= (nix-server-minor-version server) 5)
730 ;; Obsolete `use-atime' and `max-atime' parameters.
734 ;; Loop until the server is done sending error output.
735 (let loop ((done? (process-stderr server)))
736 (or done? (loop (process-stderr server))))
738 (let ((paths (read-store-path-list s))
739 (freed (read-long-long s))
740 (obsolete (read-long-long s)))
741 (unless (null? paths)
742 ;; To be on the safe side, completely invalidate both caches.
743 ;; Otherwise we could end up returning store paths that are no longer
745 (hash-clear! (nix-server-add-to-store-cache server))
746 (hash-clear! (nix-server-add-text-to-store-cache server)))
748 (values paths freed))))
750 (define-syntax-rule (%long-long-max)
751 ;; Maximum unsigned 64-bit integer.
754 (define (live-paths server)
755 "Return the list of live store paths---i.e., store paths still
756 referenced, and thus not subject to being garbage-collected."
757 (run-gc server (gc-action return-live) '() (%long-long-max)))
759 (define (dead-paths server)
760 "Return the list of dead store paths---i.e., store paths no longer
761 referenced, and thus subject to being garbage-collected."
762 (run-gc server (gc-action return-dead) '() (%long-long-max)))
764 (define* (collect-garbage server #:optional (min-freed (%long-long-max)))
765 "Collect garbage from the store at SERVER. If MIN-FREED is non-zero,
766 then collect at least MIN-FREED bytes. Return the paths that were
767 collected, and the number of bytes freed."
768 (run-gc server (gc-action delete-dead) '() min-freed))
770 (define* (delete-paths server paths #:optional (min-freed (%long-long-max)))
771 "Delete PATHS from the store at SERVER, if they are no longer
772 referenced. If MIN-FREED is non-zero, then stop after at least
773 MIN-FREED bytes have been collected. Return the paths that were
774 collected, and the number of bytes freed."
775 (run-gc server (gc-action delete-specific) paths min-freed))
777 (define (import-paths server port)
778 "Import the set of store paths read from PORT into SERVER's store. An error
779 is raised if the set of paths read from PORT is not signed (as per
780 'export-path #:sign? #t'.) Return the list of store paths imported."
781 (let ((s (nix-server-socket server)))
782 (write-int (operation-id import-paths) s)
783 (let loop ((done? (process-stderr server port)))
784 (or done? (loop (process-stderr server port))))
785 (read-store-path-list s)))
787 (define* (export-path server path port #:key (sign? #t))
788 "Export PATH to PORT. When SIGN? is true, sign it."
789 (let ((s (nix-server-socket server)))
790 (write-int (operation-id export-path) s)
791 (write-store-path path s)
792 (write-arg boolean sign? s)
793 (let loop ((done? (process-stderr server port)))
794 (or done? (loop (process-stderr server port))))
797 (define* (export-paths server paths port #:key (sign? #t))
798 "Export the store paths listed in PATHS to PORT, in topological order,
799 signing them if SIGN? is true."
801 ;; Sort PATHS, but don't include their references.
802 (filter (cut member <> paths)
803 (topologically-sorted server paths)))
805 (let ((s (nix-server-socket server)))
806 (let loop ((paths ordered))
812 (and (export-path server head port #:sign? sign?)
815 (define* (register-path path
816 #:key (references '()) deriver prefix
818 "Register PATH as a valid store file, with REFERENCES as its list of
819 references, and DERIVER as its deriver (.drv that led to it.) If PREFIX is
820 not #f, it must be the name of the directory containing the new store to
821 initialize; if STATE-DIRECTORY is not #f, it must be a string containing the
822 absolute file name to the state directory of the store being initialized.
823 Return #t on success.
825 Use with care as it directly modifies the store! This is primarily meant to
826 be used internally by the daemon's build hook."
827 ;; Currently this is implemented by calling out to the fine C++ blob.
830 (let ((pipe (apply open-pipe* OPEN_WRITE %guix-register-program
832 `("--prefix" ,prefix)
834 ,@(if state-directory
835 `("--state-directory" ,state-directory)
839 (format pipe "~a~%~a~%~a~%"
840 path (or deriver "") (length references))
841 (for-each (cut format pipe "~a~%" <>) references)
842 (zero? (close-pipe pipe))))))
844 ;; Failed to run %GUIX-REGISTER-PROGRAM.
852 ;; return:: a -> StoreM a
853 (define-inlinable (store-return value)
854 "Return VALUE from a monadic function."
855 ;; The monadic value is just this.
859 ;; >>=:: StoreM a -> (a -> StoreM b) -> StoreM b
860 (define-inlinable (store-bind mvalue mproc)
861 "Bind MVALUE in MPROC."
863 (let* ((value (mvalue store))
864 (mresult (mproc value)))
867 ;; This is essentially a state monad
868 (define-monad %store-monad
870 (return store-return))
872 (define (store-lift proc)
873 "Lift PROC, a procedure whose first argument is a connection to the store,
878 (apply proc store args))))
880 (set-object-property! result 'documentation
881 (procedure-property proc 'documentation))
885 ;; Store monad operators.
888 (define* (text-file name text)
889 "Return as a monadic value the absolute file name in the store of the file
890 containing TEXT, a string."
892 (add-text-to-store store name text '())))
894 (define* (interned-file file #:optional name
895 #:key (recursive? #t))
896 "Return the name of FILE once interned in the store. Use NAME as its store
897 name, or the basename of FILE if NAME is omitted.
899 When RECURSIVE? is true, the contents of FILE are added recursively; if FILE
900 designates a flat file and RECURSIVE? is true, its contents are added, and its
901 permission bits are kept."
903 (add-to-store store (or name (basename file))
904 recursive? "sha256" file)))
906 (define %guile-for-build
907 ;; The derivation of the Guile to be used within the build environment,
908 ;; when using 'gexp->derivation' and co.
911 (define* (run-with-store store mval
913 (guile-for-build (%guile-for-build))
914 (system (%current-system)))
915 "Run MVAL, a monadic value in the store monad, in STORE, an open store
917 (parameterize ((%guile-for-build guile-for-build)
918 (%current-system system))
926 (define %store-prefix
927 ;; Absolute path to the Nix store.
928 (make-parameter %store-directory))
930 (define (store-path? path)
931 "Return #t if PATH is a store path."
932 ;; This is a lightweight check, compared to using a regexp, but this has to
933 ;; be fast as it's called often in `derivation', for instance.
934 ;; `isStorePath' in Nix does something similar.
935 (string-prefix? (%store-prefix) path))
937 (define (direct-store-path? path)
938 "Return #t if PATH is a store path, and not a sub-directory of a store path.
939 This predicate is sometimes needed because files *under* a store path are not
941 (and (store-path? path)
942 (not (string=? path (%store-prefix)))
943 (let ((len (+ 1 (string-length (%store-prefix)))))
944 (not (string-index (substring path len) #\/)))))
946 (define (derivation-path? path)
947 "Return #t if PATH is a derivation path."
948 (and (store-path? path) (string-suffix? ".drv" path)))
950 (define store-regexp*
951 ;; The substituter makes repeated calls to 'store-path-hash-part', hence
952 ;; this optimization.
955 "Return a regexp matching a file in STORE."
956 (make-regexp (string-append "^" (regexp-quote store)
957 "/([0-9a-df-np-sv-z]{32})-([^/]+)$")))))
959 (define (store-path-package-name path)
960 "Return the package name part of PATH, a file name in the store."
961 (let ((path-rx (store-regexp* (%store-prefix))))
962 (and=> (regexp-exec path-rx path)
963 (cut match:substring <> 2))))
965 (define (store-path-hash-part path)
966 "Return the hash part of PATH as a base32 string, or #f if PATH is not a
967 syntactically valid store path."
968 (let ((path-rx (store-regexp* (%store-prefix))))
969 (and=> (regexp-exec path-rx path)
970 (cut match:substring <> 1))))
972 (define (log-file store file)
973 "Return the build log file for FILE, or #f if none could be found. FILE
974 must be an absolute store file name, or a derivation file name."
975 (cond ((derivation-path? file)
976 (let* ((base (basename file))
977 (log (string-append (dirname %state-directory) ; XXX
979 (string-take base 2) "/"
980 (string-drop base 2)))
981 (log.bz2 (string-append log ".bz2")))
982 (cond ((file-exists? log.bz2) log.bz2)
983 ((file-exists? log) log)
986 (match (valid-derivers store file)
988 ;; Return the first that works.
989 (any (cut log-file store <>) derivers))