services: Add 'network-manager-applet' to %DESKTOP-SERVICES.
[guix.git] / guix / grafts.scm
bloba3e12f6efddc3b249913936b11cbe69ce7edf14b
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018 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 (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)
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)))))
154 (define (item->deriver store item)
155   "Return two values: the derivation that led to ITEM (a store item), and the
156 name of the output of that derivation ITEM corresponds to (for example
157 \"out\").  When ITEM has no deriver, for instance because it is a plain file,
158 #f and #f are returned."
159   (match (valid-derivers store item)
160     (()                                           ;ITEM is a plain file
161      (values #f #f))
162     ((drv-file _ ...)
163      (let ((drv (read-derivation-from-file drv-file)))
164        (values drv
165                (any (match-lambda
166                       ((name . path)
167                        (and (string=? item path) name)))
168                     (derivation->output-paths drv)))))))
170 (define (non-self-references references drv outputs)
171   "Return the list of references of the OUTPUTS of DRV, excluding self
172 references.  Call REFERENCES to get the list of references."
173   (let ((refs (append-map (compose references
174                                    (cut derivation->output-path drv <>))
175                           outputs))
176         (self (match (derivation->output-paths drv)
177                 (((names . items) ...)
178                  items))))
179     (remove (cut member <> self) refs)))
181 (define (references-oracle store drv)
182   "Return a one-argument procedure that, when passed the file name of DRV's
183 outputs or their dependencies, returns the list of references of that item.
184 Use either local info or substitute info; build DRV if no information is
185 available."
186   (define (output-paths drv)
187     (match (derivation->output-paths drv)
188       (((names . items) ...)
189        items)))
191   (define (references* items)
192     (guard (c ((store-protocol-error? c)
193                ;; As a last resort, build DRV and query the references of the
194                ;; build result.
196                ;; Warm up the narinfo cache, otherwise each derivation build
197                ;; will result in one HTTP request to get one narinfo, which is
198                ;; much less efficient than fetching them all upfront.
199                (substitution-oracle store (list drv))
201                (and (build-derivations store (list drv))
202                     (map (cut references store <>) items))))
203       (references/substitutes store items)))
205   (let loop ((items (output-paths drv))
206              (result vlist-null))
207     (match items
208       (()
209        (lambda (item)
210          (match (vhash-assoc item result)
211            ((_ . refs) refs)
212            (#f         #f))))
213       (_
214        (let* ((refs   (references* items))
215               (result (fold vhash-cons result items refs)))
216          (loop (remove (cut vhash-assoc <> result)
217                        (delete-duplicates (concatenate refs) string=?))
218                result))))))
220 (define-syntax-rule (with-cache key exp ...)
221   "Cache the value of monadic expression EXP under KEY."
222   (mlet %state-monad ((cache (current-state)))
223     (match (vhash-assoc key cache)
224       ((_ . result)                               ;cache hit
225        (return result))
226       (#f                                         ;cache miss
227        (mlet %state-monad ((result (begin exp ...))
228                            (cache  (current-state)))
229          (mbegin %state-monad
230            (set-current-state (vhash-cons key result cache))
231            (return result)))))))
233 (define* (cumulative-grafts store drv grafts
234                             references
235                             #:key
236                             (outputs (derivation-output-names drv))
237                             (guile (%guile-for-build))
238                             (system (%current-system)))
239   "Augment GRAFTS with additional grafts resulting from the application of
240 GRAFTS to the dependencies of DRV; REFERENCES must be a one-argument procedure
241 that returns the list of references of the store item it is given.  Return the
242 resulting list of grafts.
244 This is a monadic procedure in %STATE-MONAD where the state is a vhash mapping
245 derivations to the corresponding set of grafts."
246   (define (graft-origin? drv graft)
247     ;; Return true if DRV corresponds to the origin of GRAFT.
248     (match graft
249       (($ <graft> (? derivation? origin) output)
250        (match (assoc-ref (derivation->output-paths drv) output)
251          ((? string? result)
252           (string=? result
253                     (derivation->output-path origin output)))
254          (_
255           #f)))
256       (_
257        #f)))
259   (define (dependency-grafts item)
260     (let-values (((drv output) (item->deriver store item)))
261       (if drv
262           ;; If GRAFTS already contains a graft from DRV, do not override it.
263           (if (find (cut graft-origin? drv <>) grafts)
264               (state-return grafts)
265               (cumulative-grafts store drv grafts references
266                                  #:outputs (list output)
267                                  #:guile guile
268                                  #:system system))
269           (state-return grafts))))
271   (with-cache (cons (derivation-file-name drv) outputs)
272     (match (non-self-references references drv outputs)
273       (()                                         ;no dependencies
274        (return grafts))
275       (deps                                       ;one or more dependencies
276        (mlet %state-monad ((grafts (mapm %state-monad dependency-grafts deps)))
277          (let ((grafts (delete-duplicates (concatenate grafts) equal?)))
278            (match (filter (lambda (graft)
279                             (member (graft-origin-file-name graft) deps))
280                           grafts)
281              (()
282               (return grafts))
283              ((applicable ..1)
284               ;; Use APPLICABLE, the subset of GRAFTS that is really
285               ;; applicable to DRV, to avoid creating several identical
286               ;; grafted variants of DRV.
287               (let* ((new    (graft-derivation/shallow store drv applicable
288                                                        #:outputs outputs
289                                                        #:guile guile
290                                                        #:system system))
291                      (grafts (append (map (lambda (output)
292                                             (graft
293                                               (origin drv)
294                                               (origin-output output)
295                                               (replacement new)
296                                               (replacement-output output)))
297                                           outputs)
298                                      grafts)))
299                 (return grafts))))))))))
301 (define* (graft-derivation store drv grafts
302                            #:key
303                            (guile (%guile-for-build))
304                            (outputs (derivation-output-names drv))
305                            (system (%current-system)))
306   "Apply GRAFTS to the OUTPUTS of DRV and all their dependencies, recursively.
307 That is, if GRAFTS apply only indirectly to DRV, graft the dependencies of
308 DRV, and graft DRV itself to refer to those grafted dependencies."
310   ;; First, pre-compute the dependency tree of the outputs of DRV.  Do this
311   ;; upfront to have as much parallelism as possible when querying substitute
312   ;; info or when building DRV.
313   (define references
314     (references-oracle store drv))
316   (match (run-with-state
317              (cumulative-grafts store drv grafts references
318                                 #:outputs outputs
319                                 #:guile guile #:system system)
320            vlist-null)                            ;the initial cache
321     ((first . rest)
322      ;; If FIRST is not a graft for DRV, it means that GRAFTS are not
323      ;; applicable to DRV and nothing needs to be done.
324      (if (equal? drv (graft-origin first))
325          (graft-replacement first)
326          drv))))
329 ;; The following might feel more at home in (guix packages) but since (guix
330 ;; gexp), which is a lower level, needs them, we put them here.
332 (define %graft?
333   ;; Whether to honor package grafts by default.
334   (make-parameter #t))
336 (define (set-grafting enable?)
337   "This monadic procedure enables grafting when ENABLE? is true, and disables
338 it otherwise.  It returns the previous setting."
339   (lambda (store)
340     (values (%graft? enable?) store)))
342 (define (grafting?)
343   "Return a Boolean indicating whether grafting is enabled."
344   (lambda (store)
345     (values (%graft?) store)))
347 ;; Local Variables:
348 ;; eval: (put 'with-cache 'scheme-indent-function 1)
349 ;; End:
351 ;;; grafts.scm ends here