1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016, 2017 Mathieu Lirzin <mthl@gnu.org>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (guix derivations)
21 #:use-module (srfi srfi-1)
22 #:use-module (srfi srfi-9)
23 #:use-module (srfi srfi-9 gnu)
24 #:use-module (srfi srfi-26)
25 #:use-module (srfi srfi-34)
26 #:use-module (srfi srfi-35)
27 #:use-module (ice-9 binary-ports)
28 #:use-module (rnrs bytevectors)
29 #:use-module (ice-9 match)
30 #:use-module (ice-9 rdelim)
31 #:use-module (ice-9 vlist)
32 #:use-module (guix store)
33 #:use-module (guix utils)
34 #:use-module (guix memoization)
35 #:use-module (guix combinators)
36 #:use-module (guix monads)
37 #:use-module (guix hash)
38 #:use-module (guix base32)
39 #:use-module (guix records)
40 #:use-module (guix sets)
41 #:export (<derivation>
48 derivation-builder-arguments
49 derivation-builder-environment-vars
51 derivation-prerequisites
52 derivation-prerequisites-to-build
56 derivation-output-path
57 derivation-output-hash-algo
58 derivation-output-hash
59 derivation-output-recursive?
64 derivation-input-sub-derivations
65 derivation-input-output-paths
66 valid-derivation-input?
70 derivation-error-derivation
71 &derivation-missing-output-error
72 derivation-missing-output-error?
73 derivation-missing-output
76 derivation-output-names
77 fixed-output-derivation?
79 offloadable-derivation?
80 substitutable-derivation?
86 derivation->output-path
87 derivation->output-paths
88 derivation-path->output-path
89 derivation-path->output-paths
99 file-search-error-file-name
100 file-search-error-search-path
103 module->source-file-name
104 build-expression->derivation)
106 ;; Re-export it from here for backward compatibility.
107 #:re-export (%guile-for-build))
110 ;;; Error conditions.
113 (define-condition-type &derivation-error &nix-error
115 (derivation derivation-error-derivation))
117 (define-condition-type &derivation-missing-output-error &derivation-error
118 derivation-missing-output-error?
119 (output derivation-missing-output))
122 ;;; Nix derivations, as implemented in Nix's `derivations.cc'.
125 (define-immutable-record-type <derivation>
126 (make-derivation outputs inputs sources system builder args env-vars
129 (outputs derivation-outputs) ; list of name/<derivation-output> pairs
130 (inputs derivation-inputs) ; list of <derivation-input>
131 (sources derivation-sources) ; list of store paths
132 (system derivation-system) ; string
133 (builder derivation-builder) ; store path
134 (args derivation-builder-arguments) ; list of strings
135 (env-vars derivation-builder-environment-vars) ; list of name/value pairs
136 (file-name derivation-file-name)) ; the .drv file name
138 (define-record-type <derivation-output>
139 (make-derivation-output path hash-algo hash recursive?)
141 (path derivation-output-path) ; store path
142 (hash-algo derivation-output-hash-algo) ; symbol | #f
143 (hash derivation-output-hash) ; bytevector | #f
144 (recursive? derivation-output-recursive?)) ; Boolean
146 (define-record-type <derivation-input>
147 (make-derivation-input path sub-derivations)
149 (path derivation-input-path) ; store path
150 (sub-derivations derivation-input-sub-derivations)) ; list of strings
152 (set-record-type-printer! <derivation>
154 (format port "#<derivation ~a => ~a ~a>"
155 (derivation-file-name drv)
159 (derivation-output-path output)))
160 (derivation-outputs drv)))
161 (number->string (object-address drv) 16))))
163 (define (derivation-name drv)
164 "Return the base name of DRV."
165 (let ((base (store-path-package-name (derivation-file-name drv))))
166 (string-drop-right base 4)))
168 (define (derivation-output-names drv)
169 "Return the names of the outputs of DRV."
170 (match (derivation-outputs drv)
174 (define (fixed-output-derivation? drv)
175 "Return #t if DRV is a fixed-output derivation, such as the result of a
176 download with a fixed hash (aka. `fetchurl')."
179 (("out" . ($ <derivation-output> _ (? symbol?) (? bytevector?)))))
183 (define (derivation-input<? input1 input2)
184 "Compare INPUT1 and INPUT2, two <derivation-input>."
185 (string<? (derivation-input-path input1)
186 (derivation-input-path input2)))
188 (define (derivation-input-output-paths input)
189 "Return the list of output paths corresponding to INPUT, a
192 (($ <derivation-input> path sub-drvs)
193 (map (cut derivation-path->output-path path <>)
196 (define (valid-derivation-input? store input)
197 "Return true if INPUT is valid--i.e., if all the outputs it requests are in
199 (every (cut valid-path? store <>)
200 (derivation-input-output-paths input)))
202 (define (coalesce-duplicate-inputs inputs)
203 "Return a list of inputs, such that when INPUTS contains the same DRV twice,
204 they are coalesced, with their sub-derivations merged. This is needed because
205 Nix itself keeps only one of them."
206 (fold (lambda (input result)
208 (($ <derivation-input> path sub-drvs)
210 (match (find (match-lambda
211 (($ <derivation-input> p s)
216 ((and dup ($ <derivation-input> _ sub-drvs2))
217 ;; Merge DUP with INPUT.
218 (let ((sub-drvs (delete-duplicates
219 (append sub-drvs sub-drvs2))))
220 (cons (make-derivation-input path
221 (sort sub-drvs string<?))
222 (delq dup result))))))))
226 (define* (derivation-prerequisites drv #:optional (cut? (const #f)))
227 "Return the list of derivation-inputs required to build DRV, recursively.
229 CUT? is a predicate that is passed a derivation-input and returns true to
230 eliminate the given input and its dependencies from the search. An example of
231 search a predicate is 'valid-derivation-input?'; when it is used as CUT?, the
232 result is the set of prerequisites of DRV not already in valid."
236 (let ((inputs (remove (lambda (input)
237 (or (set-contains? input-set input)
239 (derivation-inputs drv))))
241 (append inputs result)
242 (fold set-insert input-set inputs)
244 (call-with-input-file (derivation-input-path i)
248 (define (offloadable-derivation? drv)
249 "Return true if DRV can be offloaded, false otherwise."
250 (match (assoc "preferLocalBuild"
251 (derivation-builder-environment-vars drv))
252 (("preferLocalBuild" . "1") #f)
255 (define (substitutable-derivation? drv)
256 "Return #t if DRV can be substituted."
257 (match (assoc "allowSubstitutes"
258 (derivation-builder-environment-vars drv))
259 (("allowSubstitutes" . value)
260 (string=? value "1"))
263 (define (derivation-output-paths drv sub-drvs)
264 "Return the output paths of outputs SUB-DRVS of DRV."
266 (($ <derivation> outputs)
267 (map (lambda (sub-drv)
268 (derivation-output-path (assoc-ref outputs sub-drv)))
271 (define* (substitution-oracle store drv
272 #:key (mode (build-mode normal)))
273 "Return a one-argument procedure that, when passed a store file name,
274 returns #t if it's substitutable and #f otherwise. The returned procedure
275 knows about all substitutes for all the derivations listed in DRV, *except*
276 those that are already valid (that is, it won't bother checking whether an
277 item is substitutable if it's already on disk); it also knows about their
278 prerequisites, unless they are themselves substitutable.
280 Creating a single oracle (thus making a single 'substitutable-paths' call) and
281 reusing it is much more efficient than calling 'has-substitutes?' or similar
282 repeatedly, because it avoids the costs associated with launching the
283 substituter many times."
285 (cut valid-path? store <>))
288 (cut valid-derivation-input? store <>))
290 (define (dependencies drv)
291 ;; Skip prerequisite sub-trees of DRV whose root is valid. This allows us
292 ;; to ask the substituter for just as much as needed, instead of asking it
293 ;; for the whole world, which can be significantly faster when substitute
294 ;; info is not already in cache.
295 (append-map derivation-input-output-paths
296 (derivation-prerequisites drv valid-input?)))
298 (let* ((paths (delete-duplicates
300 (fold (lambda (drv result)
301 (let ((self (match (derivation->output-paths drv)
302 (((names . paths) ...)
304 (cond ((eqv? mode (build-mode check))
305 (cons (dependencies drv) result))
309 (cons* self (dependencies drv) result)))))
312 (subst (list->set (substitutable-paths store paths))))
313 (cut set-contains? subst <>)))
315 (define* (derivation-prerequisites-to-build store drv
317 (mode (build-mode normal))
319 (derivation-output-names drv))
321 (substitution-oracle store
324 "Return two values: the list of derivation-inputs required to build the
325 OUTPUTS of DRV and not already available in STORE, recursively, and the list
326 of required store paths that can be substituted. SUBSTITUTABLE? must be a
327 one-argument procedure similar to that returned by 'substitution-oracle'."
329 (cut valid-path? store <>))
332 (compose (cut any built? <>) derivation-input-output-paths))
334 (define input-substitutable?
335 ;; Return true if and only if all of SUB-DRVS are subsitutable. If at
336 ;; least one is missing, then everything must be rebuilt.
337 (compose (cut every substitutable? <>) derivation-input-output-paths))
339 (define (derivation-built? drv* sub-drvs)
340 ;; In 'check' mode, assume that DRV is not built.
341 (and (not (and (eqv? mode (build-mode check))
343 (every built? (derivation-output-paths drv* sub-drvs))))
345 (define (derivation-substitutable? drv sub-drvs)
346 (and (substitutable-derivation? drv)
347 (every substitutable? (derivation-output-paths drv sub-drvs))))
353 (cond ((derivation-built? drv sub-drvs)
354 (values build substitute))
355 ((derivation-substitutable? drv sub-drvs)
357 (append (derivation-output-paths drv sub-drvs)
360 (let ((build (if (substitutable-derivation? drv)
362 (cons (make-derivation-input
363 (derivation-file-name drv) sub-drvs)
365 (inputs (remove (lambda (i)
366 (or (member i build) ; XXX: quadratic
368 (input-substitutable? i)))
369 (derivation-inputs drv))))
371 (append inputs build)
372 (append (append-map (lambda (input)
373 (if (and (not (input-built? input))
374 (input-substitutable? input))
375 (derivation-input-output-paths
378 (derivation-inputs drv))
381 (call-with-input-file (derivation-input-path i)
384 (map derivation-input-sub-derivations inputs)))))))
386 (define (%read-derivation drv-port)
387 ;; Actually read derivation from DRV-PORT.
389 (define comma (string->symbol ","))
391 (define (ununquote x)
393 (('unquote x) (ununquote x))
394 ((x ...) (map ununquote x))
397 (define (outputs->alist x)
398 (fold-right (lambda (output result)
402 (make-derivation-output path #f #f #f)
404 ((name path hash-algo hash)
406 (let* ((rec? (string-prefix? "r:" hash-algo))
407 (algo (string->symbol
409 (string-drop hash-algo 2)
411 (hash (base16-string->bytevector hash)))
413 (make-derivation-output path algo
419 (define (make-input-drvs x)
420 (fold-right (lambda (input result)
422 ((path (sub-drvs ...))
423 (cons (make-derivation-input path sub-drvs)
428 ;; The contents of a derivation are typically ASCII, but choosing
429 ;; UTF-8 allows us to take the fast path for Guile's `scm_getc'.
430 (set-port-encoding! drv-port "UTF-8")
432 (let loop ((exp (read drv-port))
436 (let ((result (reverse result)))
438 (('Derive ((outputs ...) (input-drvs ...)
442 ((? string? args) ...)
444 (make-derivation (outputs->alist outputs)
445 (make-input-drvs input-drvs)
448 (fold-right alist-cons '() var value)
449 (port-filename drv-port)))
451 (error "failed to parse derivation" drv-port result)))))
452 ((? (cut eq? <> comma))
453 (loop (read drv-port) result))
455 (loop (read drv-port)
456 (cons (ununquote exp) result))))))
458 (define %derivation-cache
459 ;; Maps derivation file names to <derivation> objects.
460 ;; XXX: This is redundant with 'atts-cache' in the store.
461 (make-weak-value-hash-table 200))
463 (define (read-derivation drv-port)
464 "Read the derivation from DRV-PORT and return the corresponding
465 <derivation> object."
466 ;; Memoize that operation because `%read-derivation' is quite expensive,
467 ;; and because the same argument is read more than 15 times on average
468 ;; during something like (package-derivation s gdb).
469 (let ((file (port-filename drv-port)))
470 (or (and file (hash-ref %derivation-cache file))
471 (let ((drv (%read-derivation drv-port)))
472 (hash-set! %derivation-cache file drv)
475 (define-inlinable (write-sequence lst write-item port)
476 ;; Write each element of LST with WRITE-ITEM to PORT, separating them with a
481 ((prefix (... ...) last)
482 (for-each (lambda (item)
483 (write-item item port)
486 (write-item last port))))
488 (define-inlinable (write-list lst write-item port)
489 ;; Write LST as a derivation list to PORT, using WRITE-ITEM to write each
492 (write-sequence lst write-item port)
495 (define-inlinable (write-tuple lst write-item port)
496 ;; Same, but write LST as a tuple.
498 (write-sequence lst write-item port)
501 (define (write-derivation drv port)
502 "Write the ATerm-like serialization of DRV to PORT. See Section 2.4 of
503 Eelco Dolstra's PhD dissertation for an overview of a previous version of
506 ;; Make sure we're using the faster implementation.
507 (define format simple-format)
509 (define (write-string-list lst)
510 (write-list lst write port))
512 (define (write-output output port)
514 ((name . ($ <derivation-output> path hash-algo hash recursive?))
515 (write-tuple (list name path
517 (string-append (if recursive? "r:" "")
518 (symbol->string hash-algo))
520 (or (and=> hash bytevector->base16-string)
525 (define (write-input input port)
527 (($ <derivation-input> path sub-drvs)
531 (write-string-list sub-drvs)
532 (display ")" port))))
534 (define (write-env-var env-var port)
541 (display ")" port))))
543 ;; Assume all the lists we are writing are already sorted.
545 (($ <derivation> outputs inputs sources
546 system builder args env-vars)
547 (display "Derive(" port)
548 (write-list outputs write-output port)
550 (write-list inputs write-input port)
552 (write-string-list sources)
553 (simple-format port ",\"~a\",\"~a\"," system builder)
554 (write-string-list args)
556 (write-list env-vars write-env-var port)
557 (display ")" port))))
559 (define derivation->string
561 "Return the external representation of DRV as a string."
562 (with-fluids ((%default-port-encoding "UTF-8"))
563 (call-with-output-string
564 (cut write-derivation drv <>)))))
566 (define* (derivation->output-path drv #:optional (output "out"))
567 "Return the store path of its output OUTPUT. Raise a
568 '&derivation-missing-output-error' condition if OUTPUT is not an output of
570 (let ((output* (assoc-ref (derivation-outputs drv) output)))
572 (derivation-output-path output*)
573 (raise (condition (&derivation-missing-output-error
575 (output output)))))))
577 (define (derivation->output-paths drv)
578 "Return the list of name/path pairs of the outputs of DRV."
581 (cons name (derivation-output-path output))))
582 (derivation-outputs drv)))
584 (define derivation-path->output-path
585 ;; This procedure is called frequently, so memoize it.
586 (let ((memoized (mlambda (path output)
587 (derivation->output-path (call-with-input-file path
590 (lambda* (path #:optional (output "out"))
591 "Read the derivation from PATH (`/gnu/store/xxx.drv'), and return the store
592 path of its output OUTPUT."
593 (memoized path output))))
595 (define (derivation-path->output-paths path)
596 "Read the derivation from PATH (`/gnu/store/xxx.drv'), and return the
597 list of name/path pairs of its outputs."
598 (derivation->output-paths (call-with-input-file path read-derivation)))
602 ;;; Derivation primitive.
605 (define (compressed-hash bv size) ; `compressHash'
606 "Given the hash stored in BV, return a compressed version thereof that fits
608 (define new (make-bytevector size 0))
609 (define old-size (bytevector-length bv))
613 (let* ((j (modulo i size))
614 (o (bytevector-u8-ref new j)))
615 (bytevector-u8-set! new j
616 (logxor o (bytevector-u8-ref bv i)))
619 (define derivation-path->base16-hash
621 "Return a string containing the base16 representation of the hash of the
623 (call-with-input-file file
624 (compose bytevector->base16-string
628 (define derivation-hash ; `hashDerivationModulo' in derivations.cc
630 "Return the hash of DRV, modulo its fixed-output inputs, as a bytevector."
632 (($ <derivation> ((_ . ($ <derivation-output> path
633 (? symbol? hash-algo) (? bytevector? hash)
634 (? boolean? recursive?)))))
635 ;; A fixed-output derivation.
638 (string-append "fixed:out:"
639 (if recursive? "r:" "")
640 (symbol->string hash-algo)
641 ":" (bytevector->base16-string hash)
643 (($ <derivation> outputs inputs sources
644 system builder args env-vars)
645 ;; A regular derivation: replace the path of each input with that
646 ;; input's hash; return the hash of serialization of the resulting
648 (let* ((inputs (map (match-lambda
649 (($ <derivation-input> path sub-drvs)
650 (let ((hash (derivation-path->base16-hash path)))
651 (make-derivation-input hash sub-drvs))))
653 (drv (make-derivation outputs
654 (sort (coalesce-duplicate-inputs inputs)
657 system builder args env-vars
660 ;; XXX: At this point this remains faster than `port-sha256', because
661 ;; the SHA256 port's `write' method gets called for every single
664 (string->utf8 (derivation->string drv))))))))
666 (define (store-path type hash name) ; makeStorePath
667 "Return the store path for NAME/HASH/TYPE."
668 (let* ((s (string-append type ":sha256:"
669 (bytevector->base16-string hash) ":"
670 (%store-prefix) ":" name))
671 (h (sha256 (string->utf8 s)))
672 (c (compressed-hash h 20)))
673 (string-append (%store-prefix) "/"
674 (bytevector->nix-base32-string c) "-"
677 (define (output-path output hash name) ; makeOutputPath
678 "Return an output path for OUTPUT (the name of the output as a string) of
679 the derivation called NAME with hash HASH."
680 (store-path (string-append "output:" output) hash
681 (if (string=? output "out")
683 (string-append name "-" output))))
685 (define* (fixed-output-path name hash
690 "Return an output path for the fixed output OUTPUT defined by HASH of type
691 HASH-ALGO, of the derivation NAME. RECURSIVE? has the same meaning as for
693 (if (and recursive? (eq? hash-algo 'sha256))
694 (store-path "source" hash name)
695 (let ((tag (string-append "fixed:" output ":"
696 (if recursive? "r:" "")
697 (symbol->string hash-algo) ":"
698 (bytevector->base16-string hash) ":")))
699 (store-path (string-append "output:" output)
700 (sha256 (string->utf8 tag))
703 (define* (derivation store name builder args
705 (system (%current-system)) (env-vars '())
706 (inputs '()) (outputs '("out"))
707 hash hash-algo recursive?
709 allowed-references disallowed-references
710 leaked-env-vars local-build?
712 "Build a derivation with the given arguments, and return the resulting
713 <derivation> object. When HASH and HASH-ALGO are given, a
714 fixed-output derivation is created---i.e., one whose result is known in
715 advance, such as a file download. If, in addition, RECURSIVE? is true, then
716 that fixed output may be an executable file or a directory and HASH must be
717 the hash of an archive containing this output.
719 When REFERENCES-GRAPHS is true, it must be a list of file name/store path
720 pairs. In that case, the reference graph of each store path is exported in
721 the build environment in the corresponding file, in a simple text format.
723 When ALLOWED-REFERENCES is true, it must be a list of store items or outputs
724 that the derivation's outputs may refer to. Likewise, DISALLOWED-REFERENCES,
725 if true, must be a list of things the outputs may not refer to.
727 When LEAKED-ENV-VARS is true, it must be a list of strings denoting
728 environment variables that are allowed to \"leak\" from the daemon's
729 environment to the build environment. This is only applicable to fixed-output
730 derivations--i.e., when HASH is true. The main use is to allow variables such
731 as \"http_proxy\" to be passed to derivations that download files.
733 When LOCAL-BUILD? is true, declare that the derivation is not a good candidate
734 for offloading and should rather be built locally. This is the case for small
735 derivations where the costs of data transfers would outweigh the benefits.
737 When SUBSTITUTABLE? is false, declare that substitutes of the derivation's
738 output should not be used."
739 (define (add-output-paths drv)
740 ;; Return DRV with an actual store path for each of its output and the
741 ;; corresponding environment variable.
743 (($ <derivation> outputs inputs sources
744 system builder args env-vars)
745 (let* ((drv-hash (derivation-hash drv))
746 (outputs (map (match-lambda
747 ((output-name . ($ <derivation-output>
751 (fixed-output-path name hash
755 (output-path output-name
758 (make-derivation-output path algo
761 (make-derivation outputs inputs sources system builder args
765 (or (and=> (assoc-ref outputs name)
766 derivation-output-path)
771 (define (user+system-env-vars)
772 ;; Some options are passed to the build daemon via the env. vars of
773 ;; derivations (urgh!). We hide that from our API, but here is the place
774 ;; where we kludgify those options.
775 (let ((env-vars `(,@(if local-build?
776 `(("preferLocalBuild" . "1"))
778 ,@(if (not substitutable?)
779 `(("allowSubstitutes" . "0"))
781 ,@(if allowed-references
782 `(("allowedReferences"
783 . ,(string-join allowed-references)))
785 ,@(if disallowed-references
786 `(("disallowedReferences"
787 . ,(string-join disallowed-references)))
789 ,@(if leaked-env-vars
791 . ,(string-join leaked-env-vars)))
794 (match references-graphs
796 (let ((value (map (cut string-append <> " " <>)
798 ;; XXX: This all breaks down if an element of FILE or PATH contains
800 `(("exportReferencesGraph" . ,(string-join value " "))
805 (define (env-vars-with-empty-outputs env-vars)
806 ;; Return a variant of ENV-VARS where each OUTPUTS is associated with an
807 ;; empty string, even outputs that do not appear in ENV-VARS.
808 (let ((e (map (match-lambda
810 (if (member name outputs)
814 (fold (lambda (output-name env-vars)
815 (if (assoc output-name env-vars)
817 (append env-vars `((,output-name . "")))))
821 (define input->derivation-input
823 (((? derivation? drv))
824 (make-derivation-input (derivation-file-name drv) '("out")))
825 (((? derivation? drv) sub-drvs ...)
826 (make-derivation-input (derivation-file-name drv) sub-drvs))
827 (((? direct-store-path? input))
828 (make-derivation-input input '("out")))
829 (((? direct-store-path? input) sub-drvs ...)
830 (make-derivation-input input sub-drvs))
832 (let ((path (add-to-store store (basename input)
834 (make-derivation-input path '())))))
836 ;; Note: lists are sorted alphabetically, to conform with the behavior of
837 ;; C++ `std::map' in Nix itself.
839 (let* ((outputs (map (lambda (name)
840 ;; Return outputs with an empty path.
842 (make-derivation-output "" hash-algo
844 (sort outputs string<?)))
845 (inputs (sort (coalesce-duplicate-inputs
846 (map input->derivation-input
847 (delete-duplicates inputs)))
849 (env-vars (sort (env-vars-with-empty-outputs
850 (user+system-env-vars))
852 (string<? (car e1) (car e2)))))
853 (drv-masked (make-derivation outputs
854 (filter (compose derivation-path?
855 derivation-input-path)
857 (filter-map (lambda (i)
858 (let ((p (derivation-input-path i)))
859 (and (not (derivation-path? p))
862 system builder args env-vars #f))
863 (drv (add-output-paths drv-masked)))
865 (let* ((file (add-text-to-store store (string-append name ".drv")
866 (derivation->string drv)
867 (map derivation-input-path inputs)))
868 (drv* (set-field drv (derivation-file-name) file)))
869 (hash-set! %derivation-cache file drv*)
872 (define* (map-derivation store drv mapping
873 #:key (system (%current-system)))
874 "Given MAPPING, a list of pairs of derivations, return a derivation based on
875 DRV where all the 'car's of MAPPING have been replaced by its 'cdr's,
877 (define (substitute str initial replacements)
878 (fold (lambda (path replacement result)
879 (string-replace-substring result path
882 initial replacements))
884 (define (substitute-file file initial replacements)
886 (with-fluids ((%default-port-encoding #f))
887 (call-with-input-file file read-string)))
889 (let ((updated (substitute contents initial replacements)))
890 (if (string=? updated contents)
892 ;; XXX: permissions aren't preserved.
893 (add-text-to-store store (store-path-package-name file)
896 (define input->output-paths
898 (((? derivation? drv))
899 (list (derivation->output-path drv)))
900 (((? derivation? drv) sub-drvs ...)
901 (map (cut derivation->output-path drv <>)
906 (let ((mapping (fold (lambda (pair result)
908 (((? derivation? orig) . replacement)
909 (vhash-cons (derivation-file-name orig)
911 ((file . replacement)
912 (vhash-cons file replacement result))))
915 (define rewritten-input
916 ;; Rewrite the given input according to MAPPING, and return an input
917 ;; in the format used in 'derivation' calls.
918 (mlambda (input loop)
920 (($ <derivation-input> path (sub-drvs ...))
921 (match (vhash-assoc path mapping)
922 ((_ . (? derivation? replacement))
923 (cons replacement sub-drvs))
927 (let* ((drv (loop (call-with-input-file path read-derivation))))
928 (cons drv sub-drvs))))))))
930 (let loop ((drv drv))
931 (let* ((inputs (map (cut rewritten-input <> loop)
932 (derivation-inputs drv)))
933 (initial (append-map derivation-input-output-paths
934 (derivation-inputs drv)))
935 (replacements (append-map input->output-paths inputs))
937 ;; Sources typically refer to the output directories of the
938 ;; original inputs, INITIAL. Rewrite them by substituting
940 (sources (map (lambda (source)
941 (match (vhash-assoc source mapping)
945 (substitute-file source
946 initial replacements))))
947 (derivation-sources drv)))
949 ;; Now augment the lists of initials and replacements.
950 (initial (append (derivation-sources drv) initial))
951 (replacements (append sources replacements))
952 (name (store-path-package-name
953 (string-drop-right (derivation-file-name drv)
955 (derivation store name
956 (substitute (derivation-builder drv)
957 initial replacements)
958 (map (cut substitute <> initial replacements)
959 (derivation-builder-arguments drv))
961 #:env-vars (map (match-lambda
964 . ,(substitute value initial
966 (derivation-builder-environment-vars drv))
967 #:inputs (append (map list sources) inputs)
968 #:outputs (derivation-output-names drv)
969 #:hash (match (derivation-outputs drv)
970 ((($ <derivation-output> _ algo hash))
973 #:hash-algo (match (derivation-outputs drv)
974 ((($ <derivation-output> _ algo hash))
980 ;;; Store compatibility layer.
983 (define* (build-derivations store derivations
984 #:optional (mode (build-mode normal)))
985 "Build DERIVATIONS, a list of <derivation> objects or .drv file names, using
987 (build-things store (map (match-lambda
988 ((? string? file) file)
989 ((and drv ($ <derivation>))
990 (derivation-file-name drv)))
996 ;;; Guile-based builders.
999 (define (parent-directories file-name)
1000 "Return the list of parent dirs of FILE-NAME, in the order in which an
1001 `mkdir -p' implementation would make them."
1002 (let ((not-slash (char-set-complement (char-set #\/))))
1004 (fold (lambda (dir result)
1009 (cons (string-append prev "/" dir)
1012 (remove (cut string=? <> ".")
1013 (string-tokenize (dirname file-name) not-slash))))))
1015 (define* (imported-files store files ;deprecated
1016 #:key (name "file-import")
1017 (system (%current-system))
1018 (guile (%guile-for-build)))
1019 "Return a derivation that imports FILES into STORE. FILES must be a list
1020 of (FINAL-PATH . FILE-NAME) pairs; each FILE-NAME is read from the file
1021 system, imported, and appears under FINAL-PATH in the resulting store path."
1022 (let* ((files (map (match-lambda
1023 ((final-path . file-name)
1025 (add-to-store store (basename final-path) #f
1026 "sha256" file-name))))
1030 (mkdir %output) (chdir %output)
1031 ,@(append-map (match-lambda
1032 ((final-path store-path)
1033 (append (match (parent-directories final-path)
1036 (append (map (lambda (d)
1037 `(false-if-exception
1040 `((or (file-exists? ,tail)
1042 `((symlink ,store-path ,final-path)))))
1044 (build-expression->derivation store name builder
1047 #:guile-for-build guile
1048 #:local-build? #t)))
1050 ;; The "file not found" error condition.
1051 (define-condition-type &file-search-error &error
1053 (file file-search-error-file-name)
1054 (path file-search-error-search-path))
1056 (define search-path*
1057 ;; A memoizing version of 'search-path' so 'imported-modules' does not end
1058 ;; up looking for the same files over and over again.
1059 (mlambda (path file)
1060 "Search for FILE in PATH and memoize the result. Raise a
1061 '&file-search-error' condition if it could not be found."
1062 (or (search-path path file)
1064 (&file-search-error (file file)
1067 (define (module->source-file-name module)
1068 "Return the file name corresponding to MODULE, a Guile module name (a list
1070 (string-append (string-join (map symbol->string module) "/")
1073 (define* (%imported-modules store modules ;deprecated
1074 #:key (name "module-import")
1075 (system (%current-system))
1076 (guile (%guile-for-build))
1077 (module-path %load-path))
1078 "Return a derivation that contains the source files of MODULES, a list of
1079 module names such as `(ice-9 q)'. All of MODULES must be in the MODULE-PATH
1081 ;; TODO: Determine the closure of MODULES, build the `.go' files,
1082 ;; canonicalize the source files through read/write, etc.
1083 (let ((files (map (lambda (m)
1084 (let ((f (module->source-file-name m)))
1085 (cons f (search-path* module-path f))))
1087 (imported-files store files #:name name #:system system
1090 (define* (%compiled-modules store modules ;deprecated
1091 #:key (name "module-import-compiled")
1092 (system (%current-system))
1093 (guile (%guile-for-build))
1094 (module-path %load-path))
1095 "Return a derivation that builds a tree containing the `.go' files
1096 corresponding to MODULES. All the MODULES are built in a context where
1097 they can refer to each other."
1098 (let* ((module-drv (%imported-modules store modules
1101 #:module-path module-path))
1102 (module-dir (derivation->output-path module-drv))
1103 (files (map (lambda (m)
1104 (let ((f (string-join (map symbol->string m)
1106 (cons (string-append f ".go")
1107 (string-append module-dir "/" f ".scm"))))
1111 (use-modules (system base compile))
1112 (let ((out (assoc-ref %outputs "out")))
1117 (cons ,module-dir %load-path))
1119 ,@(map (match-lambda
1121 (let ((make-parent-dirs (map (lambda (dir)
1122 `(unless (file-exists? ,dir)
1124 (parent-directories output))))
1127 (compile-file ,input
1128 #:output-file ,output
1129 #:opts %auto-compilation-options)))))
1132 (build-expression->derivation store name builder
1133 #:inputs `(("modules" ,module-drv))
1135 #:guile-for-build guile
1136 #:local-build? #t)))
1138 (define* (build-expression->derivation store name exp ;deprecated
1140 (system (%current-system))
1143 hash hash-algo recursive?
1149 disallowed-references
1150 local-build? (substitutable? #t))
1151 "Return a derivation that executes Scheme expression EXP as a builder
1152 for derivation NAME. INPUTS must be a list of (NAME DRV-PATH SUB-DRV)
1153 tuples; when SUB-DRV is omitted, \"out\" is assumed. MODULES is a list
1154 of names of Guile modules from the current search path to be copied in
1155 the store, compiled, and made available in the load path during the
1158 EXP is evaluated in an environment where %OUTPUT is bound to the main
1159 output path, %OUTPUTS is bound to a list of output/path pairs, and where
1160 %BUILD-INPUTS is bound to an alist of string/output-path pairs made from
1161 INPUTS. Optionally, ENV-VARS is a list of string pairs specifying the
1162 name and value of environment variables visible to the builder. The
1163 builder terminates by passing the result of EXP to `exit'; thus, when
1164 EXP returns #f, the build is considered to have failed.
1166 EXP is built using GUILE-FOR-BUILD (a derivation). When GUILE-FOR-BUILD is
1167 omitted or is #f, the value of the `%guile-for-build' fluid is used instead.
1169 See the `derivation' procedure for the meaning of REFERENCES-GRAPHS,
1170 ALLOWED-REFERENCES, DISALLOWED-REFERENCES, LOCAL-BUILD?, and SUBSTITUTABLE?."
1172 (or guile-for-build (%guile-for-build)))
1175 (string-append (derivation->output-path guile-drv)
1178 (define module-form?
1180 (((or 'define-module 'use-modules) _ ...) #t)
1184 ;; When passed an input that is a source, return its path; otherwise
1187 ((_ (? derivation?) _ ...)
1190 (and (not (derivation-path? path))
1193 (let* ((prologue `(begin
1196 ;; Module forms must appear at the top-level so
1197 ;; that any macros they export can be expanded.
1198 (filter module-form? exp))
1201 (define %output (getenv "out"))
1204 (cons o (getenv o)))
1206 (define %build-inputs
1207 ',(map (match-lambda
1209 (let ((sub (match rest
1215 (derivation->output-path drv sub))
1216 ((derivation-path? drv)
1217 (derivation-path->output-path drv
1222 ,@(if (null? modules)
1224 ;; Remove our own settings.
1225 '((unsetenv "GUILE_LOAD_COMPILED_PATH")))
1227 ;; Guile sets it, but remove it to avoid conflicts when
1228 ;; building Guile-using packages.
1229 (unsetenv "LD_LIBRARY_PATH")))
1230 (builder (add-text-to-store store
1231 (string-append name "-guile-builder")
1233 ;; Explicitly use UTF-8 for determinism,
1234 ;; and also because UTF-8 output is faster.
1235 (with-fluids ((%default-port-encoding
1237 (call-with-output-string
1239 (write prologue port)
1244 (remove module-form? exp))
1248 ;; The references don't really matter
1249 ;; since the builder is always used in
1250 ;; conjunction with the drv that needs
1251 ;; it. For clarity, we add references
1252 ;; to the subset of INPUTS that are
1253 ;; sources, avoiding references to other
1254 ;; .drv; otherwise, BUILDER's hash would
1255 ;; depend on those, even if they are
1257 (filter-map source-path inputs)))
1259 (mod-drv (and (pair? modules)
1260 (%imported-modules store modules
1263 (mod-dir (and mod-drv
1264 (derivation->output-path mod-drv)))
1265 (go-drv (and (pair? modules)
1266 (%compiled-modules store modules
1270 (derivation->output-path go-drv))))
1271 (derivation store name guile
1272 `("--no-auto-compile"
1273 ,@(if mod-dir `("-L" ,mod-dir) '())
1278 #:inputs `((,(or guile-for-build (%guile-for-build)))
1281 ,@(if mod-drv `((,mod-drv) (,go-drv)) '()))
1283 ;; When MODULES is non-empty, shamelessly clobber
1284 ;; $GUILE_LOAD_COMPILED_PATH.
1285 #:env-vars (if go-dir
1286 `(("GUILE_LOAD_COMPILED_PATH" . ,go-dir)
1287 ,@(alist-delete "GUILE_LOAD_COMPILED_PATH"
1291 #:hash hash #:hash-algo hash-algo
1292 #:recursive? recursive?
1294 #:references-graphs references-graphs
1295 #:allowed-references allowed-references
1296 #:disallowed-references disallowed-references
1297 #:local-build? local-build?
1298 #:substitutable? substitutable?)))
1302 ;;; Monadic interface.
1305 (define built-derivations
1306 (store-lift build-derivations))
1308 (define raw-derivation
1309 (store-lift derivation))