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