self: Remove leftover export.
[jackhill/guix/guix.git] / guix / self.scm
CommitLineData
eaae07ec 1;;; GNU Guix --- Functional package management for GNU
76832d34 2;;; Copyright © 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
eaae07ec
LC
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/>.
18
19(define-module (guix self)
20 #:use-module (guix config)
21 #:use-module (guix i18n)
22 #:use-module (guix modules)
23 #:use-module (guix gexp)
24 #:use-module (guix store)
25 #:use-module (guix monads)
26 #:use-module (guix discovery)
27 #:use-module (guix packages)
28 #:use-module (guix sets)
eaae07ec 29 #:use-module (guix modules)
45779fa6 30 #:use-module ((guix build utils) #:select (find-files))
eaae07ec
LC
31 #:use-module ((guix build compile) #:select (%lightweight-optimizations))
32 #:use-module (srfi srfi-1)
33 #:use-module (srfi srfi-9)
6e54e488 34 #:use-module (srfi srfi-35)
eaae07ec
LC
35 #:use-module (ice-9 match)
36 #:export (make-config.scm
8a0d9bc8 37 whole-package ;for internal use in 'guix pull'
eaae07ec 38 compiled-guix
567f0d25 39 guix-derivation))
eaae07ec
LC
40
41\f
42;;;
43;;; Dependency handling.
44;;;
45
eaae07ec
LC
46(define specification->package
47 ;; Use our own variant of that procedure because that of (gnu packages)
48 ;; would traverse all the .scm files, which is wasteful.
49 (let ((ref (lambda (module variable)
50 (module-ref (resolve-interface module) variable))))
51 (match-lambda
52 ("guile" (ref '(gnu packages commencement) 'guile-final))
53 ("guile-json" (ref '(gnu packages guile) 'guile-json))
54 ("guile-ssh" (ref '(gnu packages ssh) 'guile-ssh))
55 ("guile-git" (ref '(gnu packages guile) 'guile-git))
d59e75f3 56 ("guile-sqlite3" (ref '(gnu packages guile) 'guile-sqlite3))
ca719424 57 ("guile-gcrypt" (ref '(gnu packages gnupg) 'guile-gcrypt))
108015df 58 ("gnutls" (ref '(gnu packages tls) 'gnutls))
eaae07ec
LC
59 ("zlib" (ref '(gnu packages compression) 'zlib))
60 ("gzip" (ref '(gnu packages compression) 'gzip))
61 ("bzip2" (ref '(gnu packages compression) 'bzip2))
62 ("xz" (ref '(gnu packages compression) 'xz))
6e54e488 63 (_ #f)))) ;no such package
eaae07ec
LC
64
65\f
66;;;
67;;; Derivations.
68;;;
69
70;; Node in a DAG of build tasks. Each node maps to a derivation, but it's
71;; easier to express things this way.
72(define-record-type <node>
73 (node name modules source dependencies compiled)
74 node?
75 (name node-name) ;string
76 (modules node-modules) ;list of module names
77 (source node-source) ;list of source files
78 (dependencies node-dependencies) ;list of nodes
79 (compiled node-compiled)) ;node -> lowerable object
80
f5db54ea
LC
81;; File mappings are essentially an alist as passed to 'imported-files'.
82(define-record-type <file-mapping>
83 (file-mapping name alist)
84 file-mapping?
85 (name file-mapping-name)
86 (alist file-mapping-alist))
87
88(define-gexp-compiler (file-mapping-compiler (mapping <file-mapping>)
89 system target)
90 ;; Here we use 'imported-files', which can arrange to directly import all
91 ;; the files instead of creating a derivation, when possible.
92 (imported-files (map (match-lambda
93 ((destination (? local-file? file))
94 (cons destination
95 (local-file-absolute-file-name file)))
96 ((destination source)
97 (cons destination source))) ;silliness
98 (file-mapping-alist mapping))
99 #:name (file-mapping-name mapping)
100 #:system system))
101
49c35bbb
LC
102(define (node-source+compiled node)
103 "Return a \"bundle\" containing both the source code and object files for
104NODE's modules, under their FHS directories: share/guile/site and lib/guile."
105 (define build
106 (with-imported-modules '((guix build utils))
107 #~(begin
108 (use-modules (guix build utils))
109
110 (define source
111 (string-append #$output "/share/guile/site/"
112 (effective-version)))
113
114 (define object
115 (string-append #$output "/lib/guile/" (effective-version)
116 "/site-ccache"))
117
118 (mkdir-p (dirname source))
119 (symlink #$(node-source node) source)
120 (mkdir-p (dirname object))
121 (symlink #$(node-compiled node) object))))
122
123 (computed-file (string-append (node-name node) "-modules")
124 build))
125
eaae07ec
LC
126(define (node-fold proc init nodes)
127 (let loop ((nodes nodes)
128 (visited (setq))
129 (result init))
130 (match nodes
131 (() result)
132 ((head tail ...)
133 (if (set-contains? visited head)
134 (loop tail visited result)
135 (loop tail (set-insert head visited)
136 (proc head result)))))))
137
138(define (node-modules/recursive nodes)
139 (node-fold (lambda (node modules)
140 (append (node-modules node) modules))
141 '()
142 nodes))
143
144(define* (closure modules #:optional (except '()))
145 (source-module-closure modules
146 #:select?
147 (match-lambda
148 (('guix 'config)
149 #f)
150 ((and module
151 (or ('guix _ ...) ('gnu _ ...)))
152 (not (member module except)))
153 (rest #f))))
154
155(define module->import
156 ;; Return a file-name/file-like object pair for the specified module and
157 ;; suitable for 'imported-files'.
158 (match-lambda
159 ((module '=> thing)
160 (let ((file (module-name->file-name module)))
161 (list file thing)))
162 (module
163 (let ((file (module-name->file-name module)))
164 (list file
165 (local-file (search-path %load-path file)))))))
166
167(define* (scheme-node name modules #:optional (dependencies '())
168 #:key (extra-modules '()) (extra-files '())
169 (extensions '())
170 parallel? guile-for-build)
171 "Return a node that builds the given Scheme MODULES, and depends on
172DEPENDENCIES (a list of nodes). EXTRA-MODULES is a list of additional modules
173added to the source, and EXTRA-FILES is a list of additional files.
174EXTENSIONS is a set of full-blown Guile packages (e.g., 'guile-json') that
175must be present in the search path."
176 (let* ((modules (append extra-modules
177 (closure modules
178 (node-modules/recursive dependencies))))
179 (module-files (map module->import modules))
f5db54ea
LC
180 (source (file-mapping (string-append name "-source")
181 (append module-files extra-files))))
eaae07ec 182 (node name modules source dependencies
8031b3fa
LC
183 (compiled-modules name source
184 (map car module-files)
eaae07ec
LC
185 (map node-source dependencies)
186 (map node-compiled dependencies)
187 #:extensions extensions
188 #:parallel? parallel?
189 #:guile-for-build guile-for-build))))
190
191(define (file-imports directory sub-directory pred)
192 "List all the files matching PRED under DIRECTORY/SUB-DIRECTORY. Return a
193list of file-name/file-like objects suitable as inputs to 'imported-files'."
194 (map (lambda (file)
195 (list (string-drop file (+ 1 (string-length directory)))
196 (local-file file #:recursive? #t)))
197 (find-files (string-append directory "/" sub-directory) pred)))
198
6cf502d1
LC
199(define* (file-append* item file #:key (recursive? #t))
200 "Return FILE within ITEM, which may be a file name or a file-like object.
201When ITEM is a plain file name (a string), simply return a 'local-file'
202record with the new file name."
9f1c3559
LC
203 (match item
204 ((? string?)
205 ;; This is the optimal case: we return a new "source". Thus, a
206 ;; derivation that depends on this sub-directory does not depend on ITEM
207 ;; itself.
6cf502d1
LC
208 (local-file (string-append item "/" file)
209 #:recursive? recursive?))
9f1c3559
LC
210 ;; TODO: Add 'local-file?' case.
211 (_
212 ;; In this case, anything that refers to the result also depends on ITEM,
213 ;; which isn't great.
6cf502d1 214 (file-append item "/" file))))
9f1c3559
LC
215
216(define* (locale-data source domain
217 #:optional (directory domain))
218 "Return the locale data from 'po/DIRECTORY' in SOURCE, corresponding to
219DOMAIN, a gettext domain."
220 (define gettext
221 (module-ref (resolve-interface '(gnu packages gettext))
222 'gettext-minimal))
223
224 (define build
225 (with-imported-modules '((guix build utils))
226 #~(begin
227 (use-modules (guix build utils)
228 (srfi srfi-26)
229 (ice-9 match) (ice-9 ftw))
230
231 (define po-directory
6cf502d1 232 #+(file-append* source (string-append "po/" directory)))
9f1c3559
LC
233
234 (define (compile language)
235 (let ((gmo (string-append #$output "/" language "/LC_MESSAGES/"
236 #$domain ".mo")))
237 (mkdir-p (dirname gmo))
238 (invoke #+(file-append gettext "/bin/msgfmt")
239 "-c" "--statistics" "--verbose"
240 "-o" gmo
241 (string-append po-directory "/" language ".po"))))
242
243 (define (linguas)
244 ;; Return the list of languages. Note: don't read 'LINGUAS'
245 ;; because it contains things like 'en@boldquot' that do not have
246 ;; a corresponding .po file.
247 (map (cut basename <> ".po")
248 (scandir po-directory
249 (cut string-suffix? ".po" <>))))
250
251 (for-each compile (linguas)))))
252
253 (computed-file (string-append "guix-locale-" domain)
254 build))
255
4554d4c8
LC
256(define (info-manual source)
257 "Return the Info manual built from SOURCE."
258 (define texinfo
259 (module-ref (resolve-interface '(gnu packages texinfo))
260 'texinfo))
261
262 (define graphviz
263 (module-ref (resolve-interface '(gnu packages graphviz))
264 'graphviz))
265
2d337760
LC
266 (define glibc-utf8-locales
267 (module-ref (resolve-interface '(gnu packages base))
268 'glibc-utf8-locales))
269
4554d4c8 270 (define documentation
6cf502d1 271 (file-append* source "doc"))
4554d4c8
LC
272
273 (define examples
6cf502d1 274 (file-append* source "gnu/system/examples"))
4554d4c8
LC
275
276 (define build
277 (with-imported-modules '((guix build utils))
278 #~(begin
279 (use-modules (guix build utils))
280
281 (mkdir #$output)
282
283 ;; Create 'version.texi'.
284 ;; XXX: Can we use a more meaningful version string yet one that
285 ;; doesn't change at each commit?
286 (call-with-output-file "version.texi"
287 (lambda (port)
cbe7387c 288 (let ((version "0.0-git"))
4554d4c8
LC
289 (format port "
290@set UPDATED 1 January 1970
291@set UPDATED-MONTH January 1970
292@set EDITION ~a
293@set VERSION ~a\n" version version))))
294
295 ;; Copy configuration templates that the manual includes.
296 (for-each (lambda (template)
297 (copy-file template
298 (string-append
299 "os-config-"
300 (basename template ".tmpl")
301 ".texi")))
302 (find-files #$examples "\\.tmpl$"))
303
304 ;; Build graphs.
305 (mkdir-p (string-append #$output "/images"))
306 (for-each (lambda (dot-file)
307 (invoke #+(file-append graphviz "/bin/dot")
308 "-Tpng" "-Gratio=.9" "-Gnodesep=.005"
309 "-Granksep=.00005" "-Nfontsize=9"
310 "-Nheight=.1" "-Nwidth=.1"
311 "-o" (string-append #$output "/images/"
312 (basename dot-file ".dot")
313 ".png")
314 dot-file))
315 (find-files (string-append #$documentation "/images")
316 "\\.dot$"))
317
318 ;; Copy other PNGs.
319 (for-each (lambda (png-file)
320 (install-file png-file
321 (string-append #$output "/images")))
322 (find-files (string-append #$documentation "/images")
323 "\\.png$"))
324
325 ;; Finally build the manual. Copy it the Texinfo files to $PWD and
326 ;; add a symlink to the 'images' directory so that 'makeinfo' can
327 ;; see those images and produce image references in the Info output.
328 (copy-recursively #$documentation "."
329 #:log (%make-void-port "w"))
330 (delete-file-recursively "images")
331 (symlink (string-append #$output "/images") "images")
332
2d337760
LC
333 ;; Provide UTF-8 locales needed by the 'xspara.c' code in makeinfo.
334 (setenv "GUIX_LOCPATH"
335 #+(file-append glibc-utf8-locales "/lib/locale"))
336
4554d4c8
LC
337 (for-each (lambda (texi)
338 (unless (string=? "guix.texi" texi)
339 ;; Create 'version-LL.texi'.
340 (let* ((base (basename texi ".texi"))
341 (dot (string-index base #\.))
342 (tag (string-drop base (+ 1 dot))))
343 (symlink "version.texi"
344 (string-append "version-" tag ".texi"))))
345
346 (invoke #+(file-append texinfo "/bin/makeinfo")
347 texi "-I" #$documentation
348 "-I" "."
349 "-o" (string-append #$output "/"
350 (basename texi ".texi")
351 ".info")))
352 (cons "guix.texi"
08fdee39
LC
353 (find-files "." "^guix\\.[a-z]{2}\\.texi$")))
354
355 ;; Compress Info files.
356 (setenv "PATH"
357 #+(file-append (specification->package "gzip") "/bin"))
358 (for-each (lambda (file)
359 (invoke "gzip" "-9n" file))
360 (find-files #$output "\\.info(-[0-9]+)?$")))))
4554d4c8
LC
361
362 (computed-file "guix-manual" build))
363
49c35bbb
LC
364(define* (guile-module-union things #:key (name "guix-module-union"))
365 "Return the union of the subset of THINGS (packages, computed files, etc.)
366that provide Guile modules."
367 (define build
368 (with-imported-modules '((guix build union))
369 #~(begin
370 (use-modules (guix build union))
371
372 (define (modules directory)
373 (string-append directory "/share/guile/site"))
374
375 (define (objects directory)
376 (string-append directory "/lib/guile"))
377
378 (union-build #$output
379 (filter (lambda (directory)
380 (or (file-exists? (modules directory))
381 (file-exists? (objects directory))))
382 '#$things)
383
384 #:log-port (%make-void-port "w")))))
385
386 (computed-file name build))
387
388(define* (guix-command modules
a89faa3f 389 #:key source (dependencies '())
8970a886 390 guile (guile-version (effective-version)))
8a0d9bc8
LC
391 "Return the 'guix' command such that it adds MODULES and DEPENDENCIES in its
392load path."
49c35bbb
LC
393 (define module-directory
394 ;; To minimize the number of 'stat' calls needed to locate a module,
395 ;; create the union of all the module directories.
396 (guile-module-union (cons modules dependencies)))
f2d0a2cf 397
8a0d9bc8
LC
398 (program-file "guix-command"
399 #~(begin
400 (set! %load-path
49c35bbb
LC
401 (cons (string-append #$module-directory
402 "/share/guile/site/"
403 (effective-version))
404 %load-path))
8a0d9bc8 405
8a0d9bc8 406 (set! %load-compiled-path
49c35bbb
LC
407 (cons (string-append #$module-directory
408 "/lib/guile/"
409 (effective-version)
410 "/site-ccache")
a89faa3f 411 %load-compiled-path))
8a0d9bc8
LC
412
413 (let ((guix-main (module-ref (resolve-interface '(guix ui))
414 'guix-main)))
9f1c3559
LC
415 #$(if source
416 #~(begin
417 (bindtextdomain "guix"
418 #$(locale-data source "guix"))
419 (bindtextdomain "guix-packages"
420 #$(locale-data source
421 "guix-packages"
422 "packages")))
423 #t)
8a0d9bc8
LC
424
425 ;; XXX: It would be more convenient to change it to:
426 ;; (exit (apply guix-main (command-line)))
8970a886
LC
427 (apply guix-main (command-line))))
428 #:guile guile))
8a0d9bc8 429
e3744779
LC
430(define (miscellaneous-files source)
431 "Return data files taken from SOURCE."
432 (file-mapping "guix-misc"
433 `(("etc/bash_completion.d/guix"
434 ,(file-append* source "/etc/completion/bash/guix"))
435 ("etc/bash_completion.d/guix-daemon"
436 ,(file-append* source "/etc/completion/bash/guix-daemon"))
437 ("share/zsh/site-functions/_guix"
438 ,(file-append* source "/etc/completion/zsh/_guix"))
439 ("share/fish/vendor_completions.d/guix.fish"
440 ,(file-append* source "/etc/completion/fish/guix.fish"))
441 ("share/guix/hydra.gnu.org.pub"
442 ,(file-append* source
443 "/etc/substitutes/hydra.gnu.org.pub"))
444 ("share/guix/berlin.guixsd.org.pub"
6a837b60
LC
445 ,(file-append* source
446 "/etc/substitutes/berlin.guixsd.org.pub"))
447 ("share/guix/ci.guix.info.pub" ;alias
e3744779
LC
448 ,(file-append* source "/etc/substitutes/berlin.guixsd.org.pub")))))
449
8a0d9bc8 450(define* (whole-package name modules dependencies
9f1c3559
LC
451 #:key
452 (guile-version (effective-version))
e3744779 453 info daemon miscellany
8d3beb3a 454 guile
9f1c3559
LC
455 (command (guix-command modules
456 #:dependencies dependencies
8970a886 457 #:guile guile
9f1c3559 458 #:guile-version guile-version)))
8a0d9bc8 459 "Return the whole Guix package NAME that uses MODULES, a derivation of all
49c35bbb
LC
460the modules (under share/guile/site and lib/guile), and DEPENDENCIES, a list
461of packages depended on. COMMAND is the 'guix' program to use; INFO is the
462Info manual."
9f1c3559
LC
463 (computed-file name
464 (with-imported-modules '((guix build utils))
465 #~(begin
466 (use-modules (guix build utils))
e3744779 467
9f1c3559
LC
468 (mkdir-p (string-append #$output "/bin"))
469 (symlink #$command
470 (string-append #$output "/bin/guix"))
471
baed9236
LC
472 (when #$daemon
473 (symlink (string-append #$daemon "/bin/guix-daemon")
474 (string-append #$output "/bin/guix-daemon")))
475
49c35bbb
LC
476 (let ((share (string-append #$output "/share"))
477 (lib (string-append #$output "/lib"))
478 (info #$info))
479 (mkdir-p share)
480 (symlink #$(file-append modules "/share/guile")
481 (string-append share "/guile"))
4554d4c8 482 (when info
49c35bbb
LC
483 (symlink #$info (string-append share "/info")))
484
485 (mkdir-p lib)
486 (symlink #$(file-append modules "/lib/guile")
487 (string-append lib "/guile")))
a89faa3f 488
e3744779
LC
489 (when #$miscellany
490 (copy-recursively #$miscellany #$output
49c35bbb 491 #:log (%make-void-port "w")))))))
8a0d9bc8 492
eaae07ec 493(define* (compiled-guix source #:key (version %guix-version)
8a0d9bc8 494 (pull-version 1)
eaae07ec
LC
495 (name (string-append "guix-" version))
496 (guile-version (effective-version))
6e54e488 497 (guile-for-build (default-guile))
eaae07ec
LC
498 (zlib (specification->package "zlib"))
499 (gzip (specification->package "gzip"))
500 (bzip2 (specification->package "bzip2"))
501 (xz (specification->package "xz"))
502 (guix (specification->package "guix")))
503 "Return a file-like object that contains a compiled Guix."
504 (define guile-json
6e54e488 505 (specification->package "guile-json"))
eaae07ec
LC
506
507 (define guile-ssh
6e54e488 508 (specification->package "guile-ssh"))
eaae07ec
LC
509
510 (define guile-git
6e54e488 511 (specification->package "guile-git"))
eaae07ec 512
d59e75f3 513 (define guile-sqlite3
6e54e488 514 (specification->package "guile-sqlite3"))
d59e75f3 515
ca719424 516 (define guile-gcrypt
6e54e488 517 (specification->package "guile-gcrypt"))
ca719424 518
108015df 519 (define gnutls
6e54e488 520 (specification->package "gnutls"))
108015df 521
eaae07ec
LC
522 (define dependencies
523 (match (append-map (lambda (package)
524 (cons (list "x" package)
e13240f5 525 (package-transitive-propagated-inputs package)))
ca719424 526 (list guile-gcrypt gnutls guile-git guile-json
108015df 527 guile-ssh guile-sqlite3))
eaae07ec
LC
528 (((labels packages _ ...) ...)
529 packages)))
530
531 (define *core-modules*
532 (scheme-node "guix-core"
533 '((guix)
534 (guix monad-repl)
535 (guix packages)
536 (guix download)
537 (guix discovery)
538 (guix profiles)
539 (guix build-system gnu)
540 (guix build-system trivial)
541 (guix build profiles)
542 (guix build gnu-build-system))
543
544 ;; Provide a dummy (guix config) with the default version
545 ;; number, storedir, etc. This is so that "guix-core" is the
546 ;; same across all installations and doesn't need to be
547 ;; rebuilt when the version changes, which in turn means we
548 ;; can have substitutes for it.
549 #:extra-modules
ca719424 550 `(((guix config) => ,(make-config.scm)))
eaae07ec 551
8292f5a9
LC
552 ;; (guix man-db) is needed at build-time by (guix profiles)
553 ;; but we don't need to compile it; not compiling it allows
554 ;; us to avoid an extra dependency on guile-gdbm-ffi.
555 #:extra-files
3931c761
LC
556 `(("guix/man-db.scm" ,(local-file "../guix/man-db.scm"))
557 ("guix/store/schema.sql"
558 ,(local-file "../guix/store/schema.sql")))
8292f5a9 559
ca719424 560 #:extensions (list guile-gcrypt)
eaae07ec
LC
561 #:guile-for-build guile-for-build))
562
563 (define *extra-modules*
564 (scheme-node "guix-extra"
565 (filter-map (match-lambda
566 (('guix 'scripts _ ..1) #f)
8292f5a9 567 (('guix 'man-db) #f)
eaae07ec
LC
568 (name name))
569 (scheme-modules* source "guix"))
570 (list *core-modules*)
571 #:extensions dependencies
572 #:guile-for-build guile-for-build))
573
f2e66663
LC
574 (define *core-package-modules*
575 (scheme-node "guix-packages-base"
eaae07ec 576 `((gnu packages)
f2e66663 577 (gnu packages base))
eaae07ec
LC
578 (list *core-modules* *extra-modules*)
579 #:extensions dependencies
f2e66663
LC
580
581 ;; Add all the non-Scheme files here. We must do it here so
582 ;; that 'search-patches' & co. can find them. Ideally we'd
583 ;; keep them next to the .scm files that use them but it's
584 ;; difficult to do (XXX).
585 #:extra-files
eaae07ec
LC
586 (file-imports source "gnu/packages"
587 (lambda (file stat)
588 (and (eq? 'regular (stat:type stat))
589 (not (string-suffix? ".scm" file))
590 (not (string-suffix? ".go" file))
591 (not (string-prefix? ".#" file))
592 (not (string-suffix? "~" file)))))
593 #:guile-for-build guile-for-build))
594
f2e66663
LC
595 (define *package-modules*
596 (scheme-node "guix-packages"
597 (scheme-modules* source "gnu/packages")
598 (list *core-modules* *extra-modules* *core-package-modules*)
599 #:extensions dependencies
600 #:guile-for-build guile-for-build))
601
eaae07ec
LC
602 (define *system-modules*
603 (scheme-node "guix-system"
604 `((gnu system)
605 (gnu services)
a49d633c 606 ,@(scheme-modules* source "gnu/system")
eaae07ec 607 ,@(scheme-modules* source "gnu/services"))
f2e66663
LC
608 (list *core-package-modules* *package-modules*
609 *extra-modules* *core-modules*)
eaae07ec
LC
610 #:extensions dependencies
611 #:extra-files
1458f768
LC
612 (append (file-imports source "gnu/system/examples"
613 (const #t))
8bb62ae1 614
a49d633c
MO
615 ;; All the installer code is on the build-side.
616 (file-imports source "gnu/installer/"
617 (const #t))
1458f768
LC
618 ;; Build-side code that we don't build. Some of
619 ;; these depend on guile-rsvg, the Shepherd, etc.
620 (file-imports source "gnu/build" (const #t)))
eaae07ec
LC
621 #:guile-for-build
622 guile-for-build))
623
54800977
LC
624 (define *system-test-modules*
625 ;; Ship these modules mostly so (gnu ci) can refer to them.
626 (scheme-node "guix-system-tests"
627 `((gnu tests)
628 ,@(scheme-modules* source "gnu/tests"))
629 (list *core-package-modules* *package-modules*
630 *extra-modules* *system-modules* *core-modules*)
631 #:extensions dependencies
632 #:guile-for-build guile-for-build))
633
eaae07ec
LC
634 (define *cli-modules*
635 (scheme-node "guix-cli"
b5f8c2c8
LC
636 (append (scheme-modules* source "/guix/scripts")
637 `((gnu ci)))
f2e66663
LC
638 (list *core-modules* *extra-modules*
639 *core-package-modules* *package-modules*
eaae07ec
LC
640 *system-modules*)
641 #:extensions dependencies
642 #:guile-for-build guile-for-build))
643
644 (define *config*
645 (scheme-node "guix-config"
646 '()
647 #:extra-modules
648 `(((guix config)
ca719424 649 => ,(make-config.scm #:zlib zlib
eaae07ec
LC
650 #:gzip gzip
651 #:bzip2 bzip2
652 #:xz xz
eaae07ec
LC
653 #:package-name
654 %guix-package-name
655 #:package-version
656 version
657 #:bug-report-address
658 %guix-bug-report-address
659 #:home-page-url
660 %guix-home-page-url)))
661 #:guile-for-build guile-for-build))
662
a89faa3f 663 (define (built-modules node-subset)
8a0d9bc8 664 (directory-union (string-append name "-modules")
a89faa3f 665 (append-map node-subset
8a0d9bc8
LC
666
667 ;; Note: *CONFIG* comes first so that it
668 ;; overrides the (guix config) module that
669 ;; comes with *CORE-MODULES*.
670 (list *config*
671 *cli-modules*
54800977 672 *system-test-modules*
8a0d9bc8
LC
673 *system-modules*
674 *package-modules*
675 *core-package-modules*
676 *extra-modules*
677 *core-modules*))
678
679 ;; Silently choose the first entry upon collision so that
680 ;; we choose *CONFIG*.
681 #:resolve-collision 'first
682
683 ;; When we do (add-to-store "utils.scm"), "utils.scm" must
684 ;; be a regular file, not a symlink. Thus, arrange so that
685 ;; regular files appear as regular files in the final
686 ;; output.
687 #:copy? #t
688 #:quiet? #t))
689
690 ;; Version 0 of 'guix pull' meant we'd just return Scheme modules.
691 ;; Version 1 is when we return the full package.
692 (cond ((= 1 pull-version)
693 ;; The whole package, with a standard file hierarchy.
49c35bbb
LC
694 (let* ((modules (built-modules (compose list node-source+compiled)))
695 (command (guix-command modules
a89faa3f
LC
696 #:source source
697 #:dependencies dependencies
8970a886 698 #:guile guile-for-build
a89faa3f
LC
699 #:guile-version guile-version)))
700 (whole-package name modules dependencies
9f1c3559 701 #:command command
8970a886 702 #:guile guile-for-build
baed9236
LC
703
704 ;; Include 'guix-daemon'. XXX: Here we inject an
705 ;; older snapshot of guix-daemon, but that's a good
706 ;; enough approximation for now.
707 #:daemon (module-ref (resolve-interface
708 '(gnu packages
709 package-management))
710 'guix-daemon)
711
4554d4c8 712 #:info (info-manual source)
e3744779 713 #:miscellany (miscellaneous-files source)
9f1c3559 714 #:guile-version guile-version)))
8a0d9bc8 715 ((= 0 pull-version)
a89faa3f
LC
716 ;; Legacy 'guix pull': return the .scm and .go files as one
717 ;; directory.
718 (built-modules (lambda (node)
719 (list (node-source node)
720 (node-compiled node)))))
8a0d9bc8
LC
721 (else
722 ;; Unsupported 'guix pull' version.
723 #f)))
eaae07ec
LC
724
725\f
726;;;
727;;; Generating (guix config).
728;;;
729
730(define %dependency-variables
731 ;; (guix config) variables corresponding to dependencies.
ca719424 732 '(%libz %xz %gzip %bzip2))
eaae07ec
LC
733
734(define %persona-variables
735 ;; (guix config) variables that define Guix's persona.
736 '(%guix-package-name
737 %guix-version
738 %guix-bug-report-address
739 %guix-home-page-url))
740
741(define %config-variables
45779fa6
LC
742 ;; (guix config) variables corresponding to Guix configuration.
743 (letrec-syntax ((variables (syntax-rules ()
744 ((_)
745 '())
746 ((_ variable rest ...)
747 (cons `(variable . ,variable)
748 (variables rest ...))))))
7af5c2a2 749 (variables %localstatedir %storedir %sysconfdir %system)))
eaae07ec 750
ca719424 751(define* (make-config.scm #:key zlib gzip xz bzip2
eaae07ec
LC
752 (package-name "GNU Guix")
753 (package-version "0")
754 (bug-report-address "bug-guix@gnu.org")
755 (home-page-url "https://gnu.org/s/guix"))
756
757 ;; Hack so that Geiser is not confused.
758 (define defmod 'define-module)
759
760 (scheme-file "config.scm"
eb72cdf0 761 #~(;; The following expressions get spliced.
eaae07ec
LC
762 (#$defmod (guix config)
763 #:export (%guix-package-name
764 %guix-version
765 %guix-bug-report-address
766 %guix-home-page-url
7af5c2a2
LC
767 %store-directory
768 %state-directory
769 %store-database-directory
770 %config-directory
eaae07ec
LC
771 %libz
772 %gzip
773 %bzip2
e8cb9c01 774 %xz))
eaae07ec 775
63cab418
LC
776 #$@(map (match-lambda
777 ((name . value)
778 #~(define-public #$name #$value)))
779 %config-variables)
780
7af5c2a2
LC
781 (define %store-directory
782 (or (and=> (getenv "NIX_STORE_DIR") canonicalize-path)
783 %storedir))
784
785 (define %state-directory
786 ;; This must match `NIX_STATE_DIR' as defined in
787 ;; `nix/local.mk'.
788 (or (getenv "NIX_STATE_DIR")
789 (string-append %localstatedir "/guix")))
790
791 (define %store-database-directory
792 (or (getenv "NIX_DB_DIR")
793 (string-append %state-directory "/db")))
794
795 (define %config-directory
796 ;; This must match `GUIX_CONFIGURATION_DIRECTORY' as
797 ;; defined in `nix/local.mk'.
798 (or (getenv "GUIX_CONFIGURATION_DIRECTORY")
799 (string-append %sysconfdir "/guix")))
800
63cab418
LC
801 (define %guix-package-name #$package-name)
802 (define %guix-version #$package-version)
803 (define %guix-bug-report-address #$bug-report-address)
804 (define %guix-home-page-url #$home-page-url)
805
63cab418
LC
806 (define %gzip
807 #+(and gzip (file-append gzip "/bin/gzip")))
808 (define %bzip2
809 #+(and bzip2 (file-append bzip2 "/bin/bzip2")))
810 (define %xz
811 #+(and xz (file-append xz "/bin/xz")))
812
63cab418
LC
813 (define %libz
814 #+(and zlib
e8cb9c01 815 (file-append zlib "/lib/libz"))))
eb72cdf0
LC
816
817 ;; Guile 2.0 *requires* the 'define-module' to be at the
e8cb9c01 818 ;; top-level or the 'toplevel-ref' in the resulting .go file are
eb72cdf0
LC
819 ;; made relative to a nonexistent anonymous module.
820 #:splice? #t))
eaae07ec 821
eaae07ec
LC
822\f
823;;;
824;;; Building.
825;;;
826
8031b3fa 827(define* (compiled-modules name module-tree module-files
eaae07ec
LC
828 #:optional
829 (dependencies '())
830 (dependencies-compiled '())
831 #:key
832 (extensions '()) ;full-blown Guile packages
833 parallel?
834 guile-for-build)
8031b3fa
LC
835 "Build all the MODULE-FILES from MODULE-TREE. MODULE-FILES must be a list
836like '(\"guix/foo.scm\" \"gnu/bar.scm\") and MODULE-TREE is the directory
837containing MODULE-FILES and possibly other files as well."
eaae07ec
LC
838 ;; This is a non-monadic, enhanced version of 'compiled-file' from (guix
839 ;; gexp).
840 (define build
841 (with-imported-modules (source-module-closure
842 '((guix build compile)
843 (guix build utils)))
844 #~(begin
845 (use-modules (srfi srfi-26)
846 (ice-9 match)
847 (ice-9 format)
848 (ice-9 threads)
849 (guix build compile)
850 (guix build utils))
851
852 (define (regular? file)
853 (not (member file '("." ".."))))
854
855 (define (report-load file total completed)
856 (display #\cr)
857 (format #t
858 "loading...\t~5,1f% of ~d files" ;FIXME: i18n
859 (* 100. (/ completed total)) total)
860 (force-output))
861
862 (define (report-compilation file total completed)
863 (display #\cr)
864 (format #t "compiling...\t~5,1f% of ~d files" ;FIXME: i18n
865 (* 100. (/ completed total)) total)
866 (force-output))
867
8031b3fa
LC
868 (define (process-directory directory files output)
869 ;; Hide compilation warnings.
870 (parameterize ((current-warning-port (%make-void-port "w")))
871 (compile-files directory #$output files
872 #:workers (parallel-job-count)
873 #:report-load report-load
874 #:report-compilation report-compilation)))
eaae07ec 875
76832d34
LC
876 (setvbuf (current-output-port) 'none)
877 (setvbuf (current-error-port) 'none)
eaae07ec
LC
878
879 (set! %load-path (cons #+module-tree %load-path))
880 (set! %load-path
881 (append '#+dependencies
882 (map (lambda (extension)
883 (string-append extension "/share/guile/site/"
884 (effective-version)))
885 '#+extensions)
886 %load-path))
887
888 (set! %load-compiled-path
889 (append '#+dependencies-compiled
890 (map (lambda (extension)
891 (string-append extension "/lib/guile/"
892 (effective-version)
893 "/site-ccache"))
894 '#+extensions)
895 %load-compiled-path))
896
897 ;; Load the compiler modules upfront.
898 (compile #f)
899
900 (mkdir #$output)
901 (chdir #+module-tree)
8031b3fa 902 (process-directory "." '#+module-files #$output)
69447b63 903 (newline))))
eaae07ec
LC
904
905 (computed-file name build
906 #:guile guile-for-build
907 #:options
908 `(#:local-build? #f ;allow substitutes
909
910 ;; Don't annoy people about _IONBF deprecation.
54be2b4d
LC
911 ;; Initialize 'terminal-width' in (system repl debug)
912 ;; to a large-enough value to make backtrace more
913 ;; verbose.
914 #:env-vars (("GUILE_WARN_DEPRECATED" . "no")
915 ("COLUMNS" . "200")))))
eaae07ec
LC
916
917\f
918;;;
919;;; Building.
920;;;
921
eaae07ec 922(define* (guix-derivation source version
8a0d9bc8
LC
923 #:optional (guile-version (effective-version))
924 #:key (pull-version 0))
eaae07ec 925 "Return, as a monadic value, the derivation to build the Guix from SOURCE
8a0d9bc8
LC
926for GUILE-VERSION. Use VERSION as the version string. PULL-VERSION specifies
927the version of the 'guix pull' protocol. Return #f if this PULL-VERSION value
928is not supported."
eaae07ec
LC
929 (define (shorten version)
930 (if (and (string-every char-set:hex-digit version)
931 (> (string-length version) 9))
932 (string-take version 9) ;Git commit
933 version))
934
935 (define guile
099bb017
LC
936 ;; When PULL-VERSION >= 1, produce a self-contained Guix and use Guile 2.2
937 ;; unconditionally.
6e54e488
LC
938 (default-guile))
939
940 (when (and (< pull-version 1)
941 (not (string=? (package-version guile) guile-version)))
942 ;; Guix < 0.15.0 has PULL-VERSION = 0, where the host Guile is reused and
943 ;; can be any version. When that happens and Guile is not current (e.g.,
944 ;; it's Guile 2.0), just bail out.
945 (raise (condition
946 (&message
947 (message "Guix is too old and cannot be upgraded")))))
eaae07ec
LC
948
949 (mbegin %store-monad
950 (set-guile-for-build guile)
8a0d9bc8
LC
951 (let ((guix (compiled-guix source
952 #:version version
953 #:name (string-append "guix-"
954 (shorten version))
955 #:pull-version pull-version
099bb017 956 #:guile-version (if (>= pull-version 1)
d02bb02f 957 "2.2" guile-version)
8a0d9bc8
LC
958 #:guile-for-build guile)))
959 (if guix
960 (lower-object guix)
961 (return #f)))))