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