2013-01-06 00:47:50 +01:00
|
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2013-01-13 22:27:00 +01:00
|
|
|
|
;;; Copyright © 2012, 2013 Ludovic Courtès <ludo@gnu.org>
|
2012-04-18 23:34:12 +02:00
|
|
|
|
;;;
|
2013-01-06 00:47:50 +01:00
|
|
|
|
;;; This file is part of GNU Guix.
|
2012-04-18 23:34:12 +02:00
|
|
|
|
;;;
|
2013-01-06 00:47:50 +01:00
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
2012-04-18 23:34:12 +02:00
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
|
;;; your option) any later version.
|
|
|
|
|
;;;
|
2013-01-06 00:47:50 +01:00
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
2012-04-18 23:34:12 +02:00
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
|
;;;
|
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
2013-01-06 00:47:50 +01:00
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
2012-04-18 23:34:12 +02:00
|
|
|
|
|
|
|
|
|
(define-module (guix store)
|
2012-10-24 17:03:45 +02:00
|
|
|
|
#:use-module (guix utils)
|
2012-12-05 23:02:47 +01:00
|
|
|
|
#:use-module (guix config)
|
2012-04-18 23:34:12 +02:00
|
|
|
|
#:use-module (rnrs bytevectors)
|
|
|
|
|
#:use-module (rnrs io ports)
|
|
|
|
|
#:use-module (srfi srfi-1)
|
|
|
|
|
#:use-module (srfi srfi-9)
|
|
|
|
|
#:use-module (srfi srfi-26)
|
2012-06-01 23:29:55 +02:00
|
|
|
|
#:use-module (srfi srfi-34)
|
|
|
|
|
#:use-module (srfi srfi-35)
|
2012-06-01 23:29:55 +02:00
|
|
|
|
#:use-module (srfi srfi-39)
|
2012-04-18 23:34:12 +02:00
|
|
|
|
#:use-module (ice-9 match)
|
|
|
|
|
#:use-module (ice-9 rdelim)
|
2012-06-10 22:43:02 +02:00
|
|
|
|
#:use-module (ice-9 ftw)
|
2012-11-01 01:39:23 +01:00
|
|
|
|
#:use-module (ice-9 regex)
|
2012-04-18 23:34:12 +02:00
|
|
|
|
#:export (nix-server?
|
|
|
|
|
nix-server-major-version
|
|
|
|
|
nix-server-minor-version
|
|
|
|
|
nix-server-socket
|
|
|
|
|
|
2012-06-01 23:29:55 +02:00
|
|
|
|
&nix-error nix-error?
|
|
|
|
|
&nix-protocol-error nix-protocol-error?
|
|
|
|
|
nix-protocol-error-message
|
|
|
|
|
nix-protocol-error-status
|
|
|
|
|
|
2012-06-01 23:29:55 +02:00
|
|
|
|
hash-algo
|
|
|
|
|
|
2012-04-18 23:34:12 +02:00
|
|
|
|
open-connection
|
2012-08-22 17:22:46 +02:00
|
|
|
|
close-connection
|
2012-04-18 23:34:12 +02:00
|
|
|
|
set-build-options
|
2012-07-01 00:21:16 +02:00
|
|
|
|
valid-path?
|
2012-10-24 17:03:45 +02:00
|
|
|
|
query-path-hash
|
2012-04-18 23:34:12 +02:00
|
|
|
|
add-text-to-store
|
|
|
|
|
add-to-store
|
2012-06-01 23:29:55 +02:00
|
|
|
|
build-derivations
|
2012-12-09 23:52:59 +01:00
|
|
|
|
add-temp-root
|
2012-10-30 00:20:53 +01:00
|
|
|
|
add-indirect-root
|
2012-06-01 23:29:55 +02:00
|
|
|
|
|
2012-12-09 23:52:59 +01:00
|
|
|
|
live-paths
|
|
|
|
|
dead-paths
|
|
|
|
|
collect-garbage
|
|
|
|
|
delete-paths
|
|
|
|
|
|
2012-06-16 16:13:12 +02:00
|
|
|
|
current-build-output-port
|
|
|
|
|
|
2012-06-01 23:29:55 +02:00
|
|
|
|
%store-prefix
|
|
|
|
|
store-path?
|
2012-11-01 01:39:23 +01:00
|
|
|
|
derivation-path?
|
|
|
|
|
store-path-package-name))
|
2012-04-18 23:34:12 +02:00
|
|
|
|
|
2012-07-02 00:45:23 +02:00
|
|
|
|
(define %protocol-version #x10b)
|
2012-04-18 23:34:12 +02:00
|
|
|
|
|
|
|
|
|
(define %worker-magic-1 #x6e697863)
|
|
|
|
|
(define %worker-magic-2 #x6478696f)
|
|
|
|
|
|
|
|
|
|
(define (protocol-major magic)
|
|
|
|
|
(logand magic #xff00))
|
|
|
|
|
(define (protocol-minor magic)
|
|
|
|
|
(logand magic #x00ff))
|
|
|
|
|
|
|
|
|
|
(define-syntax define-enumerate-type
|
|
|
|
|
(syntax-rules ()
|
|
|
|
|
((_ name->int (name id) ...)
|
|
|
|
|
(define-syntax name->int
|
|
|
|
|
(syntax-rules (name ...)
|
|
|
|
|
((_ name) id) ...)))))
|
|
|
|
|
|
|
|
|
|
(define-enumerate-type operation-id
|
|
|
|
|
;; operation numbers from worker-protocol.hh
|
|
|
|
|
(quit 0)
|
|
|
|
|
(valid-path? 1)
|
|
|
|
|
(has-substitutes? 3)
|
|
|
|
|
(query-path-hash 4)
|
|
|
|
|
(query-references 5)
|
|
|
|
|
(query-referrers 6)
|
|
|
|
|
(add-to-store 7)
|
|
|
|
|
(add-text-to-store 8)
|
|
|
|
|
(build-derivations 9)
|
|
|
|
|
(ensure-path 10)
|
|
|
|
|
(add-temp-root 11)
|
|
|
|
|
(add-indirect-root 12)
|
|
|
|
|
(sync-with-gc 13)
|
|
|
|
|
(find-roots 14)
|
|
|
|
|
(export-path 16)
|
|
|
|
|
(query-deriver 18)
|
|
|
|
|
(set-options 19)
|
|
|
|
|
(collect-garbage 20)
|
|
|
|
|
(query-substitutable-path-info 21)
|
|
|
|
|
(query-derivation-outputs 22)
|
|
|
|
|
(query-valid-paths 23)
|
|
|
|
|
(query-failed-paths 24)
|
|
|
|
|
(clear-failed-paths 25)
|
|
|
|
|
(query-path-info 26)
|
|
|
|
|
(import-paths 27)
|
|
|
|
|
(query-derivation-output-names 28))
|
|
|
|
|
|
|
|
|
|
(define-enumerate-type hash-algo
|
|
|
|
|
;; hash.hh
|
|
|
|
|
(md5 1)
|
|
|
|
|
(sha1 2)
|
|
|
|
|
(sha256 3))
|
|
|
|
|
|
2012-12-09 23:52:59 +01:00
|
|
|
|
(define-enumerate-type gc-action
|
|
|
|
|
;; store-api.hh
|
|
|
|
|
(return-live 0)
|
|
|
|
|
(return-dead 1)
|
|
|
|
|
(delete-dead 2)
|
|
|
|
|
(delete-specific 3))
|
|
|
|
|
|
2012-04-18 23:34:12 +02:00
|
|
|
|
(define %default-socket-path
|
2012-12-05 23:02:47 +01:00
|
|
|
|
(string-append (or (getenv "NIX_STATE_DIR") %state-directory)
|
|
|
|
|
"/daemon-socket/socket"))
|
2012-04-18 23:34:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; serialize.cc
|
|
|
|
|
|
|
|
|
|
(define (write-int n p)
|
|
|
|
|
(let ((b (make-bytevector 8 0)))
|
|
|
|
|
(bytevector-u32-set! b 0 n (endianness little))
|
|
|
|
|
(put-bytevector p b)))
|
|
|
|
|
|
|
|
|
|
(define (read-int p)
|
|
|
|
|
(let ((b (get-bytevector-n p 8)))
|
|
|
|
|
(bytevector-u32-ref b 0 (endianness little))))
|
|
|
|
|
|
|
|
|
|
(define (write-long-long n p)
|
|
|
|
|
(let ((b (make-bytevector 8 0)))
|
|
|
|
|
(bytevector-u64-set! b 0 n (endianness little))
|
|
|
|
|
(put-bytevector p b)))
|
|
|
|
|
|
2012-12-09 23:52:59 +01:00
|
|
|
|
(define (read-long-long p)
|
|
|
|
|
(let ((b (get-bytevector-n p 8)))
|
|
|
|
|
(bytevector-u64-ref b 0 (endianness little))))
|
|
|
|
|
|
2012-04-18 23:34:12 +02:00
|
|
|
|
(define write-padding
|
|
|
|
|
(let ((zero (make-bytevector 8 0)))
|
|
|
|
|
(lambda (n p)
|
|
|
|
|
(let ((m (modulo n 8)))
|
|
|
|
|
(or (zero? m)
|
|
|
|
|
(put-bytevector p zero 0 (- 8 m)))))))
|
|
|
|
|
|
|
|
|
|
(define (write-string s p)
|
|
|
|
|
(let ((b (string->utf8 s)))
|
|
|
|
|
(write-int (bytevector-length b) p)
|
|
|
|
|
(put-bytevector p b)
|
|
|
|
|
(write-padding (bytevector-length b) p)))
|
|
|
|
|
|
|
|
|
|
(define (read-string p)
|
|
|
|
|
(let* ((len (read-int p))
|
|
|
|
|
(m (modulo len 8))
|
|
|
|
|
(bv (get-bytevector-n p len))
|
|
|
|
|
(str (utf8->string bv)))
|
|
|
|
|
(or (zero? m)
|
|
|
|
|
(get-bytevector-n p (- 8 m)))
|
|
|
|
|
str))
|
|
|
|
|
|
|
|
|
|
(define (write-string-list l p)
|
|
|
|
|
(write-int (length l) p)
|
|
|
|
|
(for-each (cut write-string <> p) l))
|
|
|
|
|
|
2012-12-09 23:52:59 +01:00
|
|
|
|
(define (read-string-list p)
|
|
|
|
|
(let ((len (read-int p)))
|
|
|
|
|
(unfold (cut >= <> len)
|
|
|
|
|
(lambda (i)
|
|
|
|
|
(read-string p))
|
|
|
|
|
1+
|
|
|
|
|
0)))
|
|
|
|
|
|
|
|
|
|
(define (write-store-path f p)
|
|
|
|
|
(write-string f p)) ; TODO: assert path
|
|
|
|
|
|
2012-04-18 23:34:12 +02:00
|
|
|
|
(define (read-store-path p)
|
|
|
|
|
(read-string p)) ; TODO: assert path
|
|
|
|
|
|
2012-12-09 23:52:59 +01:00
|
|
|
|
(define write-store-path-list write-string-list)
|
|
|
|
|
(define read-store-path-list read-string-list)
|
|
|
|
|
|
2012-04-18 23:34:12 +02:00
|
|
|
|
(define (write-contents file p)
|
|
|
|
|
"Write the contents of FILE to output port P."
|
|
|
|
|
(define (dump in size)
|
|
|
|
|
(define buf-size 65536)
|
|
|
|
|
(define buf (make-bytevector buf-size))
|
|
|
|
|
|
|
|
|
|
(let loop ((left size))
|
|
|
|
|
(if (<= left 0)
|
|
|
|
|
0
|
|
|
|
|
(let ((read (get-bytevector-n! in buf 0 buf-size)))
|
|
|
|
|
(if (eof-object? read)
|
|
|
|
|
left
|
|
|
|
|
(begin
|
|
|
|
|
(put-bytevector p buf 0 read)
|
|
|
|
|
(loop (- left read))))))))
|
|
|
|
|
|
|
|
|
|
(let ((size (stat:size (lstat file))))
|
|
|
|
|
(write-string "contents" p)
|
|
|
|
|
(write-long-long size p)
|
|
|
|
|
(call-with-input-file file
|
|
|
|
|
(lambda (p)
|
|
|
|
|
(dump p size)))
|
|
|
|
|
(write-padding size p)))
|
|
|
|
|
|
|
|
|
|
(define (write-file f p)
|
|
|
|
|
(define %archive-version-1 "nix-archive-1")
|
|
|
|
|
|
2012-06-10 22:43:02 +02:00
|
|
|
|
(write-string %archive-version-1 p)
|
|
|
|
|
|
|
|
|
|
(let dump ((f f))
|
|
|
|
|
(let ((s (lstat f)))
|
|
|
|
|
(write-string "(" p)
|
|
|
|
|
(case (stat:type s)
|
|
|
|
|
((regular)
|
|
|
|
|
(write-string "type" p)
|
|
|
|
|
(write-string "regular" p)
|
|
|
|
|
(if (not (zero? (logand (stat:mode s) #o100)))
|
|
|
|
|
(begin
|
|
|
|
|
(write-string "executable" p)
|
|
|
|
|
(write-string "" p)))
|
|
|
|
|
(write-contents f p))
|
|
|
|
|
((directory)
|
|
|
|
|
(write-string "type" p)
|
|
|
|
|
(write-string "directory" p)
|
|
|
|
|
(let ((entries (remove (cut member <> '("." ".."))
|
|
|
|
|
(scandir f))))
|
|
|
|
|
(for-each (lambda (e)
|
|
|
|
|
(let ((f (string-append f "/" e)))
|
|
|
|
|
(write-string "entry" p)
|
|
|
|
|
(write-string "(" p)
|
|
|
|
|
(write-string "name" p)
|
|
|
|
|
(write-string e p)
|
|
|
|
|
(write-string "node" p)
|
|
|
|
|
(dump f)
|
|
|
|
|
(write-string ")" p)))
|
|
|
|
|
entries)))
|
|
|
|
|
(else
|
|
|
|
|
(error "ENOSYS")))
|
|
|
|
|
(write-string ")" p))))
|
2012-04-18 23:34:12 +02:00
|
|
|
|
|
|
|
|
|
(define-syntax write-arg
|
2012-12-09 23:52:59 +01:00
|
|
|
|
(syntax-rules (integer boolean file string string-list
|
|
|
|
|
store-path store-path-list base16)
|
2012-04-18 23:34:12 +02:00
|
|
|
|
((_ integer arg p)
|
|
|
|
|
(write-int arg p))
|
|
|
|
|
((_ boolean arg p)
|
|
|
|
|
(write-int (if arg 1 0) p))
|
|
|
|
|
((_ file arg p)
|
|
|
|
|
(write-file arg p))
|
|
|
|
|
((_ string arg p)
|
|
|
|
|
(write-string arg p))
|
|
|
|
|
((_ string-list arg p)
|
2012-10-24 17:03:45 +02:00
|
|
|
|
(write-string-list arg p))
|
2012-12-09 23:52:59 +01:00
|
|
|
|
((_ store-path arg p)
|
|
|
|
|
(write-store-path arg p))
|
|
|
|
|
((_ store-path-list arg p)
|
|
|
|
|
(write-store-path-list arg p))
|
2012-10-24 17:03:45 +02:00
|
|
|
|
((_ base16 arg p)
|
|
|
|
|
(write-string (bytevector->base16-string arg) p))))
|
2012-04-18 23:34:12 +02:00
|
|
|
|
|
|
|
|
|
(define-syntax read-arg
|
2012-12-09 23:52:59 +01:00
|
|
|
|
(syntax-rules (integer boolean string store-path store-path-list base16)
|
2012-04-18 23:34:12 +02:00
|
|
|
|
((_ integer p)
|
|
|
|
|
(read-int p))
|
|
|
|
|
((_ boolean p)
|
|
|
|
|
(not (zero? (read-int p))))
|
|
|
|
|
((_ string p)
|
|
|
|
|
(read-string p))
|
|
|
|
|
((_ store-path p)
|
2012-10-24 17:03:45 +02:00
|
|
|
|
(read-store-path p))
|
2012-12-09 23:52:59 +01:00
|
|
|
|
((_ store-path-list p)
|
|
|
|
|
(read-store-path-list p))
|
2012-10-24 17:03:45 +02:00
|
|
|
|
((_ hash p)
|
|
|
|
|
(base16-string->bytevector (read-string p)))))
|
2012-04-18 23:34:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; remote-store.cc
|
|
|
|
|
|
|
|
|
|
(define-record-type <nix-server>
|
|
|
|
|
(%make-nix-server socket major minor)
|
|
|
|
|
nix-server?
|
|
|
|
|
(socket nix-server-socket)
|
|
|
|
|
(major nix-server-major-version)
|
|
|
|
|
(minor nix-server-minor-version))
|
|
|
|
|
|
2012-06-01 23:29:55 +02:00
|
|
|
|
(define-condition-type &nix-error &error
|
|
|
|
|
nix-error?)
|
|
|
|
|
|
|
|
|
|
(define-condition-type &nix-protocol-error &nix-error
|
|
|
|
|
nix-protocol-error?
|
|
|
|
|
(message nix-protocol-error-message)
|
|
|
|
|
(status nix-protocol-error-status))
|
|
|
|
|
|
2012-07-02 00:45:23 +02:00
|
|
|
|
(define* (open-connection #:optional (file %default-socket-path)
|
|
|
|
|
#:key (reserve-space? #t))
|
2013-01-15 23:32:13 +01:00
|
|
|
|
"Connect to the daemon over the Unix-domain socket at FILE. When
|
|
|
|
|
RESERVE-SPACE? is true, instruct it to reserve a little bit of extra
|
|
|
|
|
space on the file system so that the garbage collector can still
|
|
|
|
|
operate, should the disk become full. Return a server object."
|
2012-04-18 23:34:12 +02:00
|
|
|
|
(let ((s (with-fluids ((%default-port-encoding #f))
|
|
|
|
|
;; This trick allows use of the `scm_c_read' optimization.
|
|
|
|
|
(socket PF_UNIX SOCK_STREAM 0)))
|
|
|
|
|
(a (make-socket-address PF_UNIX file)))
|
2012-10-13 22:36:39 +02:00
|
|
|
|
|
|
|
|
|
;; Enlarge the receive buffer.
|
|
|
|
|
(setsockopt s SOL_SOCKET SO_RCVBUF (* 12 1024))
|
|
|
|
|
|
2012-04-18 23:34:12 +02:00
|
|
|
|
(connect s a)
|
|
|
|
|
(write-int %worker-magic-1 s)
|
|
|
|
|
(let ((r (read-int s)))
|
|
|
|
|
(and (eqv? r %worker-magic-2)
|
|
|
|
|
(let ((v (read-int s)))
|
|
|
|
|
(and (eqv? (protocol-major %protocol-version)
|
|
|
|
|
(protocol-major v))
|
|
|
|
|
(begin
|
|
|
|
|
(write-int %protocol-version s)
|
2012-07-02 00:45:23 +02:00
|
|
|
|
(if (>= (protocol-minor v) 11)
|
|
|
|
|
(write-int (if reserve-space? 1 0) s))
|
2012-04-18 23:34:12 +02:00
|
|
|
|
(let ((s (%make-nix-server s
|
|
|
|
|
(protocol-major v)
|
|
|
|
|
(protocol-minor v))))
|
2013-01-13 22:27:00 +01:00
|
|
|
|
(let loop ((done? (process-stderr s)))
|
|
|
|
|
(or done? (process-stderr s)))
|
2012-04-18 23:34:12 +02:00
|
|
|
|
s))))))))
|
|
|
|
|
|
2012-08-22 17:22:46 +02:00
|
|
|
|
(define (close-connection server)
|
|
|
|
|
"Close the connection to SERVER."
|
|
|
|
|
(close (nix-server-socket server)))
|
|
|
|
|
|
2012-06-16 16:13:12 +02:00
|
|
|
|
(define current-build-output-port
|
|
|
|
|
;; The port where build output is sent.
|
|
|
|
|
(make-parameter (current-error-port)))
|
|
|
|
|
|
2012-04-18 23:34:12 +02:00
|
|
|
|
(define (process-stderr server)
|
2012-06-16 16:13:12 +02:00
|
|
|
|
"Read standard output and standard error from SERVER, writing it to
|
|
|
|
|
CURRENT-BUILD-OUTPUT-PORT. Return #t when SERVER is done sending data, and
|
|
|
|
|
#f otherwise; in the latter case, the caller should call `process-stderr'
|
|
|
|
|
again until #t is returned or an error is raised."
|
2012-04-18 23:34:12 +02:00
|
|
|
|
(define p
|
|
|
|
|
(nix-server-socket server))
|
|
|
|
|
|
|
|
|
|
;; magic cookies from worker-protocol.hh
|
|
|
|
|
(define %stderr-next #x6f6c6d67)
|
|
|
|
|
(define %stderr-read #x64617461) ; data needed from source
|
|
|
|
|
(define %stderr-write #x64617416) ; data for sink
|
|
|
|
|
(define %stderr-last #x616c7473)
|
|
|
|
|
(define %stderr-error #x63787470)
|
|
|
|
|
|
|
|
|
|
(let ((k (read-int p)))
|
|
|
|
|
(cond ((= k %stderr-write)
|
2012-06-16 16:13:12 +02:00
|
|
|
|
(read-string p)
|
|
|
|
|
#f)
|
2012-04-18 23:34:12 +02:00
|
|
|
|
((= k %stderr-read)
|
|
|
|
|
(let ((len (read-int p)))
|
|
|
|
|
(read-string p) ; FIXME: what to do?
|
2012-06-16 16:13:12 +02:00
|
|
|
|
#f))
|
2012-04-18 23:34:12 +02:00
|
|
|
|
((= k %stderr-next)
|
|
|
|
|
(let ((s (read-string p)))
|
2012-06-16 16:13:12 +02:00
|
|
|
|
(display s (current-build-output-port))
|
|
|
|
|
#f))
|
2012-04-18 23:34:12 +02:00
|
|
|
|
((= k %stderr-error)
|
|
|
|
|
(let ((error (read-string p))
|
|
|
|
|
(status (if (>= (nix-server-minor-version server) 8)
|
|
|
|
|
(read-int p)
|
|
|
|
|
1)))
|
2012-06-01 23:29:55 +02:00
|
|
|
|
(raise (condition (&nix-protocol-error
|
|
|
|
|
(message error)
|
|
|
|
|
(status status))))))
|
2012-04-18 23:34:12 +02:00
|
|
|
|
((= k %stderr-last)
|
2012-06-16 16:13:12 +02:00
|
|
|
|
;; The daemon is done (see `stopWork' in `nix-worker.cc'.)
|
2012-04-18 23:34:12 +02:00
|
|
|
|
#t)
|
|
|
|
|
(else
|
2012-06-01 23:29:55 +02:00
|
|
|
|
(raise (condition (&nix-protocol-error
|
|
|
|
|
(message "invalid error code")
|
|
|
|
|
(status k))))))))
|
2012-04-18 23:34:12 +02:00
|
|
|
|
|
|
|
|
|
(define* (set-build-options server
|
|
|
|
|
#:key keep-failed? keep-going? try-fallback?
|
|
|
|
|
(verbosity 0)
|
|
|
|
|
(max-build-jobs (current-processor-count))
|
|
|
|
|
(max-silent-time 3600)
|
|
|
|
|
(use-build-hook? #t)
|
|
|
|
|
(build-verbosity 0)
|
|
|
|
|
(log-type 0)
|
2012-07-01 00:09:47 +02:00
|
|
|
|
(print-build-trace #t)
|
|
|
|
|
(build-cores 1)
|
|
|
|
|
(use-substitutes? #t))
|
2012-04-18 23:34:12 +02:00
|
|
|
|
;; Must be called after `open-connection'.
|
|
|
|
|
|
|
|
|
|
(define socket
|
|
|
|
|
(nix-server-socket server))
|
|
|
|
|
|
|
|
|
|
(let-syntax ((send (syntax-rules ()
|
2012-07-01 00:09:47 +02:00
|
|
|
|
((_ (type option) ...)
|
|
|
|
|
(begin
|
|
|
|
|
(write-arg type option socket)
|
|
|
|
|
...)))))
|
|
|
|
|
(write-int (operation-id set-options) socket)
|
|
|
|
|
(send (boolean keep-failed?) (boolean keep-going?)
|
|
|
|
|
(boolean try-fallback?) (integer verbosity)
|
|
|
|
|
(integer max-build-jobs) (integer max-silent-time))
|
2012-04-18 23:34:12 +02:00
|
|
|
|
(if (>= (nix-server-minor-version server) 2)
|
2012-07-01 00:09:47 +02:00
|
|
|
|
(send (boolean use-build-hook?)))
|
2012-04-18 23:34:12 +02:00
|
|
|
|
(if (>= (nix-server-minor-version server) 4)
|
2012-07-01 00:09:47 +02:00
|
|
|
|
(send (integer build-verbosity) (integer log-type)
|
|
|
|
|
(boolean print-build-trace)))
|
|
|
|
|
(if (>= (nix-server-minor-version server) 6)
|
|
|
|
|
(send (integer build-cores)))
|
|
|
|
|
(if (>= (nix-server-minor-version server) 10)
|
|
|
|
|
(send (boolean use-substitutes?)))
|
2012-06-16 16:13:12 +02:00
|
|
|
|
(let loop ((done? (process-stderr server)))
|
|
|
|
|
(or done? (process-stderr server)))))
|
2012-04-18 23:34:12 +02:00
|
|
|
|
|
|
|
|
|
(define-syntax define-operation
|
|
|
|
|
(syntax-rules ()
|
2012-12-09 23:52:59 +01:00
|
|
|
|
((_ (name (type arg) ...) docstring return ...)
|
2012-04-18 23:34:12 +02:00
|
|
|
|
(define (name server arg ...)
|
|
|
|
|
docstring
|
|
|
|
|
(let ((s (nix-server-socket server)))
|
|
|
|
|
(write-int (operation-id name) s)
|
|
|
|
|
(write-arg type arg s)
|
|
|
|
|
...
|
2012-06-16 16:13:12 +02:00
|
|
|
|
;; Loop until the server is done sending error output.
|
|
|
|
|
(let loop ((done? (process-stderr server)))
|
|
|
|
|
(or done? (loop (process-stderr server))))
|
2012-12-09 23:52:59 +01:00
|
|
|
|
(values (read-arg return s) ...))))))
|
2012-04-18 23:34:12 +02:00
|
|
|
|
|
2012-07-01 00:21:16 +02:00
|
|
|
|
(define-operation (valid-path? (string path))
|
|
|
|
|
"Return #t when PATH is a valid store path."
|
|
|
|
|
boolean)
|
|
|
|
|
|
2012-10-24 17:03:45 +02:00
|
|
|
|
(define-operation (query-path-hash (string path))
|
|
|
|
|
"Return the SHA256 hash of PATH as a bytevector."
|
|
|
|
|
base16)
|
|
|
|
|
|
2012-04-18 23:34:12 +02:00
|
|
|
|
(define-operation (add-text-to-store (string name) (string text)
|
|
|
|
|
(string-list references))
|
2013-01-15 23:32:13 +01:00
|
|
|
|
"Add TEXT under file NAME in the store, and return its store path.
|
|
|
|
|
REFERENCES is the list of store paths referred to by the resulting store
|
|
|
|
|
path."
|
2012-04-18 23:34:12 +02:00
|
|
|
|
store-path)
|
|
|
|
|
|
|
|
|
|
(define-operation (add-to-store (string basename)
|
2012-06-10 22:43:02 +02:00
|
|
|
|
(boolean fixed?) ; obsolete, must be #t
|
2012-04-18 23:34:12 +02:00
|
|
|
|
(boolean recursive?)
|
2012-06-10 22:43:02 +02:00
|
|
|
|
(string hash-algo)
|
2012-04-18 23:34:12 +02:00
|
|
|
|
(file file-name))
|
2012-11-08 00:26:13 +01:00
|
|
|
|
"Add the contents of FILE-NAME under BASENAME to the store. Note that
|
|
|
|
|
FIXED? is for backward compatibility with old Nix versions and must be #t."
|
2012-04-18 23:34:12 +02:00
|
|
|
|
store-path)
|
|
|
|
|
|
|
|
|
|
(define-operation (build-derivations (string-list derivations))
|
2012-06-16 16:13:12 +02:00
|
|
|
|
"Build DERIVATIONS, and return when the worker is done building them.
|
|
|
|
|
Return #t on success."
|
2012-04-18 23:34:12 +02:00
|
|
|
|
boolean)
|
2012-06-01 23:29:55 +02:00
|
|
|
|
|
2012-12-09 23:50:17 +01:00
|
|
|
|
(define-operation (add-temp-root (store-path path))
|
|
|
|
|
"Make PATH a temporary root for the duration of the current session.
|
|
|
|
|
Return #t."
|
|
|
|
|
boolean)
|
|
|
|
|
|
2012-10-30 00:20:53 +01:00
|
|
|
|
(define-operation (add-indirect-root (string file-name))
|
|
|
|
|
"Make FILE-NAME an indirect root for the garbage collector; FILE-NAME
|
|
|
|
|
can be anywhere on the file system, but it must be an absolute file
|
|
|
|
|
name--it is the caller's responsibility to ensure that it is an absolute
|
|
|
|
|
file name. Return #t on success."
|
|
|
|
|
boolean)
|
|
|
|
|
|
2012-12-09 23:52:59 +01:00
|
|
|
|
(define (run-gc server action to-delete min-freed)
|
|
|
|
|
"Perform the garbage-collector operation ACTION, one of the
|
|
|
|
|
`gc-action' values. When ACTION is `delete-specific', the TO-DELETE is
|
|
|
|
|
the list of store paths to delete. IGNORE-LIVENESS? should always be
|
|
|
|
|
#f. MIN-FREED is the minimum amount of disk space to be freed, in
|
|
|
|
|
bytes, before the GC can stop. Return the list of store paths delete,
|
|
|
|
|
and the number of bytes freed."
|
|
|
|
|
(let ((s (nix-server-socket server)))
|
|
|
|
|
(write-int (operation-id collect-garbage) s)
|
|
|
|
|
(write-int action s)
|
|
|
|
|
(write-store-path-list to-delete s)
|
|
|
|
|
(write-arg boolean #f s) ; ignore-liveness?
|
|
|
|
|
(write-long-long min-freed s)
|
|
|
|
|
(write-int 0 s) ; obsolete
|
|
|
|
|
(when (>= (nix-server-minor-version server) 5)
|
|
|
|
|
;; Obsolete `use-atime' and `max-atime' parameters.
|
|
|
|
|
(write-int 0 s)
|
|
|
|
|
(write-int 0 s))
|
|
|
|
|
|
|
|
|
|
;; Loop until the server is done sending error output.
|
|
|
|
|
(let loop ((done? (process-stderr server)))
|
|
|
|
|
(or done? (loop (process-stderr server))))
|
|
|
|
|
|
|
|
|
|
(let ((paths (read-store-path-list s))
|
|
|
|
|
(freed (read-long-long s))
|
|
|
|
|
(obsolete (read-long-long s)))
|
|
|
|
|
(values paths freed))))
|
|
|
|
|
|
|
|
|
|
(define-syntax-rule (%long-long-max)
|
|
|
|
|
;; Maximum unsigned 64-bit integer.
|
|
|
|
|
(- (expt 2 64) 1))
|
|
|
|
|
|
|
|
|
|
(define (live-paths server)
|
|
|
|
|
"Return the list of live store paths---i.e., store paths still
|
|
|
|
|
referenced, and thus not subject to being garbage-collected."
|
|
|
|
|
(run-gc server (gc-action return-live) '() (%long-long-max)))
|
|
|
|
|
|
|
|
|
|
(define (dead-paths server)
|
|
|
|
|
"Return the list of dead store paths---i.e., store paths no longer
|
|
|
|
|
referenced, and thus subject to being garbage-collected."
|
|
|
|
|
(run-gc server (gc-action return-dead) '() (%long-long-max)))
|
|
|
|
|
|
|
|
|
|
(define* (collect-garbage server #:optional (min-freed (%long-long-max)))
|
|
|
|
|
"Collect garbage from the store at SERVER. If MIN-FREED is non-zero,
|
|
|
|
|
then collect at least MIN-FREED bytes. Return the paths that were
|
|
|
|
|
collected, and the number of bytes freed."
|
|
|
|
|
(run-gc server (gc-action delete-dead) '() min-freed))
|
|
|
|
|
|
|
|
|
|
(define* (delete-paths server paths #:optional (min-freed (%long-long-max)))
|
|
|
|
|
"Delete PATHS from the store at SERVER, if they are no longer
|
|
|
|
|
referenced. If MIN-FREED is non-zero, then stop after at least
|
|
|
|
|
MIN-FREED bytes have been collected. Return the paths that were
|
|
|
|
|
collected, and the number of bytes freed."
|
|
|
|
|
(run-gc server (gc-action delete-specific) paths min-freed))
|
|
|
|
|
|
2012-06-01 23:29:55 +02:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Store paths.
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
(define %store-prefix
|
|
|
|
|
;; Absolute path to the Nix store.
|
2012-12-04 23:48:21 +01:00
|
|
|
|
(make-parameter (or (and=> (getenv "NIX_STORE_DIR") canonicalize-path)
|
2012-12-05 23:02:47 +01:00
|
|
|
|
%store-directory)))
|
2012-06-01 23:29:55 +02:00
|
|
|
|
|
2012-09-01 19:21:06 +02:00
|
|
|
|
(define (store-path? path)
|
|
|
|
|
"Return #t if PATH is a store path."
|
|
|
|
|
;; This is a lightweight check, compared to using a regexp, but this has to
|
|
|
|
|
;; be fast as it's called often in `derivation', for instance.
|
|
|
|
|
;; `isStorePath' in Nix does something similar.
|
|
|
|
|
(string-prefix? (%store-prefix) path))
|
2012-06-01 23:29:55 +02:00
|
|
|
|
|
|
|
|
|
(define (derivation-path? path)
|
|
|
|
|
"Return #t if PATH is a derivation path."
|
|
|
|
|
(and (store-path? path) (string-suffix? ".drv" path)))
|
2012-11-01 01:39:23 +01:00
|
|
|
|
|
|
|
|
|
(define (store-path-package-name path)
|
|
|
|
|
"Return the package name part of PATH, a file name in the store."
|
|
|
|
|
(define store-path-rx
|
|
|
|
|
(make-regexp (string-append "^.*" (regexp-quote (%store-prefix))
|
|
|
|
|
"/[^-]+-(.+)$")))
|
|
|
|
|
|
|
|
|
|
(and=> (regexp-exec store-path-rx path)
|
|
|
|
|
(cut match:substring <> 1)))
|