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