channels: Record 'guix' channel metadata in (guix config).
[jackhill/guix/guix.git] / guix / self.scm
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>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
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.
11 ;;;
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.
16 ;;;
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/>.
19
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'
39 compiled-guix
40 guix-derivation))
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 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
72
73 \f
74 ;;;
75 ;;; Derivations.
76 ;;;
77
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)
82 node?
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
88
89 ;; File mappings are essentially an alist as passed to 'imported-files'.
90 (define-record-type <file-mapping>
91 (file-mapping name alist)
92 file-mapping?
93 (name file-mapping-name)
94 (alist file-mapping-alist))
95
96 (define-gexp-compiler (file-mapping-compiler (mapping <file-mapping>)
97 system target)
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))
102 (cons destination
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)
108 #:system system))
109
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."
113 (define build
114 (with-imported-modules '((guix build utils))
115 #~(begin
116 (use-modules (guix build utils))
117
118 (define source
119 (string-append #$output "/share/guile/site/"
120 (effective-version)))
121
122 (define object
123 (string-append #$output "/lib/guile/" (effective-version)
124 "/site-ccache"))
125
126 (mkdir-p (dirname source))
127 (symlink #$(node-source node) source)
128 (mkdir-p (dirname object))
129 (symlink #$(node-compiled node) object))))
130
131 (computed-file (string-append (node-name node) "-modules")
132 build
133 #:options '(#:local-build? #t
134
135 ;; "Building" it locally is faster.
136 #:substitutable? #f)))
137
138 (define (node-fold proc init nodes)
139 (let loop ((nodes nodes)
140 (visited (setq))
141 (result init))
142 (match nodes
143 (() result)
144 ((head tail ...)
145 (if (set-contains? visited head)
146 (loop tail visited result)
147 (loop tail (set-insert head visited)
148 (proc head result)))))))
149
150 (define (node-modules/recursive nodes)
151 (node-fold (lambda (node modules)
152 (append (node-modules node) modules))
153 '()
154 nodes))
155
156 (define* (closure modules #:optional (except '()))
157 (source-module-closure modules
158 #:select?
159 (match-lambda
160 (('guix 'config)
161 #f)
162 ((and module
163 (or ('guix _ ...) ('gnu _ ...)))
164 (not (member module except)))
165 (rest #f))))
166
167 (define module->import
168 ;; Return a file-name/file-like object pair for the specified module and
169 ;; suitable for 'imported-files'.
170 (match-lambda
171 ((module '=> thing)
172 (let ((file (module-name->file-name module)))
173 (list file thing)))
174 (module
175 (let ((file (module-name->file-name module)))
176 (list file
177 (local-file (search-path %load-path file)))))))
178
179 (define* (scheme-node name modules #:optional (dependencies '())
180 #:key (extra-modules '()) (extra-files '())
181 (extensions '())
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
189 (closure 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))))
202
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'."
206 (map (lambda (file)
207 (list (string-drop file (+ 1 (string-length directory)))
208 (local-file file #:recursive? #t)))
209 (find-files (string-append directory "/" sub-directory) pred)))
210
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."
215 (match item
216 ((? string?)
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
219 ;; itself.
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)
226 "/" file)
227 (basename file)
228 #:recursive? recursive?
229 #:select? (local-file-select? base))
230 (file-append base file)))
231 (_
232 ;; In this case, anything that refers to the result also depends on ITEM,
233 ;; which isn't great.
234 (file-append item "/" file))))
235
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."
240 (define gettext
241 (module-ref (resolve-interface '(gnu packages gettext))
242 'gettext-minimal))
243
244 (define build
245 (with-imported-modules '((guix build utils))
246 #~(begin
247 (use-modules (guix build utils)
248 (srfi srfi-26)
249 (ice-9 match) (ice-9 ftw))
250
251 (define po-directory
252 #+(file-append* source (string-append "po/" directory)))
253
254 (define (compile language)
255 (let ((gmo (string-append #$output "/" language "/LC_MESSAGES/"
256 #$domain ".mo")))
257 (mkdir-p (dirname gmo))
258 (invoke #+(file-append gettext "/bin/msgfmt")
259 "-c" "--statistics" "--verbose"
260 "-o" gmo
261 (string-append po-directory "/" language ".po"))))
262
263 (define (linguas)
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" <>))))
270
271 (for-each compile (linguas)))))
272
273 (computed-file (string-append "guix-locale-" domain)
274 build))
275
276 (define (translate-texi-manuals source)
277 "Return the translated texinfo manuals built from SOURCE."
278 (define po4a
279 (specification->package "po4a"))
280
281 (define gettext
282 (specification->package "gettext"))
283
284 (define glibc-utf8-locales
285 (module-ref (resolve-interface '(gnu packages base))
286 'glibc-utf8-locales))
287
288 (define documentation
289 (file-append* source "doc"))
290
291 (define documentation-po
292 (file-append* source "po/doc"))
293
294 (define build
295 (with-imported-modules '((guix build utils) (guix build po))
296 #~(begin
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)
300 (srfi srfi-1))
301
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))
308
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)
313 #\space
314 chr))
315 str))
316
317 (define xref-regexp
318 ;; Texinfo cross-reference regexp.
319 (make-regexp "@(px|x)?ref\\{([^,}]+)"))
320
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.
324 (define content
325 (call-with-input-file texi get-string-all))
326
327 (define matches
328 (list-matches xref-regexp content))
329
330 (define translation-map
331 (fold (match-lambda*
332 (((msgid . str) result)
333 (vhash-cons msgid str result)))
334 vlist-null
335 translations))
336
337 (define translated
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)
342 (offset 0)
343 (result '()))
344 (match matches
345 (()
346 (string-concatenate-reverse
347 (cons (string-drop content offset) result)))
348 ((head . tail)
349 (let ((prefix (match:substring head 1))
350 (ref (canonicalize-whitespace (match:substring head 2))))
351 (define translated
352 (string-append "@" (or prefix "")
353 "ref{"
354 (match (vhash-assoc ref translation-map)
355 (#f ref)
356 ((_ . str) str))))
357
358 (loop tail
359 (match:end head)
360 (append (list translated
361 (string-take
362 (string-drop content offset)
363 (- (match:start head) offset)))
364 result)))))))
365
366 (format (current-error-port)
367 "translated ~a cross-references in '~a'~%"
368 (length matches) texi)
369 (call-with-output-file texi
370 (lambda (port)
371 (display translated port))))
372
373 (define* (translate-texi prefix po lang
374 #:key (extras '()))
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
382 ".texi.tmp")))
383 (cons prefix extras))
384
385 (for-each (lambda (file)
386 (let* ((texi (string-append file "." lang ".texi"))
387 (tmp (string-append texi ".tmp")))
388 (copy-file tmp texi)
389 (translate-cross-references texi
390 translations)))
391 (cons prefix extras))))
392
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 ".")
400 base)
401 (match (string-split base #\.)
402 ((_ ... lang "po")
403 (cons lang po))))))
404 (find-files directory
405 "\\.[a-z]{2}(_[A-Z]{2})?\\.po$")))
406
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))
412
413 (mkdir #$output)
414 (copy-recursively #$documentation "."
415 #:log (%make-void-port "w"))
416
417 (for-each
418 (lambda (file)
419 (copy-file file (basename file)))
420 (find-files #$documentation-po ".*.po$"))
421
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")
427
428 (n-par-for-each parallel-jobs
429 (match-lambda
430 ((language . po)
431 (translate-texi "guix" po language
432 #:extras '("contributing"))))
433 (available-translations "." "guix-manual"))
434
435 (n-par-for-each parallel-jobs
436 (match-lambda
437 ((language . po)
438 (translate-texi "guix-cookbook" po language)))
439 (available-translations "." "guix-cookbook"))
440
441 (for-each (lambda (file)
442 (install-file file #$output))
443 (append
444 (find-files "." "contributing\\..*\\.texi$")
445 (find-files "." "guix\\..*\\.texi$")
446 (find-files "." "guix-cookbook\\..*\\.texi$"))))))
447
448 (computed-file "guix-translated-texinfo" build))
449
450 (define (info-manual source)
451 "Return the Info manual built from SOURCE."
452 (define texinfo
453 (module-ref (resolve-interface '(gnu packages texinfo))
454 'texinfo))
455
456 (define graphviz
457 (module-ref (resolve-interface '(gnu packages graphviz))
458 'graphviz))
459
460 (define glibc-utf8-locales
461 (module-ref (resolve-interface '(gnu packages base))
462 'glibc-utf8-locales))
463
464 (define documentation
465 (file-append* source "doc"))
466
467 (define examples
468 (file-append* source "gnu/system/examples"))
469
470 (define build
471 (with-imported-modules '((guix build utils))
472 #~(begin
473 (use-modules (guix build utils)
474 (ice-9 match))
475
476 (mkdir #$output)
477
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"
482 (lambda (port)
483 (let ((version "0.0-git"))
484 (format port "
485 @set UPDATED 1 January 1970
486 @set UPDATED-MONTH January 1970
487 @set EDITION ~a
488 @set VERSION ~a\n" version version))))
489
490 ;; Copy configuration templates that the manual includes.
491 (for-each (lambda (template)
492 (copy-file template
493 (string-append
494 "os-config-"
495 (basename template ".tmpl")
496 ".texi")))
497 (find-files #$examples "\\.tmpl$"))
498
499 ;; Build graphs.
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")
508 ".png")
509 dot-file))
510 (find-files (string-append #$documentation "/images")
511 "\\.dot$"))
512
513 ;; Copy other PNGs.
514 (for-each (lambda (png-file)
515 (install-file png-file
516 (string-append #$output "/images")))
517 (find-files (string-append #$documentation "/images")
518 "\\.png$"))
519
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")
529
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"))
533
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
540 ".texi")))
541 (_ #f))
542
543 (invoke #+(file-append texinfo "/bin/makeinfo")
544 texi "-I" #$documentation
545 "-I" "."
546 "-o" (string-append #$output "/"
547 (basename texi ".texi")
548 ".info")))
549 (cons "guix.texi"
550 (append (find-files "."
551 "^guix\\.[a-z]{2}(_[A-Z]{2})?\\.texi$")
552 (find-files "."
553 "^guix-cookbook.*\\.texi$"))))
554
555 ;; Compress Info files.
556 (setenv "PATH"
557 #+(file-append (specification->package "gzip") "/bin"))
558 (for-each (lambda (file)
559 (invoke "gzip" "-9n" file))
560 (find-files #$output "\\.info(-[0-9]+)?$")))))
561
562 (computed-file "guix-manual" build))
563
564 (define-syntax-rule (prevent-inlining! identifier ...)
565 (begin (set! identifier identifier) ...))
566
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)
570
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."
574 (define build
575 (with-imported-modules '((guix build union))
576 #~(begin
577 (use-modules (guix build union))
578
579 (define (modules directory)
580 (string-append directory "/share/guile/site"))
581
582 (define (objects directory)
583 (string-append directory "/lib/guile"))
584
585 (union-build #$output
586 (filter (lambda (directory)
587 (or (file-exists? (modules directory))
588 (file-exists? (objects directory))))
589 '#$things)
590
591 #:log-port (%make-void-port "w")))))
592
593 (computed-file name build))
594
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'
598 instead of 'C'."
599 (define gcc
600 (specification->package "gcc-toolchain"))
601
602 (define source
603 (search-path %load-path
604 "gnu/packages/aux-files/guile-launcher.c"))
605
606 (define effective
607 (version-major+minor (package-version guile)))
608
609 (define build
610 ;; XXX: Reuse <c-compiler> from (guix scripts pack) instead?
611 (with-imported-modules '((guix build utils))
612 #~(begin
613 (use-modules (guix build utils)
614 (srfi srfi-26))
615
616 (mkdir-p (string-append #$output "/bin"))
617
618 (setenv "PATH" #$(file-append gcc "/bin"))
619 (setenv "C_INCLUDE_PATH"
620 (string-join
621 (map (cut string-append <> "/include")
622 '#$(match (bag-transitive-build-inputs
623 (package->bag guile))
624 (((labels packages . _) ...)
625 (filter package? packages))))
626 ":"))
627 (setenv "LIBRARY_PATH" #$(file-append gcc "/lib"))
628
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")))))
634
635 (computed-file "guile-wrapper" build))
636
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
641 load path."
642 (define glibc-utf8-locales
643 (module-ref (resolve-interface '(gnu packages base))
644 'glibc-utf8-locales))
645
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)))
650
651 (program-file "guix-command"
652 #~(begin
653 ;; Remove the empty extension from the search path.
654 (set! %load-extensions '(".scm"))
655
656 (set! %load-path
657 (append (list (string-append #$module-directory
658 "/share/guile/site/"
659 (effective-version))
660 (string-append #$guile "/share/guile/"
661 (effective-version)))
662 %load-path))
663
664 (set! %load-compiled-path
665 (append (list (string-append #$module-directory
666 "/lib/guile/"
667 (effective-version)
668 "/site-ccache")
669 (string-append #$guile "/lib/guile/"
670 (effective-version)
671 "/ccache"))
672 %load-compiled-path))
673
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 ":")
680 "")
681 #$(file-append glibc-utf8-locales
682 "/lib/locale"))))
683
684 (let ((guix-main (module-ref (resolve-interface '(guix ui))
685 'guix-main)))
686 #$(if source
687 #~(begin
688 (bindtextdomain "guix"
689 #$(locale-data source "guix"))
690 (bindtextdomain "guix-packages"
691 #$(locale-data source
692 "guix-packages"
693 "packages")))
694 #t)
695
696 ;; XXX: It would be more convenient to change it to:
697 ;; (exit (apply guix-main (command-line)))
698 (apply guix-main (command-line))))
699
700 ;; Use a 'guile' variant that doesn't complain about locales.
701 #:guile (quiet-guile guile)))
702
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")))))
721
722 (define* (whole-package name modules dependencies
723 #:key
724 (guile-version (effective-version))
725 info daemon miscellany
726 guile
727 (command (guix-command modules
728 #:dependencies dependencies
729 #:guile guile
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
734 Info manual."
735 (define (wrap daemon)
736 (program-file "guix-daemon"
737 #~(begin
738 ;; Refer to the right 'guix' command for 'guix
739 ;; substitute' & co.
740 (setenv "GUIX" #$command)
741
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))
752
753 (apply execl #$(file-append daemon "/bin/guix-daemon")
754 "guix-daemon" (cdr (command-line))))))
755
756 (computed-file name
757 (with-imported-modules '((guix build utils))
758 #~(begin
759 (use-modules (guix build utils))
760
761 (define daemon
762 #$(and daemon (wrap daemon)))
763
764 (mkdir-p (string-append #$output "/bin"))
765 (symlink #$command
766 (string-append #$output "/bin/guix"))
767
768 (when daemon
769 (symlink daemon
770 (string-append #$output "/bin/guix-daemon")))
771
772 (let ((share (string-append #$output "/share"))
773 (lib (string-append #$output "/lib"))
774 (info #$info))
775 (mkdir-p share)
776 (symlink #$(file-append modules "/share/guile")
777 (string-append share "/guile"))
778 (when info
779 (symlink #$info (string-append share "/info")))
780
781 (mkdir-p lib)
782 (symlink #$(file-append modules "/lib/guile")
783 (string-append lib "/guile")))
784
785 (when #$miscellany
786 (copy-recursively #$miscellany #$output
787 #:log (%make-void-port "w")))))))
788
789 (define (transitive-package-dependencies package)
790 "Return the list of packages propagated by PACKAGE, including PACKAGE
791 itself."
792 (match (package-transitive-propagated-inputs package)
793 (((labels packages _ ...) ...)
794 (cons package packages))))
795
796 (define* (compiled-guix source #:key
797 (version %guix-version)
798 (channel-metadata #f)
799 (pull-version 1)
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."
808 (define guile-avahi
809 (specification->package "guile-avahi"))
810
811 (define guile-json
812 (specification->package "guile-json"))
813
814 (define guile-ssh
815 (specification->package "guile-ssh"))
816
817 (define guile-git
818 (specification->package "guile-git"))
819
820 (define guile-sqlite3
821 (specification->package "guile-sqlite3"))
822
823 (define guile-zlib
824 (specification->package "guile-zlib"))
825
826 (define guile-lzlib
827 (specification->package "guile-lzlib"))
828
829 (define guile-zstd
830 (specification->package "guile-zstd"))
831
832 (define guile-gcrypt
833 (specification->package "guile-gcrypt"))
834
835 (define guile-semver
836 (specification->package "guile-semver"))
837
838 (define gnutls
839 (specification->package "gnutls"))
840
841 (define dependencies
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)))
846
847 (define *core-modules*
848 (scheme-node "guix-core"
849 '((guix)
850 (guix monad-repl)
851 (guix packages)
852 (guix download)
853 (guix discovery)
854 (guix profiles)
855 (guix build-system gnu)
856 (guix build-system trivial)
857 (guix build profiles)
858 (guix build gnu-build-system))
859
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.
865 #:extra-modules
866 `(((guix config) => ,(make-config.scm)))
867
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.
871 #:extra-files
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")))
876
877 #:extensions (list guile-gcrypt)
878 #:guile-for-build guile-for-build))
879
880 (define *extra-modules*
881 (scheme-node "guix-extra"
882 (filter-map (match-lambda
883 (('guix 'scripts _ ..1) #f)
884 (('guix 'man-db) #f)
885 (('guix 'tests _ ...) #f)
886 (name name))
887 (scheme-modules* source "guix"))
888 (list *core-modules*)
889
890 #:extra-files
891 `(("guix/graph.js" ,(local-file "../guix/graph.js"))
892 ("guix/d3.v3.js" ,(local-file "../guix/d3.v3.js")))
893
894 #:extensions dependencies
895 #:guile-for-build guile-for-build))
896
897 (define *core-package-modules*
898 (scheme-node "guix-packages-base"
899 `((gnu packages)
900 (gnu packages base))
901 (list *core-modules* *extra-modules*)
902 #:extensions dependencies
903
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).
908 #:extra-files
909 (file-imports source "gnu/packages"
910 (lambda (file stat)
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))
917
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))
924
925 (define *system-modules*
926 (scheme-node "guix-system"
927 `((gnu system)
928 (gnu services)
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
936 #:extra-files
937 (append (file-imports source "gnu/system/examples"
938 (const #t))
939
940 ;; All the installer code is on the build-side.
941 (file-imports source "gnu/installer/"
942 (const #t))
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)))
946 #:guile-for-build
947 guile-for-build))
948
949 (define *cli-modules*
950 (scheme-node "guix-cli"
951 (append (scheme-modules* source "/guix/scripts")
952 `((gnu ci)))
953 (list *core-modules* *extra-modules*
954 *core-package-modules* *package-modules*
955 *system-modules*)
956 #:extensions dependencies
957 #:guile-for-build guile-for-build))
958
959 (define *system-test-modules*
960 ;; Ship these modules mostly so (gnu ci) can discover them.
961 (scheme-node "guix-system-tests"
962 `((gnu 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))
969
970 (define *config*
971 (scheme-node "guix-config"
972 '()
973 #:extra-modules
974 `(((guix config)
975 => ,(make-config.scm #:gzip gzip
976 #:bzip2 bzip2
977 #:xz xz
978 #:package-name
979 %guix-package-name
980 #:package-version
981 version
982 #:channel-metadata
983 channel-metadata
984 #:bug-report-address
985 %guix-bug-report-address
986 #:home-page-url
987 %guix-home-page-url)))
988 #:guile-for-build guile-for-build))
989
990 (define (built-modules node-subset)
991 (directory-union (string-append name "-modules")
992 (append-map node-subset
993
994 ;; Note: *CONFIG* comes first so that it
995 ;; overrides the (guix config) module that
996 ;; comes with *CORE-MODULES*.
997 (list *config*
998 *cli-modules*
999 *system-test-modules*
1000 *system-modules*
1001 *package-modules*
1002 *core-package-modules*
1003 *extra-modules*
1004 *core-modules*))
1005
1006 ;; Silently choose the first entry upon collision so that
1007 ;; we choose *CONFIG*.
1008 #:resolve-collision 'first
1009
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
1013 ;; output.
1014 #:copy? #t
1015 #:quiet? #t))
1016
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
1023 #:source source
1024 #:dependencies dependencies
1025 #:guile guile-for-build
1026 #:guile-version guile-version)))
1027 (whole-package name modules dependencies
1028 #:command command
1029 #:guile guile-for-build
1030
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
1035 '(gnu packages
1036 package-management))
1037 'guix-daemon)
1038
1039 #:info (info-manual source)
1040 #:miscellany (miscellaneous-files source)
1041 #:guile-version guile-version)))
1042 ((= 0 pull-version)
1043 ;; Legacy 'guix pull': return the .scm and .go files as one
1044 ;; directory.
1045 (built-modules (lambda (node)
1046 (list (node-source node)
1047 (node-compiled node)))))
1048 (else
1049 ;; Unsupported 'guix pull' version.
1050 #f)))
1051
1052 \f
1053 ;;;
1054 ;;; Generating (guix config).
1055 ;;;
1056
1057 (define %persona-variables
1058 ;; (guix config) variables that define Guix's persona.
1059 '(%guix-package-name
1060 %guix-version
1061 %guix-bug-report-address
1062 %guix-home-page-url))
1063
1064 (define %config-variables
1065 ;; (guix config) variables corresponding to Guix configuration.
1066 (letrec-syntax ((variables (syntax-rules ()
1067 ((_)
1068 '())
1069 ((_ variable rest ...)
1070 (cons `(variable . ,variable)
1071 (variables rest ...))))))
1072 (variables %localstatedir %storedir %sysconfdir)))
1073
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"))
1080
1081 ;; Hack so that Geiser is not confused.
1082 (define defmod 'define-module)
1083
1084 (scheme-file "config.scm"
1085 #~(;; The following expressions get spliced.
1086 (#$defmod (guix config)
1087 #:export (%guix-package-name
1088 %guix-version
1089 %guix-bug-report-address
1090 %guix-home-page-url
1091 %channel-metadata
1092 %system
1093 %store-directory
1094 %state-directory
1095 %store-database-directory
1096 %config-directory
1097 %gzip
1098 %bzip2
1099 %xz))
1100
1101 (define %system
1102 #$(%current-system))
1103
1104 #$@(map (match-lambda
1105 ((name . value)
1106 #~(define-public #$name #$value)))
1107 %config-variables)
1108
1109 (define %store-directory
1110 (or (and=> (getenv "NIX_STORE_DIR") canonicalize-path)
1111 %storedir))
1112
1113 (define %state-directory
1114 ;; This must match `NIX_STATE_DIR' as defined in
1115 ;; `nix/local.mk'.
1116 (or (getenv "GUIX_STATE_DIRECTORY")
1117 (string-append %localstatedir "/guix")))
1118
1119 (define %store-database-directory
1120 (or (getenv "GUIX_DATABASE_DIRECTORY")
1121 (string-append %state-directory "/db")))
1122
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")))
1128
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)
1133
1134 (define %channel-metadata
1135 ;; Metadata for the 'guix' channel in use. This
1136 ;; information is used by (guix describe).
1137 '#$channel-metadata)
1138
1139 (define %gzip
1140 #+(and gzip (file-append gzip "/bin/gzip")))
1141 (define %bzip2
1142 #+(and bzip2 (file-append bzip2 "/bin/bzip2")))
1143 (define %xz
1144 #+(and xz (file-append xz "/bin/xz"))))
1145
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.
1149 #:splice? #t))
1150
1151 \f
1152 ;;;
1153 ;;; Building.
1154 ;;;
1155
1156 (define* (compiled-modules name module-tree module-files
1157 #:optional
1158 (dependencies '())
1159 (dependencies-compiled '())
1160 #:key
1161 (extensions '()) ;full-blown Guile packages
1162 parallel?
1163 guile-for-build)
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
1168 ;; gexp).
1169 (define build
1170 (with-imported-modules (source-module-closure
1171 '((guix build compile)
1172 (guix build utils)))
1173 #~(begin
1174 (use-modules (srfi srfi-26)
1175 (ice-9 match)
1176 (ice-9 format)
1177 (ice-9 threads)
1178 (guix build compile)
1179 (guix build utils))
1180
1181 (define (regular? file)
1182 (not (member file '("." ".."))))
1183
1184 (define (report-load file total completed)
1185 (display #\cr)
1186 (format #t
1187 "[~3@a/~3@a] loading...\t~5,1f% of ~d files"
1188
1189 ;; Note: Multiply TOTAL by two to account for the
1190 ;; compilation phase that follows.
1191 completed (* total 2)
1192
1193 (* 100. (/ completed total)) total)
1194 (force-output))
1195
1196 (define (report-compilation file total completed)
1197 (display #\cr)
1198 (format #t "[~3@a/~3@a] compiling...\t~5,1f% of ~d files"
1199
1200 ;; Add TOTAL to account for the load phase that came
1201 ;; before.
1202 (+ total completed) (* total 2)
1203
1204 (* 100. (/ completed total)) total)
1205 (force-output))
1206
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)))
1214
1215 (setvbuf (current-output-port) 'line)
1216 (setvbuf (current-error-port) 'line)
1217
1218 (set! %load-path (cons #+module-tree %load-path))
1219 (set! %load-path
1220 (append '#+dependencies
1221 (map (lambda (extension)
1222 (string-append extension "/share/guile/site/"
1223 (effective-version)))
1224 '#+extensions)
1225 %load-path))
1226
1227 (set! %load-compiled-path
1228 (append '#+dependencies-compiled
1229 (map (lambda (extension)
1230 (string-append extension "/lib/guile/"
1231 (effective-version)
1232 "/site-ccache"))
1233 '#+extensions)
1234 %load-compiled-path))
1235
1236 ;; Load the compiler modules upfront.
1237 (compile #f)
1238
1239 (mkdir #$output)
1240 (chdir #+module-tree)
1241 (process-directory "." '#+module-files #$output)
1242 (newline))))
1243
1244 (computed-file name build
1245 #:guile guile-for-build
1246 #:options
1247 `(#:local-build? #f ;allow substitutes
1248
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
1252 ;; verbose.
1253 #:env-vars (("GUILE_WARN_DEPRECATED" . "no")
1254 ("COLUMNS" . "200")))))
1255
1256 \f
1257 ;;;
1258 ;;; Building.
1259 ;;;
1260
1261 (define* (guix-derivation source version
1262 #:optional (guile-version (effective-version))
1263 #:key (pull-version 0)
1264 channel-metadata)
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).
1268
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
1275 version))
1276
1277 (define guile
1278 ;; When PULL-VERSION >= 1, produce a self-contained Guix and use the
1279 ;; current Guile unconditionally.
1280 (specification->package "guile"))
1281
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.
1287 (raise (condition
1288 (&message
1289 (message "Guix is too old and cannot be upgraded")))))
1290
1291 (mbegin %store-monad
1292 (set-guile-for-build guile)
1293 (let ((guix (compiled-guix source
1294 #:version version
1295 #:channel-metadata channel-metadata
1296 #:name (string-append "guix-"
1297 (shorten version))
1298 #:pull-version pull-version
1299 #:guile-version (if (>= pull-version 1)
1300 "3.0" guile-version)
1301 #:guile-for-build guile)))
1302 (if guix
1303 (lower-object guix)
1304 (return #f)))))