1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018 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 (outputs (derivation-output-names drv))
82 (guile (%guile-for-build))
83 (system (%current-system)))
84 "Return a derivation called NAME, which applies GRAFTS to the specified
85 OUTPUTS of DRV. This procedure performs \"shallow\" grafting in that GRAFTS
86 are not recursively applied to dependencies of DRV."
87 ;; XXX: Someday rewrite using gexps.
89 ;; List of store item pairs.
91 (($ <graft> source source-output target target-output)
92 (cons (if (derivation? source)
93 (derivation->output-path source source-output)
95 (if (derivation? target)
96 (derivation->output-path target target-output)
101 (map (lambda (output)
103 (derivation-output-path
104 (assoc-ref (derivation-outputs drv) output))))
109 (use-modules (guix build graft)
113 (let* ((old-outputs ',output-pairs)
114 (mapping (append ',mapping
117 (cons (assoc-ref old-outputs name)
120 (graft old-outputs %outputs mapping))))
126 ((($ <graft> sources source-outputs targets target-outputs) ...)
127 (let ((sources (zip sources source-outputs))
128 (targets (zip targets target-outputs)))
129 (build-expression->derivation store name build
131 #:guile-for-build guile
132 #:modules '((guix build graft)
134 #:inputs `(,@(map (lambda (out)
137 ,@(append (map add-label sources)
138 (map add-label targets)))
140 #:local-build? #t)))))
141 (define (item->deriver store item)
142 "Return two values: the derivation that led to ITEM (a store item), and the
143 name of the output of that derivation ITEM corresponds to (for example
144 \"out\"). When ITEM has no deriver, for instance because it is a plain file,
145 #f and #f are returned."
146 (match (valid-derivers store item)
147 (() ;ITEM is a plain file
150 (let ((drv (read-derivation-from-file drv-file)))
154 (and (string=? item path) name)))
155 (derivation->output-paths drv)))))))
157 (define (non-self-references references drv outputs)
158 "Return the list of references of the OUTPUTS of DRV, excluding self
159 references. Call REFERENCES to get the list of references."
160 (let ((refs (append-map (compose references
161 (cut derivation->output-path drv <>))
163 (self (match (derivation->output-paths drv)
164 (((names . items) ...)
166 (remove (cut member <> self) refs)))
168 (define (references-oracle store drv)
169 "Return a one-argument procedure that, when passed the file name of DRV's
170 outputs or their dependencies, returns the list of references of that item.
171 Use either local info or substitute info; build DRV if no information is
173 (define (output-paths drv)
174 (match (derivation->output-paths drv)
175 (((names . items) ...)
178 (define (references* items)
179 (guard (c ((nix-protocol-error? c)
180 ;; As a last resort, build DRV and query the references of the
183 ;; Warm up the narinfo cache, otherwise each derivation build
184 ;; will result in one HTTP request to get one narinfo, which is
185 ;; much less efficient than fetching them all upfront.
186 (substitution-oracle store (list drv))
188 (and (build-derivations store (list drv))
189 (map (cut references store <>) items))))
190 (references/substitutes store items)))
192 (let loop ((items (output-paths drv))
197 (match (vhash-assoc item result)
201 (let* ((refs (references* items))
202 (result (fold vhash-cons result items refs)))
203 (loop (remove (cut vhash-assoc <> result)
204 (delete-duplicates (concatenate refs) string=?))
207 (define-syntax-rule (with-cache key exp ...)
208 "Cache the value of monadic expression EXP under KEY."
209 (mlet %state-monad ((cache (current-state)))
210 (match (vhash-assoc key cache)
211 ((_ . result) ;cache hit
214 (mlet %state-monad ((result (begin exp ...))
215 (cache (current-state)))
217 (set-current-state (vhash-cons key result cache))
218 (return result)))))))
220 (define* (cumulative-grafts store drv grafts
223 (outputs (derivation-output-names drv))
224 (guile (%guile-for-build))
225 (system (%current-system)))
226 "Augment GRAFTS with additional grafts resulting from the application of
227 GRAFTS to the dependencies of DRV; REFERENCES must be a one-argument procedure
228 that returns the list of references of the store item it is given. Return the
229 resulting list of grafts.
231 This is a monadic procedure in %STATE-MONAD where the state is a vhash mapping
232 derivations to the corresponding set of grafts."
233 (define (graft-origin? drv graft)
234 ;; Return true if DRV corresponds to the origin of GRAFT.
236 (($ <graft> (? derivation? origin) output)
237 (match (assoc-ref (derivation->output-paths drv) output)
240 (derivation->output-path origin output)))
246 (define (dependency-grafts item)
247 (let-values (((drv output) (item->deriver store item)))
249 ;; If GRAFTS already contains a graft from DRV, do not override it.
250 (if (find (cut graft-origin? drv <>) grafts)
251 (state-return grafts)
252 (cumulative-grafts store drv grafts references
253 #:outputs (list output)
256 (state-return grafts))))
258 (with-cache (cons (derivation-file-name drv) outputs)
259 (match (non-self-references references drv outputs)
262 (deps ;one or more dependencies
263 (mlet %state-monad ((grafts (mapm %state-monad dependency-grafts deps)))
264 (let ((grafts (delete-duplicates (concatenate grafts) equal?)))
265 (match (filter (lambda (graft)
266 (member (graft-origin-file-name graft) deps))
271 ;; Use APPLICABLE, the subset of GRAFTS that is really
272 ;; applicable to DRV, to avoid creating several identical
273 ;; grafted variants of DRV.
274 (let* ((new (graft-derivation/shallow store drv applicable
278 (grafts (append (map (lambda (output)
281 (origin-output output)
283 (replacement-output output)))
286 (return grafts))))))))))
288 (define* (graft-derivation store drv grafts
290 (guile (%guile-for-build))
291 (outputs (derivation-output-names drv))
292 (system (%current-system)))
293 "Apply GRAFTS to the OUTPUTS of DRV and all their dependencies, recursively.
294 That is, if GRAFTS apply only indirectly to DRV, graft the dependencies of
295 DRV, and graft DRV itself to refer to those grafted dependencies."
297 ;; First, pre-compute the dependency tree of the outputs of DRV. Do this
298 ;; upfront to have as much parallelism as possible when querying substitute
299 ;; info or when building DRV.
301 (references-oracle store drv))
303 (match (run-with-state
304 (cumulative-grafts store drv grafts references
306 #:guile guile #:system system)
307 vlist-null) ;the initial cache
309 ;; If FIRST is not a graft for DRV, it means that GRAFTS are not
310 ;; applicable to DRV and nothing needs to be done.
311 (if (equal? drv (graft-origin first))
312 (graft-replacement first)
316 ;; The following might feel more at home in (guix packages) but since (guix
317 ;; gexp), which is a lower level, needs them, we put them here.
320 ;; Whether to honor package grafts by default.
323 (define (set-grafting enable?)
324 "This monadic procedure enables grafting when ENABLE? is true, and disables
325 it otherwise. It returns the previous setting."
327 (values (%graft? enable?) store)))
330 ;; eval: (put 'with-cache 'scheme-indent-function 1)
333 ;;; grafts.scm ends here