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