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