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