self: Add dependency on GnuTLS.
[jackhill/guix/guix.git] / guix / self.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017, 2018 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 ((guix build compile) #:select (%lightweight-optimizations))
32 #:use-module (srfi srfi-1)
33 #:use-module (srfi srfi-9)
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 reload-guix))
40
41 \f
42 ;;;
43 ;;; Dependency handling.
44 ;;;
45
46 (define* (false-if-wrong-guile package
47 #:optional (guile-version (effective-version)))
48 "Return #f if PACKAGE depends on the \"wrong\" major version of Guile (e.g.,
49 2.0 instead of 2.2), otherwise return PACKAGE."
50 (let ((guile (any (match-lambda
51 ((label (? package? dep) _ ...)
52 (and (string=? (package-name dep) "guile")
53 dep)))
54 (package-direct-inputs package))))
55 (and (or (not guile)
56 (string-prefix? guile-version
57 (package-version guile)))
58 package)))
59
60 (define (package-for-guile guile-version . names)
61 "Return the package with one of the given NAMES that depends on
62 GUILE-VERSION (\"2.0\" or \"2.2\"), or #f if none of the packages matches."
63 (let loop ((names names))
64 (match names
65 (()
66 #f)
67 ((name rest ...)
68 (match (specification->package name)
69 (#f
70 (loop rest))
71 ((? package? package)
72 (or (false-if-wrong-guile package guile-version)
73 (loop rest))))))))
74
75 (define specification->package
76 ;; Use our own variant of that procedure because that of (gnu packages)
77 ;; would traverse all the .scm files, which is wasteful.
78 (let ((ref (lambda (module variable)
79 (module-ref (resolve-interface module) variable))))
80 (match-lambda
81 ("guile" (ref '(gnu packages commencement) 'guile-final))
82 ("guile-json" (ref '(gnu packages guile) 'guile-json))
83 ("guile-ssh" (ref '(gnu packages ssh) 'guile-ssh))
84 ("guile-git" (ref '(gnu packages guile) 'guile-git))
85 ("guile-sqlite3" (ref '(gnu packages guile) 'guile-sqlite3))
86 ("gnutls" (ref '(gnu packages tls) 'gnutls))
87 ("libgcrypt" (ref '(gnu packages gnupg) 'libgcrypt))
88 ("zlib" (ref '(gnu packages compression) 'zlib))
89 ("gzip" (ref '(gnu packages compression) 'gzip))
90 ("bzip2" (ref '(gnu packages compression) 'bzip2))
91 ("xz" (ref '(gnu packages compression) 'xz))
92 ("guile2.0-json" (ref '(gnu packages guile) 'guile2.0-json))
93 ("guile2.0-ssh" (ref '(gnu packages ssh) 'guile2.0-ssh))
94 ("guile2.0-git" (ref '(gnu packages guile) 'guile2.0-git))
95 ;; XXX: No "guile2.0-sqlite3".
96 ("guile2.0-gnutls" (ref '(gnu packages tls) 'gnutls/guile-2.0))
97 (_ #f)))) ;no such package
98
99 \f
100 ;;;
101 ;;; Derivations.
102 ;;;
103
104 ;; Node in a DAG of build tasks. Each node maps to a derivation, but it's
105 ;; easier to express things this way.
106 (define-record-type <node>
107 (node name modules source dependencies compiled)
108 node?
109 (name node-name) ;string
110 (modules node-modules) ;list of module names
111 (source node-source) ;list of source files
112 (dependencies node-dependencies) ;list of nodes
113 (compiled node-compiled)) ;node -> lowerable object
114
115 (define (node-fold proc init nodes)
116 (let loop ((nodes nodes)
117 (visited (setq))
118 (result init))
119 (match nodes
120 (() result)
121 ((head tail ...)
122 (if (set-contains? visited head)
123 (loop tail visited result)
124 (loop tail (set-insert head visited)
125 (proc head result)))))))
126
127 (define (node-modules/recursive nodes)
128 (node-fold (lambda (node modules)
129 (append (node-modules node) modules))
130 '()
131 nodes))
132
133 (define* (closure modules #:optional (except '()))
134 (source-module-closure modules
135 #:select?
136 (match-lambda
137 (('guix 'config)
138 #f)
139 ((and module
140 (or ('guix _ ...) ('gnu _ ...)))
141 (not (member module except)))
142 (rest #f))))
143
144 (define module->import
145 ;; Return a file-name/file-like object pair for the specified module and
146 ;; suitable for 'imported-files'.
147 (match-lambda
148 ((module '=> thing)
149 (let ((file (module-name->file-name module)))
150 (list file thing)))
151 (module
152 (let ((file (module-name->file-name module)))
153 (list file
154 (local-file (search-path %load-path file)))))))
155
156 (define* (scheme-node name modules #:optional (dependencies '())
157 #:key (extra-modules '()) (extra-files '())
158 (extensions '())
159 parallel? guile-for-build)
160 "Return a node that builds the given Scheme MODULES, and depends on
161 DEPENDENCIES (a list of nodes). EXTRA-MODULES is a list of additional modules
162 added to the source, and EXTRA-FILES is a list of additional files.
163 EXTENSIONS is a set of full-blown Guile packages (e.g., 'guile-json') that
164 must be present in the search path."
165 (let* ((modules (append extra-modules
166 (closure modules
167 (node-modules/recursive dependencies))))
168 (module-files (map module->import modules))
169 (source (imported-files (string-append name "-source")
170 (append module-files extra-files))))
171 (node name modules source dependencies
172 (compiled-modules name source
173 (map car module-files)
174 (map node-source dependencies)
175 (map node-compiled dependencies)
176 #:extensions extensions
177 #:parallel? parallel?
178 #:guile-for-build guile-for-build))))
179
180 (define (file-imports directory sub-directory pred)
181 "List all the files matching PRED under DIRECTORY/SUB-DIRECTORY. Return a
182 list of file-name/file-like objects suitable as inputs to 'imported-files'."
183 (map (lambda (file)
184 (list (string-drop file (+ 1 (string-length directory)))
185 (local-file file #:recursive? #t)))
186 (find-files (string-append directory "/" sub-directory) pred)))
187
188 (define (scheme-modules* directory sub-directory)
189 "Return the list of module names found under SUB-DIRECTORY in DIRECTORY."
190 (let ((prefix (string-length directory)))
191 (map (lambda (file)
192 (file-name->module-name (string-drop file prefix)))
193 (scheme-files (string-append directory "/" sub-directory)))))
194
195 (define* (sub-directory item sub-directory)
196 "Return SUB-DIRECTORY within ITEM, which may be a file name or a file-like
197 object."
198 (match item
199 ((? string?)
200 ;; This is the optimal case: we return a new "source". Thus, a
201 ;; derivation that depends on this sub-directory does not depend on ITEM
202 ;; itself.
203 (local-file (string-append item "/" sub-directory)
204 #:recursive? #t))
205 ;; TODO: Add 'local-file?' case.
206 (_
207 ;; In this case, anything that refers to the result also depends on ITEM,
208 ;; which isn't great.
209 (file-append item "/" sub-directory))))
210
211 (define* (locale-data source domain
212 #:optional (directory domain))
213 "Return the locale data from 'po/DIRECTORY' in SOURCE, corresponding to
214 DOMAIN, a gettext domain."
215 (define gettext
216 (module-ref (resolve-interface '(gnu packages gettext))
217 'gettext-minimal))
218
219 (define build
220 (with-imported-modules '((guix build utils))
221 #~(begin
222 (use-modules (guix build utils)
223 (srfi srfi-26)
224 (ice-9 match) (ice-9 ftw))
225
226 (define po-directory
227 #+(sub-directory source (string-append "po/" directory)))
228
229 (define (compile language)
230 (let ((gmo (string-append #$output "/" language "/LC_MESSAGES/"
231 #$domain ".mo")))
232 (mkdir-p (dirname gmo))
233 (invoke #+(file-append gettext "/bin/msgfmt")
234 "-c" "--statistics" "--verbose"
235 "-o" gmo
236 (string-append po-directory "/" language ".po"))))
237
238 (define (linguas)
239 ;; Return the list of languages. Note: don't read 'LINGUAS'
240 ;; because it contains things like 'en@boldquot' that do not have
241 ;; a corresponding .po file.
242 (map (cut basename <> ".po")
243 (scandir po-directory
244 (cut string-suffix? ".po" <>))))
245
246 (for-each compile (linguas)))))
247
248 (computed-file (string-append "guix-locale-" domain)
249 build))
250
251 (define (info-manual source)
252 "Return the Info manual built from SOURCE."
253 (define texinfo
254 (module-ref (resolve-interface '(gnu packages texinfo))
255 'texinfo))
256
257 (define graphviz
258 (module-ref (resolve-interface '(gnu packages graphviz))
259 'graphviz))
260
261 (define documentation
262 (sub-directory source "doc"))
263
264 (define examples
265 (sub-directory source "gnu/system/examples"))
266
267 (define build
268 (with-imported-modules '((guix build utils))
269 #~(begin
270 (use-modules (guix build utils))
271
272 (mkdir #$output)
273
274 ;; Create 'version.texi'.
275 ;; XXX: Can we use a more meaningful version string yet one that
276 ;; doesn't change at each commit?
277 (call-with-output-file "version.texi"
278 (lambda (port)
279 (let ((version "0.0-git)"))
280 (format port "
281 @set UPDATED 1 January 1970
282 @set UPDATED-MONTH January 1970
283 @set EDITION ~a
284 @set VERSION ~a\n" version version))))
285
286 ;; Copy configuration templates that the manual includes.
287 (for-each (lambda (template)
288 (copy-file template
289 (string-append
290 "os-config-"
291 (basename template ".tmpl")
292 ".texi")))
293 (find-files #$examples "\\.tmpl$"))
294
295 ;; Build graphs.
296 (mkdir-p (string-append #$output "/images"))
297 (for-each (lambda (dot-file)
298 (invoke #+(file-append graphviz "/bin/dot")
299 "-Tpng" "-Gratio=.9" "-Gnodesep=.005"
300 "-Granksep=.00005" "-Nfontsize=9"
301 "-Nheight=.1" "-Nwidth=.1"
302 "-o" (string-append #$output "/images/"
303 (basename dot-file ".dot")
304 ".png")
305 dot-file))
306 (find-files (string-append #$documentation "/images")
307 "\\.dot$"))
308
309 ;; Copy other PNGs.
310 (for-each (lambda (png-file)
311 (install-file png-file
312 (string-append #$output "/images")))
313 (find-files (string-append #$documentation "/images")
314 "\\.png$"))
315
316 ;; Finally build the manual. Copy it the Texinfo files to $PWD and
317 ;; add a symlink to the 'images' directory so that 'makeinfo' can
318 ;; see those images and produce image references in the Info output.
319 (copy-recursively #$documentation "."
320 #:log (%make-void-port "w"))
321 (delete-file-recursively "images")
322 (symlink (string-append #$output "/images") "images")
323
324 (for-each (lambda (texi)
325 (unless (string=? "guix.texi" texi)
326 ;; Create 'version-LL.texi'.
327 (let* ((base (basename texi ".texi"))
328 (dot (string-index base #\.))
329 (tag (string-drop base (+ 1 dot))))
330 (symlink "version.texi"
331 (string-append "version-" tag ".texi"))))
332
333 (invoke #+(file-append texinfo "/bin/makeinfo")
334 texi "-I" #$documentation
335 "-I" "."
336 "-o" (string-append #$output "/"
337 (basename texi ".texi")
338 ".info")))
339 (cons "guix.texi"
340 (find-files "." "^guix\\.[a-z]{2}\\.texi$"))))))
341
342 (computed-file "guix-manual" build))
343
344 (define* (guix-command modules #:optional compiled-modules
345 #:key source (dependencies '())
346 (guile-version (effective-version)))
347 "Return the 'guix' command such that it adds MODULES and DEPENDENCIES in its
348 load path."
349 (program-file "guix-command"
350 #~(begin
351 (set! %load-path
352 (append '#$(map (lambda (package)
353 (file-append package
354 "/share/guile/site/"
355 guile-version))
356 dependencies)
357 %load-path))
358
359 (set! %load-compiled-path
360 (append '#$(map (lambda (package)
361 (file-append package "/lib/guile/"
362 guile-version
363 "/site-ccache"))
364 dependencies)
365 %load-compiled-path))
366
367 (set! %load-path (cons #$modules %load-path))
368 (set! %load-compiled-path
369 (cons (or #$compiled-modules #$modules)
370 %load-compiled-path))
371
372 (let ((guix-main (module-ref (resolve-interface '(guix ui))
373 'guix-main)))
374 #$(if source
375 #~(begin
376 (bindtextdomain "guix"
377 #$(locale-data source "guix"))
378 (bindtextdomain "guix-packages"
379 #$(locale-data source
380 "guix-packages"
381 "packages")))
382 #t)
383
384 ;; XXX: It would be more convenient to change it to:
385 ;; (exit (apply guix-main (command-line)))
386 (apply guix-main (command-line))))))
387
388 (define* (whole-package name modules dependencies
389 #:key
390 (guile-version (effective-version))
391 compiled-modules
392 info daemon
393 (command (guix-command modules
394 #:dependencies dependencies
395 #:guile-version guile-version)))
396 "Return the whole Guix package NAME that uses MODULES, a derivation of all
397 the modules, and DEPENDENCIES, a list of packages depended on. COMMAND is the
398 'guix' program to use; INFO is the Info manual. When COMPILED-MODULES is
399 true, it is linked as 'lib/guile/X.Y/site-ccache'; otherwise, .go files are
400 assumed to be part of MODULES."
401 (computed-file name
402 (with-imported-modules '((guix build utils))
403 #~(begin
404 (use-modules (guix build utils))
405 (mkdir-p (string-append #$output "/bin"))
406 (symlink #$command
407 (string-append #$output "/bin/guix"))
408
409 (when #$daemon
410 (symlink (string-append #$daemon "/bin/guix-daemon")
411 (string-append #$output "/bin/guix-daemon")))
412
413 (let ((modules (string-append #$output
414 "/share/guile/site/"
415 (effective-version)))
416 (info #$info))
417 (mkdir-p (dirname modules))
418 (symlink #$modules modules)
419 (when info
420 (symlink #$info
421 (string-append #$output
422 "/share/info"))))
423
424 ;; Object files.
425 (when #$compiled-modules
426 (let ((modules (string-append #$output "/lib/guile/"
427 (effective-version)
428 "/site-ccache")))
429 (mkdir-p (dirname modules))
430 (symlink #$compiled-modules modules)))))))
431
432 (define* (compiled-guix source #:key (version %guix-version)
433 (pull-version 1)
434 (name (string-append "guix-" version))
435 (guile-version (effective-version))
436 (guile-for-build (guile-for-build guile-version))
437 (libgcrypt (specification->package "libgcrypt"))
438 (zlib (specification->package "zlib"))
439 (gzip (specification->package "gzip"))
440 (bzip2 (specification->package "bzip2"))
441 (xz (specification->package "xz"))
442 (guix (specification->package "guix")))
443 "Return a file-like object that contains a compiled Guix."
444 (define guile-json
445 (package-for-guile guile-version
446 "guile-json"
447 "guile2.0-json"))
448
449 (define guile-ssh
450 (package-for-guile guile-version
451 "guile-ssh"
452 "guile2.0-ssh"))
453
454 (define guile-git
455 (package-for-guile guile-version
456 "guile-git"
457 "guile2.0-git"))
458
459 (define guile-sqlite3
460 (package-for-guile guile-version
461 "guile-sqlite3"
462 "guile2.0-sqlite3"))
463
464 (define gnutls
465 (package-for-guile guile-version
466 "gnutls" "guile2.0-gnutls"))
467
468 (define dependencies
469 (match (append-map (lambda (package)
470 (cons (list "x" package)
471 (package-transitive-propagated-inputs package)))
472 (list gnutls guile-git guile-json
473 guile-ssh guile-sqlite3))
474 (((labels packages _ ...) ...)
475 packages)))
476
477 (define *core-modules*
478 (scheme-node "guix-core"
479 '((guix)
480 (guix monad-repl)
481 (guix packages)
482 (guix download)
483 (guix discovery)
484 (guix profiles)
485 (guix build-system gnu)
486 (guix build-system trivial)
487 (guix build profiles)
488 (guix build gnu-build-system))
489
490 ;; Provide a dummy (guix config) with the default version
491 ;; number, storedir, etc. This is so that "guix-core" is the
492 ;; same across all installations and doesn't need to be
493 ;; rebuilt when the version changes, which in turn means we
494 ;; can have substitutes for it.
495 #:extra-modules
496 `(((guix config)
497 => ,(make-config.scm #:libgcrypt
498 (specification->package
499 "libgcrypt"))))
500
501 ;; (guix man-db) is needed at build-time by (guix profiles)
502 ;; but we don't need to compile it; not compiling it allows
503 ;; us to avoid an extra dependency on guile-gdbm-ffi.
504 #:extra-files
505 `(("guix/man-db.scm" ,(local-file "../guix/man-db.scm"))
506 ("guix/store/schema.sql"
507 ,(local-file "../guix/store/schema.sql")))
508
509 #:guile-for-build guile-for-build))
510
511 (define *extra-modules*
512 (scheme-node "guix-extra"
513 (filter-map (match-lambda
514 (('guix 'scripts _ ..1) #f)
515 (('guix 'man-db) #f)
516 (name name))
517 (scheme-modules* source "guix"))
518 (list *core-modules*)
519 #:extensions dependencies
520 #:guile-for-build guile-for-build))
521
522 (define *core-package-modules*
523 (scheme-node "guix-packages-base"
524 `((gnu packages)
525 (gnu packages base))
526 (list *core-modules* *extra-modules*)
527 #:extensions dependencies
528
529 ;; Add all the non-Scheme files here. We must do it here so
530 ;; that 'search-patches' & co. can find them. Ideally we'd
531 ;; keep them next to the .scm files that use them but it's
532 ;; difficult to do (XXX).
533 #:extra-files
534 (file-imports source "gnu/packages"
535 (lambda (file stat)
536 (and (eq? 'regular (stat:type stat))
537 (not (string-suffix? ".scm" file))
538 (not (string-suffix? ".go" file))
539 (not (string-prefix? ".#" file))
540 (not (string-suffix? "~" file)))))
541 #:guile-for-build guile-for-build))
542
543 (define *package-modules*
544 (scheme-node "guix-packages"
545 (scheme-modules* source "gnu/packages")
546 (list *core-modules* *extra-modules* *core-package-modules*)
547 #:extensions dependencies
548 #:guile-for-build guile-for-build))
549
550 (define *system-modules*
551 (scheme-node "guix-system"
552 `((gnu system)
553 (gnu services)
554 ,@(scheme-modules* source "gnu/system")
555 ,@(scheme-modules* source "gnu/services"))
556 (list *core-package-modules* *package-modules*
557 *extra-modules* *core-modules*)
558 #:extensions dependencies
559 #:extra-files
560 (append (file-imports source "gnu/system/examples"
561 (const #t))
562
563 ;; Build-side code that we don't build. Some of
564 ;; these depend on guile-rsvg, the Shepherd, etc.
565 (file-imports source "gnu/build" (const #t)))
566 #:guile-for-build
567 guile-for-build))
568
569 (define *cli-modules*
570 (scheme-node "guix-cli"
571 (scheme-modules* source "/guix/scripts")
572 (list *core-modules* *extra-modules*
573 *core-package-modules* *package-modules*
574 *system-modules*)
575 #:extensions dependencies
576 #:guile-for-build guile-for-build))
577
578 (define *config*
579 (scheme-node "guix-config"
580 '()
581 #:extra-modules
582 `(((guix config)
583 => ,(make-config.scm #:libgcrypt libgcrypt
584 #:zlib zlib
585 #:gzip gzip
586 #:bzip2 bzip2
587 #:xz xz
588 #:package-name
589 %guix-package-name
590 #:package-version
591 version
592 #:bug-report-address
593 %guix-bug-report-address
594 #:home-page-url
595 %guix-home-page-url)))
596 #:guile-for-build guile-for-build))
597
598 (define (built-modules node-subset)
599 (directory-union (string-append name "-modules")
600 (append-map node-subset
601
602 ;; Note: *CONFIG* comes first so that it
603 ;; overrides the (guix config) module that
604 ;; comes with *CORE-MODULES*.
605 (list *config*
606 *cli-modules*
607 *system-modules*
608 *package-modules*
609 *core-package-modules*
610 *extra-modules*
611 *core-modules*))
612
613 ;; Silently choose the first entry upon collision so that
614 ;; we choose *CONFIG*.
615 #:resolve-collision 'first
616
617 ;; When we do (add-to-store "utils.scm"), "utils.scm" must
618 ;; be a regular file, not a symlink. Thus, arrange so that
619 ;; regular files appear as regular files in the final
620 ;; output.
621 #:copy? #t
622 #:quiet? #t))
623
624 ;; Version 0 of 'guix pull' meant we'd just return Scheme modules.
625 ;; Version 1 is when we return the full package.
626 (cond ((= 1 pull-version)
627 ;; The whole package, with a standard file hierarchy.
628 (let* ((modules (built-modules (compose list node-source)))
629 (compiled (built-modules (compose list node-compiled)))
630 (command (guix-command modules compiled
631 #:source source
632 #:dependencies dependencies
633 #:guile-version guile-version)))
634 (whole-package name modules dependencies
635 #:compiled-modules compiled
636 #:command command
637
638 ;; Include 'guix-daemon'. XXX: Here we inject an
639 ;; older snapshot of guix-daemon, but that's a good
640 ;; enough approximation for now.
641 #:daemon (module-ref (resolve-interface
642 '(gnu packages
643 package-management))
644 'guix-daemon)
645
646 #:info (info-manual source)
647 #:guile-version guile-version)))
648 ((= 0 pull-version)
649 ;; Legacy 'guix pull': return the .scm and .go files as one
650 ;; directory.
651 (built-modules (lambda (node)
652 (list (node-source node)
653 (node-compiled node)))))
654 (else
655 ;; Unsupported 'guix pull' version.
656 #f)))
657
658 \f
659 ;;;
660 ;;; Generating (guix config).
661 ;;;
662
663 (define %dependency-variables
664 ;; (guix config) variables corresponding to dependencies.
665 '(%libgcrypt %libz %xz %gzip %bzip2))
666
667 (define %persona-variables
668 ;; (guix config) variables that define Guix's persona.
669 '(%guix-package-name
670 %guix-version
671 %guix-bug-report-address
672 %guix-home-page-url))
673
674 (define %config-variables
675 ;; (guix config) variables corresponding to Guix configuration.
676 (letrec-syntax ((variables (syntax-rules ()
677 ((_)
678 '())
679 ((_ variable rest ...)
680 (cons `(variable . ,variable)
681 (variables rest ...))))))
682 (variables %localstatedir %storedir %sysconfdir %system)))
683
684 (define* (make-config.scm #:key libgcrypt zlib gzip xz bzip2
685 (package-name "GNU Guix")
686 (package-version "0")
687 (bug-report-address "bug-guix@gnu.org")
688 (home-page-url "https://gnu.org/s/guix"))
689
690 ;; Hack so that Geiser is not confused.
691 (define defmod 'define-module)
692
693 (scheme-file "config.scm"
694 #~(;; The following expressions get spliced.
695 (#$defmod (guix config)
696 #:export (%guix-package-name
697 %guix-version
698 %guix-bug-report-address
699 %guix-home-page-url
700 %store-directory
701 %state-directory
702 %store-database-directory
703 %config-directory
704 %libgcrypt
705 %libz
706 %gzip
707 %bzip2
708 %xz))
709
710 #$@(map (match-lambda
711 ((name . value)
712 #~(define-public #$name #$value)))
713 %config-variables)
714
715 (define %store-directory
716 (or (and=> (getenv "NIX_STORE_DIR") canonicalize-path)
717 %storedir))
718
719 (define %state-directory
720 ;; This must match `NIX_STATE_DIR' as defined in
721 ;; `nix/local.mk'.
722 (or (getenv "NIX_STATE_DIR")
723 (string-append %localstatedir "/guix")))
724
725 (define %store-database-directory
726 (or (getenv "NIX_DB_DIR")
727 (string-append %state-directory "/db")))
728
729 (define %config-directory
730 ;; This must match `GUIX_CONFIGURATION_DIRECTORY' as
731 ;; defined in `nix/local.mk'.
732 (or (getenv "GUIX_CONFIGURATION_DIRECTORY")
733 (string-append %sysconfdir "/guix")))
734
735 (define %guix-package-name #$package-name)
736 (define %guix-version #$package-version)
737 (define %guix-bug-report-address #$bug-report-address)
738 (define %guix-home-page-url #$home-page-url)
739
740 (define %gzip
741 #+(and gzip (file-append gzip "/bin/gzip")))
742 (define %bzip2
743 #+(and bzip2 (file-append bzip2 "/bin/bzip2")))
744 (define %xz
745 #+(and xz (file-append xz "/bin/xz")))
746
747 (define %libgcrypt
748 #+(and libgcrypt
749 (file-append libgcrypt "/lib/libgcrypt")))
750 (define %libz
751 #+(and zlib
752 (file-append zlib "/lib/libz"))))
753
754 ;; Guile 2.0 *requires* the 'define-module' to be at the
755 ;; top-level or the 'toplevel-ref' in the resulting .go file are
756 ;; made relative to a nonexistent anonymous module.
757 #:splice? #t))
758
759
760 \f
761 ;;;
762 ;;; Building.
763 ;;;
764
765 (define (imported-files name files)
766 ;; This is a non-monadic, simplified version of 'imported-files' from (guix
767 ;; gexp).
768 (define same-target?
769 (match-lambda*
770 (((file1 . _) (file2 . _))
771 (string=? file1 file2))))
772
773 (define build
774 (with-imported-modules (source-module-closure
775 '((guix build utils)))
776 #~(begin
777 (use-modules (ice-9 match)
778 (guix build utils))
779
780 (mkdir (ungexp output)) (chdir (ungexp output))
781 (for-each (match-lambda
782 ((final-path store-path)
783 (mkdir-p (dirname final-path))
784
785 ;; Note: We need regular files to be regular files, not
786 ;; symlinks, as this makes a difference for
787 ;; 'add-to-store'.
788 (copy-file store-path final-path)))
789 '#$(delete-duplicates files same-target?)))))
790
791 ;; We're just copying files around, no need to substitute or offload it.
792 (computed-file name build
793 #:options '(#:local-build? #t
794 #:substitutable? #f
795 #:env-vars (("COLUMNS" . "200")))))
796
797 (define* (compiled-modules name module-tree module-files
798 #:optional
799 (dependencies '())
800 (dependencies-compiled '())
801 #:key
802 (extensions '()) ;full-blown Guile packages
803 parallel?
804 guile-for-build)
805 "Build all the MODULE-FILES from MODULE-TREE. MODULE-FILES must be a list
806 like '(\"guix/foo.scm\" \"gnu/bar.scm\") and MODULE-TREE is the directory
807 containing MODULE-FILES and possibly other files as well."
808 ;; This is a non-monadic, enhanced version of 'compiled-file' from (guix
809 ;; gexp).
810 (define build
811 (with-imported-modules (source-module-closure
812 '((guix build compile)
813 (guix build utils)))
814 #~(begin
815 (use-modules (srfi srfi-26)
816 (ice-9 match)
817 (ice-9 format)
818 (ice-9 threads)
819 (guix build compile)
820 (guix build utils))
821
822 (define (regular? file)
823 (not (member file '("." ".."))))
824
825 (define (report-load file total completed)
826 (display #\cr)
827 (format #t
828 "loading...\t~5,1f% of ~d files" ;FIXME: i18n
829 (* 100. (/ completed total)) total)
830 (force-output))
831
832 (define (report-compilation file total completed)
833 (display #\cr)
834 (format #t "compiling...\t~5,1f% of ~d files" ;FIXME: i18n
835 (* 100. (/ completed total)) total)
836 (force-output))
837
838 (define (process-directory directory files output)
839 ;; Hide compilation warnings.
840 (parameterize ((current-warning-port (%make-void-port "w")))
841 (compile-files directory #$output files
842 #:workers (parallel-job-count)
843 #:report-load report-load
844 #:report-compilation report-compilation)))
845
846 (setvbuf (current-output-port) _IONBF)
847 (setvbuf (current-error-port) _IONBF)
848
849 (set! %load-path (cons #+module-tree %load-path))
850 (set! %load-path
851 (append '#+dependencies
852 (map (lambda (extension)
853 (string-append extension "/share/guile/site/"
854 (effective-version)))
855 '#+extensions)
856 %load-path))
857
858 (set! %load-compiled-path
859 (append '#+dependencies-compiled
860 (map (lambda (extension)
861 (string-append extension "/lib/guile/"
862 (effective-version)
863 "/site-ccache"))
864 '#+extensions)
865 %load-compiled-path))
866
867 ;; Load the compiler modules upfront.
868 (compile #f)
869
870 (mkdir #$output)
871 (chdir #+module-tree)
872 (process-directory "." '#+module-files #$output)
873 (newline))))
874
875 (computed-file name build
876 #:guile guile-for-build
877 #:options
878 `(#:local-build? #f ;allow substitutes
879
880 ;; Don't annoy people about _IONBF deprecation.
881 ;; Initialize 'terminal-width' in (system repl debug)
882 ;; to a large-enough value to make backtrace more
883 ;; verbose.
884 #:env-vars (("GUILE_WARN_DEPRECATED" . "no")
885 ("COLUMNS" . "200")))))
886
887 \f
888 ;;;
889 ;;; Building.
890 ;;;
891
892 (define (guile-for-build version)
893 "Return a derivation for Guile 2.0 or 2.2, whichever matches the currently
894 running Guile."
895 (define canonical-package ;soft reference
896 (module-ref (resolve-interface '(gnu packages base))
897 'canonical-package))
898
899 (match version
900 ("2.2.2"
901 ;; Gross hack to avoid ABI incompatibilities (see
902 ;; <https://bugs.gnu.org/29570>.)
903 (module-ref (resolve-interface '(gnu packages guile))
904 'guile-2.2.2))
905 ("2.2"
906 (canonical-package (module-ref (resolve-interface '(gnu packages guile))
907 'guile-2.2/fixed)))
908 ("2.0"
909 (module-ref (resolve-interface '(gnu packages guile))
910 'guile-2.0))))
911
912 (define* (guix-derivation source version
913 #:optional (guile-version (effective-version))
914 #:key (pull-version 0))
915 "Return, as a monadic value, the derivation to build the Guix from SOURCE
916 for GUILE-VERSION. Use VERSION as the version string. PULL-VERSION specifies
917 the version of the 'guix pull' protocol. Return #f if this PULL-VERSION value
918 is not supported."
919 (define (shorten version)
920 (if (and (string-every char-set:hex-digit version)
921 (> (string-length version) 9))
922 (string-take version 9) ;Git commit
923 version))
924
925 (define guile
926 (guile-for-build guile-version))
927
928 (mbegin %store-monad
929 (set-guile-for-build guile)
930 (let ((guix (compiled-guix source
931 #:version version
932 #:name (string-append "guix-"
933 (shorten version))
934 #:pull-version pull-version
935 #:guile-version (match guile-version
936 ("2.2.2" "2.2")
937 (version version))
938 #:guile-for-build guile)))
939 (if guix
940 (lower-object guix)
941 (return #f)))))