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 derivations)
20 #:use-module (srfi srfi-1)
21 #:use-module (srfi srfi-9)
22 #:use-module (srfi srfi-9 gnu)
23 #:use-module (srfi srfi-26)
24 #:use-module (srfi srfi-34)
25 #:use-module (srfi srfi-35)
26 #:use-module (rnrs io ports)
27 #:use-module (rnrs bytevectors)
28 #:use-module (ice-9 match)
29 #:use-module (ice-9 rdelim)
30 #:use-module (ice-9 vlist)
31 #:use-module (guix store)
32 #:use-module (guix utils)
33 #:use-module (guix monads)
34 #:use-module (guix hash)
35 #:use-module (guix base32)
36 #:use-module (guix records)
37 #:use-module (guix sets)
38 #:export (<derivation>
45 derivation-builder-arguments
46 derivation-builder-environment-vars
48 derivation-prerequisites
49 derivation-prerequisites-to-build
53 derivation-output-path
54 derivation-output-hash-algo
55 derivation-output-hash
56 derivation-output-recursive?
61 derivation-input-sub-derivations
62 derivation-input-output-paths
66 derivation-error-derivation
67 &derivation-missing-output-error
68 derivation-missing-output-error?
69 derivation-missing-output
72 derivation-output-names
73 fixed-output-derivation?
74 offloadable-derivation?
75 substitutable-derivation?
81 derivation->output-path
82 derivation->output-paths
83 derivation-path->output-path
84 derivation-path->output-paths
92 graft-replacement-output
100 build-expression->derivation)
102 ;; Re-export it from here for backward compatibility.
103 #:re-export (%guile-for-build))
106 ;;; Error conditions.
109 (define-condition-type &derivation-error &nix-error
111 (derivation derivation-error-derivation))
113 (define-condition-type &derivation-missing-output-error &derivation-error
114 derivation-missing-output-error?
115 (output derivation-missing-output))
118 ;;; Nix derivations, as implemented in Nix's `derivations.cc'.
121 (define-record-type <derivation>
122 (make-derivation outputs inputs sources system builder args env-vars
125 (outputs derivation-outputs) ; list of name/<derivation-output> pairs
126 (inputs derivation-inputs) ; list of <derivation-input>
127 (sources derivation-sources) ; list of store paths
128 (system derivation-system) ; string
129 (builder derivation-builder) ; store path
130 (args derivation-builder-arguments) ; list of strings
131 (env-vars derivation-builder-environment-vars) ; list of name/value pairs
132 (file-name derivation-file-name)) ; the .drv file name
134 (define-record-type <derivation-output>
135 (make-derivation-output path hash-algo hash recursive?)
137 (path derivation-output-path) ; store path
138 (hash-algo derivation-output-hash-algo) ; symbol | #f
139 (hash derivation-output-hash) ; bytevector | #f
140 (recursive? derivation-output-recursive?)) ; Boolean
142 (define-record-type <derivation-input>
143 (make-derivation-input path sub-derivations)
145 (path derivation-input-path) ; store path
146 (sub-derivations derivation-input-sub-derivations)) ; list of strings
148 (set-record-type-printer! <derivation>
150 (format port "#<derivation ~a => ~a ~a>"
151 (derivation-file-name drv)
155 (derivation-output-path output)))
156 (derivation-outputs drv)))
157 (number->string (object-address drv) 16))))
159 (define (derivation-name drv)
160 "Return the base name of DRV."
161 (let ((base (store-path-package-name (derivation-file-name drv))))
162 (string-drop-right base 4)))
164 (define (derivation-output-names drv)
165 "Return the names of the outputs of DRV."
166 (match (derivation-outputs drv)
170 (define (fixed-output-derivation? drv)
171 "Return #t if DRV is a fixed-output derivation, such as the result of a
172 download with a fixed hash (aka. `fetchurl')."
175 (("out" . ($ <derivation-output> _ (? symbol?) (? bytevector?)))))
179 (define (derivation-input-output-paths input)
180 "Return the list of output paths corresponding to INPUT, a
183 (($ <derivation-input> path sub-drvs)
184 (map (cut derivation-path->output-path path <>)
187 (define (derivation-prerequisites drv)
188 "Return the list of derivation-inputs required to build DRV, recursively."
192 (let ((inputs (remove (cut set-contains? input-set <>)
193 (derivation-inputs drv))))
195 (append inputs result)
196 (fold set-insert input-set inputs)
198 (call-with-input-file (derivation-input-path i)
202 (define (offloadable-derivation? drv)
203 "Return true if DRV can be offloaded, false otherwise."
204 (match (assoc "preferLocalBuild"
205 (derivation-builder-environment-vars drv))
206 (("preferLocalBuild" . "1") #f)
209 (define substitutable-derivation?
210 ;; Return #t if the derivation can be substituted. Currently the two are
211 ;; synonymous, see <http://bugs.gnu.org/18747>.
212 offloadable-derivation?)
214 (define (derivation-output-paths drv sub-drvs)
215 "Return the output paths of outputs SUB-DRVS of DRV."
217 (($ <derivation> outputs)
218 (map (lambda (sub-drv)
219 (derivation-output-path (assoc-ref outputs sub-drv)))
222 (define* (substitution-oracle store drv)
223 "Return a one-argument procedure that, when passed a store file name,
224 returns #t if it's substitutable and #f otherwise. The returned procedure
225 knows about all substitutes for all the derivations listed in DRV and their
228 Creating a single oracle (thus making a single 'substitutable-paths' call) and
229 reusing it is much more efficient than calling 'has-substitutes?' or similar
230 repeatedly, because it avoids the costs associated with launching the
231 substituter many times."
232 (let* ((paths (delete-duplicates
233 (fold (lambda (drv result)
234 (let ((self (match (derivation->output-paths drv)
235 (((names . paths) ...)
237 (deps (append-map derivation-input-output-paths
238 (derivation-prerequisites
240 (append self deps result)))
243 (subst (list->set (substitutable-paths store paths))))
244 (cut set-contains? subst <>)))
246 (define* (derivation-prerequisites-to-build store drv
249 (derivation-output-names drv))
251 (substitution-oracle store
253 "Return two values: the list of derivation-inputs required to build the
254 OUTPUTS of DRV and not already available in STORE, recursively, and the list
255 of required store paths that can be substituted. SUBSTITUTABLE? must be a
256 one-argument procedure similar to that returned by 'substitution-oracle'."
258 (cut valid-path? store <>))
261 (compose (cut any built? <>) derivation-input-output-paths))
263 (define input-substitutable?
264 ;; Return true if and only if all of SUB-DRVS are subsitutable. If at
265 ;; least one is missing, then everything must be rebuilt.
266 (compose (cut every substitutable? <>) derivation-input-output-paths))
268 (define (derivation-built? drv sub-drvs)
269 (every built? (derivation-output-paths drv sub-drvs)))
271 (define (derivation-substitutable? drv sub-drvs)
272 (and (substitutable-derivation? drv)
273 (every substitutable? (derivation-output-paths drv sub-drvs))))
279 (cond ((derivation-built? drv sub-drvs)
280 (values build substitute))
281 ((derivation-substitutable? drv sub-drvs)
283 (append (derivation-output-paths drv sub-drvs)
286 (let ((build (if (substitutable-derivation? drv)
288 (cons (make-derivation-input
289 (derivation-file-name drv) sub-drvs)
291 (inputs (remove (lambda (i)
292 (or (member i build) ; XXX: quadratic
294 (input-substitutable? i)))
295 (derivation-inputs drv))))
297 (append inputs build)
298 (append (append-map (lambda (input)
299 (if (and (not (input-built? input))
300 (input-substitutable? input))
301 (derivation-input-output-paths
304 (derivation-inputs drv))
307 (call-with-input-file (derivation-input-path i)
310 (map derivation-input-sub-derivations inputs)))))))
312 (define (%read-derivation drv-port)
313 ;; Actually read derivation from DRV-PORT.
315 (define comma (string->symbol ","))
317 (define (ununquote x)
319 (('unquote x) (ununquote x))
320 ((x ...) (map ununquote x))
323 (define (outputs->alist x)
324 (fold-right (lambda (output result)
328 (make-derivation-output path #f #f #f)
330 ((name path hash-algo hash)
332 (let* ((rec? (string-prefix? "r:" hash-algo))
333 (algo (string->symbol
335 (string-drop hash-algo 2)
337 (hash (base16-string->bytevector hash)))
339 (make-derivation-output path algo
345 (define (make-input-drvs x)
346 (fold-right (lambda (input result)
348 ((path (sub-drvs ...))
349 (cons (make-derivation-input path sub-drvs)
354 ;; The contents of a derivation are typically ASCII, but choosing
355 ;; UTF-8 allows us to take the fast path for Guile's `scm_getc'.
356 (set-port-encoding! drv-port "UTF-8")
358 (let loop ((exp (read drv-port))
362 (let ((result (reverse result)))
364 (('Derive ((outputs ...) (input-drvs ...)
368 ((? string? args) ...)
370 (make-derivation (outputs->alist outputs)
371 (make-input-drvs input-drvs)
374 (fold-right alist-cons '() var value)
375 (port-filename drv-port)))
377 (error "failed to parse derivation" drv-port result)))))
378 ((? (cut eq? <> comma))
379 (loop (read drv-port) result))
381 (loop (read drv-port)
382 (cons (ununquote exp) result))))))
384 (define read-derivation
385 (let ((cache (make-weak-value-hash-table 200)))
387 "Read the derivation from DRV-PORT and return the corresponding
388 <derivation> object."
389 ;; Memoize that operation because `%read-derivation' is quite expensive,
390 ;; and because the same argument is read more than 15 times on average
391 ;; during something like (package-derivation s gdb).
392 (let ((file (and=> (port-filename drv-port) basename)))
393 (or (and file (hash-ref cache file))
394 (let ((drv (%read-derivation drv-port)))
395 (hash-set! cache file drv)
398 (define-inlinable (write-sequence lst write-item port)
399 ;; Write each element of LST with WRITE-ITEM to PORT, separating them with a
404 ((prefix (... ...) last)
405 (for-each (lambda (item)
406 (write-item item port)
409 (write-item last port))))
411 (define-inlinable (write-list lst write-item port)
412 ;; Write LST as a derivation list to PORT, using WRITE-ITEM to write each
415 (write-sequence lst write-item port)
418 (define-inlinable (write-tuple lst write-item port)
419 ;; Same, but write LST as a tuple.
421 (write-sequence lst write-item port)
424 (define (write-derivation drv port)
425 "Write the ATerm-like serialization of DRV to PORT. See Section 2.4 of
426 Eelco Dolstra's PhD dissertation for an overview of a previous version of
429 ;; Make sure we're using the faster implementation.
430 (define format simple-format)
432 (define (write-string-list lst)
433 (write-list lst write port))
435 (define (coalesce-duplicate-inputs inputs)
436 ;; Return a list of inputs, such that when INPUTS contains the same DRV
437 ;; twice, they are coalesced, with their sub-derivations merged. This is
438 ;; needed because Nix itself keeps only one of them.
439 (fold (lambda (input result)
441 (($ <derivation-input> path sub-drvs)
443 (match (find (match-lambda
444 (($ <derivation-input> p s)
449 ((and dup ($ <derivation-input> _ sub-drvs2))
450 ;; Merge DUP with INPUT.
451 (let ((sub-drvs (delete-duplicates
452 (append sub-drvs sub-drvs2))))
453 (cons (make-derivation-input path sub-drvs)
454 (delq dup result))))))))
458 (define (write-output output port)
460 ((name . ($ <derivation-output> path hash-algo hash recursive?))
461 (write-tuple (list name path
463 (string-append (if recursive? "r:" "")
464 (symbol->string hash-algo))
466 (or (and=> hash bytevector->base16-string)
471 (define (write-input input port)
473 (($ <derivation-input> path sub-drvs)
477 (write-string-list (sort sub-drvs string<?))
478 (display ")" port))))
480 (define (write-env-var env-var port)
487 (display ")" port))))
489 ;; Note: lists are sorted alphabetically, to conform with the behavior of
490 ;; C++ `std::map' in Nix itself.
493 (($ <derivation> outputs inputs sources
494 system builder args env-vars)
495 (display "Derive(" port)
496 (write-list (sort outputs
498 (string<? (car o1) (car o2))))
502 (write-list (sort (coalesce-duplicate-inputs inputs)
504 (string<? (derivation-input-path i1)
505 (derivation-input-path i2))))
509 (write-string-list (sort sources string<?))
510 (format port ",~s,~s," system builder)
511 (write-string-list args)
513 (write-list (sort env-vars
515 (string<? (car e1) (car e2))))
518 (display ")" port))))
520 (define derivation->string
523 "Return the external representation of DRV as a string."
524 (with-fluids ((%default-port-encoding "UTF-8"))
525 (call-with-output-string
526 (cut write-derivation drv <>))))))
528 (define* (derivation->output-path drv #:optional (output "out"))
529 "Return the store path of its output OUTPUT. Raise a
530 '&derivation-missing-output-error' condition if OUTPUT is not an output of
532 (let ((output* (assoc-ref (derivation-outputs drv) output)))
534 (derivation-output-path output*)
535 (raise (condition (&derivation-missing-output-error
537 (output output)))))))
539 (define (derivation->output-paths drv)
540 "Return the list of name/path pairs of the outputs of DRV."
543 (cons name (derivation-output-path output))))
544 (derivation-outputs drv)))
546 (define derivation-path->output-path
547 ;; This procedure is called frequently, so memoize it.
549 (lambda* (path #:optional (output "out"))
550 "Read the derivation from PATH (`/gnu/store/xxx.drv'), and return the store
551 path of its output OUTPUT."
552 (derivation->output-path (call-with-input-file path read-derivation)
555 (define (derivation-path->output-paths path)
556 "Read the derivation from PATH (`/gnu/store/xxx.drv'), and return the
557 list of name/path pairs of its outputs."
558 (derivation->output-paths (call-with-input-file path read-derivation)))
562 ;;; Derivation primitive.
565 (define (compressed-hash bv size) ; `compressHash'
566 "Given the hash stored in BV, return a compressed version thereof that fits
568 (define new (make-bytevector size 0))
569 (define old-size (bytevector-length bv))
573 (let* ((j (modulo i size))
574 (o (bytevector-u8-ref new j)))
575 (bytevector-u8-set! new j
576 (logxor o (bytevector-u8-ref bv i)))
579 (define derivation-path->base16-hash
582 "Return a string containing the base16 representation of the hash of the
584 (call-with-input-file file
585 (compose bytevector->base16-string
589 (define derivation-hash ; `hashDerivationModulo' in derivations.cc
592 "Return the hash of DRV, modulo its fixed-output inputs, as a bytevector."
594 (($ <derivation> ((_ . ($ <derivation-output> path
595 (? symbol? hash-algo) (? bytevector? hash)
596 (? boolean? recursive?)))))
597 ;; A fixed-output derivation.
600 (string-append "fixed:out:"
601 (if recursive? "r:" "")
602 (symbol->string hash-algo)
603 ":" (bytevector->base16-string hash)
605 (($ <derivation> outputs inputs sources
606 system builder args env-vars)
607 ;; A regular derivation: replace the path of each input with that
608 ;; input's hash; return the hash of serialization of the resulting
610 (let* ((inputs (map (match-lambda
611 (($ <derivation-input> path sub-drvs)
612 (let ((hash (derivation-path->base16-hash path)))
613 (make-derivation-input hash sub-drvs))))
615 (drv (make-derivation outputs inputs sources
616 system builder args env-vars
619 ;; XXX: At this point this remains faster than `port-sha256', because
620 ;; the SHA256 port's `write' method gets called for every single
623 (string->utf8 (derivation->string drv)))))))))
625 (define (store-path type hash name) ; makeStorePath
626 "Return the store path for NAME/HASH/TYPE."
627 (let* ((s (string-append type ":sha256:"
628 (bytevector->base16-string hash) ":"
629 (%store-prefix) ":" name))
630 (h (sha256 (string->utf8 s)))
631 (c (compressed-hash h 20)))
632 (string-append (%store-prefix) "/"
633 (bytevector->nix-base32-string c) "-"
636 (define (output-path output hash name) ; makeOutputPath
637 "Return an output path for OUTPUT (the name of the output as a string) of
638 the derivation called NAME with hash HASH."
639 (store-path (string-append "output:" output) hash
640 (if (string=? output "out")
642 (string-append name "-" output))))
644 (define (fixed-output-path output hash-algo hash recursive? name)
645 "Return an output path for the fixed output OUTPUT defined by HASH of type
646 HASH-ALGO, of the derivation NAME. RECURSIVE? has the same meaning as for
648 (if (and recursive? (eq? hash-algo 'sha256))
649 (store-path "source" hash name)
650 (let ((tag (string-append "fixed:" output ":"
651 (if recursive? "r:" "")
652 (symbol->string hash-algo) ":"
653 (bytevector->base16-string hash) ":")))
654 (store-path (string-append "output:" output)
655 (sha256 (string->utf8 tag))
658 (define* (derivation store name builder args
660 (system (%current-system)) (env-vars '())
661 (inputs '()) (outputs '("out"))
662 hash hash-algo recursive?
663 references-graphs allowed-references
665 "Build a derivation with the given arguments, and return the resulting
666 <derivation> object. When HASH and HASH-ALGO are given, a
667 fixed-output derivation is created---i.e., one whose result is known in
668 advance, such as a file download. If, in addition, RECURSIVE? is true, then
669 that fixed output may be an executable file or a directory and HASH must be
670 the hash of an archive containing this output.
672 When REFERENCES-GRAPHS is true, it must be a list of file name/store path
673 pairs. In that case, the reference graph of each store path is exported in
674 the build environment in the corresponding file, in a simple text format.
676 When ALLOWED-REFERENCES is true, it must be a list of store items or outputs
677 that the derivation's output may refer to.
679 When LOCAL-BUILD? is true, declare that the derivation is not a good candidate
680 for offloading and should rather be built locally. This is the case for small
681 derivations where the costs of data transfers would outweigh the benefits."
682 (define (add-output-paths drv)
683 ;; Return DRV with an actual store path for each of its output and the
684 ;; corresponding environment variable.
686 (($ <derivation> outputs inputs sources
687 system builder args env-vars)
688 (let* ((drv-hash (derivation-hash drv))
689 (outputs (map (match-lambda
690 ((output-name . ($ <derivation-output>
693 (fixed-output-path output-name
696 (output-path output-name
699 (make-derivation-output path algo
702 (make-derivation outputs inputs sources system builder args
706 (or (and=> (assoc-ref outputs name)
707 derivation-output-path)
712 (define (user+system-env-vars)
713 ;; Some options are passed to the build daemon via the env. vars of
714 ;; derivations (urgh!). We hide that from our API, but here is the place
715 ;; where we kludgify those options.
716 (let ((env-vars `(,@(if local-build?
717 `(("preferLocalBuild" . "1"))
719 ,@(if allowed-references
720 `(("allowedReferences"
721 . ,(string-join allowed-references)))
724 (match references-graphs
726 (let ((value (map (cut string-append <> " " <>)
728 ;; XXX: This all breaks down if an element of FILE or PATH contains
730 `(("exportReferencesGraph" . ,(string-join value " "))
735 (define (env-vars-with-empty-outputs env-vars)
736 ;; Return a variant of ENV-VARS where each OUTPUTS is associated with an
737 ;; empty string, even outputs that do not appear in ENV-VARS.
738 (let ((e (map (match-lambda
740 (if (member name outputs)
744 (fold (lambda (output-name env-vars)
745 (if (assoc output-name env-vars)
747 (append env-vars `((,output-name . "")))))
751 (define (set-file-name drv file)
752 ;; Set FILE as the 'file-name' field of DRV.
754 (($ <derivation> outputs inputs sources system builder
756 (make-derivation outputs inputs sources system builder
757 args env-vars file))))
759 (let* ((outputs (map (lambda (name)
760 ;; Return outputs with an empty path.
762 (make-derivation-output "" hash-algo
765 (inputs (map (match-lambda
766 (((? derivation? drv))
767 (make-derivation-input (derivation-file-name drv)
769 (((? derivation? drv) sub-drvs ...)
770 (make-derivation-input (derivation-file-name drv)
772 (((? direct-store-path? input))
773 (make-derivation-input input '("out")))
774 (((? direct-store-path? input) sub-drvs ...)
775 (make-derivation-input input sub-drvs))
777 (let ((path (add-to-store store
780 (make-derivation-input path '()))))
781 (delete-duplicates inputs)))
782 (env-vars (env-vars-with-empty-outputs (user+system-env-vars)))
783 (drv-masked (make-derivation outputs
784 (filter (compose derivation-path?
785 derivation-input-path)
787 (filter-map (lambda (i)
788 (let ((p (derivation-input-path i)))
789 (and (not (derivation-path? p))
792 system builder args env-vars #f))
793 (drv (add-output-paths drv-masked)))
795 (let ((file (add-text-to-store store (string-append name ".drv")
796 (derivation->string drv)
797 (map derivation-input-path
799 (set-file-name drv file))))
801 (define* (map-derivation store drv mapping
802 #:key (system (%current-system)))
803 "Given MAPPING, a list of pairs of derivations, return a derivation based on
804 DRV where all the 'car's of MAPPING have been replaced by its 'cdr's,
806 (define (substitute str initial replacements)
807 (fold (lambda (path replacement result)
808 (string-replace-substring result path
811 initial replacements))
813 (define (substitute-file file initial replacements)
815 (with-fluids ((%default-port-encoding #f))
816 (call-with-input-file file get-string-all)))
818 (let ((updated (substitute contents initial replacements)))
819 (if (string=? updated contents)
821 ;; XXX: permissions aren't preserved.
822 (add-text-to-store store (store-path-package-name file)
825 (define input->output-paths
827 (((? derivation? drv))
828 (list (derivation->output-path drv)))
829 (((? derivation? drv) sub-drvs ...)
830 (map (cut derivation->output-path drv <>)
835 (let ((mapping (fold (lambda (pair result)
837 (((? derivation? orig) . replacement)
838 (vhash-cons (derivation-file-name orig)
840 ((file . replacement)
841 (vhash-cons file replacement result))))
844 (define rewritten-input
845 ;; Rewrite the given input according to MAPPING, and return an input
846 ;; in the format used in 'derivation' calls.
850 (($ <derivation-input> path (sub-drvs ...))
851 (match (vhash-assoc path mapping)
852 ((_ . (? derivation? replacement))
853 (cons replacement sub-drvs))
857 (let* ((drv (loop (call-with-input-file path read-derivation))))
858 (cons drv sub-drvs)))))))))
860 (let loop ((drv drv))
861 (let* ((inputs (map (cut rewritten-input <> loop)
862 (derivation-inputs drv)))
863 (initial (append-map derivation-input-output-paths
864 (derivation-inputs drv)))
865 (replacements (append-map input->output-paths inputs))
867 ;; Sources typically refer to the output directories of the
868 ;; original inputs, INITIAL. Rewrite them by substituting
870 (sources (map (lambda (source)
871 (match (vhash-assoc source mapping)
875 (substitute-file source
876 initial replacements))))
877 (derivation-sources drv)))
879 ;; Now augment the lists of initials and replacements.
880 (initial (append (derivation-sources drv) initial))
881 (replacements (append sources replacements))
882 (name (store-path-package-name
883 (string-drop-right (derivation-file-name drv)
885 (derivation store name
886 (substitute (derivation-builder drv)
887 initial replacements)
888 (map (cut substitute <> initial replacements)
889 (derivation-builder-arguments drv))
891 #:env-vars (map (match-lambda
894 . ,(substitute value initial
896 (derivation-builder-environment-vars drv))
897 #:inputs (append (map list sources) inputs)
898 #:outputs (derivation-output-names drv)
899 #:hash (match (derivation-outputs drv)
900 ((($ <derivation-output> _ algo hash))
903 #:hash-algo (match (derivation-outputs drv)
904 ((($ <derivation-output> _ algo hash))
910 ;;; Store compatibility layer.
913 (define (build-derivations store derivations)
914 "Build DERIVATIONS, a list of <derivation> objects or .drv file names."
915 (build-things store (map (match-lambda
916 ((? string? file) file)
917 ((and drv ($ <derivation>))
918 (derivation-file-name drv)))
923 ;;; Guile-based builders.
926 (define (parent-directories file-name)
927 "Return the list of parent dirs of FILE-NAME, in the order in which an
928 `mkdir -p' implementation would make them."
929 (let ((not-slash (char-set-complement (char-set #\/))))
931 (fold (lambda (dir result)
936 (cons (string-append prev "/" dir)
939 (remove (cut string=? <> ".")
940 (string-tokenize (dirname file-name) not-slash))))))
942 (define* (imported-files store files ;deprecated
943 #:key (name "file-import")
944 (system (%current-system))
945 (guile (%guile-for-build)))
946 "Return a derivation that imports FILES into STORE. FILES must be a list
947 of (FINAL-PATH . FILE-NAME) pairs; each FILE-NAME is read from the file
948 system, imported, and appears under FINAL-PATH in the resulting store path."
949 (let* ((files (map (match-lambda
950 ((final-path . file-name)
952 (add-to-store store (basename final-path) #f
953 "sha256" file-name))))
957 (mkdir %output) (chdir %output)
958 ,@(append-map (match-lambda
959 ((final-path store-path)
960 (append (match (parent-directories final-path)
963 (append (map (lambda (d)
967 `((or (file-exists? ,tail)
969 `((symlink ,store-path ,final-path)))))
971 (build-expression->derivation store name builder
974 #:guile-for-build guile
978 ;; A memoizing version of 'search-path' so 'imported-modules' does not end
979 ;; up looking for the same files over and over again.
980 (memoize search-path))
982 (define* (%imported-modules store modules ;deprecated
983 #:key (name "module-import")
984 (system (%current-system))
985 (guile (%guile-for-build))
986 (module-path %load-path))
987 "Return a derivation that contains the source files of MODULES, a list of
988 module names such as `(ice-9 q)'. All of MODULES must be in the MODULE-PATH
990 ;; TODO: Determine the closure of MODULES, build the `.go' files,
991 ;; canonicalize the source files through read/write, etc.
992 (let ((files (map (lambda (m)
993 (let ((f (string-append
994 (string-join (map symbol->string m) "/")
996 (cons f (search-path* module-path f))))
998 (imported-files store files #:name name #:system system
1001 (define* (%compiled-modules store modules ;deprecated
1002 #:key (name "module-import-compiled")
1003 (system (%current-system))
1004 (guile (%guile-for-build))
1005 (module-path %load-path))
1006 "Return a derivation that builds a tree containing the `.go' files
1007 corresponding to MODULES. All the MODULES are built in a context where
1008 they can refer to each other."
1009 (let* ((module-drv (%imported-modules store modules
1012 #:module-path module-path))
1013 (module-dir (derivation->output-path module-drv))
1014 (files (map (lambda (m)
1015 (let ((f (string-join (map symbol->string m)
1017 (cons (string-append f ".go")
1018 (string-append module-dir "/" f ".scm"))))
1022 (use-modules (system base compile))
1023 (let ((out (assoc-ref %outputs "out")))
1028 (cons ,module-dir %load-path))
1030 ,@(map (match-lambda
1032 (let ((make-parent-dirs (map (lambda (dir)
1033 `(unless (file-exists? ,dir)
1035 (parent-directories output))))
1038 (compile-file ,input
1039 #:output-file ,output
1040 #:opts %auto-compilation-options)))))
1043 (build-expression->derivation store name builder
1044 #:inputs `(("modules" ,module-drv))
1046 #:guile-for-build guile
1047 #:local-build? #t)))
1049 (define-record-type* <graft> graft make-graft
1051 (origin graft-origin) ;derivation | store item
1052 (origin-output graft-origin-output ;string | #f
1054 (replacement graft-replacement) ;derivation | store item
1055 (replacement-output graft-replacement-output ;string | #f
1058 (define* (graft-derivation store name drv grafts
1059 #:key (guile (%guile-for-build))
1060 (system (%current-system)))
1061 "Return a derivation called NAME, based on DRV but with all the GRAFTS
1063 ;; XXX: Someday rewrite using gexps.
1065 ;; List of store item pairs.
1067 (($ <graft> source source-output target target-output)
1068 (cons (if (derivation? source)
1069 (derivation->output-path source source-output)
1071 (if (derivation? target)
1072 (derivation->output-path target target-output)
1077 (match (derivation-outputs drv)
1078 (((names . outputs) ...)
1079 (map derivation-output-path outputs))))
1081 (define output-names
1082 (match (derivation-outputs drv)
1083 (((names . outputs) ...)
1088 (use-modules (guix build graft)
1092 (let ((mapping ',mapping))
1093 (for-each (lambda (input output)
1094 (format #t "grafting '~a' -> '~a'...~%" input output)
1096 (rewrite-directory input output
1097 `((,input . ,output)
1101 (((names . files) ...)
1108 ((($ <graft> sources source-outputs targets target-outputs) ...)
1109 (let ((sources (zip sources source-outputs))
1110 (targets (zip targets target-outputs)))
1111 (build-expression->derivation store name build
1113 #:guile-for-build guile
1114 #:modules '((guix build graft)
1116 #:inputs `(,@(map (lambda (out)
1119 ,@(append (map add-label sources)
1120 (map add-label targets)))
1121 #:outputs output-names
1122 #:local-build? #t)))))
1124 (define* (build-expression->derivation store name exp ;deprecated
1126 (system (%current-system))
1129 hash hash-algo recursive?
1136 "Return a derivation that executes Scheme expression EXP as a builder
1137 for derivation NAME. INPUTS must be a list of (NAME DRV-PATH SUB-DRV)
1138 tuples; when SUB-DRV is omitted, \"out\" is assumed. MODULES is a list
1139 of names of Guile modules from the current search path to be copied in
1140 the store, compiled, and made available in the load path during the
1143 EXP is evaluated in an environment where %OUTPUT is bound to the main
1144 output path, %OUTPUTS is bound to a list of output/path pairs, and where
1145 %BUILD-INPUTS is bound to an alist of string/output-path pairs made from
1146 INPUTS. Optionally, ENV-VARS is a list of string pairs specifying the
1147 name and value of environment variables visible to the builder. The
1148 builder terminates by passing the result of EXP to `exit'; thus, when
1149 EXP returns #f, the build is considered to have failed.
1151 EXP is built using GUILE-FOR-BUILD (a derivation). When GUILE-FOR-BUILD is
1152 omitted or is #f, the value of the `%guile-for-build' fluid is used instead.
1154 See the `derivation' procedure for the meaning of REFERENCES-GRAPHS,
1155 ALLOWED-REFERENCES, and LOCAL-BUILD?."
1157 (or guile-for-build (%guile-for-build)))
1160 (string-append (derivation->output-path guile-drv)
1163 (define module-form?
1165 (((or 'define-module 'use-modules) _ ...) #t)
1169 ;; When passed an input that is a source, return its path; otherwise
1172 ((_ (? derivation?) _ ...)
1175 (and (not (derivation-path? path))
1178 (let* ((prologue `(begin
1181 ;; Module forms must appear at the top-level so
1182 ;; that any macros they export can be expanded.
1183 (filter module-form? exp))
1186 (define %output (getenv "out"))
1189 (cons o (getenv o)))
1191 (define %build-inputs
1192 ',(map (match-lambda
1194 (let ((sub (match rest
1200 (derivation->output-path drv sub))
1201 ((derivation-path? drv)
1202 (derivation-path->output-path drv
1207 ,@(if (null? modules)
1209 ;; Remove our own settings.
1210 '((unsetenv "GUILE_LOAD_COMPILED_PATH")))
1212 ;; Guile sets it, but remove it to avoid conflicts when
1213 ;; building Guile-using packages.
1214 (unsetenv "LD_LIBRARY_PATH")))
1215 (builder (add-text-to-store store
1216 (string-append name "-guile-builder")
1218 ;; Explicitly use UTF-8 for determinism,
1219 ;; and also because UTF-8 output is faster.
1220 (with-fluids ((%default-port-encoding
1222 (call-with-output-string
1224 (write prologue port)
1229 (remove module-form? exp))
1233 ;; The references don't really matter
1234 ;; since the builder is always used in
1235 ;; conjunction with the drv that needs
1236 ;; it. For clarity, we add references
1237 ;; to the subset of INPUTS that are
1238 ;; sources, avoiding references to other
1239 ;; .drv; otherwise, BUILDER's hash would
1240 ;; depend on those, even if they are
1242 (filter-map source-path inputs)))
1244 (mod-drv (and (pair? modules)
1245 (%imported-modules store modules
1248 (mod-dir (and mod-drv
1249 (derivation->output-path mod-drv)))
1250 (go-drv (and (pair? modules)
1251 (%compiled-modules store modules
1255 (derivation->output-path go-drv))))
1256 (derivation store name guile
1257 `("--no-auto-compile"
1258 ,@(if mod-dir `("-L" ,mod-dir) '())
1263 #:inputs `((,(or guile-for-build (%guile-for-build)))
1266 ,@(if mod-drv `((,mod-drv) (,go-drv)) '()))
1268 ;; When MODULES is non-empty, shamelessly clobber
1269 ;; $GUILE_LOAD_COMPILED_PATH.
1270 #:env-vars (if go-dir
1271 `(("GUILE_LOAD_COMPILED_PATH" . ,go-dir)
1272 ,@(alist-delete "GUILE_LOAD_COMPILED_PATH"
1276 #:hash hash #:hash-algo hash-algo
1277 #:recursive? recursive?
1279 #:references-graphs references-graphs
1280 #:allowed-references allowed-references
1281 #:local-build? local-build?)))
1285 ;;; Monadic interface.
1288 (define built-derivations
1289 (store-lift build-derivations))