hydra: Add support for manifests.
[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
f0527ce3 266 (with-imported-modules `(((guix config)
ca719424
LC
267 => ,(make-config.scm))
268
269 ;; To avoid relying on 'with-extensions', which was
270 ;; introduced in 0.15.0, provide a fake (gcrypt
271 ;; hash) just so that we can build modules, and
272 ;; adjust %LOAD-PATH later on.
273 ((gcrypt hash) => ,fake-gcrypt-hash)
274
f0527ce3
LC
275 ,@(source-module-closure `((guix store)
276 (guix self)
277 (guix derivations)
278 (gnu packages bootstrap))
279 (list source)
280 #:select? select?))
281 (gexp->script "compute-guix-derivation"
282 #~(begin
283 (use-modules (ice-9 match))
284
285 (eval-when (expand load eval)
286 ;; Don't augment '%load-path'.
287 (unsetenv "GUIX_PACKAGE_PATH")
288
289 ;; (gnu packages …) modules are going to be looked up
290 ;; under SOURCE. (guix config) is looked up in FRONT.
1f1d76a1
LC
291 (match (command-line)
292 ((_ source _ ...)
293 (match %load-path
294 ((front _ ...)
295 (unless (string=? front source) ;already done?
ca719424
LC
296 (set! %load-path
297 (list source
298 (string-append #$guile-gcrypt
299 "/share/guile/site/"
300 (effective-version))
301 front)))))))
f0527ce3 302
ca719424
LC
303 ;; Only load Guile-Gcrypt, our own modules, or those
304 ;; of Guile.
f0527ce3
LC
305 (match %load-compiled-path
306 ((front _ ... sys1 sys2)
ca719424
LC
307 (unless (string-prefix? #$guile-gcrypt front)
308 (set! %load-compiled-path
309 (list (string-append #$guile-gcrypt
310 "/lib/guile/"
311 (effective-version)
312 "/site-ccache")
313 front sys1 sys2))))))
f0527ce3
LC
314
315 (use-modules (guix store)
316 (guix self)
317 (guix derivations)
318 (srfi srfi-1))
319
320 (define (spin system)
321 (define spin
322 (circular-list "-" "\\" "|" "/" "-" "\\" "|" "/"))
323
324 (format (current-error-port)
325 "Computing Guix derivation for '~a'... "
326 system)
327 (let loop ((spin spin))
328 (display (string-append "\b" (car spin))
329 (current-error-port))
330 (force-output (current-error-port))
331 (sleep 1)
332 (loop (cdr spin))))
333
334 (match (command-line)
790c3e01
LC
335 ((_ source system version protocol-version)
336 ;; The current input port normally wraps a file
337 ;; descriptor connected to the daemon, or it is
338 ;; connected to /dev/null. In the former case, reuse
339 ;; the connection such that we inherit build options
340 ;; such as substitute URLs and so on; in the latter
341 ;; case, attempt to open a new connection.
342 (let* ((proto (string->number protocol-version))
343 (store (if (integer? proto)
344 (port->connection (duplicate-port
345 (current-input-port)
346 "w+0")
347 #:version proto)
348 (open-connection))))
f0527ce3
LC
349 (call-with-new-thread
350 (lambda ()
351 (spin system)))
352
353 (display
8a0d9bc8 354 (and=>
f0527ce3 355 (run-with-store store
1f1d76a1 356 (guix-derivation source version
8a0d9bc8
LC
357 #$guile-version
358 #:pull-version
359 #$pull-version)
360 #:system system)
361 derivation-file-name))))))
f0527ce3 362 #:module-path (list source))))
838ba73d 363
f81ac34d 364;; The procedure below is our return value.
b006ba50 365(define* (build source
f0527ce3
LC
366 #:key verbose? (version (date-version-string)) system
367 (guile-version (match ((@ (guile) version))
368 ("2.2.2" "2.2.2")
369 (_ (effective-version))))
8a0d9bc8 370 (pull-version 0)
f81ac34d
LC
371 #:allow-other-keys
372 #:rest rest)
373 "Return a derivation that unpacks SOURCE into STORE and compiles Scheme
374files."
f0527ce3
LC
375 ;; Build the build program and then use it as a trampoline to build from
376 ;; SOURCE.
8a0d9bc8
LC
377 (mlet %store-monad ((build (build-program source version guile-version
378 #:pull-version pull-version))
790c3e01
LC
379 (system (if system (return system) (current-system)))
380 (port ((store-lift nix-server-socket)))
381 (major ((store-lift nix-server-major-version)))
382 (minor ((store-lift nix-server-minor-version))))
f0527ce3
LC
383 (mbegin %store-monad
384 (show-what-to-build* (list build))
385 (built-derivations (list build))
790c3e01
LC
386
387 ;; Use the port beneath the current store as the stdin of BUILD. This
388 ;; way, we know 'open-pipe*' will not close it on 'exec'. If PORT is
389 ;; not a file port (e.g., it's an SSH channel), then the subprocess's
390 ;; stdin will actually be /dev/null.
391 (let* ((pipe (with-input-from-port port
392 (lambda ()
393 (setenv "GUILE_WARN_DEPRECATED" "no") ;be quiet and drive
394 (open-pipe* OPEN_READ
395 (derivation->output-path build)
396 source system version
397 (if (file-port? port)
398 (number->string
399 (logior major minor))
400 "none")))))
f27a7128
LC
401 (str (get-string-all pipe))
402 (status (close-pipe pipe)))
403 (match str
f0527ce3 404 ((? eof-object?)
f27a7128 405 (error "build program failed" (list build status)))
f0527ce3
LC
406 ((? derivation-path? drv)
407 (mbegin %store-monad
408 (return (newline (current-output-port)))
409 ((store-lift add-temp-root) drv)
410 (return (read-derivation-from-file drv))))
8a0d9bc8
LC
411 ("#f"
412 ;; Unsupported PULL-VERSION.
413 (return #f))
f0527ce3
LC
414 ((? string? str)
415 (error "invalid build result" (list build str))))))))
f81ac34d
LC
416
417;; This file is loaded by 'guix pull'; return it the build procedure.
418build
419
cd295fbe
LC
420;; Local Variables:
421;; eval: (put 'with-load-path 'scheme-indent-function 1)
422;; End:
423
f81ac34d 424;;; build-self.scm ends here