1 ;;; Guix --- Nix package management from Guile. -*- coding: utf-8 -*-
2 ;;; Copyright (C) 2012 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of Guix.
6 ;;; 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 ;;; 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 Guix. If not, see <http://www.gnu.org/licenses/>.
20 #:use-module (rnrs bytevectors)
21 #:use-module (rnrs io ports)
22 #:use-module (srfi srfi-1)
23 #:use-module (srfi srfi-9)
24 #:use-module (srfi srfi-26)
25 #:use-module (ice-9 match)
26 #:use-module (ice-9 rdelim)
28 nix-server-major-version
29 nix-server-minor-version
37 (define %protocol-version #x109)
39 (define %worker-magic-1 #x6e697863)
40 (define %worker-magic-2 #x6478696f)
42 (define (protocol-major magic)
43 (logand magic #xff00))
44 (define (protocol-minor magic)
45 (logand magic #x00ff))
47 (define-syntax define-enumerate-type
49 ((_ name->int (name id) ...)
50 (define-syntax name->int
51 (syntax-rules (name ...)
52 ((_ name) id) ...)))))
54 (define-enumerate-type operation-id
55 ;; operation numbers from worker-protocol.hh
67 (add-indirect-root 12)
74 (query-substitutable-path-info 21)
75 (query-derivation-outputs 22)
76 (query-valid-paths 23)
77 (query-failed-paths 24)
78 (clear-failed-paths 25)
81 (query-derivation-output-names 28))
83 (define-enumerate-type hash-algo
89 (define %nix-state-dir "/nix/var/nix")
90 (define %default-socket-path
91 (string-append %nix-state-dir "/daemon-socket/socket"))
96 (define (write-int n p)
97 (let ((b (make-bytevector 8 0)))
98 (bytevector-u32-set! b 0 n (endianness little))
99 (put-bytevector p b)))
102 (let ((b (get-bytevector-n p 8)))
103 (bytevector-u32-ref b 0 (endianness little))))
105 (define (write-long-long n p)
106 (let ((b (make-bytevector 8 0)))
107 (bytevector-u64-set! b 0 n (endianness little))
108 (put-bytevector p b)))
110 (define write-padding
111 (let ((zero (make-bytevector 8 0)))
113 (let ((m (modulo n 8)))
115 (put-bytevector p zero 0 (- 8 m)))))))
117 (define (write-string s p)
118 (let ((b (string->utf8 s)))
119 (write-int (bytevector-length b) p)
121 (write-padding (bytevector-length b) p)))
123 (define (read-string p)
124 (let* ((len (read-int p))
126 (bv (get-bytevector-n p len))
127 (str (utf8->string bv)))
129 (get-bytevector-n p (- 8 m)))
132 (define (write-string-list l p)
133 (write-int (length l) p)
134 (for-each (cut write-string <> p) l))
136 (define (read-store-path p)
137 (read-string p)) ; TODO: assert path
139 (define (write-contents file p)
140 "Write the contents of FILE to output port P."
141 (define (dump in size)
142 (define buf-size 65536)
143 (define buf (make-bytevector buf-size))
145 (let loop ((left size))
148 (let ((read (get-bytevector-n! in buf 0 buf-size)))
149 (if (eof-object? read)
152 (put-bytevector p buf 0 read)
153 (loop (- left read))))))))
155 (let ((size (stat:size (lstat file))))
156 (write-string "contents" p)
157 (write-long-long size p)
158 (call-with-input-file file
161 (write-padding size p)))
163 (define (write-file f p)
164 (define %archive-version-1 "nix-archive-1")
167 (write-string %archive-version-1 p)
171 (write-string "type" p)
172 (write-string "regular" p)
173 (if (not (zero? (logand (stat:mode s) #o100)))
175 (write-string "executable" p)
176 (write-string "" p)))
178 (write-string ")" p))
180 (write-string "type" p)
181 (write-string "directory" p)
186 (define-syntax write-arg
187 (syntax-rules (integer boolean file string string-list)
191 (write-int (if arg 1 0) p))
195 (write-string arg p))
196 ((_ string-list arg p)
197 (write-string-list arg p))))
199 (define-syntax read-arg
200 (syntax-rules (integer boolean string store-path)
204 (not (zero? (read-int p))))
208 (read-store-path p))))
213 (define-record-type <nix-server>
214 (%make-nix-server socket major minor)
216 (socket nix-server-socket)
217 (major nix-server-major-version)
218 (minor nix-server-minor-version))
220 (define* (open-connection #:optional (file %default-socket-path))
221 (let ((s (with-fluids ((%default-port-encoding #f))
222 ;; This trick allows use of the `scm_c_read' optimization.
223 (socket PF_UNIX SOCK_STREAM 0)))
224 (a (make-socket-address PF_UNIX file)))
226 (write-int %worker-magic-1 s)
227 (let ((r (read-int s)))
228 (and (eqv? r %worker-magic-2)
229 (let ((v (read-int s)))
230 (and (eqv? (protocol-major %protocol-version)
233 (write-int %protocol-version s)
234 (let ((s (%make-nix-server s
236 (protocol-minor v))))
240 (define (process-stderr server)
242 (nix-server-socket server))
244 ;; magic cookies from worker-protocol.hh
245 (define %stderr-next #x6f6c6d67)
246 (define %stderr-read #x64617461) ; data needed from source
247 (define %stderr-write #x64617416) ; data for sink
248 (define %stderr-last #x616c7473)
249 (define %stderr-error #x63787470)
251 (let ((k (read-int p)))
252 (cond ((= k %stderr-write)
255 (let ((len (read-int p)))
256 (read-string p) ; FIXME: what to do?
259 (let ((s (read-string p)))
260 (display s (current-error-port))
263 (let ((error (read-string p))
264 (status (if (>= (nix-server-minor-version server) 8)
267 (format (current-error-port) "error: ~a (status: ~a)~%"
273 (error "invalid standard error code" k)))))
275 (define* (set-build-options server
276 #:key keep-failed? keep-going? try-fallback?
278 (max-build-jobs (current-processor-count))
279 (max-silent-time 3600)
283 (print-build-trace #t))
284 ;; Must be called after `open-connection'.
287 (nix-server-socket server))
289 (let-syntax ((send (syntax-rules ()
291 (for-each (lambda (i)
293 (write-int (if i 1 0) socket))
295 (write-int i socket))
297 (error "invalid build option"
299 (list option ...))))))
300 (send (operation-id set-options)
301 keep-failed? keep-going? try-fallback? verbosity
302 max-build-jobs max-silent-time)
303 (if (>= (nix-server-minor-version server) 2)
304 (send use-build-hook?))
305 (if (>= (nix-server-minor-version server) 4)
306 (send build-verbosity log-type print-build-trace))
307 (process-stderr server)))
309 (define-syntax define-operation
311 ((_ (name (type arg) ...) docstring return)
312 (define (name server arg ...)
314 (let ((s (nix-server-socket server)))
315 (write-int (operation-id name) s)
316 (write-arg type arg s)
318 (process-stderr server)
319 (read-arg return s))))))
321 (define-operation (add-text-to-store (string name) (string text)
322 (string-list references))
323 "Add TEXT under file NAME in the store."
326 (define-operation (add-to-store (string basename)
328 (boolean sha256-and-recursive?)
331 "Add the contents of FILE-NAME under BASENAME to the store."
334 (define-operation (build-derivations (string-list derivations))
335 "Build DERIVATIONS; return #t on success."
341 (define-record-type <derivation>
342 (make-derivation outputs inputs sources system builder args env-vars)
344 (outputs derivation-outputs) ; list of name/<derivation-output> pairs
345 (inputs derivation-inputs) ; list of <derivation-input>
346 (sources derivation-sources) ; list of store paths
347 (system derivation-system) ; string
348 (builder derivation-builder) ; store path
349 (args derivation-builder-arguments) ; list of strings
350 (env-vars derivation-builder-environment-vars)) ; list of name/value pairs
352 (define-record-type <derivation-output>
353 (make-derivation-output path hash-algo hash)
355 (path derivation-output-path) ; store path
356 (hash-algo derivation-output-hash-algo) ; symbol | #f
357 (hash derivation-output-hash)) ; symbol | #f
359 (define-record-type <derivation-input>
360 (make-derivation-input path sub-derivations)
362 (path derivation-input-path) ; store path
363 (sub-derivations derivation-input-sub-derivations)) ; list of strings
365 (define (fixed-output-derivation? drv)
366 "Return #t if DRV is a fixed-output derivation, such as the result of a
367 download with a fixed hash (aka. `fetchurl')."
370 (($ <derivation-output> _ (? symbol?) (? string?))))
374 (define (read-derivation drv-port)
375 "Read the derivation from DRV-PORT and return the corresponding
376 <derivation> object."
378 (define comma (string->symbol ","))
380 (define (ununquote x)
382 (('unquote x) (ununquote x))
383 ((x ...) (map ununquote x))
386 (define (outputs->alist x)
387 (fold-right (lambda (output result)
391 (make-derivation-output path #f #f)
393 ((name path hash-algo hash)
395 (let ((algo (string->symbol hash-algo)))
397 (make-derivation-output path algo hash)
402 (define (make-input-drvs x)
403 (fold-right (lambda (input result)
405 ((path (sub-drvs ...))
406 (cons (make-derivation-input path sub-drvs)
411 (let loop ((exp (read drv-port))
415 (let ((result (reverse result)))
417 (('Derive ((outputs ...) (input-drvs ...)
421 ((? string? args) ...)
423 (make-derivation (outputs->alist outputs)
424 (make-input-drvs input-drvs)
427 (fold-right alist-cons '() var value)))
429 (error "failed to parse derivation" drv-port result)))))
430 ((? (cut eq? <> comma))
431 (loop (read drv-port) result))
433 (loop (read drv-port)
434 (cons (ununquote exp) result))))))
436 (define (write-derivation drv port)
437 "Write the ATerm-like serialization of DRV to PORT."
438 (define (list->string lst)
439 (string-append "[" (string-join lst ",") "]"))
441 (define (write-list lst)
442 (display (list->string lst) port))
445 (($ <derivation> outputs inputs sources
446 system builder args env-vars)
447 (display "Derive(" port)
448 (write-list (map (match-lambda
449 ((name . ($ <derivation-output> path hash-algo hash))
450 (format #f "(~s,~s,~s,~s)"
451 name path (or hash-algo "")
455 (write-list (map (match-lambda
456 (($ <derivation-input> path sub-drvs)
457 (format #f "(~s,~a)" path
458 (list->string (map object->string sub-drvs)))))
462 (format port ",~s,~s," system builder)
463 (write-list (map object->string args))
465 (write-list (map (match-lambda
467 (format #f "(~s,~s)" name value)))
469 (display ")" port))))
472 "Return the SHA256 of BV as an string of hexadecimal digits."
473 ;; XXX: Poor programmer's implementation that uses Coreutils.
476 (pid (primitive-fork)))
483 (dup2 (fileno (car in)) 0)
484 (dup2 (fileno (cdr out)) 1)
485 (execlp "sha256sum" "sha256sum"))
489 (put-bytevector (cdr in) bv)
490 (close (cdr in)) ; EOF
491 (let ((line (car (string-tokenize (read-line (car out))))))
493 (and (and=> (status:exit-val (cdr (waitpid pid)))
497 (define (derivation-hash drv) ; `hashDerivationModulo' in derivations.cc
499 (($ <derivation> ((_ . ($ <derivation-output> path
500 (? symbol? hash-algo) (? string? hash)))))
501 ;; A fixed-output derivation.
504 (string-append "fixed:out:" hash-algo ":" hash ":" path))))
505 (($ <derivation> outputs inputs sources
506 system builder args env-vars)
507 ;; A regular derivation: replace that path of each input with that
508 ;; inputs hash; return the hash of serialization of the resulting
510 (let* ((inputs (map (match-lambda
511 (($ <derivation-input> path sub-drvs)
512 (let ((hash (call-with-input-file path
513 (compose derivation-hash
515 (make-derivation-input hash sub-drvs))))
517 (drv (make-derivation outputs inputs sources
518 system builder args env-vars)))
520 (string->utf8 (call-with-output-string
521 (cut write-derivation drv <>))))))))
523 (define (instantiate server derivation)