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/>.
19 (define-module (guix derivations)
20 #:use-module (srfi srfi-1)
21 #:use-module (srfi srfi-9)
22 #:use-module (srfi srfi-26)
23 #:use-module (rnrs io ports)
24 #:use-module (rnrs bytevectors)
25 #:use-module (ice-9 match)
26 #:use-module (ice-9 rdelim)
27 #:use-module (guix store)
28 #:use-module (guix utils)
34 derivation-builder-arguments
35 derivation-builder-environment-vars
38 derivation-output-path
39 derivation-output-hash-algo
40 derivation-output-hash
44 derivation-input-sub-derivations
46 fixed-output-derivation?
51 derivation-path->output-path
55 build-expression->derivation
59 ;;; Nix derivations, as implemented in Nix's `derivations.cc'.
62 (define-record-type <derivation>
63 (make-derivation outputs inputs sources system builder args env-vars)
65 (outputs derivation-outputs) ; list of name/<derivation-output> pairs
66 (inputs derivation-inputs) ; list of <derivation-input>
67 (sources derivation-sources) ; list of store paths
68 (system derivation-system) ; string
69 (builder derivation-builder) ; store path
70 (args derivation-builder-arguments) ; list of strings
71 (env-vars derivation-builder-environment-vars)) ; list of name/value pairs
73 (define-record-type <derivation-output>
74 (make-derivation-output path hash-algo hash)
76 (path derivation-output-path) ; store path
77 (hash-algo derivation-output-hash-algo) ; symbol | #f
78 (hash derivation-output-hash)) ; bytevector | #f
80 (define-record-type <derivation-input>
81 (make-derivation-input path sub-derivations)
83 (path derivation-input-path) ; store path
84 (sub-derivations derivation-input-sub-derivations)) ; list of strings
86 (define (fixed-output-derivation? drv)
87 "Return #t if DRV is a fixed-output derivation, such as the result of a
88 download with a fixed hash (aka. `fetchurl')."
91 (($ <derivation-output> _ (? symbol?) (? string?))))
95 (define (read-derivation drv-port)
96 "Read the derivation from DRV-PORT and return the corresponding
99 (define comma (string->symbol ","))
101 (define (ununquote x)
103 (('unquote x) (ununquote x))
104 ((x ...) (map ununquote x))
107 (define (outputs->alist x)
108 (fold-right (lambda (output result)
112 (make-derivation-output path #f #f)
114 ((name path hash-algo hash)
116 (let ((algo (string->symbol hash-algo))
117 (hash (base16-string->bytevector hash)))
119 (make-derivation-output path algo hash)
124 (define (make-input-drvs x)
125 (fold-right (lambda (input result)
127 ((path (sub-drvs ...))
128 (cons (make-derivation-input path sub-drvs)
133 (let loop ((exp (read drv-port))
137 (let ((result (reverse result)))
139 (('Derive ((outputs ...) (input-drvs ...)
143 ((? string? args) ...)
145 (make-derivation (outputs->alist outputs)
146 (make-input-drvs input-drvs)
149 (fold-right alist-cons '() var value)))
151 (error "failed to parse derivation" drv-port result)))))
152 ((? (cut eq? <> comma))
153 (loop (read drv-port) result))
155 (loop (read drv-port)
156 (cons (ununquote exp) result))))))
158 (define (write-derivation drv port)
159 "Write the ATerm-like serialization of DRV to PORT. See Section 2.4 of
160 Eelco Dolstra's PhD dissertation for an overview of a previous version of
162 (define (list->string lst)
163 (string-append "[" (string-join lst ",") "]"))
165 (define (write-list lst)
166 (display (list->string lst) port))
169 (($ <derivation> outputs inputs sources
170 system builder args env-vars)
171 (display "Derive(" port)
172 (write-list (map (match-lambda
173 ((name . ($ <derivation-output> path hash-algo hash))
174 (format #f "(~s,~s,~s,~s)"
176 (or (and=> hash-algo symbol->string) "")
177 (or (and=> hash bytevector->base16-string)
181 (write-list (map (match-lambda
182 (($ <derivation-input> path sub-drvs)
183 (format #f "(~s,~a)" path
184 (list->string (map object->string sub-drvs)))))
187 (write-list (map object->string sources))
188 (format port ",~s,~s," system builder)
189 (write-list (map object->string args))
191 (write-list (map (match-lambda
193 (format #f "(~s,~s)" name value)))
195 (display ")" port))))
197 (define* (derivation-path->output-path path #:optional (output "out"))
198 "Read the derivation from PATH (`/nix/store/xxx.drv'), and return the store
199 path of its output OUTPUT."
200 (let* ((drv (call-with-input-file path read-derivation))
201 (outputs (derivation-outputs drv)))
202 (and=> (assoc-ref outputs output) derivation-output-path)))
206 ;;; Derivation primitive.
209 (define (compressed-hash bv size) ; `compressHash'
210 "Given the hash stored in BV, return a compressed version thereof that fits
212 (define new (make-bytevector size 0))
213 (define old-size (bytevector-length bv))
217 (let* ((j (modulo i size))
218 (o (bytevector-u8-ref new j)))
219 (bytevector-u8-set! new j
220 (logxor o (bytevector-u8-ref bv i)))
223 (define derivation-hash ; `hashDerivationModulo' in derivations.cc
226 "Return the hash of DRV, modulo its fixed-output inputs, as a bytevector."
228 (($ <derivation> ((_ . ($ <derivation-output> path
229 (? symbol? hash-algo) (? bytevector? hash)))))
230 ;; A fixed-output derivation.
233 (string-append "fixed:out:" (symbol->string hash-algo)
234 ":" (bytevector->base16-string hash)
236 (($ <derivation> outputs inputs sources
237 system builder args env-vars)
238 ;; A regular derivation: replace the path of each input with that
239 ;; input's hash; return the hash of serialization of the resulting
240 ;; derivation. Note: inputs are sorted as in the order of their hex
241 ;; hash representation because that's what the C++ `std::map' code
243 (let* ((inputs (sort (map (match-lambda
244 (($ <derivation-input> path sub-drvs)
245 (let ((hash (call-with-input-file path
246 (compose bytevector->base16-string
249 (make-derivation-input hash sub-drvs))))
252 (string<? (derivation-input-path i1)
253 (derivation-input-path i2)))))
254 (sources (sort sources string<?))
255 (drv (make-derivation outputs inputs sources
256 system builder args env-vars)))
258 (string->utf8 (call-with-output-string
259 (cut write-derivation drv <>))))))))))
261 (define (store-path type hash name) ; makeStorePath
262 "Return the store path for NAME/HASH/TYPE."
263 (let* ((s (string-append type ":sha256:"
264 (bytevector->base16-string hash) ":"
265 (%store-prefix) ":" name))
266 (h (sha256 (string->utf8 s)))
267 (c (compressed-hash h 20)))
268 (string-append (%store-prefix) "/"
269 (bytevector->nix-base32-string c) "-"
272 (define (output-path output hash name) ; makeOutputPath
273 "Return an output path for OUTPUT (the name of the output as a string) of
274 the derivation called NAME with hash HASH."
275 (store-path (string-append "output:" output) hash
276 (if (string=? output "out")
278 (string-append name "-" output))))
280 (define* (derivation store name system builder args env-vars inputs
281 #:key (outputs '("out")) hash hash-algo hash-mode)
282 "Build a derivation with the given arguments. Return the resulting
283 store path and <derivation> object. When HASH, HASH-ALGO, and HASH-MODE
284 are given, a fixed-output derivation is created---i.e., one whose result is
285 known in advance, such as a file download."
286 (define (add-output-paths drv)
287 ;; Return DRV with an actual store path for each of its output and the
288 ;; corresponding environment variable.
290 (($ <derivation> outputs inputs sources
291 system builder args env-vars)
292 (let* ((drv-hash (derivation-hash drv))
293 (outputs (map (match-lambda
294 ((output-name . ($ <derivation-output>
296 (let ((path (output-path output-name
299 (make-derivation-output path algo
302 (make-derivation outputs inputs sources system builder args
306 (or (and=> (assoc-ref outputs name)
307 derivation-output-path)
311 (define (env-vars-with-empty-outputs)
312 ;; Return a variant of ENV-VARS where each OUTPUTS is associated with an
313 ;; empty string, even outputs that do not appear in ENV-VARS. Note: the
314 ;; result is sorted alphabetically, as with C++ `std::map'.
315 (let ((e (map (match-lambda
317 (if (member name outputs)
321 (sort (fold (lambda (output-name env-vars)
322 (if (assoc output-name env-vars)
324 (append env-vars `((,output-name . "")))))
328 (string<? (car e1) (car e2))))))
330 (let* ((outputs (map (lambda (name)
331 ;; Return outputs with an empty path.
333 (make-derivation-output "" hash-algo hash)))
335 (inputs (map (match-lambda
336 (((? store-path? input))
337 (make-derivation-input input '("out")))
338 (((? store-path? input) sub-drvs ...)
339 (make-derivation-input input sub-drvs))
341 (let ((path (add-to-store store
343 (hash-algo sha256) #t #t
345 (make-derivation-input path '()))))
346 (delete-duplicates inputs)))
347 (env-vars (env-vars-with-empty-outputs))
348 (drv-masked (make-derivation outputs
349 (filter (compose derivation-path?
350 derivation-input-path)
352 (filter-map (lambda (i)
353 (let ((p (derivation-input-path i)))
354 (and (not (derivation-path? p))
357 system builder args env-vars))
358 (drv (add-output-paths drv-masked)))
360 (values (add-text-to-store store (string-append name ".drv")
361 (call-with-output-string
362 (cut write-derivation drv <>))
363 (map derivation-input-path
369 ;;; Guile-based builders.
372 (define %guile-for-build
373 ;; The derivation of the Guile to be used within the build environment,
374 ;; when using `build-expression->derivation'.
375 (make-parameter (false-if-exception (nixpkgs-derivation "guile"))))
377 (define* (imported-files store files
378 #:key (name "file-import") (system (%current-system)))
379 "Return a derivation that imports FILES into STORE. FILES must be a list
380 of (FINAL-PATH . FILE-NAME) pairs; each FILE-NAME is read from the file
381 system, imported, and appears under FINAL-PATH in the resulting store path."
382 (define (parent-dirs file-name)
383 ;; Return the list of parent dirs of FILE-NAME, in the order in which an
384 ;; `mkdir -p' implementation would make them.
385 (let ((not-slash (char-set-complement (char-set #\/))))
387 (fold (lambda (dir result)
392 (cons (string-append prev "/" dir)
395 (remove (cut string=? <> ".")
396 (string-tokenize (dirname file-name) not-slash))))))
398 (let* ((files (map (match-lambda
399 ((final-path . file-name)
401 (add-to-store store (basename final-path) #t #f
402 "sha256" file-name))))
406 (mkdir %output) (chdir %output)
407 ,@(append-map (match-lambda
408 ((final-path store-path)
409 (append (match (parent-dirs final-path)
412 (append (map (lambda (d)
416 `((or (file-exists? ,tail)
418 `((symlink ,store-path ,final-path)))))
420 (build-expression->derivation store name (%current-system)
423 (define* (imported-modules store modules
424 #:key (name "module-import")
425 (system (%current-system)))
426 "Return a derivation that contains the source files of MODULES, a list of
427 module names such as `(ice-9 q)'. All of MODULES must be in the current
429 ;; TODO: Determine the closure of MODULES, build the `.go' files,
430 ;; canonicalize the source files through read/write, etc.
431 (let ((files (map (lambda (m)
432 (let ((f (string-append
433 (string-join (map symbol->string m) "/")
435 (cons f (search-path %load-path f))))
437 (imported-files store files #:name name #:system system)))
440 (define* (build-expression->derivation store name system exp inputs
441 #:key (outputs '("out"))
444 "Return a derivation that executes Scheme expression EXP as a builder for
445 derivation NAME. INPUTS must be a list of (NAME DRV-PATH SUB-DRV) tuples;
446 when SUB-DRV is omitted, \"out\" is assumed. EXP is evaluated in an
447 environment where %OUTPUT is bound to the main output path, %OUTPUTS is bound
448 to a list of output/path pairs, and where %BUILD-INPUTS is bound to an alist
449 of string/output-path pairs made from INPUTS. The builder terminates by
450 passing the result of EXP to `exit'; thus, when EXP returns #f, the build is
451 considered to have failed."
453 (string-append (derivation-path->output-path (%guile-for-build))
458 (((or 'define-module 'use-modules) _ ...) #t)
461 (let* ((prologue `(begin
464 ;; Module forms must appear at the top-level so
465 ;; that any macros they export can be expanded.
466 (filter module-form? exp))
469 (define %output (getenv "out"))
474 (define %build-inputs
477 (let ((sub (match rest
481 (if (derivation-path? drv)
482 (derivation-path->output-path drv
486 (builder (add-text-to-store store
487 (string-append name "-guile-builder")
489 (object->string prologue)
494 (remove module-form? exp))
496 (map second inputs)))
497 (mod-drv (if (null? modules)
499 (imported-modules store modules)))
500 (mod-dir (and mod-drv
501 (derivation-path->output-path mod-drv))))
502 (derivation store name system guile
503 `("--no-auto-compile"
504 ,@(if mod-dir `("-L" ,mod-dir) '())
506 '(("HOME" . "/homeless"))
507 `((,(%guile-for-build))
510 ,@(if mod-drv `((,mod-drv)) '()))
511 #:hash hash #:hash-algo hash-algo