self: Move statements after definitions in translation derivation.
[jackhill/guix/guix.git] / guix / self.scm
CommitLineData
eaae07ec 1;;; GNU Guix --- Functional package management for GNU
4d52e0f6 2;;; Copyright © 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
eaae07ec
LC
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)
eaae07ec 29 #:use-module (guix modules)
45779fa6 30 #:use-module ((guix build utils) #:select (find-files))
eaae07ec
LC
31 #:use-module (srfi srfi-1)
32 #:use-module (srfi srfi-9)
6e54e488 33 #:use-module (srfi srfi-35)
eaae07ec
LC
34 #:use-module (ice-9 match)
35 #:export (make-config.scm
8a0d9bc8 36 whole-package ;for internal use in 'guix pull'
eaae07ec 37 compiled-guix
567f0d25 38 guix-derivation))
eaae07ec
LC
39
40\f
41;;;
42;;; Dependency handling.
43;;;
44
eaae07ec
LC
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
cef392f3 51 ("guile" (ref '(gnu packages guile) 'guile-3.0/libgc-7))
b6bee63b
LC
52 ("guile-json" (ref '(gnu packages guile) 'guile-json-3))
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-gcrypt" (ref '(gnu packages gnupg) 'guile-gcrypt))
8234fe65 57 ("gnutls" (ref '(gnu packages tls) 'guile3.0-gnutls))
eaae07ec 58 ("zlib" (ref '(gnu packages compression) 'zlib))
73ac9098 59 ("lzlib" (ref '(gnu packages compression) 'lzlib))
eaae07ec
LC
60 ("gzip" (ref '(gnu packages compression) 'gzip))
61 ("bzip2" (ref '(gnu packages compression) 'bzip2))
62 ("xz" (ref '(gnu packages compression) 'xz))
554b30d2
JL
63 ("po4a" (ref '(gnu packages gettext) 'po4a))
64 ("gettext" (ref '(gnu packages gettext) 'gettext-minimal))
6e54e488 65 (_ #f)))) ;no such package
eaae07ec
LC
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
f5db54ea
LC
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
49c35bbb
LC
104(define (node-source+compiled node)
105 "Return a \"bundle\" containing both the source code and object files for
106NODE'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")
8727e030
LC
126 build
127 #:options '(#:local-build? #t
128
129 ;; "Building" it locally is faster.
130 #:substitutable? #f)))
49c35bbb 131
eaae07ec
LC
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
178DEPENDENCIES (a list of nodes). EXTRA-MODULES is a list of additional modules
179added to the source, and EXTRA-FILES is a list of additional files.
180EXTENSIONS is a set of full-blown Guile packages (e.g., 'guile-json') that
181must 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))
f5db54ea
LC
186 (source (file-mapping (string-append name "-source")
187 (append module-files extra-files))))
eaae07ec 188 (node name modules source dependencies
8031b3fa
LC
189 (compiled-modules name source
190 (map car module-files)
eaae07ec
LC
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
199list 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
6cf502d1
LC
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.
207When ITEM is a plain file name (a string), simply return a 'local-file'
208record with the new file name."
9f1c3559
LC
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.
6cf502d1
LC
214 (local-file (string-append item "/" file)
215 #:recursive? recursive?))
f408d8d6
LC
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)))
9f1c3559
LC
225 (_
226 ;; In this case, anything that refers to the result also depends on ITEM,
227 ;; which isn't great.
6cf502d1 228 (file-append item "/" file))))
9f1c3559
LC
229
230(define* (locale-data source domain
231 #:optional (directory domain))
232 "Return the locale data from 'po/DIRECTORY' in SOURCE, corresponding to
233DOMAIN, 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
6cf502d1 246 #+(file-append* source (string-append "po/" directory)))
9f1c3559
LC
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
554b30d2
JL
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)
a524a31d 293 (ice-9 vlist)
554b30d2
JL
294 (srfi srfi-1))
295
554b30d2
JL
296 (define (translate-tmp-texi po source output)
297 "Translate Texinfo file SOURCE using messages from PO, and write
298the 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
a524a31d
LC
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))))
cc753650
LC
366
367 (define* (translate-texi prefix po lang
368 #:key (extras '()))
369 "Translate the manual for one language LANG using the PO file.
370PREFIX must be the prefix of the manual, 'guix' or 'guix-cookbook'. EXTRAS is
371a list of extra files, such as '(\"contributing\")."
554b30d2 372 (let ((translations (call-with-input-file po read-po-file)))
cc753650
LC
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")))
a524a31d
LC
382 (copy-file tmp texi)
383 (translate-cross-references texi
384 translations)))
cc753650 385 (cons prefix extras))))
554b30d2 386
84c37e63
LC
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
301527be
LC
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
84c37e63
LC
416 (for-each (match-lambda
417 ((language . po)
418 (translate-texi "guix" po language
419 #:extras '("contributing"))))
420 (available-translations "." "guix-manual"))
554b30d2 421
e1e64912
LC
422 (for-each (match-lambda
423 ((language . po)
424 (translate-texi "guix-cookbook" po language)))
425 (available-translations "." "guix-cookbook"))
554b30d2 426
e1e64912
LC
427 (for-each (lambda (file)
428 (install-file file #$output))
429 (append
430 (find-files "." "contributing\\..*\\.texi$")
431 (find-files "." "guix\\..*\\.texi$")
432 (find-files "." "guix-cookbook\\..*\\.texi$"))))))
554b30d2
JL
433
434 (computed-file "guix-translated-texinfo" build))
435
4554d4c8
LC
436(define (info-manual source)
437 "Return the Info manual built from SOURCE."
438 (define texinfo
439 (module-ref (resolve-interface '(gnu packages texinfo))
440 'texinfo))
441
442 (define graphviz
443 (module-ref (resolve-interface '(gnu packages graphviz))
444 'graphviz))
445
2d337760
LC
446 (define glibc-utf8-locales
447 (module-ref (resolve-interface '(gnu packages base))
448 'glibc-utf8-locales))
449
4554d4c8 450 (define documentation
6cf502d1 451 (file-append* source "doc"))
4554d4c8
LC
452
453 (define examples
6cf502d1 454 (file-append* source "gnu/system/examples"))
4554d4c8
LC
455
456 (define build
457 (with-imported-modules '((guix build utils))
458 #~(begin
e1e64912
LC
459 (use-modules (guix build utils)
460 (ice-9 match))
4554d4c8
LC
461
462 (mkdir #$output)
463
464 ;; Create 'version.texi'.
465 ;; XXX: Can we use a more meaningful version string yet one that
466 ;; doesn't change at each commit?
467 (call-with-output-file "version.texi"
468 (lambda (port)
cbe7387c 469 (let ((version "0.0-git"))
4554d4c8
LC
470 (format port "
471@set UPDATED 1 January 1970
472@set UPDATED-MONTH January 1970
473@set EDITION ~a
474@set VERSION ~a\n" version version))))
475
476 ;; Copy configuration templates that the manual includes.
477 (for-each (lambda (template)
478 (copy-file template
479 (string-append
480 "os-config-"
481 (basename template ".tmpl")
482 ".texi")))
483 (find-files #$examples "\\.tmpl$"))
484
485 ;; Build graphs.
486 (mkdir-p (string-append #$output "/images"))
487 (for-each (lambda (dot-file)
488 (invoke #+(file-append graphviz "/bin/dot")
489 "-Tpng" "-Gratio=.9" "-Gnodesep=.005"
490 "-Granksep=.00005" "-Nfontsize=9"
491 "-Nheight=.1" "-Nwidth=.1"
492 "-o" (string-append #$output "/images/"
493 (basename dot-file ".dot")
494 ".png")
495 dot-file))
496 (find-files (string-append #$documentation "/images")
497 "\\.dot$"))
498
499 ;; Copy other PNGs.
500 (for-each (lambda (png-file)
501 (install-file png-file
502 (string-append #$output "/images")))
503 (find-files (string-append #$documentation "/images")
504 "\\.png$"))
505
506 ;; Finally build the manual. Copy it the Texinfo files to $PWD and
507 ;; add a symlink to the 'images' directory so that 'makeinfo' can
508 ;; see those images and produce image references in the Info output.
509 (copy-recursively #$documentation "."
510 #:log (%make-void-port "w"))
554b30d2
JL
511 (copy-recursively #+(translate-texi-manuals source) "."
512 #:log (%make-void-port "w"))
4554d4c8
LC
513 (delete-file-recursively "images")
514 (symlink (string-append #$output "/images") "images")
515
2d337760
LC
516 ;; Provide UTF-8 locales needed by the 'xspara.c' code in makeinfo.
517 (setenv "GUIX_LOCPATH"
518 #+(file-append glibc-utf8-locales "/lib/locale"))
519
4554d4c8 520 (for-each (lambda (texi)
e1e64912
LC
521 (match (string-split (basename texi) #\.)
522 (("guix" language "texi")
523 ;; Create 'version-LL.texi'.
524 (symlink "version.texi"
525 (string-append "version-" language
526 ".texi")))
527 (_ #f))
4554d4c8
LC
528
529 (invoke #+(file-append texinfo "/bin/makeinfo")
530 texi "-I" #$documentation
531 "-I" "."
532 "-o" (string-append #$output "/"
533 (basename texi ".texi")
534 ".info")))
535 (cons "guix.texi"
e1e64912
LC
536 (append (find-files "."
537 "^guix\\.[a-z]{2}(_[A-Z]{2})?\\.texi$")
538 (find-files "."
539 "^guix-cookbook.*\\.texi$"))))
08fdee39
LC
540
541 ;; Compress Info files.
542 (setenv "PATH"
543 #+(file-append (specification->package "gzip") "/bin"))
544 (for-each (lambda (file)
545 (invoke "gzip" "-9n" file))
546 (find-files #$output "\\.info(-[0-9]+)?$")))))
4554d4c8
LC
547
548 (computed-file "guix-manual" build))
549
b36217c5
LC
550(define-syntax-rule (prevent-inlining! identifier ...)
551 (begin (set! identifier identifier) ...))
552
553;; XXX: These procedures are actually used by 'doc/build.scm'. Protect them
554;; from inlining on Guile 3.
555(prevent-inlining! file-append* translate-texi-manuals info-manual)
556
49c35bbb
LC
557(define* (guile-module-union things #:key (name "guix-module-union"))
558 "Return the union of the subset of THINGS (packages, computed files, etc.)
559that provide Guile modules."
560 (define build
561 (with-imported-modules '((guix build union))
562 #~(begin
563 (use-modules (guix build union))
564
565 (define (modules directory)
566 (string-append directory "/share/guile/site"))
567
568 (define (objects directory)
569 (string-append directory "/lib/guile"))
570
571 (union-build #$output
572 (filter (lambda (directory)
573 (or (file-exists? (modules directory))
574 (file-exists? (objects directory))))
575 '#$things)
576
577 #:log-port (%make-void-port "w")))))
578
579 (computed-file name build))
580
581(define* (guix-command modules
a89faa3f 582 #:key source (dependencies '())
8970a886 583 guile (guile-version (effective-version)))
8a0d9bc8
LC
584 "Return the 'guix' command such that it adds MODULES and DEPENDENCIES in its
585load path."
ba488958
LC
586 (define glibc-utf8-locales
587 (module-ref (resolve-interface '(gnu packages base))
588 'glibc-utf8-locales))
589
49c35bbb
LC
590 (define module-directory
591 ;; To minimize the number of 'stat' calls needed to locate a module,
592 ;; create the union of all the module directories.
593 (guile-module-union (cons modules dependencies)))
f2d0a2cf 594
8a0d9bc8
LC
595 (program-file "guix-command"
596 #~(begin
597 (set! %load-path
49c35bbb
LC
598 (cons (string-append #$module-directory
599 "/share/guile/site/"
600 (effective-version))
601 %load-path))
8a0d9bc8 602
8a0d9bc8 603 (set! %load-compiled-path
49c35bbb
LC
604 (cons (string-append #$module-directory
605 "/lib/guile/"
606 (effective-version)
607 "/site-ccache")
a89faa3f 608 %load-compiled-path))
8a0d9bc8 609
ba488958
LC
610 ;; To maximize the chances that locales are set up right
611 ;; out-of-the-box, bundle "common" UTF-8 locales.
612 (let ((locpath (getenv "GUIX_LOCPATH")))
613 (setenv "GUIX_LOCPATH"
614 (string-append (if locpath
615 (string-append locpath ":")
616 "")
617 #$(file-append glibc-utf8-locales
618 "/lib/locale"))))
619
8a0d9bc8
LC
620 (let ((guix-main (module-ref (resolve-interface '(guix ui))
621 'guix-main)))
9f1c3559
LC
622 #$(if source
623 #~(begin
624 (bindtextdomain "guix"
625 #$(locale-data source "guix"))
626 (bindtextdomain "guix-packages"
627 #$(locale-data source
628 "guix-packages"
629 "packages")))
630 #t)
8a0d9bc8
LC
631
632 ;; XXX: It would be more convenient to change it to:
633 ;; (exit (apply guix-main (command-line)))
8970a886
LC
634 (apply guix-main (command-line))))
635 #:guile guile))
8a0d9bc8 636
e3744779
LC
637(define (miscellaneous-files source)
638 "Return data files taken from SOURCE."
639 (file-mapping "guix-misc"
640 `(("etc/bash_completion.d/guix"
641 ,(file-append* source "/etc/completion/bash/guix"))
642 ("etc/bash_completion.d/guix-daemon"
643 ,(file-append* source "/etc/completion/bash/guix-daemon"))
644 ("share/zsh/site-functions/_guix"
645 ,(file-append* source "/etc/completion/zsh/_guix"))
646 ("share/fish/vendor_completions.d/guix.fish"
647 ,(file-append* source "/etc/completion/fish/guix.fish"))
e3744779 648 ("share/guix/berlin.guixsd.org.pub"
6a837b60
LC
649 ,(file-append* source
650 "/etc/substitutes/berlin.guixsd.org.pub"))
757e633d
LC
651 ("share/guix/ci.guix.gnu.org.pub" ;alias
652 ,(file-append* source "/etc/substitutes/berlin.guixsd.org.pub"))
6a837b60 653 ("share/guix/ci.guix.info.pub" ;alias
e3744779
LC
654 ,(file-append* source "/etc/substitutes/berlin.guixsd.org.pub")))))
655
8a0d9bc8 656(define* (whole-package name modules dependencies
9f1c3559
LC
657 #:key
658 (guile-version (effective-version))
e3744779 659 info daemon miscellany
8d3beb3a 660 guile
9f1c3559
LC
661 (command (guix-command modules
662 #:dependencies dependencies
8970a886 663 #:guile guile
9f1c3559 664 #:guile-version guile-version)))
8a0d9bc8 665 "Return the whole Guix package NAME that uses MODULES, a derivation of all
49c35bbb
LC
666the modules (under share/guile/site and lib/guile), and DEPENDENCIES, a list
667of packages depended on. COMMAND is the 'guix' program to use; INFO is the
668Info manual."
765a5bf1
LC
669 (define (wrap daemon)
670 (program-file "guix-daemon"
671 #~(begin
dfc69e4b
LC
672 ;; Refer to the right 'guix' command for 'guix
673 ;; substitute' & co.
765a5bf1 674 (setenv "GUIX" #$command)
dfc69e4b
LC
675
676 ;; Honor the user's settings rather than those hardcoded
677 ;; in the 'guix-daemon' package.
678 (unless (getenv "GUIX_STATE_DIRECTORY")
679 (setenv "GUIX_STATE_DIRECTORY"
680 #$(string-append %localstatedir "/guix")))
681 (unless (getenv "GUIX_CONFIGURATION_DIRECTORY")
682 (setenv "GUIX_CONFIGURATION_DIRECTORY"
683 #$(string-append %sysconfdir "/guix")))
684 (unless (getenv "NIX_STORE_DIR")
fa866548 685 (setenv "NIX_STORE_DIR" #$%storedir))
dfc69e4b 686
765a5bf1
LC
687 (apply execl #$(file-append daemon "/bin/guix-daemon")
688 "guix-daemon" (cdr (command-line))))))
689
9f1c3559
LC
690 (computed-file name
691 (with-imported-modules '((guix build utils))
692 #~(begin
693 (use-modules (guix build utils))
e3744779 694
765a5bf1
LC
695 (define daemon
696 #$(and daemon (wrap daemon)))
697
9f1c3559
LC
698 (mkdir-p (string-append #$output "/bin"))
699 (symlink #$command
700 (string-append #$output "/bin/guix"))
701
765a5bf1
LC
702 (when daemon
703 (symlink daemon
baed9236
LC
704 (string-append #$output "/bin/guix-daemon")))
705
49c35bbb
LC
706 (let ((share (string-append #$output "/share"))
707 (lib (string-append #$output "/lib"))
708 (info #$info))
709 (mkdir-p share)
710 (symlink #$(file-append modules "/share/guile")
711 (string-append share "/guile"))
4554d4c8 712 (when info
49c35bbb
LC
713 (symlink #$info (string-append share "/info")))
714
715 (mkdir-p lib)
716 (symlink #$(file-append modules "/lib/guile")
717 (string-append lib "/guile")))
a89faa3f 718
e3744779
LC
719 (when #$miscellany
720 (copy-recursively #$miscellany #$output
49c35bbb 721 #:log (%make-void-port "w")))))))
8a0d9bc8 722
eaae07ec 723(define* (compiled-guix source #:key (version %guix-version)
8a0d9bc8 724 (pull-version 1)
eaae07ec
LC
725 (name (string-append "guix-" version))
726 (guile-version (effective-version))
6e54e488 727 (guile-for-build (default-guile))
eaae07ec 728 (zlib (specification->package "zlib"))
73ac9098 729 (lzlib (specification->package "lzlib"))
eaae07ec
LC
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
6e54e488 736 (specification->package "guile-json"))
eaae07ec
LC
737
738 (define guile-ssh
6e54e488 739 (specification->package "guile-ssh"))
eaae07ec
LC
740
741 (define guile-git
6e54e488 742 (specification->package "guile-git"))
eaae07ec 743
d59e75f3 744 (define guile-sqlite3
6e54e488 745 (specification->package "guile-sqlite3"))
d59e75f3 746
ca719424 747 (define guile-gcrypt
6e54e488 748 (specification->package "guile-gcrypt"))
ca719424 749
108015df 750 (define gnutls
6e54e488 751 (specification->package "gnutls"))
108015df 752
eaae07ec
LC
753 (define dependencies
754 (match (append-map (lambda (package)
755 (cons (list "x" package)
e13240f5 756 (package-transitive-propagated-inputs package)))
ca719424 757 (list guile-gcrypt gnutls guile-git guile-json
108015df 758 guile-ssh guile-sqlite3))
eaae07ec
LC
759 (((labels packages _ ...) ...)
760 packages)))
761
762 (define *core-modules*
763 (scheme-node "guix-core"
764 '((guix)
765 (guix monad-repl)
766 (guix packages)
767 (guix download)
768 (guix discovery)
769 (guix profiles)
770 (guix build-system gnu)
771 (guix build-system trivial)
772 (guix build profiles)
773 (guix build gnu-build-system))
774
775 ;; Provide a dummy (guix config) with the default version
776 ;; number, storedir, etc. This is so that "guix-core" is the
777 ;; same across all installations and doesn't need to be
778 ;; rebuilt when the version changes, which in turn means we
779 ;; can have substitutes for it.
780 #:extra-modules
ca719424 781 `(((guix config) => ,(make-config.scm)))
eaae07ec 782
8292f5a9
LC
783 ;; (guix man-db) is needed at build-time by (guix profiles)
784 ;; but we don't need to compile it; not compiling it allows
785 ;; us to avoid an extra dependency on guile-gdbm-ffi.
786 #:extra-files
3931c761 787 `(("guix/man-db.scm" ,(local-file "../guix/man-db.scm"))
554b30d2 788 ("guix/build/po.scm" ,(local-file "../guix/build/po.scm"))
3931c761
LC
789 ("guix/store/schema.sql"
790 ,(local-file "../guix/store/schema.sql")))
8292f5a9 791
ca719424 792 #:extensions (list guile-gcrypt)
eaae07ec
LC
793 #:guile-for-build guile-for-build))
794
795 (define *extra-modules*
796 (scheme-node "guix-extra"
797 (filter-map (match-lambda
798 (('guix 'scripts _ ..1) #f)
8292f5a9 799 (('guix 'man-db) #f)
852d30a6 800 (('guix 'tests _ ...) #f)
eaae07ec
LC
801 (name name))
802 (scheme-modules* source "guix"))
803 (list *core-modules*)
804 #:extensions dependencies
805 #:guile-for-build guile-for-build))
806
f2e66663
LC
807 (define *core-package-modules*
808 (scheme-node "guix-packages-base"
eaae07ec 809 `((gnu packages)
f2e66663 810 (gnu packages base))
eaae07ec
LC
811 (list *core-modules* *extra-modules*)
812 #:extensions dependencies
f2e66663
LC
813
814 ;; Add all the non-Scheme files here. We must do it here so
815 ;; that 'search-patches' & co. can find them. Ideally we'd
816 ;; keep them next to the .scm files that use them but it's
817 ;; difficult to do (XXX).
818 #:extra-files
eaae07ec
LC
819 (file-imports source "gnu/packages"
820 (lambda (file stat)
821 (and (eq? 'regular (stat:type stat))
822 (not (string-suffix? ".scm" file))
823 (not (string-suffix? ".go" file))
824 (not (string-prefix? ".#" file))
825 (not (string-suffix? "~" file)))))
826 #:guile-for-build guile-for-build))
827
f2e66663
LC
828 (define *package-modules*
829 (scheme-node "guix-packages"
830 (scheme-modules* source "gnu/packages")
831 (list *core-modules* *extra-modules* *core-package-modules*)
832 #:extensions dependencies
833 #:guile-for-build guile-for-build))
834
eaae07ec
LC
835 (define *system-modules*
836 (scheme-node "guix-system"
837 `((gnu system)
838 (gnu services)
5a6e04c5 839 ,@(scheme-modules* source "gnu/bootloader")
a49d633c 840 ,@(scheme-modules* source "gnu/system")
079c93e1
MW
841 ,@(scheme-modules* source "gnu/services")
842 ,@(scheme-modules* source "gnu/machine"))
f2e66663
LC
843 (list *core-package-modules* *package-modules*
844 *extra-modules* *core-modules*)
eaae07ec
LC
845 #:extensions dependencies
846 #:extra-files
1458f768
LC
847 (append (file-imports source "gnu/system/examples"
848 (const #t))
8bb62ae1 849
a49d633c
MO
850 ;; All the installer code is on the build-side.
851 (file-imports source "gnu/installer/"
852 (const #t))
1458f768
LC
853 ;; Build-side code that we don't build. Some of
854 ;; these depend on guile-rsvg, the Shepherd, etc.
855 (file-imports source "gnu/build" (const #t)))
eaae07ec
LC
856 #:guile-for-build
857 guile-for-build))
858
859 (define *cli-modules*
860 (scheme-node "guix-cli"
b5f8c2c8
LC
861 (append (scheme-modules* source "/guix/scripts")
862 `((gnu ci)))
f2e66663
LC
863 (list *core-modules* *extra-modules*
864 *core-package-modules* *package-modules*
eaae07ec
LC
865 *system-modules*)
866 #:extensions dependencies
867 #:guile-for-build guile-for-build))
868
5f2daffe
LC
869 (define *system-test-modules*
870 ;; Ship these modules mostly so (gnu ci) can discover them.
871 (scheme-node "guix-system-tests"
872 `((gnu tests)
873 ,@(scheme-modules* source "gnu/tests"))
874 (list *core-package-modules* *package-modules*
875 *extra-modules* *system-modules* *core-modules*
876 *cli-modules*) ;for (guix scripts pack), etc.
877 #:extensions dependencies
878 #:guile-for-build guile-for-build))
879
eaae07ec
LC
880 (define *config*
881 (scheme-node "guix-config"
882 '()
883 #:extra-modules
884 `(((guix config)
ca719424 885 => ,(make-config.scm #:zlib zlib
73ac9098 886 #:lzlib lzlib
eaae07ec
LC
887 #:gzip gzip
888 #:bzip2 bzip2
889 #:xz xz
eaae07ec
LC
890 #:package-name
891 %guix-package-name
892 #:package-version
893 version
894 #:bug-report-address
895 %guix-bug-report-address
896 #:home-page-url
897 %guix-home-page-url)))
898 #:guile-for-build guile-for-build))
899
a89faa3f 900 (define (built-modules node-subset)
8a0d9bc8 901 (directory-union (string-append name "-modules")
a89faa3f 902 (append-map node-subset
8a0d9bc8
LC
903
904 ;; Note: *CONFIG* comes first so that it
905 ;; overrides the (guix config) module that
906 ;; comes with *CORE-MODULES*.
907 (list *config*
908 *cli-modules*
54800977 909 *system-test-modules*
8a0d9bc8
LC
910 *system-modules*
911 *package-modules*
912 *core-package-modules*
913 *extra-modules*
914 *core-modules*))
915
916 ;; Silently choose the first entry upon collision so that
917 ;; we choose *CONFIG*.
918 #:resolve-collision 'first
919
920 ;; When we do (add-to-store "utils.scm"), "utils.scm" must
921 ;; be a regular file, not a symlink. Thus, arrange so that
922 ;; regular files appear as regular files in the final
923 ;; output.
924 #:copy? #t
925 #:quiet? #t))
926
927 ;; Version 0 of 'guix pull' meant we'd just return Scheme modules.
928 ;; Version 1 is when we return the full package.
929 (cond ((= 1 pull-version)
930 ;; The whole package, with a standard file hierarchy.
49c35bbb
LC
931 (let* ((modules (built-modules (compose list node-source+compiled)))
932 (command (guix-command modules
a89faa3f
LC
933 #:source source
934 #:dependencies dependencies
8970a886 935 #:guile guile-for-build
a89faa3f
LC
936 #:guile-version guile-version)))
937 (whole-package name modules dependencies
9f1c3559 938 #:command command
8970a886 939 #:guile guile-for-build
baed9236
LC
940
941 ;; Include 'guix-daemon'. XXX: Here we inject an
942 ;; older snapshot of guix-daemon, but that's a good
943 ;; enough approximation for now.
944 #:daemon (module-ref (resolve-interface
945 '(gnu packages
946 package-management))
947 'guix-daemon)
948
4554d4c8 949 #:info (info-manual source)
e3744779 950 #:miscellany (miscellaneous-files source)
9f1c3559 951 #:guile-version guile-version)))
8a0d9bc8 952 ((= 0 pull-version)
a89faa3f
LC
953 ;; Legacy 'guix pull': return the .scm and .go files as one
954 ;; directory.
955 (built-modules (lambda (node)
956 (list (node-source node)
957 (node-compiled node)))))
8a0d9bc8
LC
958 (else
959 ;; Unsupported 'guix pull' version.
960 #f)))
eaae07ec
LC
961
962\f
963;;;
964;;; Generating (guix config).
965;;;
966
eaae07ec
LC
967(define %persona-variables
968 ;; (guix config) variables that define Guix's persona.
969 '(%guix-package-name
970 %guix-version
971 %guix-bug-report-address
972 %guix-home-page-url))
973
974(define %config-variables
45779fa6
LC
975 ;; (guix config) variables corresponding to Guix configuration.
976 (letrec-syntax ((variables (syntax-rules ()
977 ((_)
978 '())
979 ((_ variable rest ...)
980 (cons `(variable . ,variable)
981 (variables rest ...))))))
54eadc42 982 (variables %localstatedir %storedir %sysconfdir)))
eaae07ec 983
73ac9098 984(define* (make-config.scm #:key zlib lzlib gzip xz bzip2
eaae07ec
LC
985 (package-name "GNU Guix")
986 (package-version "0")
987 (bug-report-address "bug-guix@gnu.org")
3fb3291e 988 (home-page-url "https://guix.gnu.org"))
eaae07ec
LC
989
990 ;; Hack so that Geiser is not confused.
991 (define defmod 'define-module)
992
993 (scheme-file "config.scm"
eb72cdf0 994 #~(;; The following expressions get spliced.
eaae07ec
LC
995 (#$defmod (guix config)
996 #:export (%guix-package-name
997 %guix-version
998 %guix-bug-report-address
999 %guix-home-page-url
54eadc42 1000 %system
7af5c2a2
LC
1001 %store-directory
1002 %state-directory
1003 %store-database-directory
1004 %config-directory
eaae07ec 1005 %libz
73ac9098 1006 %liblz
eaae07ec
LC
1007 %gzip
1008 %bzip2
e8cb9c01 1009 %xz))
eaae07ec 1010
54eadc42
LC
1011 (define %system
1012 #$(%current-system))
1013
63cab418
LC
1014 #$@(map (match-lambda
1015 ((name . value)
1016 #~(define-public #$name #$value)))
1017 %config-variables)
1018
7af5c2a2
LC
1019 (define %store-directory
1020 (or (and=> (getenv "NIX_STORE_DIR") canonicalize-path)
1021 %storedir))
1022
1023 (define %state-directory
1024 ;; This must match `NIX_STATE_DIR' as defined in
1025 ;; `nix/local.mk'.
a87d66f3 1026 (or (getenv "GUIX_STATE_DIRECTORY")
7af5c2a2
LC
1027 (string-append %localstatedir "/guix")))
1028
1029 (define %store-database-directory
a87d66f3 1030 (or (getenv "GUIX_DATABASE_DIRECTORY")
7af5c2a2
LC
1031 (string-append %state-directory "/db")))
1032
1033 (define %config-directory
1034 ;; This must match `GUIX_CONFIGURATION_DIRECTORY' as
1035 ;; defined in `nix/local.mk'.
1036 (or (getenv "GUIX_CONFIGURATION_DIRECTORY")
1037 (string-append %sysconfdir "/guix")))
1038
63cab418
LC
1039 (define %guix-package-name #$package-name)
1040 (define %guix-version #$package-version)
1041 (define %guix-bug-report-address #$bug-report-address)
1042 (define %guix-home-page-url #$home-page-url)
1043
63cab418
LC
1044 (define %gzip
1045 #+(and gzip (file-append gzip "/bin/gzip")))
1046 (define %bzip2
1047 #+(and bzip2 (file-append bzip2 "/bin/bzip2")))
1048 (define %xz
1049 #+(and xz (file-append xz "/bin/xz")))
1050
63cab418
LC
1051 (define %libz
1052 #+(and zlib
73ac9098
LC
1053 (file-append zlib "/lib/libz")))
1054
1055 (define %liblz
1056 #+(and lzlib
1057 (file-append lzlib "/lib/liblz"))))
eb72cdf0
LC
1058
1059 ;; Guile 2.0 *requires* the 'define-module' to be at the
e8cb9c01 1060 ;; top-level or the 'toplevel-ref' in the resulting .go file are
eb72cdf0
LC
1061 ;; made relative to a nonexistent anonymous module.
1062 #:splice? #t))
eaae07ec 1063
eaae07ec
LC
1064\f
1065;;;
1066;;; Building.
1067;;;
1068
8031b3fa 1069(define* (compiled-modules name module-tree module-files
eaae07ec
LC
1070 #:optional
1071 (dependencies '())
1072 (dependencies-compiled '())
1073 #:key
1074 (extensions '()) ;full-blown Guile packages
1075 parallel?
1076 guile-for-build)
8031b3fa
LC
1077 "Build all the MODULE-FILES from MODULE-TREE. MODULE-FILES must be a list
1078like '(\"guix/foo.scm\" \"gnu/bar.scm\") and MODULE-TREE is the directory
1079containing MODULE-FILES and possibly other files as well."
eaae07ec
LC
1080 ;; This is a non-monadic, enhanced version of 'compiled-file' from (guix
1081 ;; gexp).
1082 (define build
1083 (with-imported-modules (source-module-closure
1084 '((guix build compile)
1085 (guix build utils)))
1086 #~(begin
1087 (use-modules (srfi srfi-26)
1088 (ice-9 match)
1089 (ice-9 format)
1090 (ice-9 threads)
1091 (guix build compile)
1092 (guix build utils))
1093
1094 (define (regular? file)
1095 (not (member file '("." ".."))))
1096
1097 (define (report-load file total completed)
1098 (display #\cr)
1099 (format #t
35dcaa11
LC
1100 "[~3@a/~3@a] loading...\t~5,1f% of ~d files"
1101
1102 ;; Note: Multiply TOTAL by two to account for the
1103 ;; compilation phase that follows.
1104 completed (* total 2)
1105
eaae07ec
LC
1106 (* 100. (/ completed total)) total)
1107 (force-output))
1108
1109 (define (report-compilation file total completed)
1110 (display #\cr)
35dcaa11
LC
1111 (format #t "[~3@a/~3@a] compiling...\t~5,1f% of ~d files"
1112
1113 ;; Add TOTAL to account for the load phase that came
1114 ;; before.
1115 (+ total completed) (* total 2)
1116
eaae07ec
LC
1117 (* 100. (/ completed total)) total)
1118 (force-output))
1119
8031b3fa
LC
1120 (define (process-directory directory files output)
1121 ;; Hide compilation warnings.
1122 (parameterize ((current-warning-port (%make-void-port "w")))
1123 (compile-files directory #$output files
1124 #:workers (parallel-job-count)
1125 #:report-load report-load
1126 #:report-compilation report-compilation)))
eaae07ec 1127
35dcaa11
LC
1128 (setvbuf (current-output-port) 'line)
1129 (setvbuf (current-error-port) 'line)
eaae07ec
LC
1130
1131 (set! %load-path (cons #+module-tree %load-path))
1132 (set! %load-path
1133 (append '#+dependencies
1134 (map (lambda (extension)
1135 (string-append extension "/share/guile/site/"
1136 (effective-version)))
1137 '#+extensions)
1138 %load-path))
1139
1140 (set! %load-compiled-path
1141 (append '#+dependencies-compiled
1142 (map (lambda (extension)
1143 (string-append extension "/lib/guile/"
1144 (effective-version)
1145 "/site-ccache"))
1146 '#+extensions)
1147 %load-compiled-path))
1148
1149 ;; Load the compiler modules upfront.
1150 (compile #f)
1151
1152 (mkdir #$output)
1153 (chdir #+module-tree)
8031b3fa 1154 (process-directory "." '#+module-files #$output)
69447b63 1155 (newline))))
eaae07ec
LC
1156
1157 (computed-file name build
1158 #:guile guile-for-build
1159 #:options
1160 `(#:local-build? #f ;allow substitutes
1161
1162 ;; Don't annoy people about _IONBF deprecation.
54be2b4d
LC
1163 ;; Initialize 'terminal-width' in (system repl debug)
1164 ;; to a large-enough value to make backtrace more
1165 ;; verbose.
1166 #:env-vars (("GUILE_WARN_DEPRECATED" . "no")
1167 ("COLUMNS" . "200")))))
eaae07ec
LC
1168
1169\f
1170;;;
1171;;; Building.
1172;;;
1173
eaae07ec 1174(define* (guix-derivation source version
8a0d9bc8
LC
1175 #:optional (guile-version (effective-version))
1176 #:key (pull-version 0))
eaae07ec 1177 "Return, as a monadic value, the derivation to build the Guix from SOURCE
8a0d9bc8
LC
1178for GUILE-VERSION. Use VERSION as the version string. PULL-VERSION specifies
1179the version of the 'guix pull' protocol. Return #f if this PULL-VERSION value
1180is not supported."
eaae07ec
LC
1181 (define (shorten version)
1182 (if (and (string-every char-set:hex-digit version)
1183 (> (string-length version) 9))
1184 (string-take version 9) ;Git commit
1185 version))
1186
1187 (define guile
8234fe65
LC
1188 ;; When PULL-VERSION >= 1, produce a self-contained Guix and use the
1189 ;; current Guile unconditionally.
1190 (specification->package "guile"))
6e54e488
LC
1191
1192 (when (and (< pull-version 1)
1193 (not (string=? (package-version guile) guile-version)))
1194 ;; Guix < 0.15.0 has PULL-VERSION = 0, where the host Guile is reused and
1195 ;; can be any version. When that happens and Guile is not current (e.g.,
1196 ;; it's Guile 2.0), just bail out.
1197 (raise (condition
1198 (&message
1199 (message "Guix is too old and cannot be upgraded")))))
eaae07ec
LC
1200
1201 (mbegin %store-monad
1202 (set-guile-for-build guile)
8a0d9bc8
LC
1203 (let ((guix (compiled-guix source
1204 #:version version
1205 #:name (string-append "guix-"
1206 (shorten version))
1207 #:pull-version pull-version
099bb017 1208 #:guile-version (if (>= pull-version 1)
8234fe65 1209 "3.0" guile-version)
8a0d9bc8
LC
1210 #:guile-for-build guile)))
1211 (if guix
1212 (lower-object guix)
1213 (return #f)))))