Commit | Line | Data |
---|---|---|
233e7676 | 1 | ;;; GNU Guix --- Functional package management for GNU |
98a7b528 | 2 | ;;; Copyright © 2012, 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org> |
77d3cf08 | 3 | ;;; |
233e7676 | 4 | ;;; This file is part of GNU Guix. |
77d3cf08 | 5 | ;;; |
233e7676 | 6 | ;;; GNU Guix is free software; you can redistribute it and/or modify it |
77d3cf08 LC |
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 | ;;; | |
233e7676 | 11 | ;;; GNU Guix is distributed in the hope that it will be useful, but |
77d3cf08 LC |
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 | |
233e7676 | 17 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. |
77d3cf08 LC |
18 | |
19 | (define-module (guix store) | |
82058eff | 20 | #:use-module (guix utils) |
d8eea3d2 | 21 | #:use-module (guix config) |
0f41c26f | 22 | #:use-module (guix serialization) |
e87f0591 | 23 | #:use-module (guix monads) |
a9d2a105 | 24 | #:autoload (guix base32) (bytevector->base32-string) |
77d3cf08 LC |
25 | #:use-module (rnrs bytevectors) |
26 | #:use-module (rnrs io ports) | |
27 | #:use-module (srfi srfi-1) | |
28 | #:use-module (srfi srfi-9) | |
bf8e7fc5 | 29 | #:use-module (srfi srfi-9 gnu) |
6581ec9a | 30 | #:use-module (srfi srfi-11) |
77d3cf08 | 31 | #:use-module (srfi srfi-26) |
e87088c9 LC |
32 | #:use-module (srfi srfi-34) |
33 | #:use-module (srfi srfi-35) | |
26bbbb95 | 34 | #:use-module (srfi srfi-39) |
77d3cf08 | 35 | #:use-module (ice-9 match) |
e3d74106 | 36 | #:use-module (ice-9 regex) |
3f1e6939 | 37 | #:use-module (ice-9 vlist) |
6bfec3ed | 38 | #:use-module (ice-9 popen) |
9fd72fb1 | 39 | #:export (%daemon-socket-file |
a9d2a105 | 40 | %gc-roots-directory |
f8a8e0fe | 41 | %default-substitute-urls |
9fd72fb1 LC |
42 | |
43 | nix-server? | |
77d3cf08 LC |
44 | nix-server-major-version |
45 | nix-server-minor-version | |
46 | nix-server-socket | |
47 | ||
e87088c9 | 48 | &nix-error nix-error? |
ef86c39f LC |
49 | &nix-connection-error nix-connection-error? |
50 | nix-connection-error-file | |
51 | nix-connection-error-code | |
e87088c9 LC |
52 | &nix-protocol-error nix-protocol-error? |
53 | nix-protocol-error-message | |
54 | nix-protocol-error-status | |
55 | ||
26bbbb95 | 56 | hash-algo |
07e70f48 | 57 | build-mode |
26bbbb95 | 58 | |
77d3cf08 | 59 | open-connection |
3abaf0c4 | 60 | close-connection |
ce4a4829 | 61 | with-store |
77d3cf08 | 62 | set-build-options |
4f740b67 | 63 | set-build-options* |
31ef99a8 | 64 | valid-path? |
82058eff | 65 | query-path-hash |
11e7a6cf | 66 | hash-part->path |
533d1768 | 67 | query-path-info |
77d3cf08 LC |
68 | add-text-to-store |
69 | add-to-store | |
abac874b LC |
70 | build-things |
71 | build | |
16748d80 LC |
72 | query-failed-paths |
73 | clear-failed-paths | |
3259877d | 74 | add-temp-root |
34811f02 | 75 | add-indirect-root |
a9d2a105 LC |
76 | add-permanent-root |
77 | remove-permanent-root | |
26bbbb95 | 78 | |
0f3d2504 LC |
79 | substitutable? |
80 | substitutable-path | |
81 | substitutable-deriver | |
82 | substitutable-references | |
83 | substitutable-download-size | |
84 | substitutable-nar-size | |
85 | has-substitutes? | |
86 | substitutable-paths | |
87 | substitutable-path-info | |
88 | ||
533d1768 DT |
89 | path-info? |
90 | path-info-deriver | |
91 | path-info-hash | |
92 | path-info-references | |
93 | path-info-registration-time | |
94 | path-info-nar-size | |
95 | ||
fae31edc | 96 | references |
6581ec9a | 97 | references/substitutes |
3f1e6939 | 98 | requisites |
fae31edc | 99 | referrers |
e3fd0ce6 | 100 | optimize-store |
c63d9403 | 101 | verify-store |
50add477 | 102 | topologically-sorted |
fae31edc LC |
103 | valid-derivers |
104 | query-derivation-outputs | |
3259877d LC |
105 | live-paths |
106 | dead-paths | |
107 | collect-garbage | |
108 | delete-paths | |
526382ff LC |
109 | import-paths |
110 | export-paths | |
3259877d | 111 | |
dcee50c1 LC |
112 | current-build-output-port |
113 | ||
6bfec3ed LC |
114 | register-path |
115 | ||
e87f0591 LC |
116 | %store-monad |
117 | store-bind | |
118 | store-return | |
119 | store-lift | |
023d9892 | 120 | store-lower |
e87f0591 LC |
121 | run-with-store |
122 | %guile-for-build | |
98a7b528 LC |
123 | current-system |
124 | set-current-system | |
e87f0591 LC |
125 | text-file |
126 | interned-file | |
127 | ||
26bbbb95 LC |
128 | %store-prefix |
129 | store-path? | |
9336e5b5 | 130 | direct-store-path? |
e3d74106 | 131 | derivation-path? |
2c6ab6cc | 132 | store-path-package-name |
eddd4077 | 133 | store-path-hash-part |
cdb5b075 | 134 | direct-store-path |
eddd4077 | 135 | log-file)) |
77d3cf08 | 136 | |
07e70f48 | 137 | (define %protocol-version #x10f) |
77d3cf08 | 138 | |
d66b704b LC |
139 | (define %worker-magic-1 #x6e697863) ; "nixc" |
140 | (define %worker-magic-2 #x6478696f) ; "dxio" | |
77d3cf08 LC |
141 | |
142 | (define (protocol-major magic) | |
143 | (logand magic #xff00)) | |
144 | (define (protocol-minor magic) | |
145 | (logand magic #x00ff)) | |
146 | ||
147 | (define-syntax define-enumerate-type | |
148 | (syntax-rules () | |
149 | ((_ name->int (name id) ...) | |
150 | (define-syntax name->int | |
151 | (syntax-rules (name ...) | |
152 | ((_ name) id) ...))))) | |
153 | ||
154 | (define-enumerate-type operation-id | |
155 | ;; operation numbers from worker-protocol.hh | |
156 | (quit 0) | |
157 | (valid-path? 1) | |
158 | (has-substitutes? 3) | |
159 | (query-path-hash 4) | |
160 | (query-references 5) | |
161 | (query-referrers 6) | |
162 | (add-to-store 7) | |
163 | (add-text-to-store 8) | |
abac874b | 164 | (build-things 9) |
77d3cf08 LC |
165 | (ensure-path 10) |
166 | (add-temp-root 11) | |
167 | (add-indirect-root 12) | |
168 | (sync-with-gc 13) | |
169 | (find-roots 14) | |
170 | (export-path 16) | |
171 | (query-deriver 18) | |
172 | (set-options 19) | |
173 | (collect-garbage 20) | |
63193ebf | 174 | ;;(query-substitutable-path-info 21) ; obsolete as of #x10c |
77d3cf08 | 175 | (query-derivation-outputs 22) |
63193ebf | 176 | (query-all-valid-paths 23) |
77d3cf08 LC |
177 | (query-failed-paths 24) |
178 | (clear-failed-paths 25) | |
179 | (query-path-info 26) | |
180 | (import-paths 27) | |
63193ebf LC |
181 | (query-derivation-output-names 28) |
182 | (query-path-from-hash-part 29) | |
183 | (query-substitutable-path-infos 30) | |
184 | (query-valid-paths 31) | |
fae31edc | 185 | (query-substitutable-paths 32) |
e3fd0ce6 | 186 | (query-valid-derivers 33) |
c63d9403 LC |
187 | (optimize-store 34) |
188 | (verify-store 35)) | |
77d3cf08 LC |
189 | |
190 | (define-enumerate-type hash-algo | |
191 | ;; hash.hh | |
192 | (md5 1) | |
193 | (sha1 2) | |
194 | (sha256 3)) | |
195 | ||
07e70f48 LC |
196 | (define-enumerate-type build-mode |
197 | ;; store-api.hh | |
198 | (normal 0) | |
199 | (repair 1) | |
200 | (check 2)) | |
201 | ||
3259877d LC |
202 | (define-enumerate-type gc-action |
203 | ;; store-api.hh | |
204 | (return-live 0) | |
205 | (return-dead 1) | |
206 | (delete-dead 2) | |
207 | (delete-specific 3)) | |
208 | ||
77d3cf08 | 209 | (define %default-socket-path |
80d0447c | 210 | (string-append %state-directory "/daemon-socket/socket")) |
77d3cf08 | 211 | |
9fd72fb1 LC |
212 | (define %daemon-socket-file |
213 | ;; File name of the socket the daemon listens too. | |
214 | (make-parameter (or (getenv "GUIX_DAEMON_SOCKET") | |
215 | %default-socket-path))) | |
216 | ||
217 | ||
77d3cf08 | 218 | \f |
0f3d2504 LC |
219 | ;; Information about a substitutable store path. |
220 | (define-record-type <substitutable> | |
221 | (substitutable path deriver refs dl-size nar-size) | |
222 | substitutable? | |
223 | (path substitutable-path) | |
224 | (deriver substitutable-deriver) | |
225 | (refs substitutable-references) | |
226 | (dl-size substitutable-download-size) | |
227 | (nar-size substitutable-nar-size)) | |
228 | ||
229 | (define (read-substitutable-path-list p) | |
230 | (let loop ((len (read-int p)) | |
231 | (result '())) | |
232 | (if (zero? len) | |
233 | (reverse result) | |
234 | (let ((path (read-store-path p)) | |
235 | (deriver (read-store-path p)) | |
236 | (refs (read-store-path-list p)) | |
237 | (dl-size (read-long-long p)) | |
238 | (nar-size (read-long-long p))) | |
239 | (loop (- len 1) | |
240 | (cons (substitutable path deriver refs dl-size nar-size) | |
241 | result)))))) | |
242 | ||
533d1768 DT |
243 | ;; Information about a store path. |
244 | (define-record-type <path-info> | |
245 | (path-info deriver hash references registration-time nar-size) | |
246 | path-info? | |
22572d56 | 247 | (deriver path-info-deriver) ;string | #f |
533d1768 DT |
248 | (hash path-info-hash) |
249 | (references path-info-references) | |
250 | (registration-time path-info-registration-time) | |
251 | (nar-size path-info-nar-size)) | |
252 | ||
253 | (define (read-path-info p) | |
22572d56 LC |
254 | (let ((deriver (match (read-store-path p) |
255 | ("" #f) | |
256 | (x x))) | |
533d1768 DT |
257 | (hash (base16-string->bytevector (read-string p))) |
258 | (refs (read-store-path-list p)) | |
259 | (registration-time (read-int p)) | |
260 | (nar-size (read-long-long p))) | |
261 | (path-info deriver hash refs registration-time nar-size))) | |
262 | ||
77d3cf08 | 263 | (define-syntax write-arg |
6c20d1d0 | 264 | (syntax-rules (integer boolean file string string-list string-pairs |
3259877d | 265 | store-path store-path-list base16) |
77d3cf08 LC |
266 | ((_ integer arg p) |
267 | (write-int arg p)) | |
268 | ((_ boolean arg p) | |
269 | (write-int (if arg 1 0) p)) | |
270 | ((_ file arg p) | |
271 | (write-file arg p)) | |
272 | ((_ string arg p) | |
273 | (write-string arg p)) | |
274 | ((_ string-list arg p) | |
82058eff | 275 | (write-string-list arg p)) |
6c20d1d0 LC |
276 | ((_ string-pairs arg p) |
277 | (write-string-pairs arg p)) | |
3259877d LC |
278 | ((_ store-path arg p) |
279 | (write-store-path arg p)) | |
280 | ((_ store-path-list arg p) | |
281 | (write-store-path-list arg p)) | |
82058eff LC |
282 | ((_ base16 arg p) |
283 | (write-string (bytevector->base16-string arg) p)))) | |
77d3cf08 LC |
284 | |
285 | (define-syntax read-arg | |
0f3d2504 | 286 | (syntax-rules (integer boolean string store-path store-path-list |
533d1768 | 287 | substitutable-path-list path-info base16) |
77d3cf08 LC |
288 | ((_ integer p) |
289 | (read-int p)) | |
290 | ((_ boolean p) | |
291 | (not (zero? (read-int p)))) | |
292 | ((_ string p) | |
293 | (read-string p)) | |
294 | ((_ store-path p) | |
82058eff | 295 | (read-store-path p)) |
3259877d LC |
296 | ((_ store-path-list p) |
297 | (read-store-path-list p)) | |
0f3d2504 LC |
298 | ((_ substitutable-path-list p) |
299 | (read-substitutable-path-list p)) | |
533d1768 DT |
300 | ((_ path-info p) |
301 | (read-path-info p)) | |
0f3d2504 | 302 | ((_ base16 p) |
82058eff | 303 | (base16-string->bytevector (read-string p))))) |
77d3cf08 LC |
304 | |
305 | \f | |
306 | ;; remote-store.cc | |
307 | ||
308 | (define-record-type <nix-server> | |
2c3f47ee | 309 | (%make-nix-server socket major minor |
bdcf35a6 | 310 | ats-cache atts-cache) |
77d3cf08 LC |
311 | nix-server? |
312 | (socket nix-server-socket) | |
313 | (major nix-server-major-version) | |
2c3f47ee LC |
314 | (minor nix-server-minor-version) |
315 | ||
316 | ;; Caches. We keep them per-connection, because store paths build | |
317 | ;; during the session are temporary GC roots kept for the duration of | |
318 | ;; the session. | |
bdcf35a6 LC |
319 | (ats-cache nix-server-add-to-store-cache) |
320 | (atts-cache nix-server-add-text-to-store-cache)) | |
77d3cf08 | 321 | |
bf8e7fc5 LC |
322 | (set-record-type-printer! <nix-server> |
323 | (lambda (obj port) | |
324 | (format port "#<build-daemon ~a.~a ~a>" | |
325 | (nix-server-major-version obj) | |
326 | (nix-server-minor-version obj) | |
327 | (number->string (object-address obj) | |
328 | 16)))) | |
329 | ||
e87088c9 LC |
330 | (define-condition-type &nix-error &error |
331 | nix-error?) | |
332 | ||
ef86c39f LC |
333 | (define-condition-type &nix-connection-error &nix-error |
334 | nix-connection-error? | |
335 | (file nix-connection-error-file) | |
336 | (errno nix-connection-error-code)) | |
337 | ||
e87088c9 LC |
338 | (define-condition-type &nix-protocol-error &nix-error |
339 | nix-protocol-error? | |
340 | (message nix-protocol-error-message) | |
341 | (status nix-protocol-error-status)) | |
342 | ||
9fd72fb1 | 343 | (define* (open-connection #:optional (file (%daemon-socket-file)) |
d203d3d4 | 344 | #:key (reserve-space? #t) cpu-affinity) |
e531ac2a | 345 | "Connect to the daemon over the Unix-domain socket at FILE. When |
d203d3d4 LC |
346 | RESERVE-SPACE? is true, instruct it to reserve a little bit of extra space on |
347 | the file system so that the garbage collector can still operate, should the | |
348 | disk become full. When CPU-AFFINITY is true, it must be an integer | |
349 | corresponding to an OS-level CPU number to which the daemon's worker process | |
350 | for this connection will be pinned. Return a server object." | |
77d3cf08 LC |
351 | (let ((s (with-fluids ((%default-port-encoding #f)) |
352 | ;; This trick allows use of the `scm_c_read' optimization. | |
353 | (socket PF_UNIX SOCK_STREAM 0))) | |
354 | (a (make-socket-address PF_UNIX file))) | |
df1fab58 | 355 | |
ef86c39f LC |
356 | (catch 'system-error |
357 | (cut connect s a) | |
358 | (lambda args | |
359 | ;; Translate the error to something user-friendly. | |
360 | (let ((errno (system-error-errno args))) | |
361 | (raise (condition (&nix-connection-error | |
362 | (file file) | |
363 | (errno errno))))))) | |
364 | ||
77d3cf08 LC |
365 | (write-int %worker-magic-1 s) |
366 | (let ((r (read-int s))) | |
367 | (and (eqv? r %worker-magic-2) | |
368 | (let ((v (read-int s))) | |
369 | (and (eqv? (protocol-major %protocol-version) | |
370 | (protocol-major v)) | |
371 | (begin | |
372 | (write-int %protocol-version s) | |
d203d3d4 LC |
373 | (when (>= (protocol-minor v) 14) |
374 | (write-int (if cpu-affinity 1 0) s) | |
375 | (when cpu-affinity | |
376 | (write-int cpu-affinity s))) | |
377 | (when (>= (protocol-minor v) 11) | |
378 | (write-int (if reserve-space? 1 0) s)) | |
77d3cf08 LC |
379 | (let ((s (%make-nix-server s |
380 | (protocol-major v) | |
2c3f47ee | 381 | (protocol-minor v) |
fce2394e LC |
382 | (make-hash-table 100) |
383 | (make-hash-table 100)))) | |
34fcbe3a LC |
384 | (let loop ((done? (process-stderr s))) |
385 | (or done? (process-stderr s))) | |
77d3cf08 LC |
386 | s)))))))) |
387 | ||
3abaf0c4 LC |
388 | (define (close-connection server) |
389 | "Close the connection to SERVER." | |
390 | (close (nix-server-socket server))) | |
391 | ||
ce4a4829 LC |
392 | (define-syntax-rule (with-store store exp ...) |
393 | "Bind STORE to an open connection to the store and evaluate EXPs; | |
394 | automatically close the store when the dynamic extent of EXP is left." | |
395 | (let ((store (open-connection))) | |
396 | (dynamic-wind | |
397 | (const #f) | |
398 | (lambda () | |
399 | exp ...) | |
400 | (lambda () | |
401 | (false-if-exception (close-connection store)))))) | |
402 | ||
dcee50c1 LC |
403 | (define current-build-output-port |
404 | ;; The port where build output is sent. | |
405 | (make-parameter (current-error-port))) | |
406 | ||
526382ff LC |
407 | (define* (dump-port in out |
408 | #:optional len | |
409 | #:key (buffer-size 16384)) | |
410 | "Read LEN bytes from IN (or as much as possible if LEN is #f) and write it | |
411 | to OUT, using chunks of BUFFER-SIZE bytes." | |
412 | (define buffer | |
413 | (make-bytevector buffer-size)) | |
414 | ||
415 | (let loop ((total 0) | |
416 | (bytes (get-bytevector-n! in buffer 0 | |
417 | (if len | |
418 | (min len buffer-size) | |
419 | buffer-size)))) | |
420 | (or (eof-object? bytes) | |
421 | (and len (= total len)) | |
422 | (let ((total (+ total bytes))) | |
423 | (put-bytevector out buffer 0 bytes) | |
424 | (loop total | |
425 | (get-bytevector-n! in buffer 0 | |
426 | (if len | |
427 | (min (- len total) buffer-size) | |
428 | buffer-size))))))) | |
429 | ||
d28869af LC |
430 | (define %newlines |
431 | ;; Newline characters triggering a flush of 'current-build-output-port'. | |
432 | ;; Unlike Guile's _IOLBF, we flush upon #\return so that progress reports | |
433 | ;; that use that trick are correctly displayed. | |
434 | (char-set #\newline #\return)) | |
435 | ||
526382ff | 436 | (define* (process-stderr server #:optional user-port) |
dcee50c1 LC |
437 | "Read standard output and standard error from SERVER, writing it to |
438 | CURRENT-BUILD-OUTPUT-PORT. Return #t when SERVER is done sending data, and | |
439 | #f otherwise; in the latter case, the caller should call `process-stderr' | |
bbdb3ffa LC |
440 | again until #t is returned or an error is raised. |
441 | ||
442 | Since the build process's output cannot be assumed to be UTF-8, we | |
443 | conservatively consider it to be Latin-1, thereby avoiding possible | |
444 | encoding conversion errors." | |
77d3cf08 LC |
445 | (define p |
446 | (nix-server-socket server)) | |
447 | ||
448 | ;; magic cookies from worker-protocol.hh | |
5674a3fd LC |
449 | (define %stderr-next #x6f6c6d67) ; "olmg", build log |
450 | (define %stderr-read #x64617461) ; "data", data needed from source | |
451 | (define %stderr-write #x64617416) ; "dat\x16", data for sink | |
452 | (define %stderr-last #x616c7473) ; "alts", we're done | |
453 | (define %stderr-error #x63787470) ; "cxtp", error reporting | |
77d3cf08 LC |
454 | |
455 | (let ((k (read-int p))) | |
456 | (cond ((= k %stderr-write) | |
526382ff LC |
457 | ;; Write a byte stream to USER-PORT. |
458 | (let* ((len (read-int p)) | |
459 | (m (modulo len 8))) | |
460 | (dump-port p user-port len) | |
461 | (unless (zero? m) | |
462 | ;; Consume padding, as for strings. | |
463 | (get-bytevector-n p (- 8 m)))) | |
dcee50c1 | 464 | #f) |
77d3cf08 | 465 | ((= k %stderr-read) |
526382ff | 466 | ;; Read a byte stream from USER-PORT. |
5895f244 LC |
467 | ;; Note: Avoid 'get-bytevector-n' to work around |
468 | ;; <http://bugs.gnu.org/17591> in Guile up to 2.0.11. | |
526382ff | 469 | (let* ((max-len (read-int p)) |
5895f244 LC |
470 | (data (make-bytevector max-len)) |
471 | (len (get-bytevector-n! user-port data 0 max-len))) | |
526382ff | 472 | (write-int len p) |
5895f244 | 473 | (put-bytevector p data 0 len) |
526382ff | 474 | (write-padding len p) |
dcee50c1 | 475 | #f)) |
77d3cf08 | 476 | ((= k %stderr-next) |
ce72c780 LC |
477 | ;; Log a string. Build logs are usually UTF-8-encoded, but they |
478 | ;; may also contain arbitrary byte sequences that should not cause | |
479 | ;; this to fail. Thus, use the permissive | |
480 | ;; 'read-maybe-utf8-string'. | |
481 | (let ((s (read-maybe-utf8-string p))) | |
dcee50c1 | 482 | (display s (current-build-output-port)) |
d28869af LC |
483 | (when (string-any %newlines s) |
484 | (flush-output-port (current-build-output-port))) | |
dcee50c1 | 485 | #f)) |
77d3cf08 | 486 | ((= k %stderr-error) |
526382ff | 487 | ;; Report an error. |
ce72c780 | 488 | (let ((error (read-maybe-utf8-string p)) |
0ff3e3aa LC |
489 | ;; Currently the daemon fails to send a status code for early |
490 | ;; errors like DB schema version mismatches, so check for EOF. | |
491 | (status (if (and (>= (nix-server-minor-version server) 8) | |
492 | (not (eof-object? (lookahead-u8 p)))) | |
77d3cf08 LC |
493 | (read-int p) |
494 | 1))) | |
e87088c9 LC |
495 | (raise (condition (&nix-protocol-error |
496 | (message error) | |
497 | (status status)))))) | |
77d3cf08 | 498 | ((= k %stderr-last) |
dcee50c1 | 499 | ;; The daemon is done (see `stopWork' in `nix-worker.cc'.) |
77d3cf08 LC |
500 | #t) |
501 | (else | |
e87088c9 LC |
502 | (raise (condition (&nix-protocol-error |
503 | (message "invalid error code") | |
504 | (status k)))))))) | |
77d3cf08 | 505 | |
4d581220 LC |
506 | (define %default-substitute-urls |
507 | ;; Default list of substituters. | |
508 | '("http://hydra.gnu.org")) | |
509 | ||
77d3cf08 | 510 | (define* (set-build-options server |
c3eb878f | 511 | #:key keep-failed? keep-going? fallback? |
77d3cf08 | 512 | (verbosity 0) |
2fba87ac | 513 | rounds ;number of build rounds |
8b47758f | 514 | (max-build-jobs 1) |
6c20d1d0 | 515 | timeout |
77d3cf08 LC |
516 | (max-silent-time 3600) |
517 | (use-build-hook? #t) | |
518 | (build-verbosity 0) | |
519 | (log-type 0) | |
e036c31b | 520 | (print-build-trace #t) |
8b47758f | 521 | (build-cores (current-processor-count)) |
63193ebf | 522 | (use-substitutes? #t) |
4d581220 | 523 | |
fb4bf72b LC |
524 | ;; Client-provided substitute URLs. If it is #f, |
525 | ;; the daemon's settings are used. Otherwise, it | |
526 | ;; overrides the daemons settings; see 'guix | |
527 | ;; substitute'. | |
528 | (substitute-urls #f)) | |
77d3cf08 LC |
529 | ;; Must be called after `open-connection'. |
530 | ||
531 | (define socket | |
532 | (nix-server-socket server)) | |
533 | ||
534 | (let-syntax ((send (syntax-rules () | |
e036c31b LC |
535 | ((_ (type option) ...) |
536 | (begin | |
537 | (write-arg type option socket) | |
538 | ...))))) | |
539 | (write-int (operation-id set-options) socket) | |
540 | (send (boolean keep-failed?) (boolean keep-going?) | |
c3eb878f | 541 | (boolean fallback?) (integer verbosity) |
e036c31b | 542 | (integer max-build-jobs) (integer max-silent-time)) |
371e87d2 LC |
543 | (when (>= (nix-server-minor-version server) 2) |
544 | (send (boolean use-build-hook?))) | |
545 | (when (>= (nix-server-minor-version server) 4) | |
546 | (send (integer build-verbosity) (integer log-type) | |
547 | (boolean print-build-trace))) | |
548 | (when (>= (nix-server-minor-version server) 6) | |
549 | (send (integer build-cores))) | |
550 | (when (>= (nix-server-minor-version server) 10) | |
551 | (send (boolean use-substitutes?))) | |
552 | (when (>= (nix-server-minor-version server) 12) | |
41c45e78 LC |
553 | (let ((pairs `(,@(if timeout |
554 | `(("build-timeout" . ,(number->string timeout))) | |
555 | '()) | |
fb4bf72b LC |
556 | ,@(if substitute-urls |
557 | `(("substitute-urls" | |
558 | . ,(string-join substitute-urls))) | |
2fba87ac LC |
559 | '()) |
560 | ,@(if rounds | |
561 | `(("build-repeat" | |
562 | . ,(number->string (max 0 (1- rounds))))) | |
fb4bf72b | 563 | '())))) |
f401b1e9 | 564 | (send (string-pairs pairs)))) |
dcee50c1 LC |
565 | (let loop ((done? (process-stderr server))) |
566 | (or done? (process-stderr server))))) | |
77d3cf08 | 567 | |
fd060fd3 | 568 | (define-syntax operation |
77d3cf08 | 569 | (syntax-rules () |
fd060fd3 | 570 | "Define a client-side RPC stub for the given operation." |
3259877d | 571 | ((_ (name (type arg) ...) docstring return ...) |
fd060fd3 | 572 | (lambda (server arg ...) |
77d3cf08 LC |
573 | docstring |
574 | (let ((s (nix-server-socket server))) | |
575 | (write-int (operation-id name) s) | |
576 | (write-arg type arg s) | |
577 | ... | |
dcee50c1 LC |
578 | ;; Loop until the server is done sending error output. |
579 | (let loop ((done? (process-stderr server))) | |
580 | (or done? (loop (process-stderr server)))) | |
3259877d | 581 | (values (read-arg return s) ...)))))) |
77d3cf08 | 582 | |
fd060fd3 LC |
583 | (define-syntax-rule (define-operation (name args ...) |
584 | docstring return ...) | |
585 | (define name | |
586 | (operation (name args ...) docstring return ...))) | |
587 | ||
31ef99a8 | 588 | (define-operation (valid-path? (string path)) |
06b76acc LC |
589 | "Return #t when PATH designates a valid store item and #f otherwise (an |
590 | invalid item may exist on disk but still be invalid, for instance because it | |
591 | is the result of an aborted or failed build.) | |
592 | ||
593 | A '&nix-protocol-error' condition is raised if PATH is not prefixed by the | |
594 | store directory (/gnu/store)." | |
31ef99a8 LC |
595 | boolean) |
596 | ||
63193ebf | 597 | (define-operation (query-path-hash (store-path path)) |
82058eff LC |
598 | "Return the SHA256 hash of PATH as a bytevector." |
599 | base16) | |
600 | ||
11e7a6cf LC |
601 | (define hash-part->path |
602 | (let ((query-path-from-hash-part | |
603 | (operation (query-path-from-hash-part (string hash)) | |
604 | #f | |
605 | store-path))) | |
606 | (lambda (server hash-part) | |
607 | "Return the store path whose hash part is HASH-PART (a nix-base32 | |
608 | string). Raise an error if no such path exists." | |
609 | ;; This RPC is primarily used by Hydra to reply to HTTP GETs of | |
610 | ;; /HASH.narinfo. | |
611 | (query-path-from-hash-part server hash-part)))) | |
612 | ||
533d1768 DT |
613 | (define-operation (query-path-info (store-path path)) |
614 | "Return the info (hash, references, etc.) for PATH." | |
615 | path-info) | |
616 | ||
fd060fd3 LC |
617 | (define add-text-to-store |
618 | ;; A memoizing version of `add-to-store', to avoid repeated RPCs with | |
619 | ;; the very same arguments during a given session. | |
620 | (let ((add-text-to-store | |
621 | (operation (add-text-to-store (string name) (string text) | |
622 | (string-list references)) | |
623 | #f | |
624 | store-path))) | |
cfbf9160 | 625 | (lambda* (server name text #:optional (references '())) |
bdcf35a6 LC |
626 | "Add TEXT under file NAME in the store, and return its store path. |
627 | REFERENCES is the list of store paths referred to by the resulting store | |
628 | path." | |
fce2394e | 629 | (let ((args `(,text ,name ,references)) |
bdcf35a6 LC |
630 | (cache (nix-server-add-text-to-store-cache server))) |
631 | (or (hash-ref cache args) | |
fd060fd3 | 632 | (let ((path (add-text-to-store server name text references))) |
bdcf35a6 LC |
633 | (hash-set! cache args path) |
634 | path)))))) | |
635 | ||
fd060fd3 | 636 | (define add-to-store |
a7b6ffee LC |
637 | ;; A memoizing version of `add-to-store'. This is important because |
638 | ;; `add-to-store' leads to huge data transfers to the server, and | |
639 | ;; because it's often called many times with the very same argument. | |
fd060fd3 LC |
640 | (let ((add-to-store (operation (add-to-store (string basename) |
641 | (boolean fixed?) ; obsolete, must be #t | |
642 | (boolean recursive?) | |
643 | (string hash-algo) | |
644 | (file file-name)) | |
645 | #f | |
646 | store-path))) | |
a9ebd9ef LC |
647 | (lambda (server basename recursive? hash-algo file-name) |
648 | "Add the contents of FILE-NAME under BASENAME to the store. When | |
69792b28 LC |
649 | RECURSIVE? is false, FILE-NAME must designate a regular file--not a directory |
650 | nor a symlink. When RECURSIVE? is true and FILE-NAME designates a directory, | |
651 | the contents of FILE-NAME are added recursively; if FILE-NAME designates a | |
652 | flat file and RECURSIVE? is true, its contents are added, and its permission | |
653 | bits are kept. HASH-ALGO must be a string such as \"sha256\"." | |
654 | (let* ((st (false-if-exception (lstat file-name))) | |
fce2394e | 655 | (args `(,st ,basename ,recursive? ,hash-algo)) |
2c3f47ee | 656 | (cache (nix-server-add-to-store-cache server))) |
a7b6ffee | 657 | (or (and st (hash-ref cache args)) |
a9ebd9ef | 658 | (let ((path (add-to-store server basename #t recursive? |
a7b6ffee LC |
659 | hash-algo file-name))) |
660 | (hash-set! cache args path) | |
661 | path)))))) | |
662 | ||
07e70f48 LC |
663 | (define build-things |
664 | (let ((build (operation (build-things (string-list things) | |
665 | (integer mode)) | |
666 | "Do it!" | |
2734cbb8 LC |
667 | boolean)) |
668 | (build/old (operation (build-things (string-list things)) | |
669 | "Do it!" | |
670 | boolean))) | |
07e70f48 LC |
671 | (lambda* (store things #:optional (mode (build-mode normal))) |
672 | "Build THINGS, a list of store items which may be either '.drv' files or | |
abac874b LC |
673 | outputs, and return when the worker is done building them. Elements of THINGS |
674 | that are not derivations can only be substituted and not built locally. | |
dcee50c1 | 675 | Return #t on success." |
2734cbb8 LC |
676 | (if (>= (nix-server-minor-version store) 15) |
677 | (build store things mode) | |
678 | (if (= mode (build-mode normal)) | |
679 | (build/old store things) | |
680 | (raise (condition (&nix-protocol-error | |
681 | (message "unsupported build mode") | |
682 | (status 1))))))))) | |
26bbbb95 | 683 | |
d3648e01 LC |
684 | (define-operation (add-temp-root (store-path path)) |
685 | "Make PATH a temporary root for the duration of the current session. | |
686 | Return #t." | |
687 | boolean) | |
688 | ||
34811f02 | 689 | (define-operation (add-indirect-root (string file-name)) |
a9d2a105 LC |
690 | "Make the symlink FILE-NAME an indirect root for the garbage collector: |
691 | whatever store item FILE-NAME points to will not be collected. Return #t on | |
692 | success. | |
693 | ||
694 | FILE-NAME can be anywhere on the file system, but it must be an absolute file | |
695 | name--it is the caller's responsibility to ensure that it is an absolute file | |
696 | name." | |
34811f02 LC |
697 | boolean) |
698 | ||
a9d2a105 LC |
699 | (define %gc-roots-directory |
700 | ;; The place where garbage collector roots (symlinks) are kept. | |
701 | (string-append %state-directory "/gcroots")) | |
702 | ||
703 | (define (add-permanent-root target) | |
704 | "Add a garbage collector root pointing to TARGET, an element of the store, | |
705 | preventing TARGET from even being collected. This can also be used if TARGET | |
706 | does not exist yet. | |
707 | ||
708 | Raise an error if the caller does not have write access to the GC root | |
709 | directory." | |
710 | (let* ((root (string-append %gc-roots-directory "/" (basename target)))) | |
711 | (catch 'system-error | |
712 | (lambda () | |
713 | (symlink target root)) | |
714 | (lambda args | |
715 | ;; If ROOT already exists, this is fine; otherwise, re-throw. | |
716 | (unless (= EEXIST (system-error-errno args)) | |
717 | (apply throw args)))))) | |
718 | ||
719 | (define (remove-permanent-root target) | |
720 | "Remove the permanent garbage collector root pointing to TARGET. Raise an | |
721 | error if there is no such root." | |
722 | (delete-file (string-append %gc-roots-directory "/" (basename target)))) | |
723 | ||
fae31edc LC |
724 | (define references |
725 | (operation (query-references (store-path path)) | |
726 | "Return the list of references of PATH." | |
727 | store-path-list)) | |
728 | ||
6581ec9a LC |
729 | (define (references/substitutes store items) |
730 | "Return the list of list of references of ITEMS; the result has the same | |
731 | length as ITEMS. Query substitute information for any item missing from the | |
732 | store at once. Raise a '&nix-protocol-error' exception if reference | |
733 | information for one of ITEMS is missing." | |
734 | (let* ((local-refs (map (lambda (item) | |
735 | (guard (c ((nix-protocol-error? c) #f)) | |
736 | (references store item))) | |
737 | items)) | |
738 | (missing (fold-right (lambda (item local-ref result) | |
739 | (if local-ref | |
740 | result | |
741 | (cons item result))) | |
742 | '() | |
743 | items local-refs)) | |
744 | ||
745 | ;; Query all the substitutes at once to minimize the cost of | |
746 | ;; launching 'guix substitute' and making HTTP requests. | |
747 | (substs (substitutable-path-info store missing))) | |
748 | (when (< (length substs) (length missing)) | |
749 | (raise (condition (&nix-protocol-error | |
750 | (message "cannot determine \ | |
751 | the list of references") | |
752 | (status 1))))) | |
753 | ||
754 | ;; Intersperse SUBSTS and LOCAL-REFS. | |
755 | (let loop ((local-refs local-refs) | |
756 | (remote-refs (map substitutable-references substs)) | |
757 | (result '())) | |
758 | (match local-refs | |
759 | (() | |
760 | (reverse result)) | |
761 | ((#f tail ...) | |
762 | (match remote-refs | |
763 | ((remote rest ...) | |
764 | (loop tail rest (cons remote result))))) | |
765 | ((head tail ...) | |
766 | (loop tail remote-refs (cons head result))))))) | |
767 | ||
3f1e6939 LC |
768 | (define* (fold-path store proc seed path |
769 | #:optional (relatives (cut references store <>))) | |
770 | "Call PROC for each of the RELATIVES of PATH, exactly once, and return the | |
771 | result formed from the successive calls to PROC, the first of which is passed | |
772 | SEED." | |
773 | (let loop ((paths (list path)) | |
774 | (result seed) | |
775 | (seen vlist-null)) | |
776 | (match paths | |
777 | ((path rest ...) | |
778 | (if (vhash-assoc path seen) | |
779 | (loop rest result seen) | |
780 | (let ((seen (vhash-cons path #t seen)) | |
781 | (rest (append rest (relatives path))) | |
782 | (result (proc path result))) | |
783 | (loop rest result seen)))) | |
784 | (() | |
785 | result)))) | |
786 | ||
787 | (define (requisites store path) | |
788 | "Return the requisites of PATH, including PATH---i.e., its closure (all its | |
789 | references, recursively)." | |
790 | (fold-path store cons '() path)) | |
791 | ||
50add477 LC |
792 | (define (topologically-sorted store paths) |
793 | "Return a list containing PATHS and all their references sorted in | |
794 | topological order." | |
795 | (define (traverse) | |
796 | ;; Do a simple depth-first traversal of all of PATHS. | |
797 | (let loop ((paths paths) | |
798 | (visited vlist-null) | |
799 | (result '())) | |
800 | (define (visit n) | |
801 | (vhash-cons n #t visited)) | |
802 | ||
803 | (define (visited? n) | |
804 | (vhash-assoc n visited)) | |
805 | ||
806 | (match paths | |
807 | ((head tail ...) | |
808 | (if (visited? head) | |
809 | (loop tail visited result) | |
810 | (call-with-values | |
811 | (lambda () | |
812 | (loop (references store head) | |
813 | (visit head) | |
814 | result)) | |
815 | (lambda (visited result) | |
816 | (loop tail | |
817 | visited | |
818 | (cons head result)))))) | |
819 | (() | |
820 | (values visited result))))) | |
821 | ||
822 | (call-with-values traverse | |
823 | (lambda (_ result) | |
824 | (reverse result)))) | |
825 | ||
fae31edc LC |
826 | (define referrers |
827 | (operation (query-referrers (store-path path)) | |
828 | "Return the list of path that refer to PATH." | |
829 | store-path-list)) | |
830 | ||
831 | (define valid-derivers | |
832 | (operation (query-valid-derivers (store-path path)) | |
833 | "Return the list of valid \"derivers\" of PATH---i.e., all the | |
834 | .drv present in the store that have PATH among their outputs." | |
835 | store-path-list)) | |
836 | ||
837 | (define query-derivation-outputs ; avoid name clash with `derivation-outputs' | |
838 | (operation (query-derivation-outputs (store-path path)) | |
839 | "Return the list of outputs of PATH, a .drv file." | |
840 | store-path-list)) | |
841 | ||
0f3d2504 LC |
842 | (define-operation (has-substitutes? (store-path path)) |
843 | "Return #t if binary substitutes are available for PATH, and #f otherwise." | |
844 | boolean) | |
845 | ||
846 | (define substitutable-paths | |
847 | (operation (query-substitutable-paths (store-path-list paths)) | |
848 | "Return the subset of PATHS that is substitutable." | |
849 | store-path-list)) | |
850 | ||
851 | (define substitutable-path-info | |
f65cf81a | 852 | (operation (query-substitutable-path-infos (store-path-list paths)) |
0f3d2504 LC |
853 | "Return information about the subset of PATHS that is |
854 | substitutable. For each substitutable path, a `substitutable?' object is | |
855 | returned." | |
856 | substitutable-path-list)) | |
857 | ||
e3fd0ce6 LC |
858 | (define-operation (optimize-store) |
859 | "Optimize the store by hard-linking identical files (\"deduplication\".) | |
860 | Return #t on success." | |
861 | ;; Note: the daemon in Guix <= 0.8.2 does not implement this RPC. | |
862 | boolean) | |
863 | ||
c63d9403 LC |
864 | (define verify-store |
865 | (let ((verify (operation (verify-store (boolean check-contents?) | |
866 | (boolean repair?)) | |
867 | "Verify the store." | |
868 | boolean))) | |
869 | (lambda* (store #:key check-contents? repair?) | |
870 | "Verify the integrity of the store and return false if errors remain, | |
871 | and true otherwise. When REPAIR? is true, repair any missing or altered store | |
872 | items by substituting them (this typically requires root privileges because it | |
873 | is not an atomic operation.) When CHECK-CONTENTS? is true, check the contents | |
874 | of store items; this can take a lot of time." | |
875 | (not (verify store check-contents? repair?))))) | |
876 | ||
3259877d LC |
877 | (define (run-gc server action to-delete min-freed) |
878 | "Perform the garbage-collector operation ACTION, one of the | |
879 | `gc-action' values. When ACTION is `delete-specific', the TO-DELETE is | |
880 | the list of store paths to delete. IGNORE-LIVENESS? should always be | |
881 | #f. MIN-FREED is the minimum amount of disk space to be freed, in | |
882 | bytes, before the GC can stop. Return the list of store paths delete, | |
883 | and the number of bytes freed." | |
884 | (let ((s (nix-server-socket server))) | |
885 | (write-int (operation-id collect-garbage) s) | |
886 | (write-int action s) | |
887 | (write-store-path-list to-delete s) | |
888 | (write-arg boolean #f s) ; ignore-liveness? | |
889 | (write-long-long min-freed s) | |
890 | (write-int 0 s) ; obsolete | |
891 | (when (>= (nix-server-minor-version server) 5) | |
892 | ;; Obsolete `use-atime' and `max-atime' parameters. | |
893 | (write-int 0 s) | |
894 | (write-int 0 s)) | |
895 | ||
896 | ;; Loop until the server is done sending error output. | |
897 | (let loop ((done? (process-stderr server))) | |
898 | (or done? (loop (process-stderr server)))) | |
899 | ||
900 | (let ((paths (read-store-path-list s)) | |
901 | (freed (read-long-long s)) | |
902 | (obsolete (read-long-long s))) | |
000c59b6 LC |
903 | (unless (null? paths) |
904 | ;; To be on the safe side, completely invalidate both caches. | |
905 | ;; Otherwise we could end up returning store paths that are no longer | |
906 | ;; valid. | |
907 | (hash-clear! (nix-server-add-to-store-cache server)) | |
908 | (hash-clear! (nix-server-add-text-to-store-cache server))) | |
909 | ||
3259877d LC |
910 | (values paths freed)))) |
911 | ||
912 | (define-syntax-rule (%long-long-max) | |
913 | ;; Maximum unsigned 64-bit integer. | |
914 | (- (expt 2 64) 1)) | |
915 | ||
916 | (define (live-paths server) | |
917 | "Return the list of live store paths---i.e., store paths still | |
918 | referenced, and thus not subject to being garbage-collected." | |
919 | (run-gc server (gc-action return-live) '() (%long-long-max))) | |
920 | ||
921 | (define (dead-paths server) | |
922 | "Return the list of dead store paths---i.e., store paths no longer | |
923 | referenced, and thus subject to being garbage-collected." | |
924 | (run-gc server (gc-action return-dead) '() (%long-long-max))) | |
925 | ||
926 | (define* (collect-garbage server #:optional (min-freed (%long-long-max))) | |
927 | "Collect garbage from the store at SERVER. If MIN-FREED is non-zero, | |
928 | then collect at least MIN-FREED bytes. Return the paths that were | |
929 | collected, and the number of bytes freed." | |
930 | (run-gc server (gc-action delete-dead) '() min-freed)) | |
931 | ||
932 | (define* (delete-paths server paths #:optional (min-freed (%long-long-max))) | |
933 | "Delete PATHS from the store at SERVER, if they are no longer | |
934 | referenced. If MIN-FREED is non-zero, then stop after at least | |
935 | MIN-FREED bytes have been collected. Return the paths that were | |
936 | collected, and the number of bytes freed." | |
937 | (run-gc server (gc-action delete-specific) paths min-freed)) | |
938 | ||
526382ff LC |
939 | (define (import-paths server port) |
940 | "Import the set of store paths read from PORT into SERVER's store. An error | |
941 | is raised if the set of paths read from PORT is not signed (as per | |
942 | 'export-path #:sign? #t'.) Return the list of store paths imported." | |
943 | (let ((s (nix-server-socket server))) | |
944 | (write-int (operation-id import-paths) s) | |
945 | (let loop ((done? (process-stderr server port))) | |
946 | (or done? (loop (process-stderr server port)))) | |
947 | (read-store-path-list s))) | |
948 | ||
949 | (define* (export-path server path port #:key (sign? #t)) | |
950 | "Export PATH to PORT. When SIGN? is true, sign it." | |
951 | (let ((s (nix-server-socket server))) | |
952 | (write-int (operation-id export-path) s) | |
953 | (write-store-path path s) | |
954 | (write-arg boolean sign? s) | |
955 | (let loop ((done? (process-stderr server port))) | |
956 | (or done? (loop (process-stderr server port)))) | |
957 | (= 1 (read-int s)))) | |
958 | ||
5b3d863f | 959 | (define* (export-paths server paths port #:key (sign? #t) recursive?) |
99fbddf9 | 960 | "Export the store paths listed in PATHS to PORT, in topological order, |
5b3d863f LC |
961 | signing them if SIGN? is true. When RECURSIVE? is true, export the closure of |
962 | PATHS---i.e., PATHS and all their dependencies." | |
cafb92d8 | 963 | (define ordered |
5b3d863f LC |
964 | (let ((sorted (topologically-sorted server paths))) |
965 | ;; When RECURSIVE? is #f, filter out the references of PATHS. | |
966 | (if recursive? | |
967 | sorted | |
968 | (filter (cut member <> paths) sorted)))) | |
cafb92d8 | 969 | |
1d506993 LC |
970 | (let loop ((paths ordered)) |
971 | (match paths | |
972 | (() | |
973 | (write-int 0 port)) | |
974 | ((head tail ...) | |
975 | (write-int 1 port) | |
976 | (and (export-path server head port #:sign? sign?) | |
977 | (loop tail)))))) | |
526382ff | 978 | |
16748d80 LC |
979 | (define-operation (query-failed-paths) |
980 | "Return the list of store items for which a build failure is cached. | |
981 | ||
982 | The result is always the empty list unless the daemon was started with | |
983 | '--cache-failures'." | |
984 | store-path-list) | |
985 | ||
986 | (define-operation (clear-failed-paths (store-path-list items)) | |
987 | "Remove ITEMS from the list of cached build failures. | |
988 | ||
989 | This makes sense only when the daemon was started with '--cache-failures'." | |
990 | boolean) | |
991 | ||
6bfec3ed | 992 | (define* (register-path path |
689142cd LC |
993 | #:key (references '()) deriver prefix |
994 | state-directory) | |
6bfec3ed | 995 | "Register PATH as a valid store file, with REFERENCES as its list of |
bb31e0a3 LC |
996 | references, and DERIVER as its deriver (.drv that led to it.) If PREFIX is |
997 | not #f, it must be the name of the directory containing the new store to | |
689142cd LC |
998 | initialize; if STATE-DIRECTORY is not #f, it must be a string containing the |
999 | absolute file name to the state directory of the store being initialized. | |
1000 | Return #t on success. | |
6bfec3ed LC |
1001 | |
1002 | Use with care as it directly modifies the store! This is primarily meant to | |
1003 | be used internally by the daemon's build hook." | |
1004 | ;; Currently this is implemented by calling out to the fine C++ blob. | |
1005 | (catch 'system-error | |
1006 | (lambda () | |
e901ef29 | 1007 | (let ((pipe (apply open-pipe* OPEN_WRITE %guix-register-program |
689142cd LC |
1008 | `(,@(if prefix |
1009 | `("--prefix" ,prefix) | |
1010 | '()) | |
1011 | ,@(if state-directory | |
1012 | `("--state-directory" ,state-directory) | |
1013 | '()))))) | |
6bfec3ed LC |
1014 | (and pipe |
1015 | (begin | |
1016 | (format pipe "~a~%~a~%~a~%" | |
1017 | path (or deriver "") (length references)) | |
1018 | (for-each (cut format pipe "~a~%" <>) references) | |
1019 | (zero? (close-pipe pipe)))))) | |
1020 | (lambda args | |
1021 | ;; Failed to run %GUIX-REGISTER-PROGRAM. | |
1022 | #f))) | |
1023 | ||
26bbbb95 | 1024 | \f |
e87f0591 LC |
1025 | ;;; |
1026 | ;;; Store monad. | |
1027 | ;;; | |
1028 | ||
4e190c28 LC |
1029 | (define-syntax-rule (define-alias new old) |
1030 | (define-syntax new (identifier-syntax old))) | |
e87f0591 | 1031 | |
4e190c28 LC |
1032 | ;; The store monad allows us to (1) build sequences of operations in the |
1033 | ;; store, and (2) make the store an implicit part of the execution context, | |
1034 | ;; rather than a parameter of every single function. | |
1035 | (define-alias %store-monad %state-monad) | |
1036 | (define-alias store-return state-return) | |
1037 | (define-alias store-bind state-bind) | |
e87f0591 | 1038 | |
5808dcc2 LC |
1039 | (define (preserve-documentation original proc) |
1040 | "Return PROC with documentation taken from ORIGINAL." | |
1041 | (set-object-property! proc 'documentation | |
1042 | (procedure-property original 'documentation)) | |
1043 | proc) | |
1044 | ||
e87f0591 LC |
1045 | (define (store-lift proc) |
1046 | "Lift PROC, a procedure whose first argument is a connection to the store, | |
1047 | in the store monad." | |
5808dcc2 LC |
1048 | (preserve-documentation proc |
1049 | (lambda args | |
1050 | (lambda (store) | |
1051 | (values (apply proc store args) store))))) | |
e87f0591 | 1052 | |
023d9892 LC |
1053 | (define (store-lower proc) |
1054 | "Lower PROC, a monadic procedure in %STORE-MONAD, to a \"normal\" procedure | |
1055 | taking the store as its first argument." | |
5808dcc2 LC |
1056 | (preserve-documentation proc |
1057 | (lambda (store . args) | |
1058 | (run-with-store store (apply proc args))))) | |
023d9892 | 1059 | |
e87f0591 LC |
1060 | ;; |
1061 | ;; Store monad operators. | |
1062 | ;; | |
1063 | ||
ad372953 LC |
1064 | (define* (text-file name text |
1065 | #:optional (references '())) | |
e87f0591 | 1066 | "Return as a monadic value the absolute file name in the store of the file |
ad372953 LC |
1067 | containing TEXT, a string. REFERENCES is a list of store items that the |
1068 | resulting text file refers to; it defaults to the empty list." | |
e87f0591 | 1069 | (lambda (store) |
ad372953 | 1070 | (values (add-text-to-store store name text references) |
4e190c28 | 1071 | store))) |
e87f0591 LC |
1072 | |
1073 | (define* (interned-file file #:optional name | |
1074 | #:key (recursive? #t)) | |
1075 | "Return the name of FILE once interned in the store. Use NAME as its store | |
1076 | name, or the basename of FILE if NAME is omitted. | |
1077 | ||
1078 | When RECURSIVE? is true, the contents of FILE are added recursively; if FILE | |
1079 | designates a flat file and RECURSIVE? is true, its contents are added, and its | |
1080 | permission bits are kept." | |
1081 | (lambda (store) | |
4e190c28 LC |
1082 | (values (add-to-store store (or name (basename file)) |
1083 | recursive? "sha256" file) | |
1084 | store))) | |
e87f0591 | 1085 | |
abac874b LC |
1086 | (define build |
1087 | ;; Monadic variant of 'build-things'. | |
1088 | (store-lift build-things)) | |
1089 | ||
4f740b67 AK |
1090 | (define set-build-options* |
1091 | (store-lift set-build-options)) | |
1092 | ||
98a7b528 LC |
1093 | (define-inlinable (current-system) |
1094 | ;; Consult the %CURRENT-SYSTEM fluid at bind time. This is equivalent to | |
1095 | ;; (lift0 %current-system %store-monad), but inlinable, thus avoiding | |
1096 | ;; closure allocation in some cases. | |
1097 | (lambda (state) | |
1098 | (values (%current-system) state))) | |
1099 | ||
1100 | (define-inlinable (set-current-system system) | |
1101 | ;; Set the %CURRENT-SYSTEM fluid at bind time. | |
1102 | (lambda (state) | |
1103 | (values (%current-system system) state))) | |
1104 | ||
e87f0591 LC |
1105 | (define %guile-for-build |
1106 | ;; The derivation of the Guile to be used within the build environment, | |
1107 | ;; when using 'gexp->derivation' and co. | |
1108 | (make-parameter #f)) | |
1109 | ||
1110 | (define* (run-with-store store mval | |
1111 | #:key | |
1112 | (guile-for-build (%guile-for-build)) | |
1113 | (system (%current-system))) | |
1114 | "Run MVAL, a monadic value in the store monad, in STORE, an open store | |
3698f524 | 1115 | connection, and return the result." |
a8afb9ae LC |
1116 | ;; Initialize the dynamic bindings here to avoid bad surprises. The |
1117 | ;; difficulty lies in the fact that dynamic bindings are resolved at | |
1118 | ;; bind-time and not at call time, which can be disconcerting. | |
e87f0591 | 1119 | (parameterize ((%guile-for-build guile-for-build) |
a8afb9ae LC |
1120 | (%current-system system) |
1121 | (%current-target-system #f)) | |
3698f524 LC |
1122 | (call-with-values (lambda () |
1123 | (run-with-state mval store)) | |
1124 | (lambda (result store) | |
1125 | ;; Discard the state. | |
1126 | result)))) | |
e87f0591 LC |
1127 | |
1128 | \f | |
26bbbb95 LC |
1129 | ;;; |
1130 | ;;; Store paths. | |
1131 | ;;; | |
1132 | ||
1133 | (define %store-prefix | |
1134 | ;; Absolute path to the Nix store. | |
1d6816f9 | 1135 | (make-parameter %store-directory)) |
26bbbb95 | 1136 | |
f39bd08a LC |
1137 | (define (store-path? path) |
1138 | "Return #t if PATH is a store path." | |
1139 | ;; This is a lightweight check, compared to using a regexp, but this has to | |
1140 | ;; be fast as it's called often in `derivation', for instance. | |
1141 | ;; `isStorePath' in Nix does something similar. | |
1142 | (string-prefix? (%store-prefix) path)) | |
26bbbb95 | 1143 | |
9336e5b5 LC |
1144 | (define (direct-store-path? path) |
1145 | "Return #t if PATH is a store path, and not a sub-directory of a store path. | |
1146 | This predicate is sometimes needed because files *under* a store path are not | |
1147 | valid inputs." | |
1148 | (and (store-path? path) | |
eee21271 | 1149 | (not (string=? path (%store-prefix))) |
9336e5b5 LC |
1150 | (let ((len (+ 1 (string-length (%store-prefix))))) |
1151 | (not (string-index (substring path len) #\/))))) | |
1152 | ||
cdb5b075 CS |
1153 | (define (direct-store-path path) |
1154 | "Return the direct store path part of PATH, stripping components after | |
1155 | '/gnu/store/xxxx-foo'." | |
1156 | (let ((prefix-length (+ (string-length (%store-prefix)) 35))) | |
1157 | (if (> (string-length path) prefix-length) | |
1158 | (let ((slash (string-index path #\/ prefix-length))) | |
1159 | (if slash (string-take path slash) path)) | |
1160 | path))) | |
1161 | ||
26bbbb95 LC |
1162 | (define (derivation-path? path) |
1163 | "Return #t if PATH is a derivation path." | |
1164 | (and (store-path? path) (string-suffix? ".drv" path))) | |
e3d74106 | 1165 | |
5c0f1845 LC |
1166 | (define store-regexp* |
1167 | ;; The substituter makes repeated calls to 'store-path-hash-part', hence | |
1168 | ;; this optimization. | |
1169 | (memoize | |
1170 | (lambda (store) | |
1171 | "Return a regexp matching a file in STORE." | |
1172 | (make-regexp (string-append "^" (regexp-quote store) | |
1173 | "/([0-9a-df-np-sv-z]{32})-([^/]+)$"))))) | |
1174 | ||
e3d74106 LC |
1175 | (define (store-path-package-name path) |
1176 | "Return the package name part of PATH, a file name in the store." | |
5c0f1845 LC |
1177 | (let ((path-rx (store-regexp* (%store-prefix)))) |
1178 | (and=> (regexp-exec path-rx path) | |
1179 | (cut match:substring <> 2)))) | |
2c6ab6cc LC |
1180 | |
1181 | (define (store-path-hash-part path) | |
1182 | "Return the hash part of PATH as a base32 string, or #f if PATH is not a | |
1183 | syntactically valid store path." | |
5c0f1845 | 1184 | (let ((path-rx (store-regexp* (%store-prefix)))) |
2c6ab6cc LC |
1185 | (and=> (regexp-exec path-rx path) |
1186 | (cut match:substring <> 1)))) | |
eddd4077 LC |
1187 | |
1188 | (define (log-file store file) | |
1189 | "Return the build log file for FILE, or #f if none could be found. FILE | |
1190 | must be an absolute store file name, or a derivation file name." | |
eddd4077 | 1191 | (cond ((derivation-path? file) |
021a201f | 1192 | (let* ((base (basename file)) |
80d0447c | 1193 | (log (string-append (dirname %state-directory) ; XXX |
f5768afa | 1194 | "/log/guix/drvs/" |
021a201f LC |
1195 | (string-take base 2) "/" |
1196 | (string-drop base 2))) | |
1197 | (log.bz2 (string-append log ".bz2"))) | |
1198 | (cond ((file-exists? log.bz2) log.bz2) | |
1199 | ((file-exists? log) log) | |
1200 | (else #f)))) | |
eddd4077 LC |
1201 | (else |
1202 | (match (valid-derivers store file) | |
1203 | ((derivers ...) | |
1204 | ;; Return the first that works. | |
1205 | (any (cut log-file store <>) derivers)) | |
1206 | (_ #f))))) |