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