build-self: Add a dummy (git) module to 'compute-guix-derivation'.
[jackhill/guix/guix.git] / build-aux / build-self.scm
CommitLineData
f81ac34d 1;;; GNU Guix --- Functional package management for GNU
f0527ce3 2;;; Copyright © 2014, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
f81ac34d
LC
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (build-self)
cd295fbe
LC
20 #:use-module (gnu)
21 #:use-module (guix)
f0527ce3 22 #:use-module (guix ui)
cd295fbe 23 #:use-module (guix config)
f0527ce3 24 #:use-module (guix modules)
ca719424 25 #:use-module (guix build-system gnu)
f81ac34d 26 #:use-module (srfi srfi-1)
b006ba50 27 #:use-module (srfi srfi-19)
f0527ce3 28 #:use-module (rnrs io ports)
838ba73d 29 #:use-module (ice-9 match)
f0527ce3 30 #:use-module (ice-9 popen)
f81ac34d
LC
31 #:export (build))
32
33;;; Commentary:
34;;;
35;;; When loaded, this module returns a monadic procedure of at least one
36;;; argument: the source tree to build. It returns a derivation that
37;;; builds it.
38;;;
cd295fbe
LC
39;;; This file uses modules provided by the already-installed Guix. Those
40;;; modules may be arbitrarily old compared to the version we want to
41;;; build. Because of that, it must rely on the smallest set of features
42;;; that are likely to be provided by the (guix) and (gnu) modules, and by
43;;; Guile itself, forever and ever.
44;;;
f81ac34d
LC
45;;; Code:
46
cd295fbe 47\f
f0527ce3
LC
48;;;
49;;; Generating (guix config).
50;;;
51;;; This is copied from (guix self) because we cannot assume (guix self) is
52;;; available at this point.
53;;;
54
55(define %dependency-variables
56 ;; (guix config) variables corresponding to dependencies.
e8cb9c01 57 '(%libgcrypt %libz %xz %gzip %bzip2))
f0527ce3
LC
58
59(define %persona-variables
60 ;; (guix config) variables that define Guix's persona.
61 '(%guix-package-name
62 %guix-version
63 %guix-bug-report-address
64 %guix-home-page-url))
65
66(define %config-variables
45779fa6
LC
67 ;; (guix config) variables corresponding to Guix configuration.
68 (letrec-syntax ((variables (syntax-rules ()
69 ((_)
70 '())
71 ((_ variable rest ...)
72 (cons `(variable . ,variable)
73 (variables rest ...))))))
7af5c2a2 74 (variables %localstatedir %storedir %sysconfdir %system)))
f0527ce3 75
ca719424 76(define* (make-config.scm #:key zlib gzip xz bzip2
f0527ce3
LC
77 (package-name "GNU Guix")
78 (package-version "0")
79 (bug-report-address "bug-guix@gnu.org")
80 (home-page-url "https://gnu.org/s/guix"))
81
82 ;; Hack so that Geiser is not confused.
83 (define defmod 'define-module)
84
85 (scheme-file "config.scm"
86 #~(begin
87 (#$defmod (guix config)
88 #:export (%guix-package-name
89 %guix-version
90 %guix-bug-report-address
91 %guix-home-page-url
7af5c2a2
LC
92 %store-directory
93 %state-directory
94 %store-database-directory
95 %config-directory
f0527ce3
LC
96 %libz
97 %gzip
98 %bzip2
e8cb9c01 99 %xz))
f0527ce3
LC
100
101 ;; XXX: Work around <http://bugs.gnu.org/15602>.
102 (eval-when (expand load eval)
103 #$@(map (match-lambda
104 ((name . value)
105 #~(define-public #$name #$value)))
106 %config-variables)
107
7af5c2a2
LC
108 (define %store-directory
109 (or (and=> (getenv "NIX_STORE_DIR") canonicalize-path)
110 %storedir))
111
112 (define %state-directory
113 ;; This must match `NIX_STATE_DIR' as defined in
114 ;; `nix/local.mk'.
115 (or (getenv "NIX_STATE_DIR")
116 (string-append %localstatedir "/guix")))
117
118 (define %store-database-directory
119 (or (getenv "NIX_DB_DIR")
120 (string-append %state-directory "/db")))
121
122 (define %config-directory
123 ;; This must match `GUIX_CONFIGURATION_DIRECTORY' as
124 ;; defined in `nix/local.mk'.
125 (or (getenv "GUIX_CONFIGURATION_DIRECTORY")
126 (string-append %sysconfdir "/guix")))
127
f0527ce3
LC
128 (define %guix-package-name #$package-name)
129 (define %guix-version #$package-version)
130 (define %guix-bug-report-address #$bug-report-address)
131 (define %guix-home-page-url #$home-page-url)
132
133 (define %gzip
134 #+(and gzip (file-append gzip "/bin/gzip")))
135 (define %bzip2
136 #+(and bzip2 (file-append bzip2 "/bin/bzip2")))
137 (define %xz
138 #+(and xz (file-append xz "/bin/xz")))
139
f0527ce3
LC
140 (define %libz
141 #+(and zlib
e8cb9c01 142 (file-append zlib "/lib/libz")))))))
cd295fbe 143
f0527ce3
LC
144\f
145;;;
146;;; 'gexp->script'.
147;;;
148;;; This is our own variant of 'gexp->script' with an extra #:module-path
149;;; parameter, which was unavailable in (guix gexp) until commit
150;;; 1ae16033f34cebe802023922436883867010850f (March 2018.)
151;;;
cd295fbe 152
f0527ce3
LC
153(define (load-path-expression modules path)
154 "Return as a monadic value a gexp that sets '%load-path' and
155'%load-compiled-path' to point to MODULES, a list of module names. MODULES
156are searched for in PATH."
157 (mlet %store-monad ((modules (imported-modules modules
158 #:module-path path))
159 (compiled (compiled-modules modules
160 #:module-path path)))
161 (return (gexp (eval-when (expand load eval)
162 (set! %load-path
163 (cons (ungexp modules) %load-path))
164 (set! %load-compiled-path
165 (cons (ungexp compiled)
166 %load-compiled-path)))))))
167
168(define* (gexp->script name exp
169 #:key (guile (default-guile))
170 (module-path %load-path))
171 "Return an executable script NAME that runs EXP using GUILE, with EXP's
172imported modules in its search path."
173 (mlet %store-monad ((set-load-path
174 (load-path-expression (gexp-modules exp)
175 module-path)))
176 (gexp->derivation name
177 (gexp
178 (call-with-output-file (ungexp output)
179 (lambda (port)
180 ;; Note: that makes a long shebang. When the store
181 ;; is /gnu/store, that fits within the 128-byte
182 ;; limit imposed by Linux, but that may go beyond
183 ;; when running tests.
184 (format port
185 "#!~a/bin/guile --no-auto-compile~%!#~%"
186 (ungexp guile))
187
188 (write '(ungexp set-load-path) port)
189 (write '(ungexp exp) port)
190 (chmod port #o555))))
191 #:module-path module-path)))
b006ba50 192
f0527ce3 193\f
b006ba50
LC
194(define (date-version-string)
195 "Return the current date and hour in UTC timezone, for use as a poor
196person's version identifier."
cd295fbe 197 ;; XXX: Replace with a Git commit id.
b006ba50
LC
198 (date->string (current-date 0) "~Y~m~d.~H"))
199
ca719424
LC
200(define guile-gcrypt
201 ;; The host Guix may or may not have 'guile-gcrypt', which was introduced in
202 ;; August 2018. If it has it, it's at least version 0.1.0, which is good
203 ;; enough. If it doesn't, specify our own package because the target Guix
204 ;; requires it.
205 (match (find-best-packages-by-name "guile-gcrypt" #f)
206 (()
207 (package
208 (name "guile-gcrypt")
209 (version "0.1.0")
210 (home-page "https://notabug.org/cwebber/guile-gcrypt")
211 (source (origin
212 (method url-fetch)
213 (uri (string-append home-page "/archive/v" version ".tar.gz"))
214 (sha256
215 (base32
216 "1gir7ifknbmbvjlql5j6wzk7bkb5lnmq80q59ngz43hhpclrk5k3"))
217 (file-name (string-append name "-" version ".tar.gz"))))
218 (build-system gnu-build-system)
3ffcad7d
LC
219 (arguments
220 ;; The 'bootstrap' phase appeared in 'core-updates', which was merged
221 ;; into 'master' ca. June 2018.
222 '(#:phases (modify-phases %standard-phases
223 (delete 'bootstrap)
224 (add-before 'configure 'bootstrap
225 (lambda _
226 (unless (zero? (system* "autoreconf" "-vfi"))
227 (error "autoreconf failed"))
228 #t)))))
ca719424
LC
229 (native-inputs
230 `(("pkg-config" ,(specification->package "pkg-config"))
231 ("autoconf" ,(specification->package "autoconf"))
232 ("automake" ,(specification->package "automake"))
233 ("texinfo" ,(specification->package "texinfo"))))
234 (inputs
235 `(("guile" ,(specification->package "guile"))
236 ("libgcrypt" ,(specification->package "libgcrypt"))))
237 (synopsis "Cryptography library for Guile using Libgcrypt")
238 (description
239 "Guile-Gcrypt provides a Guile 2.x interface to a subset of the
240GNU Libgcrypt crytographic library. It provides modules for cryptographic
241hash functions, message authentication codes (MAC), public-key cryptography,
242strong randomness, and more. It is implemented using the foreign function
243interface (FFI) of Guile.")
244 (license #f))) ;license:gpl3+
245 ((package . _)
246 package)))
247
f0527ce3 248(define* (build-program source version
8a0d9bc8
LC
249 #:optional (guile-version (effective-version))
250 #:key (pull-version 0))
f0527ce3
LC
251 "Return a program that computes the derivation to build Guix from SOURCE."
252 (define select?
253 ;; Select every module but (guix config) and non-Guix modules.
254 (match-lambda
255 (('guix 'config) #f)
256 (('guix _ ...) #t)
257 (('gnu _ ...) #t)
258 (_ #f)))
259
ca719424
LC
260 (define fake-gcrypt-hash
261 ;; Fake (gcrypt hash) module; see below.
262 (scheme-file "hash.scm"
263 #~(define-module (gcrypt hash)
264 #:export (sha1 sha256))))
265
78c9058d
LC
266 (define fake-git
267 (scheme-file "git.scm" #~(define-module (git))))
268
f0527ce3 269 (with-imported-modules `(((guix config)
ca719424
LC
270 => ,(make-config.scm))
271
272 ;; To avoid relying on 'with-extensions', which was
273 ;; introduced in 0.15.0, provide a fake (gcrypt
274 ;; hash) just so that we can build modules, and
275 ;; adjust %LOAD-PATH later on.
276 ((gcrypt hash) => ,fake-gcrypt-hash)
277
78c9058d
LC
278 ;; (guix git-download) depends on (git) but only
279 ;; for peripheral functionality. Provide a dummy
280 ;; (git) to placate it.
281 ((git) => ,fake-git)
282
f0527ce3
LC
283 ,@(source-module-closure `((guix store)
284 (guix self)
285 (guix derivations)
286 (gnu packages bootstrap))
287 (list source)
288 #:select? select?))
289 (gexp->script "compute-guix-derivation"
290 #~(begin
291 (use-modules (ice-9 match))
292
293 (eval-when (expand load eval)
294 ;; Don't augment '%load-path'.
295 (unsetenv "GUIX_PACKAGE_PATH")
296
297 ;; (gnu packages …) modules are going to be looked up
298 ;; under SOURCE. (guix config) is looked up in FRONT.
1f1d76a1
LC
299 (match (command-line)
300 ((_ source _ ...)
301 (match %load-path
302 ((front _ ...)
303 (unless (string=? front source) ;already done?
ca719424
LC
304 (set! %load-path
305 (list source
306 (string-append #$guile-gcrypt
307 "/share/guile/site/"
308 (effective-version))
309 front)))))))
f0527ce3 310
ca719424
LC
311 ;; Only load Guile-Gcrypt, our own modules, or those
312 ;; of Guile.
f0527ce3
LC
313 (match %load-compiled-path
314 ((front _ ... sys1 sys2)
ca719424
LC
315 (unless (string-prefix? #$guile-gcrypt front)
316 (set! %load-compiled-path
317 (list (string-append #$guile-gcrypt
318 "/lib/guile/"
319 (effective-version)
320 "/site-ccache")
321 front sys1 sys2))))))
f0527ce3
LC
322
323 (use-modules (guix store)
324 (guix self)
325 (guix derivations)
326 (srfi srfi-1))
327
328 (define (spin system)
329 (define spin
330 (circular-list "-" "\\" "|" "/" "-" "\\" "|" "/"))
331
332 (format (current-error-port)
333 "Computing Guix derivation for '~a'... "
334 system)
335 (let loop ((spin spin))
336 (display (string-append "\b" (car spin))
337 (current-error-port))
338 (force-output (current-error-port))
339 (sleep 1)
340 (loop (cdr spin))))
341
342 (match (command-line)
790c3e01
LC
343 ((_ source system version protocol-version)
344 ;; The current input port normally wraps a file
345 ;; descriptor connected to the daemon, or it is
346 ;; connected to /dev/null. In the former case, reuse
347 ;; the connection such that we inherit build options
348 ;; such as substitute URLs and so on; in the latter
349 ;; case, attempt to open a new connection.
350 (let* ((proto (string->number protocol-version))
351 (store (if (integer? proto)
352 (port->connection (duplicate-port
353 (current-input-port)
354 "w+0")
355 #:version proto)
356 (open-connection))))
f0527ce3
LC
357 (call-with-new-thread
358 (lambda ()
359 (spin system)))
360
361 (display
8a0d9bc8 362 (and=>
f0527ce3 363 (run-with-store store
1f1d76a1 364 (guix-derivation source version
8a0d9bc8
LC
365 #$guile-version
366 #:pull-version
367 #$pull-version)
368 #:system system)
369 derivation-file-name))))))
f0527ce3 370 #:module-path (list source))))
838ba73d 371
f81ac34d 372;; The procedure below is our return value.
b006ba50 373(define* (build source
f0527ce3
LC
374 #:key verbose? (version (date-version-string)) system
375 (guile-version (match ((@ (guile) version))
376 ("2.2.2" "2.2.2")
377 (_ (effective-version))))
8a0d9bc8 378 (pull-version 0)
f81ac34d
LC
379 #:allow-other-keys
380 #:rest rest)
381 "Return a derivation that unpacks SOURCE into STORE and compiles Scheme
382files."
f0527ce3
LC
383 ;; Build the build program and then use it as a trampoline to build from
384 ;; SOURCE.
8a0d9bc8
LC
385 (mlet %store-monad ((build (build-program source version guile-version
386 #:pull-version pull-version))
790c3e01
LC
387 (system (if system (return system) (current-system)))
388 (port ((store-lift nix-server-socket)))
389 (major ((store-lift nix-server-major-version)))
390 (minor ((store-lift nix-server-minor-version))))
f0527ce3
LC
391 (mbegin %store-monad
392 (show-what-to-build* (list build))
393 (built-derivations (list build))
790c3e01
LC
394
395 ;; Use the port beneath the current store as the stdin of BUILD. This
396 ;; way, we know 'open-pipe*' will not close it on 'exec'. If PORT is
397 ;; not a file port (e.g., it's an SSH channel), then the subprocess's
398 ;; stdin will actually be /dev/null.
399 (let* ((pipe (with-input-from-port port
400 (lambda ()
401 (setenv "GUILE_WARN_DEPRECATED" "no") ;be quiet and drive
402 (open-pipe* OPEN_READ
403 (derivation->output-path build)
404 source system version
405 (if (file-port? port)
406 (number->string
407 (logior major minor))
408 "none")))))
f27a7128
LC
409 (str (get-string-all pipe))
410 (status (close-pipe pipe)))
411 (match str
f0527ce3 412 ((? eof-object?)
f27a7128 413 (error "build program failed" (list build status)))
f0527ce3
LC
414 ((? derivation-path? drv)
415 (mbegin %store-monad
416 (return (newline (current-output-port)))
417 ((store-lift add-temp-root) drv)
418 (return (read-derivation-from-file drv))))
8a0d9bc8
LC
419 ("#f"
420 ;; Unsupported PULL-VERSION.
421 (return #f))
f0527ce3
LC
422 ((? string? str)
423 (error "invalid build result" (list build str))))))))
f81ac34d
LC
424
425;; This file is loaded by 'guix pull'; return it the build procedure.
426build
427
cd295fbe
LC
428;; Local Variables:
429;; eval: (put 'with-load-path 'scheme-indent-function 1)
430;; End:
431
f81ac34d 432;;; build-self.scm ends here