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