1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016 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 grafts)
20 #:use-module (guix store)
21 #:use-module (guix monads)
22 #:use-module (guix records)
23 #:use-module (guix derivations)
24 #:use-module ((guix utils) #:select (%current-system))
25 #:use-module (srfi srfi-1)
26 #:use-module (srfi srfi-9 gnu)
27 #:use-module (srfi srfi-11)
28 #:use-module (srfi srfi-26)
29 #:use-module (srfi srfi-34)
30 #:use-module (ice-9 match)
31 #:use-module (ice-9 vlist)
37 graft-replacement-output
40 graft-derivation/shallow
45 (define-record-type* <graft> graft make-graft
47 (origin graft-origin) ;derivation | store item
48 (origin-output graft-origin-output ;string | #f
50 (replacement graft-replacement) ;derivation | store item
51 (replacement-output graft-replacement-output ;string | #f
54 (define (write-graft graft port)
55 "Write a concise representation of GRAFT to PORT."
56 (define (->string thing output)
57 (if (derivation? thing)
58 (derivation->output-path thing output)
62 (($ <graft> origin origin-output replacement replacement-output)
63 (format port "#<graft ~a ==> ~a ~a>"
64 (->string origin origin-output)
65 (->string replacement replacement-output)
66 (number->string (object-address graft) 16)))))
68 (set-record-type-printer! <graft> write-graft)
70 (define (graft-origin-file-name graft)
71 "Return the output file name of the origin of GRAFT."
73 (($ <graft> (? derivation? origin) output)
74 (derivation->output-path origin output))
75 (($ <graft> (? string? item))
78 (define* (graft-derivation/shallow store drv grafts
80 (name (derivation-name drv))
81 (guile (%guile-for-build))
82 (system (%current-system)))
83 "Return a derivation called NAME, based on DRV but with all the GRAFTS
84 applied. This procedure performs \"shallow\" grafting in that GRAFTS are not
85 recursively applied to dependencies of DRV."
86 ;; XXX: Someday rewrite using gexps.
88 ;; List of store item pairs.
90 (($ <graft> source source-output target target-output)
91 (cons (if (derivation? source)
92 (derivation->output-path source source-output)
94 (if (derivation? target)
95 (derivation->output-path target target-output)
102 (cons name (derivation-output-path output))))
103 (derivation-outputs drv)))
106 (derivation-output-names drv))
110 (use-modules (guix build graft)
114 (let* ((old-outputs ',outputs)
115 (mapping (append ',mapping
118 (cons (assoc-ref old-outputs name)
121 (for-each (lambda (input output)
122 (format #t "grafting '~a' -> '~a'...~%" input output)
124 (rewrite-directory input output mapping))
126 (((names . files) ...)
129 (((names . files) ...)
136 ((($ <graft> sources source-outputs targets target-outputs) ...)
137 (let ((sources (zip sources source-outputs))
138 (targets (zip targets target-outputs)))
139 (build-expression->derivation store name build
141 #:guile-for-build guile
142 #:modules '((guix build graft)
144 #:inputs `(,@(map (lambda (out)
147 ,@(append (map add-label sources)
148 (map add-label targets)))
149 #:outputs output-names
150 #:local-build? #t)))))
151 (define (item->deriver store item)
152 "Return two values: the derivation that led to ITEM (a store item), and the
153 name of the output of that derivation ITEM corresponds to (for example
154 \"out\"). When ITEM has no deriver, for instance because it is a plain file,
155 #f and #f are returned."
156 (match (valid-derivers store item)
157 (() ;ITEM is a plain file
160 (let ((drv (call-with-input-file drv-file read-derivation)))
164 (and (string=? item path) name)))
165 (derivation->output-paths drv)))))))
167 (define (non-self-references references drv outputs)
168 "Return the list of references of the OUTPUTS of DRV, excluding self
169 references. Call REFERENCES to get the list of references."
170 (let ((refs (append-map (compose references
171 (cut derivation->output-path drv <>))
173 (self (match (derivation->output-paths drv)
174 (((names . items) ...)
176 (remove (cut member <> self) refs)))
178 (define (references-oracle store drv)
179 "Return a one-argument procedure that, when passed the file name of DRV's
180 outputs or their dependencies, returns the list of references of that item.
181 Use either local info or substitute info; build DRV if no information is
183 (define (output-paths drv)
184 (match (derivation->output-paths drv)
185 (((names . items) ...)
188 (define (references* items)
189 (guard (c ((nix-protocol-error? c)
190 ;; As a last resort, build DRV and query the references of the
193 ;; Warm up the narinfo cache, otherwise each derivation build
194 ;; will result in one HTTP request to get one narinfo, which is
195 ;; much less efficient than fetching them all upfront.
196 (substitution-oracle store (list drv))
198 (and (build-derivations store (list drv))
199 (map (cut references store <>) items))))
200 (references/substitutes store items)))
202 (let loop ((items (output-paths drv))
207 (match (vhash-assoc item result)
211 (let* ((refs (references* items))
212 (result (fold vhash-cons result items refs)))
213 (loop (remove (cut vhash-assoc <> result)
214 (delete-duplicates (concatenate refs) string=?))
217 (define* (cumulative-grafts store drv grafts
220 (outputs (derivation-output-names drv))
221 (guile (%guile-for-build))
222 (system (%current-system)))
223 "Augment GRAFTS with additional grafts resulting from the application of
224 GRAFTS to the dependencies of DRV; REFERENCES must be a one-argument procedure
225 that returns the list of references of the store item it is given. Return the
226 resulting list of grafts.
228 This is a monadic procedure in %STATE-MONAD where the state is a vhash mapping
229 derivations to the corresponding set of grafts."
230 (define (dependency-grafts item)
231 (let-values (((drv output) (item->deriver store item)))
233 (cumulative-grafts store drv grafts references
234 #:outputs (list output)
237 (state-return grafts))))
239 (define (return/cache cache value)
241 (set-current-state (vhash-consq drv value cache))
244 (mlet %state-monad ((cache (current-state)))
245 (match (vhash-assq drv cache)
249 (match (non-self-references references drv outputs)
251 (return/cache cache grafts))
252 (deps ;one or more dependencies
253 (mlet %state-monad ((grafts (mapm %state-monad dependency-grafts deps))
254 (cache (current-state)))
255 (let* ((grafts (delete-duplicates (concatenate grafts) equal?))
256 (origins (map graft-origin-file-name grafts)))
257 (if (find (cut member <> deps) origins)
258 (let* ((new (graft-derivation/shallow store drv grafts
261 (grafts (cons (graft (origin drv) (replacement new))
263 (return/cache cache grafts))
264 (return/cache cache grafts))))))))))
266 (define* (graft-derivation store drv grafts
267 #:key (guile (%guile-for-build))
268 (system (%current-system)))
269 "Applied GRAFTS to DRV and all its dependencies, recursively. That is, if
270 GRAFTS apply only indirectly to DRV, graft the dependencies of DRV, and graft
271 DRV itself to refer to those grafted dependencies."
273 ;; First, pre-compute the dependency tree of the outputs of DRV. Do this
274 ;; upfront to have as much parallelism as possible when querying substitute
275 ;; info or when building DRV.
277 (references-oracle store drv))
279 (match (run-with-state
280 (cumulative-grafts store drv grafts references
281 #:guile guile #:system system)
282 vlist-null) ;the initial cache
284 ;; If FIRST is not a graft for DRV, it means that GRAFTS are not
285 ;; applicable to DRV and nothing needs to be done.
286 (if (equal? drv (graft-origin first))
287 (graft-replacement first)
291 ;; The following might feel more at home in (guix packages) but since (guix
292 ;; gexp), which is a lower level, needs them, we put them here.
295 ;; Whether to honor package grafts by default.
298 (define (set-grafting enable?)
299 "This monadic procedure enables grafting when ENABLE? is true, and disables
300 it otherwise. It returns the previous setting."
302 (values (%graft? enable?) store)))
304 ;;; grafts.scm ends here