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