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