Revert "gnu: nss: Update to 3.45 [security fixes]."
[guix.git] / guix / grafts.scm
blobadc7bfafaec351f82c10cf8e6f5951421e64714f
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
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.
10 ;;;
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.
15 ;;;
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 (guix sets)
26   #:use-module (srfi srfi-1)
27   #:use-module (srfi srfi-9 gnu)
28   #:use-module (srfi srfi-26)
29   #:use-module (srfi srfi-34)
30   #:use-module (ice-9 match)
31   #:use-module (ice-9 vlist)
32   #:export (graft?
33             graft
34             graft-origin
35             graft-replacement
36             graft-origin-output
37             graft-replacement-output
39             graft-derivation
40             graft-derivation/shallow
42             %graft?
43             set-grafting
44             grafting?))
46 (define-record-type* <graft> graft make-graft
47   graft?
48   (origin             graft-origin)               ;derivation | store item
49   (origin-output      graft-origin-output         ;string | #f
50                       (default "out"))
51   (replacement        graft-replacement)          ;derivation | store item
52   (replacement-output graft-replacement-output    ;string | #f
53                       (default "out")))
55 (define (write-graft graft port)
56   "Write a concise representation of GRAFT to PORT."
57   (define (->string thing output)
58     (if (derivation? thing)
59         (derivation->output-path thing output)
60         thing))
62   (match graft
63     (($ <graft> origin origin-output replacement replacement-output)
64      (format port "#<graft ~a ==> ~a ~a>"
65              (->string origin origin-output)
66              (->string replacement replacement-output)
67              (number->string (object-address graft) 16)))))
69 (set-record-type-printer! <graft> write-graft)
71 (define (graft-origin-file-name graft)
72   "Return the output file name of the origin of GRAFT."
73   (match graft
74     (($ <graft> (? derivation? origin) output)
75      (derivation->output-path origin output))
76     (($ <graft> (? string? item))
77      item)))
79 (define* (graft-derivation/shallow store drv grafts
80                                    #:key
81                                    (name (derivation-name drv))
82                                    (outputs (derivation-output-names drv))
83                                    (guile (%guile-for-build))
84                                    (system (%current-system)))
85   "Return a derivation called NAME, which applies GRAFTS to the specified
86 OUTPUTS of DRV.  This procedure performs \"shallow\" grafting in that GRAFTS
87 are not recursively applied to dependencies of DRV."
88   ;; XXX: Someday rewrite using gexps.
89   (define mapping
90     ;; List of store item pairs.
91     (map (match-lambda
92           (($ <graft> source source-output target target-output)
93            (cons (if (derivation? source)
94                      (derivation->output-path source source-output)
95                      source)
96                  (if (derivation? target)
97                      (derivation->output-path target target-output)
98                      target))))
99          grafts))
101   (define output-pairs
102     (map (lambda (output)
103            (cons output
104                  (derivation-output-path
105                   (assoc-ref (derivation-outputs drv) output))))
106          outputs))
108   (define build
109     `(begin
110        (use-modules (guix build graft)
111                     (guix build utils)
112                     (ice-9 match))
114        (let* ((old-outputs ',output-pairs)
115               (mapping (append ',mapping
116                                (map (match-lambda
117                                       ((name . file)
118                                        (cons (assoc-ref old-outputs name)
119                                              file)))
120                                     %outputs))))
121          (graft old-outputs %outputs mapping))))
123   (define add-label
124     (cut cons "x" <>))
126   (define properties
127     `((type . graft)
128       (graft (count . ,(length grafts)))))
130   (match grafts
131     ((($ <graft> sources source-outputs targets target-outputs) ...)
132      (let ((sources (zip sources source-outputs))
133            (targets (zip targets target-outputs)))
134        (build-expression->derivation store name build
135                                      #:system system
136                                      #:guile-for-build guile
137                                      #:modules '((guix build graft)
138                                                  (guix build utils)
139                                                  (guix build debug-link)
140                                                  (guix elf))
141                                      #:inputs `(,@(map (lambda (out)
142                                                          `("x" ,drv ,out))
143                                                        outputs)
144                                                 ,@(append (map add-label sources)
145                                                           (map add-label targets)))
146                                      #:outputs outputs
148                                      ;; Grafts are computationally cheap so no
149                                      ;; need to offload or substitute.
150                                      #:local-build? #t
151                                      #:substitutable? #f
153                                      #:properties properties)))))
155 (define (non-self-references references drv outputs)
156   "Return the list of references of the OUTPUTS of DRV, excluding self
157 references.  Call REFERENCES to get the list of references."
158   (let ((refs (append-map (compose references
159                                    (cut derivation->output-path drv <>))
160                           outputs))
161         (self (match (derivation->output-paths drv)
162                 (((names . items) ...)
163                  items))))
164     (remove (cut member <> self) refs)))
166 (define (references-oracle store input)
167   "Return a one-argument procedure that, when passed the output file names of
168 INPUT, a derivation input, or their dependencies, returns the list of
169 references of that item.  Use either local info or substitute info; build
170 INPUT if no information is available."
171   (define (references* items)
172     (guard (c ((store-protocol-error? c)
173                ;; As a last resort, build DRV and query the references of the
174                ;; build result.
176                ;; Warm up the narinfo cache, otherwise each derivation build
177                ;; will result in one HTTP request to get one narinfo, which is
178                ;; much less efficient than fetching them all upfront.
179                (substitution-oracle store
180                                     (list (derivation-input-derivation input)))
182                (and (build-derivations store (list input))
183                     (map (cut references store <>) items))))
184       (references/substitutes store items)))
186   (let loop ((items (derivation-input-output-paths input))
187              (result vlist-null))
188     (match items
189       (()
190        (lambda (item)
191          (match (vhash-assoc item result)
192            ((_ . refs) refs)
193            (#f         #f))))
194       (_
195        (let* ((refs   (references* items))
196               (result (fold vhash-cons result items refs)))
197          (loop (remove (cut vhash-assoc <> result)
198                        (delete-duplicates (concatenate refs) string=?))
199                result))))))
201 (define-syntax-rule (with-cache key exp ...)
202   "Cache the value of monadic expression EXP under KEY."
203   (mlet %state-monad ((cache (current-state)))
204     (match (vhash-assoc key cache)
205       ((_ . result)                               ;cache hit
206        (return result))
207       (#f                                         ;cache miss
208        (mlet %state-monad ((result (begin exp ...))
209                            (cache  (current-state)))
210          (mbegin %state-monad
211            (set-current-state (vhash-cons key result cache))
212            (return result)))))))
214 (define (reference-origin drv item)
215   "Return the derivation/output pair among the inputs of DRV, recursively,
216 that produces ITEM.  Return #f if ITEM is not produced by a derivation (i.e.,
217 it's a content-addressed \"source\"), or if it's not produced by a dependency
218 of DRV."
219   ;; Perform a breadth-first traversal of the dependency graph of DRV in
220   ;; search of the derivation that produces ITEM.
221   (let loop ((drv (list drv))
222              (visited (setq)))
223     (match drv
224       (()
225        #f)
226       ((drv . rest)
227        (if (set-contains? visited drv)
228            (loop rest visited)
229            (let ((inputs (derivation-inputs drv)))
230              (or (any (lambda (input)
231                         (let ((drv (derivation-input-derivation input)))
232                           (any (match-lambda
233                                  ((output . file)
234                                   (and (string=? file item)
235                                        (cons drv output))))
236                                (derivation->output-paths drv))))
237                       inputs)
238                  (loop (append rest (map derivation-input-derivation inputs))
239                        (set-insert drv visited)))))))))
241 (define* (cumulative-grafts store drv grafts
242                             references
243                             #:key
244                             (outputs (derivation-output-names drv))
245                             (guile (%guile-for-build))
246                             (system (%current-system)))
247   "Augment GRAFTS with additional grafts resulting from the application of
248 GRAFTS to the dependencies of DRV; REFERENCES must be a one-argument procedure
249 that returns the list of references of the store item it is given.  Return the
250 resulting list of grafts.
252 This is a monadic procedure in %STATE-MONAD where the state is a vhash mapping
253 derivations to the corresponding set of grafts."
254   (define (graft-origin? drv graft)
255     ;; Return true if DRV corresponds to the origin of GRAFT.
256     (match graft
257       (($ <graft> (? derivation? origin) output)
258        (match (assoc-ref (derivation->output-paths drv) output)
259          ((? string? result)
260           (string=? result
261                     (derivation->output-path origin output)))
262          (_
263           #f)))
264       (_
265        #f)))
267   (define (dependency-grafts item)
268     (match (reference-origin drv item)
269       ((drv . output)
270        ;; If GRAFTS already contains a graft from DRV, do not override it.
271        (if (find (cut graft-origin? drv <>) grafts)
272            (state-return grafts)
273            (cumulative-grafts store drv grafts references
274                               #:outputs (list output)
275                               #:guile guile
276                               #:system system)))
277       (#f
278        (state-return grafts))))
280   (with-cache (cons (derivation-file-name drv) outputs)
281     (match (non-self-references references drv outputs)
282       (()                                         ;no dependencies
283        (return grafts))
284       (deps                                       ;one or more dependencies
285        (mlet %state-monad ((grafts (mapm %state-monad dependency-grafts deps)))
286          (let ((grafts (delete-duplicates (concatenate grafts) equal?)))
287            (match (filter (lambda (graft)
288                             (member (graft-origin-file-name graft) deps))
289                           grafts)
290              (()
291               (return grafts))
292              ((applicable ..1)
293               ;; Use APPLICABLE, the subset of GRAFTS that is really
294               ;; applicable to DRV, to avoid creating several identical
295               ;; grafted variants of DRV.
296               (let* ((new    (graft-derivation/shallow store drv applicable
297                                                        #:outputs outputs
298                                                        #:guile guile
299                                                        #:system system))
300                      (grafts (append (map (lambda (output)
301                                             (graft
302                                               (origin drv)
303                                               (origin-output output)
304                                               (replacement new)
305                                               (replacement-output output)))
306                                           outputs)
307                                      grafts)))
308                 (return grafts))))))))))
310 (define* (graft-derivation store drv grafts
311                            #:key
312                            (guile (%guile-for-build))
313                            (outputs (derivation-output-names drv))
314                            (system (%current-system)))
315   "Apply GRAFTS to the OUTPUTS of DRV and all their dependencies, recursively.
316 That is, if GRAFTS apply only indirectly to DRV, graft the dependencies of
317 DRV, and graft DRV itself to refer to those grafted dependencies."
319   ;; First, pre-compute the dependency tree of the outputs of DRV.  Do this
320   ;; upfront to have as much parallelism as possible when querying substitute
321   ;; info or when building DRV.
322   (define references
323     (references-oracle store (derivation-input drv outputs)))
325   (match (run-with-state
326              (cumulative-grafts store drv grafts references
327                                 #:outputs outputs
328                                 #:guile guile #:system system)
329            vlist-null)                            ;the initial cache
330     ((first . rest)
331      ;; If FIRST is not a graft for DRV, it means that GRAFTS are not
332      ;; applicable to DRV and nothing needs to be done.
333      (if (equal? drv (graft-origin first))
334          (graft-replacement first)
335          drv))))
338 ;; The following might feel more at home in (guix packages) but since (guix
339 ;; gexp), which is a lower level, needs them, we put them here.
341 (define %graft?
342   ;; Whether to honor package grafts by default.
343   (make-parameter #t))
345 (define (set-grafting enable?)
346   "This monadic procedure enables grafting when ENABLE? is true, and disables
347 it otherwise.  It returns the previous setting."
348   (lambda (store)
349     (values (%graft? enable?) store)))
351 (define (grafting?)
352   "Return a Boolean indicating whether grafting is enabled."
353   (lambda (store)
354     (values (%graft?) store)))
356 ;; Local Variables:
357 ;; eval: (put 'with-cache 'scheme-indent-function 1)
358 ;; End:
360 ;;; grafts.scm ends here