Merge branch 'master' into staging
[jackhill/guix/guix.git] / guix / self.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 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/>.
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)
29 #:use-module (guix modules)
30 #:use-module ((guix build utils) #:select (find-files))
31 #:use-module ((guix build compile) #:select (%lightweight-optimizations))
32 #:use-module (srfi srfi-1)
33 #:use-module (srfi srfi-9)
34 #:use-module (srfi srfi-35)
35 #:use-module (ice-9 match)
36 #:export (make-config.scm
37 whole-package ;for internal use in 'guix pull'
38 compiled-guix
39 guix-derivation))
40
41 \f
42 ;;;
43 ;;; Dependency handling.
44 ;;;
45
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))
56 ("guile-sqlite3" (ref '(gnu packages guile) 'guile-sqlite3))
57 ("guile-gcrypt" (ref '(gnu packages gnupg) 'guile-gcrypt))
58 ("gnutls" (ref '(gnu packages tls) 'gnutls))
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))
63 (_ #f)))) ;no such package
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
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
102 (define (node-source+compiled node)
103 "Return a \"bundle\" containing both the source code and object files for
104 NODE'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
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
172 DEPENDENCIES (a list of nodes). EXTRA-MODULES is a list of additional modules
173 added to the source, and EXTRA-FILES is a list of additional files.
174 EXTENSIONS is a set of full-blown Guile packages (e.g., 'guile-json') that
175 must 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))
180 (source (file-mapping (string-append name "-source")
181 (append module-files extra-files))))
182 (node name modules source dependencies
183 (compiled-modules name source
184 (map car module-files)
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
193 list 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
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.
201 When ITEM is a plain file name (a string), simply return a 'local-file'
202 record with the new file name."
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.
208 (local-file (string-append item "/" file)
209 #:recursive? recursive?))
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.
214 (file-append item "/" file))))
215
216 (define* (locale-data source domain
217 #:optional (directory domain))
218 "Return the locale data from 'po/DIRECTORY' in SOURCE, corresponding to
219 DOMAIN, 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
232 #+(file-append* source (string-append "po/" directory)))
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
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
266 (define glibc-utf8-locales
267 (module-ref (resolve-interface '(gnu packages base))
268 'glibc-utf8-locales))
269
270 (define documentation
271 (file-append* source "doc"))
272
273 (define examples
274 (file-append* source "gnu/system/examples"))
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)
288 (let ((version "0.0-git"))
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
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
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"
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]+)?$")))))
361
362 (computed-file "guix-manual" build))
363
364 (define* (guile-module-union things #:key (name "guix-module-union"))
365 "Return the union of the subset of THINGS (packages, computed files, etc.)
366 that 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
389 #:key source (dependencies '())
390 guile (guile-version (effective-version)))
391 "Return the 'guix' command such that it adds MODULES and DEPENDENCIES in its
392 load path."
393 (define glibc-utf8-locales
394 (module-ref (resolve-interface '(gnu packages base))
395 'glibc-utf8-locales))
396
397 (define module-directory
398 ;; To minimize the number of 'stat' calls needed to locate a module,
399 ;; create the union of all the module directories.
400 (guile-module-union (cons modules dependencies)))
401
402 (program-file "guix-command"
403 #~(begin
404 (set! %load-path
405 (cons (string-append #$module-directory
406 "/share/guile/site/"
407 (effective-version))
408 %load-path))
409
410 (set! %load-compiled-path
411 (cons (string-append #$module-directory
412 "/lib/guile/"
413 (effective-version)
414 "/site-ccache")
415 %load-compiled-path))
416
417 ;; To maximize the chances that locales are set up right
418 ;; out-of-the-box, bundle "common" UTF-8 locales.
419 (let ((locpath (getenv "GUIX_LOCPATH")))
420 (setenv "GUIX_LOCPATH"
421 (string-append (if locpath
422 (string-append locpath ":")
423 "")
424 #$(file-append glibc-utf8-locales
425 "/lib/locale"))))
426
427 (let ((guix-main (module-ref (resolve-interface '(guix ui))
428 'guix-main)))
429 #$(if source
430 #~(begin
431 (bindtextdomain "guix"
432 #$(locale-data source "guix"))
433 (bindtextdomain "guix-packages"
434 #$(locale-data source
435 "guix-packages"
436 "packages")))
437 #t)
438
439 ;; XXX: It would be more convenient to change it to:
440 ;; (exit (apply guix-main (command-line)))
441 (apply guix-main (command-line))))
442 #:guile guile))
443
444 (define (miscellaneous-files source)
445 "Return data files taken from SOURCE."
446 (file-mapping "guix-misc"
447 `(("etc/bash_completion.d/guix"
448 ,(file-append* source "/etc/completion/bash/guix"))
449 ("etc/bash_completion.d/guix-daemon"
450 ,(file-append* source "/etc/completion/bash/guix-daemon"))
451 ("share/zsh/site-functions/_guix"
452 ,(file-append* source "/etc/completion/zsh/_guix"))
453 ("share/fish/vendor_completions.d/guix.fish"
454 ,(file-append* source "/etc/completion/fish/guix.fish"))
455 ("share/guix/hydra.gnu.org.pub"
456 ,(file-append* source
457 "/etc/substitutes/hydra.gnu.org.pub"))
458 ("share/guix/berlin.guixsd.org.pub"
459 ,(file-append* source
460 "/etc/substitutes/berlin.guixsd.org.pub"))
461 ("share/guix/ci.guix.info.pub" ;alias
462 ,(file-append* source "/etc/substitutes/berlin.guixsd.org.pub")))))
463
464 (define* (whole-package name modules dependencies
465 #:key
466 (guile-version (effective-version))
467 info daemon miscellany
468 guile
469 (command (guix-command modules
470 #:dependencies dependencies
471 #:guile guile
472 #:guile-version guile-version)))
473 "Return the whole Guix package NAME that uses MODULES, a derivation of all
474 the modules (under share/guile/site and lib/guile), and DEPENDENCIES, a list
475 of packages depended on. COMMAND is the 'guix' program to use; INFO is the
476 Info manual."
477 (define (wrap daemon)
478 (program-file "guix-daemon"
479 #~(begin
480 (setenv "GUIX" #$command)
481 (apply execl #$(file-append daemon "/bin/guix-daemon")
482 "guix-daemon" (cdr (command-line))))))
483
484 (computed-file name
485 (with-imported-modules '((guix build utils))
486 #~(begin
487 (use-modules (guix build utils))
488
489 (define daemon
490 #$(and daemon (wrap daemon)))
491
492 (mkdir-p (string-append #$output "/bin"))
493 (symlink #$command
494 (string-append #$output "/bin/guix"))
495
496 (when daemon
497 (symlink daemon
498 (string-append #$output "/bin/guix-daemon")))
499
500 (let ((share (string-append #$output "/share"))
501 (lib (string-append #$output "/lib"))
502 (info #$info))
503 (mkdir-p share)
504 (symlink #$(file-append modules "/share/guile")
505 (string-append share "/guile"))
506 (when info
507 (symlink #$info (string-append share "/info")))
508
509 (mkdir-p lib)
510 (symlink #$(file-append modules "/lib/guile")
511 (string-append lib "/guile")))
512
513 (when #$miscellany
514 (copy-recursively #$miscellany #$output
515 #:log (%make-void-port "w")))))))
516
517 (define* (compiled-guix source #:key (version %guix-version)
518 (pull-version 1)
519 (name (string-append "guix-" version))
520 (guile-version (effective-version))
521 (guile-for-build (default-guile))
522 (zlib (specification->package "zlib"))
523 (gzip (specification->package "gzip"))
524 (bzip2 (specification->package "bzip2"))
525 (xz (specification->package "xz"))
526 (guix (specification->package "guix")))
527 "Return a file-like object that contains a compiled Guix."
528 (define guile-json
529 (specification->package "guile-json"))
530
531 (define guile-ssh
532 (specification->package "guile-ssh"))
533
534 (define guile-git
535 (specification->package "guile-git"))
536
537 (define guile-sqlite3
538 (specification->package "guile-sqlite3"))
539
540 (define guile-gcrypt
541 (specification->package "guile-gcrypt"))
542
543 (define gnutls
544 (specification->package "gnutls"))
545
546 (define dependencies
547 (match (append-map (lambda (package)
548 (cons (list "x" package)
549 (package-transitive-propagated-inputs package)))
550 (list guile-gcrypt gnutls guile-git guile-json
551 guile-ssh guile-sqlite3))
552 (((labels packages _ ...) ...)
553 packages)))
554
555 (define *core-modules*
556 (scheme-node "guix-core"
557 '((guix)
558 (guix monad-repl)
559 (guix packages)
560 (guix download)
561 (guix discovery)
562 (guix profiles)
563 (guix build-system gnu)
564 (guix build-system trivial)
565 (guix build profiles)
566 (guix build gnu-build-system))
567
568 ;; Provide a dummy (guix config) with the default version
569 ;; number, storedir, etc. This is so that "guix-core" is the
570 ;; same across all installations and doesn't need to be
571 ;; rebuilt when the version changes, which in turn means we
572 ;; can have substitutes for it.
573 #:extra-modules
574 `(((guix config) => ,(make-config.scm)))
575
576 ;; (guix man-db) is needed at build-time by (guix profiles)
577 ;; but we don't need to compile it; not compiling it allows
578 ;; us to avoid an extra dependency on guile-gdbm-ffi.
579 #:extra-files
580 `(("guix/man-db.scm" ,(local-file "../guix/man-db.scm"))
581 ("guix/store/schema.sql"
582 ,(local-file "../guix/store/schema.sql")))
583
584 #:extensions (list guile-gcrypt)
585 #:guile-for-build guile-for-build))
586
587 (define *extra-modules*
588 (scheme-node "guix-extra"
589 (filter-map (match-lambda
590 (('guix 'scripts _ ..1) #f)
591 (('guix 'man-db) #f)
592 (name name))
593 (scheme-modules* source "guix"))
594 (list *core-modules*)
595 #:extensions dependencies
596 #:guile-for-build guile-for-build))
597
598 (define *core-package-modules*
599 (scheme-node "guix-packages-base"
600 `((gnu packages)
601 (gnu packages base))
602 (list *core-modules* *extra-modules*)
603 #:extensions dependencies
604
605 ;; Add all the non-Scheme files here. We must do it here so
606 ;; that 'search-patches' & co. can find them. Ideally we'd
607 ;; keep them next to the .scm files that use them but it's
608 ;; difficult to do (XXX).
609 #:extra-files
610 (file-imports source "gnu/packages"
611 (lambda (file stat)
612 (and (eq? 'regular (stat:type stat))
613 (not (string-suffix? ".scm" file))
614 (not (string-suffix? ".go" file))
615 (not (string-prefix? ".#" file))
616 (not (string-suffix? "~" file)))))
617 #:guile-for-build guile-for-build))
618
619 (define *package-modules*
620 (scheme-node "guix-packages"
621 (scheme-modules* source "gnu/packages")
622 (list *core-modules* *extra-modules* *core-package-modules*)
623 #:extensions dependencies
624 #:guile-for-build guile-for-build))
625
626 (define *system-modules*
627 (scheme-node "guix-system"
628 `((gnu system)
629 (gnu services)
630 ,@(scheme-modules* source "gnu/bootloader")
631 ,@(scheme-modules* source "gnu/system")
632 ,@(scheme-modules* source "gnu/services"))
633 (list *core-package-modules* *package-modules*
634 *extra-modules* *core-modules*)
635 #:extensions dependencies
636 #:extra-files
637 (append (file-imports source "gnu/system/examples"
638 (const #t))
639
640 ;; All the installer code is on the build-side.
641 (file-imports source "gnu/installer/"
642 (const #t))
643 ;; Build-side code that we don't build. Some of
644 ;; these depend on guile-rsvg, the Shepherd, etc.
645 (file-imports source "gnu/build" (const #t)))
646 #:guile-for-build
647 guile-for-build))
648
649 (define *cli-modules*
650 (scheme-node "guix-cli"
651 (append (scheme-modules* source "/guix/scripts")
652 `((gnu ci)))
653 (list *core-modules* *extra-modules*
654 *core-package-modules* *package-modules*
655 *system-modules*)
656 #:extensions dependencies
657 #:guile-for-build guile-for-build))
658
659 (define *system-test-modules*
660 ;; Ship these modules mostly so (gnu ci) can discover them.
661 (scheme-node "guix-system-tests"
662 `((gnu tests)
663 ,@(scheme-modules* source "gnu/tests"))
664 (list *core-package-modules* *package-modules*
665 *extra-modules* *system-modules* *core-modules*
666 *cli-modules*) ;for (guix scripts pack), etc.
667 #:extensions dependencies
668 #:guile-for-build guile-for-build))
669
670 (define *config*
671 (scheme-node "guix-config"
672 '()
673 #:extra-modules
674 `(((guix config)
675 => ,(make-config.scm #:zlib zlib
676 #:gzip gzip
677 #:bzip2 bzip2
678 #:xz xz
679 #:package-name
680 %guix-package-name
681 #:package-version
682 version
683 #:bug-report-address
684 %guix-bug-report-address
685 #:home-page-url
686 %guix-home-page-url)))
687 #:guile-for-build guile-for-build))
688
689 (define (built-modules node-subset)
690 (directory-union (string-append name "-modules")
691 (append-map node-subset
692
693 ;; Note: *CONFIG* comes first so that it
694 ;; overrides the (guix config) module that
695 ;; comes with *CORE-MODULES*.
696 (list *config*
697 *cli-modules*
698 *system-test-modules*
699 *system-modules*
700 *package-modules*
701 *core-package-modules*
702 *extra-modules*
703 *core-modules*))
704
705 ;; Silently choose the first entry upon collision so that
706 ;; we choose *CONFIG*.
707 #:resolve-collision 'first
708
709 ;; When we do (add-to-store "utils.scm"), "utils.scm" must
710 ;; be a regular file, not a symlink. Thus, arrange so that
711 ;; regular files appear as regular files in the final
712 ;; output.
713 #:copy? #t
714 #:quiet? #t))
715
716 ;; Version 0 of 'guix pull' meant we'd just return Scheme modules.
717 ;; Version 1 is when we return the full package.
718 (cond ((= 1 pull-version)
719 ;; The whole package, with a standard file hierarchy.
720 (let* ((modules (built-modules (compose list node-source+compiled)))
721 (command (guix-command modules
722 #:source source
723 #:dependencies dependencies
724 #:guile guile-for-build
725 #:guile-version guile-version)))
726 (whole-package name modules dependencies
727 #:command command
728 #:guile guile-for-build
729
730 ;; Include 'guix-daemon'. XXX: Here we inject an
731 ;; older snapshot of guix-daemon, but that's a good
732 ;; enough approximation for now.
733 #:daemon (module-ref (resolve-interface
734 '(gnu packages
735 package-management))
736 'guix-daemon)
737
738 #:info (info-manual source)
739 #:miscellany (miscellaneous-files source)
740 #:guile-version guile-version)))
741 ((= 0 pull-version)
742 ;; Legacy 'guix pull': return the .scm and .go files as one
743 ;; directory.
744 (built-modules (lambda (node)
745 (list (node-source node)
746 (node-compiled node)))))
747 (else
748 ;; Unsupported 'guix pull' version.
749 #f)))
750
751 \f
752 ;;;
753 ;;; Generating (guix config).
754 ;;;
755
756 (define %dependency-variables
757 ;; (guix config) variables corresponding to dependencies.
758 '(%libz %xz %gzip %bzip2))
759
760 (define %persona-variables
761 ;; (guix config) variables that define Guix's persona.
762 '(%guix-package-name
763 %guix-version
764 %guix-bug-report-address
765 %guix-home-page-url))
766
767 (define %config-variables
768 ;; (guix config) variables corresponding to Guix configuration.
769 (letrec-syntax ((variables (syntax-rules ()
770 ((_)
771 '())
772 ((_ variable rest ...)
773 (cons `(variable . ,variable)
774 (variables rest ...))))))
775 (variables %localstatedir %storedir %sysconfdir)))
776
777 (define* (make-config.scm #:key zlib gzip xz bzip2
778 (package-name "GNU Guix")
779 (package-version "0")
780 (bug-report-address "bug-guix@gnu.org")
781 (home-page-url "https://gnu.org/s/guix"))
782
783 ;; Hack so that Geiser is not confused.
784 (define defmod 'define-module)
785
786 (scheme-file "config.scm"
787 #~(;; The following expressions get spliced.
788 (#$defmod (guix config)
789 #:export (%guix-package-name
790 %guix-version
791 %guix-bug-report-address
792 %guix-home-page-url
793 %system
794 %store-directory
795 %state-directory
796 %store-database-directory
797 %config-directory
798 %libz
799 %gzip
800 %bzip2
801 %xz))
802
803 (define %system
804 #$(%current-system))
805
806 #$@(map (match-lambda
807 ((name . value)
808 #~(define-public #$name #$value)))
809 %config-variables)
810
811 (define %store-directory
812 (or (and=> (getenv "NIX_STORE_DIR") canonicalize-path)
813 %storedir))
814
815 (define %state-directory
816 ;; This must match `NIX_STATE_DIR' as defined in
817 ;; `nix/local.mk'.
818 (or (getenv "GUIX_STATE_DIRECTORY")
819 (string-append %localstatedir "/guix")))
820
821 (define %store-database-directory
822 (or (getenv "GUIX_DATABASE_DIRECTORY")
823 (string-append %state-directory "/db")))
824
825 (define %config-directory
826 ;; This must match `GUIX_CONFIGURATION_DIRECTORY' as
827 ;; defined in `nix/local.mk'.
828 (or (getenv "GUIX_CONFIGURATION_DIRECTORY")
829 (string-append %sysconfdir "/guix")))
830
831 (define %guix-package-name #$package-name)
832 (define %guix-version #$package-version)
833 (define %guix-bug-report-address #$bug-report-address)
834 (define %guix-home-page-url #$home-page-url)
835
836 (define %gzip
837 #+(and gzip (file-append gzip "/bin/gzip")))
838 (define %bzip2
839 #+(and bzip2 (file-append bzip2 "/bin/bzip2")))
840 (define %xz
841 #+(and xz (file-append xz "/bin/xz")))
842
843 (define %libz
844 #+(and zlib
845 (file-append zlib "/lib/libz"))))
846
847 ;; Guile 2.0 *requires* the 'define-module' to be at the
848 ;; top-level or the 'toplevel-ref' in the resulting .go file are
849 ;; made relative to a nonexistent anonymous module.
850 #:splice? #t))
851
852 \f
853 ;;;
854 ;;; Building.
855 ;;;
856
857 (define* (compiled-modules name module-tree module-files
858 #:optional
859 (dependencies '())
860 (dependencies-compiled '())
861 #:key
862 (extensions '()) ;full-blown Guile packages
863 parallel?
864 guile-for-build)
865 "Build all the MODULE-FILES from MODULE-TREE. MODULE-FILES must be a list
866 like '(\"guix/foo.scm\" \"gnu/bar.scm\") and MODULE-TREE is the directory
867 containing MODULE-FILES and possibly other files as well."
868 ;; This is a non-monadic, enhanced version of 'compiled-file' from (guix
869 ;; gexp).
870 (define build
871 (with-imported-modules (source-module-closure
872 '((guix build compile)
873 (guix build utils)))
874 #~(begin
875 (use-modules (srfi srfi-26)
876 (ice-9 match)
877 (ice-9 format)
878 (ice-9 threads)
879 (guix build compile)
880 (guix build utils))
881
882 (define (regular? file)
883 (not (member file '("." ".."))))
884
885 (define (report-load file total completed)
886 (display #\cr)
887 (format #t
888 "[~3@a/~3@a] loading...\t~5,1f% of ~d files"
889
890 ;; Note: Multiply TOTAL by two to account for the
891 ;; compilation phase that follows.
892 completed (* total 2)
893
894 (* 100. (/ completed total)) total)
895 (force-output))
896
897 (define (report-compilation file total completed)
898 (display #\cr)
899 (format #t "[~3@a/~3@a] compiling...\t~5,1f% of ~d files"
900
901 ;; Add TOTAL to account for the load phase that came
902 ;; before.
903 (+ total completed) (* total 2)
904
905 (* 100. (/ completed total)) total)
906 (force-output))
907
908 (define (process-directory directory files output)
909 ;; Hide compilation warnings.
910 (parameterize ((current-warning-port (%make-void-port "w")))
911 (compile-files directory #$output files
912 #:workers (parallel-job-count)
913 #:report-load report-load
914 #:report-compilation report-compilation)))
915
916 (setvbuf (current-output-port) 'line)
917 (setvbuf (current-error-port) 'line)
918
919 (set! %load-path (cons #+module-tree %load-path))
920 (set! %load-path
921 (append '#+dependencies
922 (map (lambda (extension)
923 (string-append extension "/share/guile/site/"
924 (effective-version)))
925 '#+extensions)
926 %load-path))
927
928 (set! %load-compiled-path
929 (append '#+dependencies-compiled
930 (map (lambda (extension)
931 (string-append extension "/lib/guile/"
932 (effective-version)
933 "/site-ccache"))
934 '#+extensions)
935 %load-compiled-path))
936
937 ;; Load the compiler modules upfront.
938 (compile #f)
939
940 (mkdir #$output)
941 (chdir #+module-tree)
942 (process-directory "." '#+module-files #$output)
943 (newline))))
944
945 (computed-file name build
946 #:guile guile-for-build
947 #:options
948 `(#:local-build? #f ;allow substitutes
949
950 ;; Don't annoy people about _IONBF deprecation.
951 ;; Initialize 'terminal-width' in (system repl debug)
952 ;; to a large-enough value to make backtrace more
953 ;; verbose.
954 #:env-vars (("GUILE_WARN_DEPRECATED" . "no")
955 ("COLUMNS" . "200")))))
956
957 \f
958 ;;;
959 ;;; Building.
960 ;;;
961
962 (define* (guix-derivation source version
963 #:optional (guile-version (effective-version))
964 #:key (pull-version 0))
965 "Return, as a monadic value, the derivation to build the Guix from SOURCE
966 for GUILE-VERSION. Use VERSION as the version string. PULL-VERSION specifies
967 the version of the 'guix pull' protocol. Return #f if this PULL-VERSION value
968 is not supported."
969 (define (shorten version)
970 (if (and (string-every char-set:hex-digit version)
971 (> (string-length version) 9))
972 (string-take version 9) ;Git commit
973 version))
974
975 (define guile
976 ;; When PULL-VERSION >= 1, produce a self-contained Guix and use Guile 2.2
977 ;; unconditionally.
978 (default-guile))
979
980 (when (and (< pull-version 1)
981 (not (string=? (package-version guile) guile-version)))
982 ;; Guix < 0.15.0 has PULL-VERSION = 0, where the host Guile is reused and
983 ;; can be any version. When that happens and Guile is not current (e.g.,
984 ;; it's Guile 2.0), just bail out.
985 (raise (condition
986 (&message
987 (message "Guix is too old and cannot be upgraded")))))
988
989 (mbegin %store-monad
990 (set-guile-for-build guile)
991 (let ((guix (compiled-guix source
992 #:version version
993 #:name (string-append "guix-"
994 (shorten version))
995 #:pull-version pull-version
996 #:guile-version (if (>= pull-version 1)
997 "2.2" guile-version)
998 #:guile-for-build guile)))
999 (if guix
1000 (lower-object guix)
1001 (return #f)))))