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