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