1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2020 Martin Becze <mjbecze@riseup.net>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (guix self)
21 #:use-module (guix config)
22 #:use-module (guix i18n)
23 #:use-module (guix modules)
24 #:use-module (guix gexp)
25 #:use-module (guix store)
26 #:use-module (guix monads)
27 #:use-module (guix discovery)
28 #:use-module (guix packages)
29 #:use-module (guix sets)
30 #:use-module (guix modules)
31 #:use-module ((guix utils) #:select (version-major+minor))
32 #:use-module ((guix build utils) #:select (find-files))
33 #:use-module (srfi srfi-1)
34 #:use-module (srfi srfi-9)
35 #:use-module (srfi srfi-35)
36 #:use-module (ice-9 match)
37 #:export (make-config.scm
38 whole-package ;for internal use in 'guix pull'
44 ;;; Dependency handling.
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))))
53 ("guile" (ref '(gnu packages guile) 'guile-3.0/libgc-7))
54 ("guile-avahi" (ref '(gnu packages guile-xyz) 'guile-avahi))
55 ("guile-json" (ref '(gnu packages guile) 'guile-json-4))
56 ("guile-ssh" (ref '(gnu packages ssh) 'guile-ssh))
57 ("guile-git" (ref '(gnu packages guile) 'guile-git))
58 ("guile-semver" (ref '(gnu packages guile-xyz) 'guile-semver))
59 ("guile-sqlite3" (ref '(gnu packages guile) 'guile-sqlite3))
60 ("guile-zlib" (ref '(gnu packages guile) 'guile-zlib))
61 ("guile-lzlib" (ref '(gnu packages guile) 'guile-lzlib))
62 ("guile-zstd" (ref '(gnu packages guile) 'guile-zstd))
63 ("guile-gcrypt" (ref '(gnu packages gnupg) 'guile-gcrypt))
64 ("gnutls" (ref '(gnu packages tls) 'gnutls))
65 ("gzip" (ref '(gnu packages compression) 'gzip))
66 ("bzip2" (ref '(gnu packages compression) 'bzip2))
67 ("xz" (ref '(gnu packages compression) 'xz))
68 ("po4a" (ref '(gnu packages gettext) 'po4a))
69 ("gettext" (ref '(gnu packages gettext) 'gettext-minimal))
70 ("gcc-toolchain" (ref '(gnu packages commencement) 'gcc-toolchain))
71 (_ #f)))) ;no such package
78 ;; Node in a DAG of build tasks. Each node maps to a derivation, but it's
79 ;; easier to express things this way.
80 (define-record-type <node>
81 (node name modules source dependencies compiled)
83 (name node-name) ;string
84 (modules node-modules) ;list of module names
85 (source node-source) ;list of source files
86 (dependencies node-dependencies) ;list of nodes
87 (compiled node-compiled)) ;node -> lowerable object
89 ;; File mappings are essentially an alist as passed to 'imported-files'.
90 (define-record-type <file-mapping>
91 (file-mapping name alist)
93 (name file-mapping-name)
94 (alist file-mapping-alist))
96 (define-gexp-compiler (file-mapping-compiler (mapping <file-mapping>)
98 ;; Here we use 'imported-files', which can arrange to directly import all
99 ;; the files instead of creating a derivation, when possible.
100 (imported-files (map (match-lambda
101 ((destination (? local-file? file))
103 (local-file-absolute-file-name file)))
104 ((destination source)
105 (cons destination source))) ;silliness
106 (file-mapping-alist mapping))
107 #:name (file-mapping-name mapping)
110 (define (node-source+compiled node)
111 "Return a \"bundle\" containing both the source code and object files for
112 NODE's modules, under their FHS directories: share/guile/site and lib/guile."
114 (with-imported-modules '((guix build utils))
116 (use-modules (guix build utils))
119 (string-append #$output "/share/guile/site/"
120 (effective-version)))
123 (string-append #$output "/lib/guile/" (effective-version)
126 (mkdir-p (dirname source))
127 (symlink #$(node-source node) source)
128 (mkdir-p (dirname object))
129 (symlink #$(node-compiled node) object))))
131 (computed-file (string-append (node-name node) "-modules")
133 #:options '(#:local-build? #t
135 ;; "Building" it locally is faster.
136 #:substitutable? #f)))
138 (define (node-fold proc init nodes)
139 (let loop ((nodes nodes)
145 (if (set-contains? visited head)
146 (loop tail visited result)
147 (loop tail (set-insert head visited)
148 (proc head result)))))))
150 (define (node-modules/recursive nodes)
151 (node-fold (lambda (node modules)
152 (append (node-modules node) modules))
156 (define* (closure modules #:optional (except '()))
157 (source-module-closure modules
163 (or ('guix _ ...) ('gnu _ ...)))
164 (not (member module except)))
167 (define module->import
168 ;; Return a file-name/file-like object pair for the specified module and
169 ;; suitable for 'imported-files'.
172 (let ((file (module-name->file-name module)))
175 (let ((file (module-name->file-name module)))
177 (local-file (search-path %load-path file)))))))
179 (define* (scheme-node name modules #:optional (dependencies '())
180 #:key (extra-modules '()) (extra-files '())
182 parallel? guile-for-build)
183 "Return a node that builds the given Scheme MODULES, and depends on
184 DEPENDENCIES (a list of nodes). EXTRA-MODULES is a list of additional modules
185 added to the source, and EXTRA-FILES is a list of additional files.
186 EXTENSIONS is a set of full-blown Guile packages (e.g., 'guile-json') that
187 must be present in the search path."
188 (let* ((modules (append extra-modules
190 (node-modules/recursive dependencies))))
191 (module-files (map module->import modules))
192 (source (file-mapping (string-append name "-source")
193 (append module-files extra-files))))
194 (node name modules source dependencies
195 (compiled-modules name source
196 (map car module-files)
197 (map node-source dependencies)
198 (map node-compiled dependencies)
199 #:extensions extensions
200 #:parallel? parallel?
201 #:guile-for-build guile-for-build))))
203 (define (file-imports directory sub-directory pred)
204 "List all the files matching PRED under DIRECTORY/SUB-DIRECTORY. Return a
205 list of file-name/file-like objects suitable as inputs to 'imported-files'."
207 (list (string-drop file (+ 1 (string-length directory)))
208 (local-file file #:recursive? #t)))
209 (find-files (string-append directory "/" sub-directory) pred)))
211 (define* (file-append* item file #:key (recursive? #t))
212 "Return FILE within ITEM, which may be a file name or a file-like object.
213 When ITEM is a plain file name (a string), simply return a 'local-file'
214 record with the new file name."
217 ;; This is the optimal case: we return a new "source". Thus, a
218 ;; derivation that depends on this sub-directory does not depend on ITEM
220 (local-file (string-append item "/" file)
221 #:recursive? recursive?))
222 ((? local-file? base)
223 ;; Likewise, but with a <local-file>.
224 (if (local-file-recursive? base)
225 (local-file (string-append (local-file-absolute-file-name base)
228 #:recursive? recursive?
229 #:select? (local-file-select? base))
230 (file-append base file)))
232 ;; In this case, anything that refers to the result also depends on ITEM,
233 ;; which isn't great.
234 (file-append item "/" file))))
236 (define* (locale-data source domain
237 #:optional (directory domain))
238 "Return the locale data from 'po/DIRECTORY' in SOURCE, corresponding to
239 DOMAIN, a gettext domain."
241 (module-ref (resolve-interface '(gnu packages gettext))
245 (with-imported-modules '((guix build utils))
247 (use-modules (guix build utils)
249 (ice-9 match) (ice-9 ftw))
252 #+(file-append* source (string-append "po/" directory)))
254 (define (compile language)
255 (let ((gmo (string-append #$output "/" language "/LC_MESSAGES/"
257 (mkdir-p (dirname gmo))
258 (invoke #+(file-append gettext "/bin/msgfmt")
259 "-c" "--statistics" "--verbose"
261 (string-append po-directory "/" language ".po"))))
264 ;; Return the list of languages. Note: don't read 'LINGUAS'
265 ;; because it contains things like 'en@boldquot' that do not have
266 ;; a corresponding .po file.
267 (map (cut basename <> ".po")
268 (scandir po-directory
269 (cut string-suffix? ".po" <>))))
271 (for-each compile (linguas)))))
273 (computed-file (string-append "guix-locale-" domain)
276 (define (translate-texi-manuals source)
277 "Return the translated texinfo manuals built from SOURCE."
279 (specification->package "po4a"))
282 (specification->package "gettext"))
284 (define glibc-utf8-locales
285 (module-ref (resolve-interface '(gnu packages base))
286 'glibc-utf8-locales))
288 (define documentation
289 (file-append* source "doc"))
291 (define documentation-po
292 (file-append* source "po/doc"))
295 (with-imported-modules '((guix build utils) (guix build po))
297 (use-modules (guix build utils) (guix build po)
298 (ice-9 match) (ice-9 regex) (ice-9 textual-ports)
299 (ice-9 vlist) (ice-9 threads)
302 (define (translate-tmp-texi po source output)
303 "Translate Texinfo file SOURCE using messages from PO, and write
304 the result to OUTPUT."
305 (invoke #+(file-append po4a "/bin/po4a-translate")
306 "-M" "UTF-8" "-L" "UTF-8" "-k" "0" "-f" "texinfo"
307 "-m" source "-p" po "-l" output))
309 (define (canonicalize-whitespace str)
310 ;; Change whitespace (newlines, etc.) in STR to #\space.
311 (string-map (lambda (chr)
312 (if (char-set-contains? char-set:whitespace chr)
318 ;; Texinfo cross-reference regexp.
319 (make-regexp "@(px|x)?ref\\{([^,}]+)"))
321 (define (translate-cross-references texi translations)
322 ;; Translate the cross-references that appear in TEXI, a Texinfo
323 ;; file, using the msgid/msgstr pairs from TRANSLATIONS.
325 (call-with-input-file texi get-string-all))
328 (list-matches xref-regexp content))
330 (define translation-map
332 (((msgid . str) result)
333 (vhash-cons msgid str result)))
338 ;; Iterate over MATCHES and replace cross-references with their
339 ;; translation found in TRANSLATION-MAP. (We can't use
340 ;; 'substitute*' because matches can span multiple lines.)
341 (let loop ((matches matches)
346 (string-concatenate-reverse
347 (cons (string-drop content offset) result)))
349 (let ((prefix (match:substring head 1))
350 (ref (canonicalize-whitespace (match:substring head 2))))
352 (string-append "@" (or prefix "")
354 (match (vhash-assoc ref translation-map)
360 (append (list translated
362 (string-drop content offset)
363 (- (match:start head) offset)))
366 (format (current-error-port)
367 "translated ~a cross-references in '~a'~%"
368 (length matches) texi)
369 (call-with-output-file texi
371 (display translated port))))
373 (define* (translate-texi prefix po lang
375 "Translate the manual for one language LANG using the PO file.
376 PREFIX must be the prefix of the manual, 'guix' or 'guix-cookbook'. EXTRAS is
377 a list of extra files, such as '(\"contributing\")."
378 (let ((translations (call-with-input-file po read-po-file)))
379 (for-each (lambda (file)
380 (translate-tmp-texi po (string-append file ".texi")
381 (string-append file "." lang
383 (cons prefix extras))
385 (for-each (lambda (file)
386 (let* ((texi (string-append file "." lang ".texi"))
387 (tmp (string-append texi ".tmp")))
389 (translate-cross-references texi
391 (cons prefix extras))))
393 (define (available-translations directory domain)
394 ;; Return the list of available translations under DIRECTORY for
395 ;; DOMAIN, a gettext domain such as "guix-manual". The result is
396 ;; a list of language/PO file pairs.
397 (filter-map (lambda (po)
398 (let ((base (basename po)))
399 (and (string-prefix? (string-append domain ".")
401 (match (string-split base #\.)
404 (find-files directory
405 "\\.[a-z]{2}(_[A-Z]{2})?\\.po$")))
407 (define parallel-jobs
408 ;; Limit thread creation by 'n-par-for-each'. Going beyond can
409 ;; lead libgc 8.0.4 to abort with:
410 ;; mmap(PROT_NONE) failed
411 (min (parallel-job-count) 4))
414 (copy-recursively #$documentation "."
415 #:log (%make-void-port "w"))
419 (copy-file file (basename file)))
420 (find-files #$documentation-po ".*.po$"))
422 (setenv "GUIX_LOCPATH"
423 #+(file-append glibc-utf8-locales "/lib/locale"))
424 (setenv "PATH" #+(file-append gettext "/bin"))
425 (setenv "LC_ALL" "en_US.UTF-8")
426 (setlocale LC_ALL "en_US.UTF-8")
428 (n-par-for-each parallel-jobs
431 (translate-texi "guix" po language
432 #:extras '("contributing"))))
433 (available-translations "." "guix-manual"))
435 (n-par-for-each parallel-jobs
438 (translate-texi "guix-cookbook" po language)))
439 (available-translations "." "guix-cookbook"))
441 (for-each (lambda (file)
442 (install-file file #$output))
444 (find-files "." "contributing\\..*\\.texi$")
445 (find-files "." "guix\\..*\\.texi$")
446 (find-files "." "guix-cookbook\\..*\\.texi$"))))))
448 (computed-file "guix-translated-texinfo" build))
450 (define (info-manual source)
451 "Return the Info manual built from SOURCE."
453 (module-ref (resolve-interface '(gnu packages texinfo))
457 (module-ref (resolve-interface '(gnu packages graphviz))
460 (define glibc-utf8-locales
461 (module-ref (resolve-interface '(gnu packages base))
462 'glibc-utf8-locales))
464 (define documentation
465 (file-append* source "doc"))
468 (file-append* source "gnu/system/examples"))
471 (with-imported-modules '((guix build utils))
473 (use-modules (guix build utils)
478 ;; Create 'version.texi'.
479 ;; XXX: Can we use a more meaningful version string yet one that
480 ;; doesn't change at each commit?
481 (call-with-output-file "version.texi"
483 (let ((version "0.0-git"))
485 @set UPDATED 1 January 1970
486 @set UPDATED-MONTH January 1970
488 @set VERSION ~a\n" version version))))
490 ;; Copy configuration templates that the manual includes.
491 (for-each (lambda (template)
495 (basename template ".tmpl")
497 (find-files #$examples "\\.tmpl$"))
500 (mkdir-p (string-append #$output "/images"))
501 (for-each (lambda (dot-file)
502 (invoke #+(file-append graphviz "/bin/dot")
503 "-Tpng" "-Gratio=.9" "-Gnodesep=.005"
504 "-Granksep=.00005" "-Nfontsize=9"
505 "-Nheight=.1" "-Nwidth=.1"
506 "-o" (string-append #$output "/images/"
507 (basename dot-file ".dot")
510 (find-files (string-append #$documentation "/images")
514 (for-each (lambda (png-file)
515 (install-file png-file
516 (string-append #$output "/images")))
517 (find-files (string-append #$documentation "/images")
520 ;; Finally build the manual. Copy it the Texinfo files to $PWD and
521 ;; add a symlink to the 'images' directory so that 'makeinfo' can
522 ;; see those images and produce image references in the Info output.
523 (copy-recursively #$documentation "."
524 #:log (%make-void-port "w"))
525 (copy-recursively #+(translate-texi-manuals source) "."
526 #:log (%make-void-port "w"))
527 (delete-file-recursively "images")
528 (symlink (string-append #$output "/images") "images")
530 ;; Provide UTF-8 locales needed by the 'xspara.c' code in makeinfo.
531 (setenv "GUIX_LOCPATH"
532 #+(file-append glibc-utf8-locales "/lib/locale"))
534 (for-each (lambda (texi)
535 (match (string-split (basename texi) #\.)
536 (("guix" language "texi")
537 ;; Create 'version-LL.texi'.
538 (symlink "version.texi"
539 (string-append "version-" language
543 (invoke #+(file-append texinfo "/bin/makeinfo")
544 texi "-I" #$documentation
546 "-o" (string-append #$output "/"
547 (basename texi ".texi")
550 (append (find-files "."
551 "^guix\\.[a-z]{2}(_[A-Z]{2})?\\.texi$")
553 "^guix-cookbook.*\\.texi$"))))
555 ;; Compress Info files.
557 #+(file-append (specification->package "gzip") "/bin"))
558 (for-each (lambda (file)
559 (invoke "gzip" "-9n" file))
560 (find-files #$output "\\.info(-[0-9]+)?$")))))
562 (computed-file "guix-manual" build))
564 (define-syntax-rule (prevent-inlining! identifier ...)
565 (begin (set! identifier identifier) ...))
567 ;; XXX: These procedures are actually used by 'doc/build.scm'. Protect them
568 ;; from inlining on Guile 3.
569 (prevent-inlining! file-append* translate-texi-manuals info-manual)
571 (define* (guile-module-union things #:key (name "guix-module-union"))
572 "Return the union of the subset of THINGS (packages, computed files, etc.)
573 that provide Guile modules."
575 (with-imported-modules '((guix build union))
577 (use-modules (guix build union))
579 (define (modules directory)
580 (string-append directory "/share/guile/site"))
582 (define (objects directory)
583 (string-append directory "/lib/guile"))
585 (union-build #$output
586 (filter (lambda (directory)
587 (or (file-exists? (modules directory))
588 (file-exists? (objects directory))))
591 #:log-port (%make-void-port "w")))))
593 (computed-file name build))
595 (define (quiet-guile guile)
596 "Return a wrapper that does the same as the 'guile' executable of GUILE,
597 except that it does not complain about locales and falls back to 'en_US.utf8'
600 (specification->package "gcc-toolchain"))
603 (search-path %load-path
604 "gnu/packages/aux-files/guile-launcher.c"))
607 (version-major+minor (package-version guile)))
610 ;; XXX: Reuse <c-compiler> from (guix scripts pack) instead?
611 (with-imported-modules '((guix build utils))
613 (use-modules (guix build utils)
616 (mkdir-p (string-append #$output "/bin"))
618 (setenv "PATH" #$(file-append gcc "/bin"))
619 (setenv "C_INCLUDE_PATH"
621 (map (cut string-append <> "/include")
622 '#$(match (bag-transitive-build-inputs
623 (package->bag guile))
624 (((labels packages . _) ...)
625 (filter package? packages))))
627 (setenv "LIBRARY_PATH" #$(file-append gcc "/lib"))
629 (invoke "gcc" #$(local-file source) "-Wall" "-g0" "-O2"
630 "-I" #$(file-append guile "/include/guile/" effective)
631 "-L" #$(file-append guile "/lib")
632 #$(string-append "-lguile-" effective)
633 "-o" (string-append #$output "/bin/guile")))))
635 (computed-file "guile-wrapper" build))
637 (define* (guix-command modules
638 #:key source (dependencies '())
639 guile (guile-version (effective-version)))
640 "Return the 'guix' command such that it adds MODULES and DEPENDENCIES in its
642 (define glibc-utf8-locales
643 (module-ref (resolve-interface '(gnu packages base))
644 'glibc-utf8-locales))
646 (define module-directory
647 ;; To minimize the number of 'stat' calls needed to locate a module,
648 ;; create the union of all the module directories.
649 (guile-module-union (cons modules dependencies)))
651 (program-file "guix-command"
653 ;; Remove the empty extension from the search path.
654 (set! %load-extensions '(".scm"))
657 (append (list (string-append #$module-directory
660 (string-append #$guile "/share/guile/"
661 (effective-version)))
664 (set! %load-compiled-path
665 (append (list (string-append #$module-directory
669 (string-append #$guile "/lib/guile/"
672 %load-compiled-path))
674 ;; To maximize the chances that locales are set up right
675 ;; out-of-the-box, bundle "common" UTF-8 locales.
676 (let ((locpath (getenv "GUIX_LOCPATH")))
677 (setenv "GUIX_LOCPATH"
678 (string-append (if locpath
679 (string-append locpath ":")
681 #$(file-append glibc-utf8-locales
684 (let ((guix-main (module-ref (resolve-interface '(guix ui))
688 (bindtextdomain "guix"
689 #$(locale-data source "guix"))
690 (bindtextdomain "guix-packages"
691 #$(locale-data source
696 ;; XXX: It would be more convenient to change it to:
697 ;; (exit (apply guix-main (command-line)))
698 (apply guix-main (command-line))))
700 ;; Use a 'guile' variant that doesn't complain about locales.
701 #:guile (quiet-guile guile)))
703 (define (miscellaneous-files source)
704 "Return data files taken from SOURCE."
705 (file-mapping "guix-misc"
706 `(("etc/bash_completion.d/guix"
707 ,(file-append* source "/etc/completion/bash/guix"))
708 ("etc/bash_completion.d/guix-daemon"
709 ,(file-append* source "/etc/completion/bash/guix-daemon"))
710 ("share/zsh/site-functions/_guix"
711 ,(file-append* source "/etc/completion/zsh/_guix"))
712 ("share/fish/vendor_completions.d/guix.fish"
713 ,(file-append* source "/etc/completion/fish/guix.fish"))
714 ("share/guix/berlin.guix.gnu.org.pub"
715 ,(file-append* source
716 "/etc/substitutes/berlin.guix.gnu.org.pub"))
717 ("share/guix/ci.guix.gnu.org.pub" ;alias
718 ,(file-append* source "/etc/substitutes/berlin.guix.gnu.org.pub"))
719 ("share/guix/ci.guix.info.pub" ;alias
720 ,(file-append* source "/etc/substitutes/berlin.guix.gnu.org.pub")))))
722 (define* (whole-package name modules dependencies
724 (guile-version (effective-version))
725 info daemon miscellany
727 (command (guix-command modules
728 #:dependencies dependencies
730 #:guile-version guile-version)))
731 "Return the whole Guix package NAME that uses MODULES, a derivation of all
732 the modules (under share/guile/site and lib/guile), and DEPENDENCIES, a list
733 of packages depended on. COMMAND is the 'guix' program to use; INFO is the
735 (define (wrap daemon)
736 (program-file "guix-daemon"
738 ;; Refer to the right 'guix' command for 'guix
740 (setenv "GUIX" #$command)
742 ;; Honor the user's settings rather than those hardcoded
743 ;; in the 'guix-daemon' package.
744 (unless (getenv "GUIX_STATE_DIRECTORY")
745 (setenv "GUIX_STATE_DIRECTORY"
746 #$(string-append %localstatedir "/guix")))
747 (unless (getenv "GUIX_CONFIGURATION_DIRECTORY")
748 (setenv "GUIX_CONFIGURATION_DIRECTORY"
749 #$(string-append %sysconfdir "/guix")))
750 (unless (getenv "NIX_STORE_DIR")
751 (setenv "NIX_STORE_DIR" #$%storedir))
753 (apply execl #$(file-append daemon "/bin/guix-daemon")
754 "guix-daemon" (cdr (command-line))))))
757 (with-imported-modules '((guix build utils))
759 (use-modules (guix build utils))
762 #$(and daemon (wrap daemon)))
764 (mkdir-p (string-append #$output "/bin"))
766 (string-append #$output "/bin/guix"))
770 (string-append #$output "/bin/guix-daemon")))
772 (let ((share (string-append #$output "/share"))
773 (lib (string-append #$output "/lib"))
776 (symlink #$(file-append modules "/share/guile")
777 (string-append share "/guile"))
779 (symlink #$info (string-append share "/info")))
782 (symlink #$(file-append modules "/lib/guile")
783 (string-append lib "/guile")))
786 (copy-recursively #$miscellany #$output
787 #:log (%make-void-port "w")))))))
789 (define (transitive-package-dependencies package)
790 "Return the list of packages propagated by PACKAGE, including PACKAGE
792 (match (package-transitive-propagated-inputs package)
793 (((labels packages _ ...) ...)
794 (cons package packages))))
796 (define* (compiled-guix source #:key
797 (version %guix-version)
798 (channel-metadata #f)
800 (name (string-append "guix-" version))
801 (guile-version (effective-version))
802 (guile-for-build (default-guile))
803 (gzip (specification->package "gzip"))
804 (bzip2 (specification->package "bzip2"))
805 (xz (specification->package "xz"))
806 (guix (specification->package "guix")))
807 "Return a file-like object that contains a compiled Guix."
809 (specification->package "guile-avahi"))
812 (specification->package "guile-json"))
815 (specification->package "guile-ssh"))
818 (specification->package "guile-git"))
820 (define guile-sqlite3
821 (specification->package "guile-sqlite3"))
824 (specification->package "guile-zlib"))
827 (specification->package "guile-lzlib"))
830 (specification->package "guile-zstd"))
833 (specification->package "guile-gcrypt"))
836 (specification->package "guile-semver"))
839 (specification->package "gnutls"))
842 (append-map transitive-package-dependencies
843 (list guile-gcrypt gnutls guile-git guile-avahi
844 guile-json guile-semver guile-ssh guile-sqlite3
845 guile-zlib guile-lzlib guile-zstd)))
847 (define *core-modules*
848 (scheme-node "guix-core"
855 (guix build-system gnu)
856 (guix build-system trivial)
857 (guix build profiles)
858 (guix build gnu-build-system))
860 ;; Provide a dummy (guix config) with the default version
861 ;; number, storedir, etc. This is so that "guix-core" is the
862 ;; same across all installations and doesn't need to be
863 ;; rebuilt when the version changes, which in turn means we
864 ;; can have substitutes for it.
866 `(((guix config) => ,(make-config.scm)))
868 ;; (guix man-db) is needed at build-time by (guix profiles)
869 ;; but we don't need to compile it; not compiling it allows
870 ;; us to avoid an extra dependency on guile-gdbm-ffi.
872 `(("guix/man-db.scm" ,(local-file "../guix/man-db.scm"))
873 ("guix/build/po.scm" ,(local-file "../guix/build/po.scm"))
874 ("guix/store/schema.sql"
875 ,(local-file "../guix/store/schema.sql")))
877 #:extensions (list guile-gcrypt)
878 #:guile-for-build guile-for-build))
880 (define *extra-modules*
881 (scheme-node "guix-extra"
882 (filter-map (match-lambda
883 (('guix 'scripts _ ..1) #f)
885 (('guix 'tests _ ...) #f)
887 (scheme-modules* source "guix"))
888 (list *core-modules*)
891 `(("guix/graph.js" ,(local-file "../guix/graph.js"))
892 ("guix/d3.v3.js" ,(local-file "../guix/d3.v3.js")))
894 #:extensions dependencies
895 #:guile-for-build guile-for-build))
897 (define *core-package-modules*
898 (scheme-node "guix-packages-base"
901 (list *core-modules* *extra-modules*)
902 #:extensions dependencies
904 ;; Add all the non-Scheme files here. We must do it here so
905 ;; that 'search-patches' & co. can find them. Ideally we'd
906 ;; keep them next to the .scm files that use them but it's
907 ;; difficult to do (XXX).
909 (file-imports source "gnu/packages"
911 (and (eq? 'regular (stat:type stat))
912 (not (string-suffix? ".scm" file))
913 (not (string-suffix? ".go" file))
914 (not (string-prefix? ".#" file))
915 (not (string-suffix? "~" file)))))
916 #:guile-for-build guile-for-build))
918 (define *package-modules*
919 (scheme-node "guix-packages"
920 (scheme-modules* source "gnu/packages")
921 (list *core-modules* *extra-modules* *core-package-modules*)
922 #:extensions dependencies
923 #:guile-for-build guile-for-build))
925 (define *system-modules*
926 (scheme-node "guix-system"
929 ,@(scheme-modules* source "gnu/bootloader")
930 ,@(scheme-modules* source "gnu/system")
931 ,@(scheme-modules* source "gnu/services")
932 ,@(scheme-modules* source "gnu/machine"))
933 (list *core-package-modules* *package-modules*
934 *extra-modules* *core-modules*)
935 #:extensions dependencies
937 (append (file-imports source "gnu/system/examples"
940 ;; All the installer code is on the build-side.
941 (file-imports source "gnu/installer/"
943 ;; Build-side code that we don't build. Some of
944 ;; these depend on guile-rsvg, the Shepherd, etc.
945 (file-imports source "gnu/build" (const #t)))
949 (define *cli-modules*
950 (scheme-node "guix-cli"
951 (append (scheme-modules* source "/guix/scripts")
953 (list *core-modules* *extra-modules*
954 *core-package-modules* *package-modules*
956 #:extensions dependencies
957 #:guile-for-build guile-for-build))
959 (define *system-test-modules*
960 ;; Ship these modules mostly so (gnu ci) can discover them.
961 (scheme-node "guix-system-tests"
963 ,@(scheme-modules* source "gnu/tests"))
964 (list *core-package-modules* *package-modules*
965 *extra-modules* *system-modules* *core-modules*
966 *cli-modules*) ;for (guix scripts pack), etc.
967 #:extensions dependencies
968 #:guile-for-build guile-for-build))
971 (scheme-node "guix-config"
975 => ,(make-config.scm #:gzip gzip
985 %guix-bug-report-address
987 %guix-home-page-url)))
988 #:guile-for-build guile-for-build))
990 (define (built-modules node-subset)
991 (directory-union (string-append name "-modules")
992 (append-map node-subset
994 ;; Note: *CONFIG* comes first so that it
995 ;; overrides the (guix config) module that
996 ;; comes with *CORE-MODULES*.
999 *system-test-modules*
1002 *core-package-modules*
1006 ;; Silently choose the first entry upon collision so that
1007 ;; we choose *CONFIG*.
1008 #:resolve-collision 'first
1010 ;; When we do (add-to-store "utils.scm"), "utils.scm" must
1011 ;; be a regular file, not a symlink. Thus, arrange so that
1012 ;; regular files appear as regular files in the final
1017 ;; Version 0 of 'guix pull' meant we'd just return Scheme modules.
1018 ;; Version 1 is when we return the full package.
1019 (cond ((= 1 pull-version)
1020 ;; The whole package, with a standard file hierarchy.
1021 (let* ((modules (built-modules (compose list node-source+compiled)))
1022 (command (guix-command modules
1024 #:dependencies dependencies
1025 #:guile guile-for-build
1026 #:guile-version guile-version)))
1027 (whole-package name modules dependencies
1029 #:guile guile-for-build
1031 ;; Include 'guix-daemon'. XXX: Here we inject an
1032 ;; older snapshot of guix-daemon, but that's a good
1033 ;; enough approximation for now.
1034 #:daemon (module-ref (resolve-interface
1036 package-management))
1039 #:info (info-manual source)
1040 #:miscellany (miscellaneous-files source)
1041 #:guile-version guile-version)))
1043 ;; Legacy 'guix pull': return the .scm and .go files as one
1045 (built-modules (lambda (node)
1046 (list (node-source node)
1047 (node-compiled node)))))
1049 ;; Unsupported 'guix pull' version.
1054 ;;; Generating (guix config).
1057 (define %persona-variables
1058 ;; (guix config) variables that define Guix's persona.
1059 '(%guix-package-name
1061 %guix-bug-report-address
1062 %guix-home-page-url))
1064 (define %config-variables
1065 ;; (guix config) variables corresponding to Guix configuration.
1066 (letrec-syntax ((variables (syntax-rules ()
1069 ((_ variable rest ...)
1070 (cons `(variable . ,variable)
1071 (variables rest ...))))))
1072 (variables %localstatedir %storedir %sysconfdir)))
1074 (define* (make-config.scm #:key gzip xz bzip2
1075 (package-name "GNU Guix")
1076 (package-version "0")
1077 (channel-metadata #f)
1078 (bug-report-address "bug-guix@gnu.org")
1079 (home-page-url "https://guix.gnu.org"))
1081 ;; Hack so that Geiser is not confused.
1082 (define defmod 'define-module)
1084 (scheme-file "config.scm"
1085 #~(;; The following expressions get spliced.
1086 (#$defmod (guix config)
1087 #:export (%guix-package-name
1089 %guix-bug-report-address
1095 %store-database-directory
1102 #$(%current-system))
1104 #$@(map (match-lambda
1106 #~(define-public #$name #$value)))
1109 (define %store-directory
1110 (or (and=> (getenv "NIX_STORE_DIR") canonicalize-path)
1113 (define %state-directory
1114 ;; This must match `NIX_STATE_DIR' as defined in
1116 (or (getenv "GUIX_STATE_DIRECTORY")
1117 (string-append %localstatedir "/guix")))
1119 (define %store-database-directory
1120 (or (getenv "GUIX_DATABASE_DIRECTORY")
1121 (string-append %state-directory "/db")))
1123 (define %config-directory
1124 ;; This must match `GUIX_CONFIGURATION_DIRECTORY' as
1125 ;; defined in `nix/local.mk'.
1126 (or (getenv "GUIX_CONFIGURATION_DIRECTORY")
1127 (string-append %sysconfdir "/guix")))
1129 (define %guix-package-name #$package-name)
1130 (define %guix-version #$package-version)
1131 (define %guix-bug-report-address #$bug-report-address)
1132 (define %guix-home-page-url #$home-page-url)
1134 (define %channel-metadata
1135 ;; Metadata for the 'guix' channel in use. This
1136 ;; information is used by (guix describe).
1137 '#$channel-metadata)
1140 #+(and gzip (file-append gzip "/bin/gzip")))
1142 #+(and bzip2 (file-append bzip2 "/bin/bzip2")))
1144 #+(and xz (file-append xz "/bin/xz"))))
1146 ;; Guile 2.0 *requires* the 'define-module' to be at the
1147 ;; top-level or the 'toplevel-ref' in the resulting .go file are
1148 ;; made relative to a nonexistent anonymous module.
1156 (define* (compiled-modules name module-tree module-files
1159 (dependencies-compiled '())
1161 (extensions '()) ;full-blown Guile packages
1164 "Build all the MODULE-FILES from MODULE-TREE. MODULE-FILES must be a list
1165 like '(\"guix/foo.scm\" \"gnu/bar.scm\") and MODULE-TREE is the directory
1166 containing MODULE-FILES and possibly other files as well."
1167 ;; This is a non-monadic, enhanced version of 'compiled-file' from (guix
1170 (with-imported-modules (source-module-closure
1171 '((guix build compile)
1172 (guix build utils)))
1174 (use-modules (srfi srfi-26)
1178 (guix build compile)
1181 (define (regular? file)
1182 (not (member file '("." ".."))))
1184 (define (report-load file total completed)
1187 "[~3@a/~3@a] loading...\t~5,1f% of ~d files"
1189 ;; Note: Multiply TOTAL by two to account for the
1190 ;; compilation phase that follows.
1191 completed (* total 2)
1193 (* 100. (/ completed total)) total)
1196 (define (report-compilation file total completed)
1198 (format #t "[~3@a/~3@a] compiling...\t~5,1f% of ~d files"
1200 ;; Add TOTAL to account for the load phase that came
1202 (+ total completed) (* total 2)
1204 (* 100. (/ completed total)) total)
1207 (define (process-directory directory files output)
1208 ;; Hide compilation warnings.
1209 (parameterize ((current-warning-port (%make-void-port "w")))
1210 (compile-files directory #$output files
1211 #:workers (parallel-job-count)
1212 #:report-load report-load
1213 #:report-compilation report-compilation)))
1215 (setvbuf (current-output-port) 'line)
1216 (setvbuf (current-error-port) 'line)
1218 (set! %load-path (cons #+module-tree %load-path))
1220 (append '#+dependencies
1221 (map (lambda (extension)
1222 (string-append extension "/share/guile/site/"
1223 (effective-version)))
1227 (set! %load-compiled-path
1228 (append '#+dependencies-compiled
1229 (map (lambda (extension)
1230 (string-append extension "/lib/guile/"
1234 %load-compiled-path))
1236 ;; Load the compiler modules upfront.
1240 (chdir #+module-tree)
1241 (process-directory "." '#+module-files #$output)
1244 (computed-file name build
1245 #:guile guile-for-build
1247 `(#:local-build? #f ;allow substitutes
1249 ;; Don't annoy people about _IONBF deprecation.
1250 ;; Initialize 'terminal-width' in (system repl debug)
1251 ;; to a large-enough value to make backtrace more
1253 #:env-vars (("GUILE_WARN_DEPRECATED" . "no")
1254 ("COLUMNS" . "200")))))
1261 (define* (guix-derivation source version
1262 #:optional (guile-version (effective-version))
1263 #:key (pull-version 0)
1265 "Return, as a monadic value, the derivation to build the Guix from SOURCE
1266 for GUILE-VERSION. Use VERSION as the version string. Use CHANNEL-METADATA
1267 as the channel metadata sexp to include in (guix config).
1269 PULL-VERSION specifies the version of the 'guix pull' protocol. Return #f if
1270 this PULL-VERSION value is not supported."
1271 (define (shorten version)
1272 (if (and (string-every char-set:hex-digit version)
1273 (> (string-length version) 9))
1274 (string-take version 9) ;Git commit
1278 ;; When PULL-VERSION >= 1, produce a self-contained Guix and use the
1279 ;; current Guile unconditionally.
1280 (specification->package "guile"))
1282 (when (and (< pull-version 1)
1283 (not (string=? (package-version guile) guile-version)))
1284 ;; Guix < 0.15.0 has PULL-VERSION = 0, where the host Guile is reused and
1285 ;; can be any version. When that happens and Guile is not current (e.g.,
1286 ;; it's Guile 2.0), just bail out.
1289 (message "Guix is too old and cannot be upgraded")))))
1291 (mbegin %store-monad
1292 (set-guile-for-build guile)
1293 (let ((guix (compiled-guix source
1295 #:channel-metadata channel-metadata
1296 #:name (string-append "guix-"
1298 #:pull-version pull-version
1299 #:guile-version (if (>= pull-version 1)
1300 "3.0" guile-version)
1301 #:guile-for-build guile)))