Commit | Line | Data |
---|---|---|
233e7676 | 1 | ;;; GNU Guix --- Functional package management for GNU |
041b340d | 2 | ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org> |
f3a42251 | 3 | ;;; Copyright © 2018 Jan Nieuwenhuizen <janneke@gnu.org> |
fdae0fa5 | 4 | ;;; Copyright © 2019, 2020 Mathieu Othacehe <m.othacehe@gmail.com> |
771c5e15 | 5 | ;;; Copyright © 2020 Florian Pelz <pelzflorian@pelzflorian.de> |
7a45b5d5 | 6 | ;;; Copyright © 2020 Lars-Dominik Braun <ldb@leibniz-psychology.org> |
77d3cf08 | 7 | ;;; |
233e7676 | 8 | ;;; This file is part of GNU Guix. |
77d3cf08 | 9 | ;;; |
233e7676 | 10 | ;;; GNU Guix is free software; you can redistribute it and/or modify it |
77d3cf08 LC |
11 | ;;; under the terms of the GNU General Public License as published by |
12 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
13 | ;;; your option) any later version. | |
14 | ;;; | |
233e7676 | 15 | ;;; GNU Guix is distributed in the hope that it will be useful, but |
77d3cf08 LC |
16 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;;; GNU General Public License for more details. | |
19 | ;;; | |
20 | ;;; You should have received a copy of the GNU General Public License | |
233e7676 | 21 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. |
77d3cf08 LC |
22 | |
23 | (define-module (guix store) | |
82058eff | 24 | #:use-module (guix utils) |
d8eea3d2 | 25 | #:use-module (guix config) |
de9fbe9c | 26 | #:use-module (guix deprecation) |
f9704f17 | 27 | #:use-module (guix memoization) |
0f41c26f | 28 | #:use-module (guix serialization) |
e87f0591 | 29 | #:use-module (guix monads) |
c6080c32 | 30 | #:use-module (guix records) |
4c0c4db0 | 31 | #:use-module (guix base16) |
cd041b26 | 32 | #:use-module (guix base32) |
ca719424 | 33 | #:use-module (gcrypt hash) |
03870da8 | 34 | #:use-module (guix profiling) |
b0a6a971 | 35 | #:autoload (guix build syscalls) (terminal-columns) |
77d3cf08 | 36 | #:use-module (rnrs bytevectors) |
2535635f | 37 | #:use-module (ice-9 binary-ports) |
c6080c32 | 38 | #:use-module ((ice-9 control) #:select (let/ec)) |
77d3cf08 LC |
39 | #:use-module (srfi srfi-1) |
40 | #:use-module (srfi srfi-9) | |
bf8e7fc5 | 41 | #:use-module (srfi srfi-9 gnu) |
6581ec9a | 42 | #:use-module (srfi srfi-11) |
77d3cf08 | 43 | #:use-module (srfi srfi-26) |
e87088c9 LC |
44 | #:use-module (srfi srfi-34) |
45 | #:use-module (srfi srfi-35) | |
26bbbb95 | 46 | #:use-module (srfi srfi-39) |
77d3cf08 | 47 | #:use-module (ice-9 match) |
3f1e6939 | 48 | #:use-module (ice-9 vlist) |
6bfec3ed | 49 | #:use-module (ice-9 popen) |
b100a704 | 50 | #:use-module (ice-9 threads) |
1f3ea898 | 51 | #:use-module (ice-9 format) |
1397b422 LC |
52 | #:use-module (web uri) |
53 | #:export (%daemon-socket-uri | |
a9d2a105 | 54 | %gc-roots-directory |
f8a8e0fe | 55 | %default-substitute-urls |
9fd72fb1 | 56 | |
de9fbe9c LC |
57 | store-connection? |
58 | store-connection-version | |
59 | store-connection-major-version | |
60 | store-connection-minor-version | |
61 | store-connection-socket | |
62 | ||
63 | ;; Deprecated forms for 'store-connection'. | |
9fd72fb1 | 64 | nix-server? |
dc0f74e5 | 65 | nix-server-version |
77d3cf08 LC |
66 | nix-server-major-version |
67 | nix-server-minor-version | |
68 | nix-server-socket | |
de9fbe9c | 69 | |
dc0f74e5 | 70 | current-store-protocol-version ;for internal use |
c6080c32 | 71 | mcached |
77d3cf08 | 72 | |
f9e8a123 LC |
73 | &store-error store-error? |
74 | &store-connection-error store-connection-error? | |
75 | store-connection-error-file | |
76 | store-connection-error-code | |
77 | &store-protocol-error store-protocol-error? | |
78 | store-protocol-error-message | |
79 | store-protocol-error-status | |
80 | ||
81 | ;; Deprecated forms for '&store-error' et al. | |
e87088c9 | 82 | &nix-error nix-error? |
ef86c39f LC |
83 | &nix-connection-error nix-connection-error? |
84 | nix-connection-error-file | |
85 | nix-connection-error-code | |
e87088c9 LC |
86 | &nix-protocol-error nix-protocol-error? |
87 | nix-protocol-error-message | |
88 | nix-protocol-error-status | |
89 | ||
26bbbb95 | 90 | hash-algo |
07e70f48 | 91 | build-mode |
26bbbb95 | 92 | |
77d3cf08 | 93 | open-connection |
2f608c14 | 94 | port->connection |
3abaf0c4 | 95 | close-connection |
ce4a4829 | 96 | with-store |
77d3cf08 | 97 | set-build-options |
4f740b67 | 98 | set-build-options* |
31ef99a8 | 99 | valid-path? |
82058eff | 100 | query-path-hash |
11e7a6cf | 101 | hash-part->path |
533d1768 | 102 | query-path-info |
0d268c5d | 103 | add-data-to-store |
77d3cf08 LC |
104 | add-text-to-store |
105 | add-to-store | |
7f11efba | 106 | add-file-tree-to-store |
68dbd5c9 | 107 | file-mapping->tree |
f3a42251 | 108 | binary-file |
041b340d | 109 | with-build-handler |
c40bf581 LC |
110 | map/accumulate-builds |
111 | mapm/accumulate-builds | |
abac874b LC |
112 | build-things |
113 | build | |
16748d80 LC |
114 | query-failed-paths |
115 | clear-failed-paths | |
3259877d | 116 | add-temp-root |
34811f02 | 117 | add-indirect-root |
a9d2a105 LC |
118 | add-permanent-root |
119 | remove-permanent-root | |
26bbbb95 | 120 | |
0f3d2504 LC |
121 | substitutable? |
122 | substitutable-path | |
123 | substitutable-deriver | |
124 | substitutable-references | |
125 | substitutable-download-size | |
126 | substitutable-nar-size | |
127 | has-substitutes? | |
128 | substitutable-paths | |
129 | substitutable-path-info | |
130 | ||
533d1768 DT |
131 | path-info? |
132 | path-info-deriver | |
133 | path-info-hash | |
134 | path-info-references | |
135 | path-info-registration-time | |
136 | path-info-nar-size | |
137 | ||
f9aefa2d | 138 | built-in-builders |
fae31edc | 139 | references |
c70cf1a7 | 140 | references/cached |
6581ec9a | 141 | references/substitutes |
e74f64b9 | 142 | references* |
0744a9f0 | 143 | query-path-info* |
3f1e6939 | 144 | requisites |
fae31edc | 145 | referrers |
e3fd0ce6 | 146 | optimize-store |
c63d9403 | 147 | verify-store |
50add477 | 148 | topologically-sorted |
fae31edc LC |
149 | valid-derivers |
150 | query-derivation-outputs | |
3259877d LC |
151 | live-paths |
152 | dead-paths | |
153 | collect-garbage | |
154 | delete-paths | |
526382ff LC |
155 | import-paths |
156 | export-paths | |
3259877d | 157 | |
dcee50c1 LC |
158 | current-build-output-port |
159 | ||
e87f0591 LC |
160 | %store-monad |
161 | store-bind | |
162 | store-return | |
163 | store-lift | |
023d9892 | 164 | store-lower |
e87f0591 LC |
165 | run-with-store |
166 | %guile-for-build | |
98a7b528 LC |
167 | current-system |
168 | set-current-system | |
91be09de | 169 | current-target-system |
fdae0fa5 | 170 | set-current-target |
e87f0591 LC |
171 | text-file |
172 | interned-file | |
7f11efba | 173 | interned-file-tree |
e87f0591 | 174 | |
26bbbb95 | 175 | %store-prefix |
cd041b26 LC |
176 | store-path |
177 | output-path | |
178 | fixed-output-path | |
26bbbb95 | 179 | store-path? |
9336e5b5 | 180 | direct-store-path? |
e3d74106 | 181 | derivation-path? |
771c5e15 | 182 | store-path-base |
2c6ab6cc | 183 | store-path-package-name |
eddd4077 | 184 | store-path-hash-part |
cdb5b075 | 185 | direct-store-path |
fb94d82b | 186 | derivation-log-file |
eddd4077 | 187 | log-file)) |
77d3cf08 | 188 | |
6ef61cc4 | 189 | (define %protocol-version #x163) |
77d3cf08 | 190 | |
d66b704b LC |
191 | (define %worker-magic-1 #x6e697863) ; "nixc" |
192 | (define %worker-magic-2 #x6478696f) ; "dxio" | |
77d3cf08 LC |
193 | |
194 | (define (protocol-major magic) | |
195 | (logand magic #xff00)) | |
196 | (define (protocol-minor magic) | |
197 | (logand magic #x00ff)) | |
dc0f74e5 LC |
198 | (define (protocol-version major minor) |
199 | (logior major minor)) | |
77d3cf08 LC |
200 | |
201 | (define-syntax define-enumerate-type | |
202 | (syntax-rules () | |
203 | ((_ name->int (name id) ...) | |
204 | (define-syntax name->int | |
205 | (syntax-rules (name ...) | |
206 | ((_ name) id) ...))))) | |
207 | ||
208 | (define-enumerate-type operation-id | |
209 | ;; operation numbers from worker-protocol.hh | |
210 | (quit 0) | |
211 | (valid-path? 1) | |
212 | (has-substitutes? 3) | |
213 | (query-path-hash 4) | |
214 | (query-references 5) | |
215 | (query-referrers 6) | |
216 | (add-to-store 7) | |
217 | (add-text-to-store 8) | |
abac874b | 218 | (build-things 9) |
77d3cf08 LC |
219 | (ensure-path 10) |
220 | (add-temp-root 11) | |
221 | (add-indirect-root 12) | |
222 | (sync-with-gc 13) | |
223 | (find-roots 14) | |
224 | (export-path 16) | |
225 | (query-deriver 18) | |
226 | (set-options 19) | |
227 | (collect-garbage 20) | |
63193ebf | 228 | ;;(query-substitutable-path-info 21) ; obsolete as of #x10c |
77d3cf08 | 229 | (query-derivation-outputs 22) |
63193ebf | 230 | (query-all-valid-paths 23) |
77d3cf08 LC |
231 | (query-failed-paths 24) |
232 | (clear-failed-paths 25) | |
233 | (query-path-info 26) | |
234 | (import-paths 27) | |
63193ebf LC |
235 | (query-derivation-output-names 28) |
236 | (query-path-from-hash-part 29) | |
237 | (query-substitutable-path-infos 30) | |
238 | (query-valid-paths 31) | |
fae31edc | 239 | (query-substitutable-paths 32) |
e3fd0ce6 | 240 | (query-valid-derivers 33) |
c63d9403 | 241 | (optimize-store 34) |
f9aefa2d LC |
242 | (verify-store 35) |
243 | (built-in-builders 80)) | |
77d3cf08 LC |
244 | |
245 | (define-enumerate-type hash-algo | |
246 | ;; hash.hh | |
247 | (md5 1) | |
248 | (sha1 2) | |
249 | (sha256 3)) | |
250 | ||
07e70f48 LC |
251 | (define-enumerate-type build-mode |
252 | ;; store-api.hh | |
253 | (normal 0) | |
254 | (repair 1) | |
255 | (check 2)) | |
256 | ||
3259877d LC |
257 | (define-enumerate-type gc-action |
258 | ;; store-api.hh | |
259 | (return-live 0) | |
260 | (return-dead 1) | |
261 | (delete-dead 2) | |
262 | (delete-specific 3)) | |
263 | ||
77d3cf08 | 264 | (define %default-socket-path |
80d0447c | 265 | (string-append %state-directory "/daemon-socket/socket")) |
77d3cf08 | 266 | |
1397b422 LC |
267 | (define %daemon-socket-uri |
268 | ;; URI or file name of the socket the daemon listens too. | |
9fd72fb1 LC |
269 | (make-parameter (or (getenv "GUIX_DAEMON_SOCKET") |
270 | %default-socket-path))) | |
271 | ||
272 | ||
77d3cf08 | 273 | \f |
0f3d2504 LC |
274 | ;; Information about a substitutable store path. |
275 | (define-record-type <substitutable> | |
276 | (substitutable path deriver refs dl-size nar-size) | |
277 | substitutable? | |
278 | (path substitutable-path) | |
279 | (deriver substitutable-deriver) | |
280 | (refs substitutable-references) | |
281 | (dl-size substitutable-download-size) | |
282 | (nar-size substitutable-nar-size)) | |
283 | ||
284 | (define (read-substitutable-path-list p) | |
285 | (let loop ((len (read-int p)) | |
286 | (result '())) | |
287 | (if (zero? len) | |
288 | (reverse result) | |
289 | (let ((path (read-store-path p)) | |
290 | (deriver (read-store-path p)) | |
291 | (refs (read-store-path-list p)) | |
292 | (dl-size (read-long-long p)) | |
293 | (nar-size (read-long-long p))) | |
294 | (loop (- len 1) | |
295 | (cons (substitutable path deriver refs dl-size nar-size) | |
296 | result)))))) | |
297 | ||
533d1768 DT |
298 | ;; Information about a store path. |
299 | (define-record-type <path-info> | |
300 | (path-info deriver hash references registration-time nar-size) | |
301 | path-info? | |
22572d56 | 302 | (deriver path-info-deriver) ;string | #f |
533d1768 DT |
303 | (hash path-info-hash) |
304 | (references path-info-references) | |
305 | (registration-time path-info-registration-time) | |
306 | (nar-size path-info-nar-size)) | |
307 | ||
308 | (define (read-path-info p) | |
22572d56 LC |
309 | (let ((deriver (match (read-store-path p) |
310 | ("" #f) | |
311 | (x x))) | |
533d1768 DT |
312 | (hash (base16-string->bytevector (read-string p))) |
313 | (refs (read-store-path-list p)) | |
314 | (registration-time (read-int p)) | |
315 | (nar-size (read-long-long p))) | |
316 | (path-info deriver hash refs registration-time nar-size))) | |
317 | ||
77d3cf08 | 318 | (define-syntax write-arg |
0d268c5d LC |
319 | (syntax-rules (integer boolean bytevector |
320 | string string-list string-pairs | |
3259877d | 321 | store-path store-path-list base16) |
77d3cf08 LC |
322 | ((_ integer arg p) |
323 | (write-int arg p)) | |
324 | ((_ boolean arg p) | |
325 | (write-int (if arg 1 0) p)) | |
0d268c5d LC |
326 | ((_ bytevector arg p) |
327 | (write-bytevector arg p)) | |
77d3cf08 LC |
328 | ((_ string arg p) |
329 | (write-string arg p)) | |
330 | ((_ string-list arg p) | |
82058eff | 331 | (write-string-list arg p)) |
6c20d1d0 LC |
332 | ((_ string-pairs arg p) |
333 | (write-string-pairs arg p)) | |
3259877d LC |
334 | ((_ store-path arg p) |
335 | (write-store-path arg p)) | |
336 | ((_ store-path-list arg p) | |
337 | (write-store-path-list arg p)) | |
82058eff LC |
338 | ((_ base16 arg p) |
339 | (write-string (bytevector->base16-string arg) p)))) | |
77d3cf08 LC |
340 | |
341 | (define-syntax read-arg | |
f9aefa2d | 342 | (syntax-rules (integer boolean string store-path store-path-list string-list |
533d1768 | 343 | substitutable-path-list path-info base16) |
77d3cf08 LC |
344 | ((_ integer p) |
345 | (read-int p)) | |
346 | ((_ boolean p) | |
347 | (not (zero? (read-int p)))) | |
348 | ((_ string p) | |
349 | (read-string p)) | |
350 | ((_ store-path p) | |
82058eff | 351 | (read-store-path p)) |
3259877d LC |
352 | ((_ store-path-list p) |
353 | (read-store-path-list p)) | |
f9aefa2d LC |
354 | ((_ string-list p) |
355 | (read-string-list p)) | |
0f3d2504 LC |
356 | ((_ substitutable-path-list p) |
357 | (read-substitutable-path-list p)) | |
533d1768 DT |
358 | ((_ path-info p) |
359 | (read-path-info p)) | |
0f3d2504 | 360 | ((_ base16 p) |
82058eff | 361 | (base16-string->bytevector (read-string p))))) |
77d3cf08 LC |
362 | |
363 | \f | |
364 | ;; remote-store.cc | |
365 | ||
de9fbe9c LC |
366 | (define-record-type* <store-connection> store-connection %make-store-connection |
367 | store-connection? | |
368 | (socket store-connection-socket) | |
369 | (major store-connection-major-version) | |
370 | (minor store-connection-minor-version) | |
2c3f47ee | 371 | |
de9fbe9c LC |
372 | (buffer store-connection-output-port) ;output port |
373 | (flush store-connection-flush-output) ;thunk | |
e037e9db | 374 | |
2c3f47ee LC |
375 | ;; Caches. We keep them per-connection, because store paths build |
376 | ;; during the session are temporary GC roots kept for the duration of | |
377 | ;; the session. | |
de9fbe9c LC |
378 | (ats-cache store-connection-add-to-store-cache) |
379 | (atts-cache store-connection-add-text-to-store-cache) | |
380 | (object-cache store-connection-object-cache | |
3961edf2 LC |
381 | (default vlist-null)) ;vhash |
382 | (built-in-builders store-connection-built-in-builders | |
383 | (default (delay '())))) ;promise | |
77d3cf08 | 384 | |
de9fbe9c | 385 | (set-record-type-printer! <store-connection> |
bf8e7fc5 | 386 | (lambda (obj port) |
de9fbe9c LC |
387 | (format port "#<store-connection ~a.~a ~a>" |
388 | (store-connection-major-version obj) | |
389 | (store-connection-minor-version obj) | |
bf8e7fc5 LC |
390 | (number->string (object-address obj) |
391 | 16)))) | |
392 | ||
de9fbe9c LC |
393 | (define-deprecated/alias nix-server? store-connection?) |
394 | (define-deprecated/alias nix-server-major-version | |
395 | store-connection-major-version) | |
396 | (define-deprecated/alias nix-server-minor-version | |
397 | store-connection-minor-version) | |
398 | (define-deprecated/alias nix-server-socket store-connection-socket) | |
399 | ||
400 | ||
f9e8a123 LC |
401 | (define-condition-type &store-error &error |
402 | store-error?) | |
e87088c9 | 403 | |
f9e8a123 LC |
404 | (define-condition-type &store-connection-error &store-error |
405 | store-connection-error? | |
406 | (file store-connection-error-file) | |
407 | (errno store-connection-error-code)) | |
408 | ||
409 | (define-condition-type &store-protocol-error &store-error | |
410 | store-protocol-error? | |
411 | (message store-protocol-error-message) | |
412 | (status store-protocol-error-status)) | |
413 | ||
414 | (define-deprecated/alias &nix-error &store-error) | |
415 | (define-deprecated/alias nix-error? store-error?) | |
416 | (define-deprecated/alias &nix-connection-error &store-connection-error) | |
417 | (define-deprecated/alias nix-connection-error? store-connection-error?) | |
418 | (define-deprecated/alias nix-connection-error-file | |
419 | store-connection-error-file) | |
420 | (define-deprecated/alias nix-connection-error-code | |
421 | store-connection-error-code) | |
422 | (define-deprecated/alias &nix-protocol-error &store-protocol-error) | |
423 | (define-deprecated/alias nix-protocol-error? store-protocol-error?) | |
424 | (define-deprecated/alias nix-protocol-error-message | |
425 | store-protocol-error-message) | |
426 | (define-deprecated/alias nix-protocol-error-status | |
427 | store-protocol-error-status) | |
ef86c39f | 428 | |
e87088c9 | 429 | |
3b5cd17a LC |
430 | (define-syntax-rule (system-error-to-connection-error file exp ...) |
431 | "Catch 'system-error' exceptions and translate them to | |
f9e8a123 | 432 | '&store-connection-error'." |
3b5cd17a LC |
433 | (catch 'system-error |
434 | (lambda () | |
435 | exp ...) | |
436 | (lambda args | |
437 | (let ((errno (system-error-errno args))) | |
f9e8a123 | 438 | (raise (condition (&store-connection-error |
3b5cd17a LC |
439 | (file file) |
440 | (errno errno)))))))) | |
441 | ||
6230d6f0 LC |
442 | (define (open-unix-domain-socket file) |
443 | "Connect to the Unix-domain socket at FILE and return it. Raise a | |
f9e8a123 | 444 | '&store-connection-error' upon error." |
77d3cf08 LC |
445 | (let ((s (with-fluids ((%default-port-encoding #f)) |
446 | ;; This trick allows use of the `scm_c_read' optimization. | |
447 | (socket PF_UNIX SOCK_STREAM 0))) | |
448 | (a (make-socket-address PF_UNIX file))) | |
df1fab58 | 449 | |
3b5cd17a LC |
450 | (system-error-to-connection-error file |
451 | (connect s a) | |
452 | s))) | |
ef86c39f | 453 | |
5df1395a LC |
454 | (define %default-guix-port |
455 | ;; Default port when connecting to a daemon over TCP/IP. | |
456 | 44146) | |
457 | ||
3dff90ce LC |
458 | (define (open-inet-socket host port) |
459 | "Connect to the Unix-domain socket at HOST:PORT and return it. Raise a | |
f9e8a123 | 460 | '&store-connection-error' upon error." |
3dff90ce LC |
461 | (let ((sock (with-fluids ((%default-port-encoding #f)) |
462 | ;; This trick allows use of the `scm_c_read' optimization. | |
463 | (socket PF_UNIX SOCK_STREAM 0)))) | |
464 | (define addresses | |
465 | (getaddrinfo host | |
466 | (if (number? port) (number->string port) port) | |
467 | (if (number? port) | |
468 | (logior AI_ADDRCONFIG AI_NUMERICSERV) | |
7ae97a4c LC |
469 | AI_ADDRCONFIG) |
470 | 0 ;any address family | |
471 | SOCK_STREAM)) ;TCP only | |
3dff90ce LC |
472 | |
473 | (let loop ((addresses addresses)) | |
474 | (match addresses | |
475 | ((ai rest ...) | |
476 | (let ((s (socket (addrinfo:fam ai) | |
477 | ;; TCP/IP only | |
478 | SOCK_STREAM IPPROTO_IP))) | |
479 | ||
480 | (catch 'system-error | |
481 | (lambda () | |
482 | (connect s (addrinfo:addr ai)) | |
950d51c9 LC |
483 | |
484 | ;; Setting this option makes a dramatic difference because it | |
485 | ;; avoids the "ACK delay" on our RPC messages. | |
486 | (setsockopt s IPPROTO_TCP TCP_NODELAY 1) | |
3dff90ce LC |
487 | s) |
488 | (lambda args | |
489 | ;; Connection failed, so try one of the other addresses. | |
490 | (close s) | |
491 | (if (null? rest) | |
f9e8a123 | 492 | (raise (condition (&store-connection-error |
3dff90ce LC |
493 | (file host) |
494 | (errno (system-error-errno args))))) | |
495 | (loop rest)))))))))) | |
496 | ||
1397b422 LC |
497 | (define (connect-to-daemon uri) |
498 | "Connect to the daemon at URI, a string that may be an actual URI or a file | |
499 | name." | |
285f63e8 | 500 | (define (not-supported) |
f9e8a123 | 501 | (raise (condition (&store-connection-error |
285f63e8 LC |
502 | (file uri) |
503 | (errno ENOTSUP))))) | |
504 | ||
1397b422 LC |
505 | (define connect |
506 | (match (string->uri uri) | |
507 | (#f ;URI is a file name | |
508 | open-unix-domain-socket) | |
509 | ((? uri? uri) | |
510 | (match (uri-scheme uri) | |
511 | ((or #f 'file 'unix) | |
512 | (lambda (_) | |
513 | (open-unix-domain-socket (uri-path uri)))) | |
3dff90ce LC |
514 | ('guix |
515 | (lambda (_) | |
5df1395a LC |
516 | (open-inet-socket (uri-host uri) |
517 | (or (uri-port uri) %default-guix-port)))) | |
285f63e8 LC |
518 | ((? symbol? scheme) |
519 | ;; Try to dynamically load a module for SCHEME. | |
520 | ;; XXX: Errors are swallowed. | |
521 | (match (false-if-exception | |
522 | (resolve-interface `(guix store ,scheme))) | |
523 | ((? module? module) | |
524 | (match (false-if-exception | |
525 | (module-ref module 'connect-to-daemon)) | |
526 | ((? procedure? connect) | |
527 | (lambda (_) | |
528 | (connect uri))) | |
529 | (x (not-supported)))) | |
530 | (#f (not-supported)))) | |
1397b422 | 531 | (x |
285f63e8 | 532 | (not-supported)))))) |
1397b422 LC |
533 | |
534 | (connect uri)) | |
535 | ||
536 | (define* (open-connection #:optional (uri (%daemon-socket-uri)) | |
6230d6f0 | 537 | #:key port (reserve-space? #t) cpu-affinity) |
1397b422 LC |
538 | "Connect to the daemon at URI (a string), or, if PORT is not #f, use it as |
539 | the I/O port over which to communicate to a build daemon. | |
6230d6f0 LC |
540 | |
541 | When RESERVE-SPACE? is true, instruct it to reserve a little bit of extra | |
542 | space on the file system so that the garbage collector can still operate, | |
543 | should the disk become full. When CPU-AFFINITY is true, it must be an integer | |
544 | corresponding to an OS-level CPU number to which the daemon's worker process | |
545 | for this connection will be pinned. Return a server object." | |
13d5e8da LC |
546 | (guard (c ((nar-error? c) |
547 | ;; One of the 'write-' or 'read-' calls below failed, but this is | |
548 | ;; really a connection error. | |
549 | (raise (condition | |
f9e8a123 LC |
550 | (&store-connection-error (file (or port uri)) |
551 | (errno EPROTO)) | |
13d5e8da | 552 | (&message (message "build daemon handshake failed")))))) |
e037e9db LC |
553 | (let*-values (((port) |
554 | (or port (connect-to-daemon uri))) | |
555 | ((output flush) | |
556 | (buffering-output-port port | |
557 | (make-bytevector 8192)))) | |
13d5e8da LC |
558 | (write-int %worker-magic-1 port) |
559 | (let ((r (read-int port))) | |
641631c1 | 560 | (and (= r %worker-magic-2) |
13d5e8da | 561 | (let ((v (read-int port))) |
641631c1 LC |
562 | (and (= (protocol-major %protocol-version) |
563 | (protocol-major v)) | |
13d5e8da LC |
564 | (begin |
565 | (write-int %protocol-version port) | |
566 | (when (>= (protocol-minor v) 14) | |
567 | (write-int (if cpu-affinity 1 0) port) | |
568 | (when cpu-affinity | |
569 | (write-int cpu-affinity port))) | |
570 | (when (>= (protocol-minor v) 11) | |
571 | (write-int (if reserve-space? 1 0) port)) | |
3961edf2 LC |
572 | (letrec* ((built-in-builders |
573 | (delay (%built-in-builders conn))) | |
574 | (conn | |
575 | (%make-store-connection port | |
576 | (protocol-major v) | |
577 | (protocol-minor v) | |
578 | output flush | |
579 | (make-hash-table 100) | |
580 | (make-hash-table 100) | |
581 | vlist-null | |
582 | built-in-builders))) | |
13d5e8da LC |
583 | (let loop ((done? (process-stderr conn))) |
584 | (or done? (process-stderr conn))) | |
585 | conn))))))))) | |
77d3cf08 | 586 | |
2f608c14 LC |
587 | (define* (port->connection port |
588 | #:key (version %protocol-version)) | |
589 | "Assimilate PORT, an input/output port, and return a connection to the | |
590 | daemon, assuming the given protocol VERSION. | |
591 | ||
592 | Warning: this procedure assumes that the initial handshake with the daemon has | |
593 | already taken place on PORT and that we're just continuing on this established | |
594 | connection. Use with care." | |
595 | (let-values (((output flush) | |
596 | (buffering-output-port port (make-bytevector 8192)))) | |
3961edf2 LC |
597 | (define connection |
598 | (%make-store-connection port | |
599 | (protocol-major version) | |
600 | (protocol-minor version) | |
601 | output flush | |
602 | (make-hash-table 100) | |
603 | (make-hash-table 100) | |
604 | vlist-null | |
605 | (delay (%built-in-builders connection)))) | |
606 | ||
607 | connection)) | |
de9fbe9c LC |
608 | |
609 | (define (store-connection-version store) | |
dc0f74e5 | 610 | "Return the protocol version of STORE as an integer." |
de9fbe9c LC |
611 | (protocol-version (store-connection-major-version store) |
612 | (store-connection-minor-version store))) | |
613 | ||
614 | (define-deprecated/alias nix-server-version store-connection-version) | |
dc0f74e5 | 615 | |
e037e9db LC |
616 | (define (write-buffered-output server) |
617 | "Flush SERVER's output port." | |
de9fbe9c LC |
618 | (force-output (store-connection-output-port server)) |
619 | ((store-connection-flush-output server))) | |
e037e9db | 620 | |
3abaf0c4 LC |
621 | (define (close-connection server) |
622 | "Close the connection to SERVER." | |
de9fbe9c | 623 | (close (store-connection-socket server))) |
3abaf0c4 | 624 | |
95207e70 LC |
625 | (define (call-with-store proc) |
626 | "Call PROC with an open store connection." | |
ce4a4829 | 627 | (let ((store (open-connection))) |
376ba0ce LC |
628 | (define (thunk) |
629 | (parameterize ((current-store-protocol-version | |
630 | (store-connection-version store))) | |
3d9ea605 LC |
631 | (call-with-values (lambda () (proc store)) |
632 | (lambda results | |
633 | (close-connection store) | |
634 | (apply values results))))) | |
376ba0ce LC |
635 | |
636 | (cond-expand | |
637 | (guile-3 | |
638 | (with-exception-handler (lambda (exception) | |
639 | (close-connection store) | |
640 | (raise-exception exception)) | |
641 | thunk)) | |
642 | (else ;Guile 2.2 | |
643 | (catch #t | |
644 | thunk | |
645 | (lambda (key . args) | |
646 | (close-connection store) | |
647 | (apply throw key args))))))) | |
ce4a4829 | 648 | |
95207e70 LC |
649 | (define-syntax-rule (with-store store exp ...) |
650 | "Bind STORE to an open connection to the store and evaluate EXPs; | |
651 | automatically close the store when the dynamic extent of EXP is left." | |
652 | (call-with-store (lambda (store) exp ...))) | |
653 | ||
dc0f74e5 LC |
654 | (define current-store-protocol-version |
655 | ;; Protocol version of the store currently used. XXX: This is a hack to | |
656 | ;; communicate the protocol version to the build output port. It's a hack | |
657 | ;; because it could be inaccurrate, for instance if there's code that | |
658 | ;; manipulates several store connections at once; it works well for the | |
659 | ;; purposes of (guix status) though. | |
660 | (make-parameter #f)) | |
661 | ||
dcee50c1 LC |
662 | (define current-build-output-port |
663 | ;; The port where build output is sent. | |
664 | (make-parameter (current-error-port))) | |
665 | ||
526382ff LC |
666 | (define* (dump-port in out |
667 | #:optional len | |
668 | #:key (buffer-size 16384)) | |
669 | "Read LEN bytes from IN (or as much as possible if LEN is #f) and write it | |
670 | to OUT, using chunks of BUFFER-SIZE bytes." | |
671 | (define buffer | |
672 | (make-bytevector buffer-size)) | |
673 | ||
674 | (let loop ((total 0) | |
675 | (bytes (get-bytevector-n! in buffer 0 | |
676 | (if len | |
677 | (min len buffer-size) | |
678 | buffer-size)))) | |
679 | (or (eof-object? bytes) | |
680 | (and len (= total len)) | |
681 | (let ((total (+ total bytes))) | |
682 | (put-bytevector out buffer 0 bytes) | |
683 | (loop total | |
684 | (get-bytevector-n! in buffer 0 | |
685 | (if len | |
686 | (min (- len total) buffer-size) | |
687 | buffer-size))))))) | |
688 | ||
d28869af LC |
689 | (define %newlines |
690 | ;; Newline characters triggering a flush of 'current-build-output-port'. | |
76832d34 | 691 | ;; Unlike Guile's 'line, we flush upon #\return so that progress reports |
d28869af LC |
692 | ;; that use that trick are correctly displayed. |
693 | (char-set #\newline #\return)) | |
694 | ||
526382ff | 695 | (define* (process-stderr server #:optional user-port) |
dcee50c1 LC |
696 | "Read standard output and standard error from SERVER, writing it to |
697 | CURRENT-BUILD-OUTPUT-PORT. Return #t when SERVER is done sending data, and | |
698 | #f otherwise; in the latter case, the caller should call `process-stderr' | |
bbdb3ffa LC |
699 | again until #t is returned or an error is raised. |
700 | ||
701 | Since the build process's output cannot be assumed to be UTF-8, we | |
702 | conservatively consider it to be Latin-1, thereby avoiding possible | |
703 | encoding conversion errors." | |
77d3cf08 | 704 | (define p |
de9fbe9c | 705 | (store-connection-socket server)) |
77d3cf08 LC |
706 | |
707 | ;; magic cookies from worker-protocol.hh | |
5674a3fd LC |
708 | (define %stderr-next #x6f6c6d67) ; "olmg", build log |
709 | (define %stderr-read #x64617461) ; "data", data needed from source | |
710 | (define %stderr-write #x64617416) ; "dat\x16", data for sink | |
711 | (define %stderr-last #x616c7473) ; "alts", we're done | |
712 | (define %stderr-error #x63787470) ; "cxtp", error reporting | |
77d3cf08 LC |
713 | |
714 | (let ((k (read-int p))) | |
715 | (cond ((= k %stderr-write) | |
526382ff LC |
716 | ;; Write a byte stream to USER-PORT. |
717 | (let* ((len (read-int p)) | |
718 | (m (modulo len 8))) | |
6374633b LC |
719 | (dump-port p user-port len |
720 | #:buffer-size (if (<= len 16384) 16384 65536)) | |
526382ff LC |
721 | (unless (zero? m) |
722 | ;; Consume padding, as for strings. | |
723 | (get-bytevector-n p (- 8 m)))) | |
dcee50c1 | 724 | #f) |
77d3cf08 | 725 | ((= k %stderr-read) |
526382ff | 726 | ;; Read a byte stream from USER-PORT. |
5895f244 LC |
727 | ;; Note: Avoid 'get-bytevector-n' to work around |
728 | ;; <http://bugs.gnu.org/17591> in Guile up to 2.0.11. | |
526382ff | 729 | (let* ((max-len (read-int p)) |
5895f244 LC |
730 | (data (make-bytevector max-len)) |
731 | (len (get-bytevector-n! user-port data 0 max-len))) | |
39d1e965 | 732 | (write-bytevector data p len) |
dcee50c1 | 733 | #f)) |
77d3cf08 | 734 | ((= k %stderr-next) |
ce72c780 LC |
735 | ;; Log a string. Build logs are usually UTF-8-encoded, but they |
736 | ;; may also contain arbitrary byte sequences that should not cause | |
737 | ;; this to fail. Thus, use the permissive | |
738 | ;; 'read-maybe-utf8-string'. | |
739 | (let ((s (read-maybe-utf8-string p))) | |
dcee50c1 | 740 | (display s (current-build-output-port)) |
d28869af | 741 | (when (string-any %newlines s) |
2535635f | 742 | (force-output (current-build-output-port))) |
dcee50c1 | 743 | #f)) |
77d3cf08 | 744 | ((= k %stderr-error) |
526382ff | 745 | ;; Report an error. |
ce72c780 | 746 | (let ((error (read-maybe-utf8-string p)) |
0ff3e3aa LC |
747 | ;; Currently the daemon fails to send a status code for early |
748 | ;; errors like DB schema version mismatches, so check for EOF. | |
de9fbe9c | 749 | (status (if (and (>= (store-connection-minor-version server) 8) |
0ff3e3aa | 750 | (not (eof-object? (lookahead-u8 p)))) |
77d3cf08 LC |
751 | (read-int p) |
752 | 1))) | |
f9e8a123 | 753 | (raise (condition (&store-protocol-error |
e87088c9 LC |
754 | (message error) |
755 | (status status)))))) | |
77d3cf08 | 756 | ((= k %stderr-last) |
dcee50c1 | 757 | ;; The daemon is done (see `stopWork' in `nix-worker.cc'.) |
77d3cf08 LC |
758 | #t) |
759 | (else | |
f9e8a123 | 760 | (raise (condition (&store-protocol-error |
e87088c9 LC |
761 | (message "invalid error code") |
762 | (status k)))))))) | |
77d3cf08 | 763 | |
4d581220 | 764 | (define %default-substitute-urls |
d70533cb LC |
765 | ;; Default list of substituters. This is *not* the list baked in |
766 | ;; 'guix-daemon', but it is used by 'guix-service-type' and and a couple of | |
767 | ;; clients ('guix build --log-file' uses it.) | |
df061d07 LC |
768 | (map (if (false-if-exception (resolve-interface '(gnutls))) |
769 | (cut string-append "https://" <>) | |
770 | (cut string-append "http://" <>)) | |
757e633d | 771 | '("ci.guix.gnu.org"))) |
4d581220 | 772 | |
81c580c8 LC |
773 | (define (current-user-name) |
774 | "Return the name of the calling user." | |
775 | (catch #t | |
776 | (lambda () | |
777 | (passwd:name (getpwuid (getuid)))) | |
778 | (lambda _ | |
779 | (getenv "USER")))) | |
780 | ||
77d3cf08 | 781 | (define* (set-build-options server |
c3eb878f | 782 | #:key keep-failed? keep-going? fallback? |
77d3cf08 | 783 | (verbosity 0) |
2fba87ac | 784 | rounds ;number of build rounds |
deac976d | 785 | max-build-jobs |
6c20d1d0 | 786 | timeout |
deac976d | 787 | max-silent-time |
7f44ab48 LC |
788 | (offload? #t) |
789 | (use-build-hook? *unspecified*) ;deprecated | |
77d3cf08 LC |
790 | (build-verbosity 0) |
791 | (log-type 0) | |
e036c31b | 792 | (print-build-trace #t) |
81c580c8 | 793 | (user-name (current-user-name)) |
dc0f74e5 LC |
794 | |
795 | ;; When true, provide machine-readable "build | |
796 | ;; traces" for use by (guix status). Old clients | |
797 | ;; are unable to make sense, which is why it's | |
798 | ;; disabled by default. | |
799 | print-extended-build-trace? | |
800 | ||
6ef61cc4 LC |
801 | ;; When true, the daemon prefixes builder output |
802 | ;; with "@ build-log" traces so we can | |
803 | ;; distinguish it from daemon output, and we can | |
804 | ;; distinguish each builder's output | |
805 | ;; (PRINT-BUILD-TRACE must be true as well.) The | |
806 | ;; latter is particularly useful when | |
807 | ;; MAX-BUILD-JOBS > 1. | |
808 | multiplexed-build-output? | |
809 | ||
deac976d | 810 | build-cores |
63193ebf | 811 | (use-substitutes? #t) |
4d581220 | 812 | |
fb4bf72b LC |
813 | ;; Client-provided substitute URLs. If it is #f, |
814 | ;; the daemon's settings are used. Otherwise, it | |
815 | ;; overrides the daemons settings; see 'guix | |
816 | ;; substitute'. | |
b0a6a971 LC |
817 | (substitute-urls #f) |
818 | ||
819 | ;; Number of columns in the client's terminal. | |
38f50f49 LC |
820 | (terminal-columns (terminal-columns)) |
821 | ||
822 | ;; Locale of the client. | |
739f6309 | 823 | (locale (false-if-exception (setlocale LC_MESSAGES)))) |
77d3cf08 LC |
824 | ;; Must be called after `open-connection'. |
825 | ||
7a45b5d5 LDB |
826 | (define buffered |
827 | (store-connection-output-port server)) | |
77d3cf08 | 828 | |
7f44ab48 LC |
829 | (unless (unspecified? use-build-hook?) |
830 | (warn-about-deprecation #:use-build-hook? #f | |
831 | #:replacement #:offload?)) | |
832 | ||
77d3cf08 | 833 | (let-syntax ((send (syntax-rules () |
e036c31b LC |
834 | ((_ (type option) ...) |
835 | (begin | |
7a45b5d5 | 836 | (write-arg type option buffered) |
e036c31b | 837 | ...))))) |
7a45b5d5 | 838 | (write-int (operation-id set-options) buffered) |
e036c31b | 839 | (send (boolean keep-failed?) (boolean keep-going?) |
deac976d | 840 | (boolean fallback?) (integer verbosity)) |
de9fbe9c | 841 | (when (< (store-connection-minor-version server) #x61) |
deac976d LC |
842 | (let ((max-build-jobs (or max-build-jobs 1)) |
843 | (max-silent-time (or max-silent-time 3600))) | |
844 | (send (integer max-build-jobs) (integer max-silent-time)))) | |
de9fbe9c | 845 | (when (>= (store-connection-minor-version server) 2) |
7f44ab48 LC |
846 | (send (boolean (if (unspecified? use-build-hook?) |
847 | offload? | |
848 | use-build-hook?)))) | |
de9fbe9c | 849 | (when (>= (store-connection-minor-version server) 4) |
371e87d2 LC |
850 | (send (integer build-verbosity) (integer log-type) |
851 | (boolean print-build-trace))) | |
de9fbe9c LC |
852 | (when (and (>= (store-connection-minor-version server) 6) |
853 | (< (store-connection-minor-version server) #x61)) | |
deac976d LC |
854 | (let ((build-cores (or build-cores (current-processor-count)))) |
855 | (send (integer build-cores)))) | |
de9fbe9c | 856 | (when (>= (store-connection-minor-version server) 10) |
371e87d2 | 857 | (send (boolean use-substitutes?))) |
de9fbe9c | 858 | (when (>= (store-connection-minor-version server) 12) |
dc0f74e5 LC |
859 | (let ((pairs `(;; This option is honored by 'guix substitute' et al. |
860 | ,@(if print-build-trace | |
861 | `(("print-extended-build-trace" | |
862 | . ,(if print-extended-build-trace? "1" "0"))) | |
863 | '()) | |
6ef61cc4 LC |
864 | ,@(if multiplexed-build-output? |
865 | `(("multiplexed-build-output" | |
866 | . ,(if multiplexed-build-output? "true" "false"))) | |
867 | '()) | |
dc0f74e5 | 868 | ,@(if timeout |
41c45e78 LC |
869 | `(("build-timeout" . ,(number->string timeout))) |
870 | '()) | |
deac976d LC |
871 | ,@(if max-silent-time |
872 | `(("build-max-silent-time" | |
873 | . ,(number->string max-silent-time))) | |
874 | '()) | |
875 | ,@(if max-build-jobs | |
876 | `(("build-max-jobs" | |
877 | . ,(number->string max-build-jobs))) | |
878 | '()) | |
879 | ,@(if build-cores | |
880 | `(("build-cores" . ,(number->string build-cores))) | |
881 | '()) | |
fb4bf72b LC |
882 | ,@(if substitute-urls |
883 | `(("substitute-urls" | |
884 | . ,(string-join substitute-urls))) | |
2fba87ac LC |
885 | '()) |
886 | ,@(if rounds | |
887 | `(("build-repeat" | |
888 | . ,(number->string (max 0 (1- rounds))))) | |
b0a6a971 | 889 | '()) |
81c580c8 LC |
890 | ,@(if user-name |
891 | `(("user-name" . ,user-name)) | |
892 | '()) | |
b0a6a971 LC |
893 | ,@(if terminal-columns |
894 | `(("terminal-columns" | |
895 | . ,(number->string terminal-columns))) | |
38f50f49 LC |
896 | '()) |
897 | ,@(if locale | |
898 | `(("locale" . ,locale)) | |
fb4bf72b | 899 | '())))) |
f401b1e9 | 900 | (send (string-pairs pairs)))) |
7a45b5d5 | 901 | (write-buffered-output server) |
dcee50c1 LC |
902 | (let loop ((done? (process-stderr server))) |
903 | (or done? (process-stderr server))))) | |
77d3cf08 | 904 | |
e037e9db LC |
905 | (define (buffering-output-port port buffer) |
906 | "Return two value: an output port wrapped around PORT that uses BUFFER (a | |
907 | bytevector) as its internal buffer, and a thunk to flush this output port." | |
908 | ;; Note: In Guile 2.2.2, custom binary output ports already have their own | |
909 | ;; 4K internal buffer. | |
910 | (define size | |
911 | (bytevector-length buffer)) | |
912 | ||
913 | (define total 0) | |
914 | ||
915 | (define (flush) | |
916 | (put-bytevector port buffer 0 total) | |
38098241 | 917 | (force-output port) |
e037e9db LC |
918 | (set! total 0)) |
919 | ||
920 | (define (write bv offset count) | |
921 | (if (zero? count) ;end of file | |
922 | (flush) | |
923 | (let loop ((offset offset) | |
924 | (count count) | |
925 | (written 0)) | |
926 | (cond ((= total size) | |
927 | (flush) | |
928 | (loop offset count written)) | |
929 | ((zero? count) | |
930 | written) | |
931 | (else | |
932 | (let ((to-copy (min count (- size total)))) | |
933 | (bytevector-copy! bv offset buffer total to-copy) | |
934 | (set! total (+ total to-copy)) | |
935 | (loop (+ offset to-copy) (- count to-copy) | |
936 | (+ written to-copy)))))))) | |
937 | ||
938 | ;; Note: We need to return FLUSH because the custom binary port has no way | |
939 | ;; to be notified of a 'force-output' call on itself. | |
940 | (values (make-custom-binary-output-port "buffering-output-port" | |
941 | write #f #f flush) | |
942 | flush)) | |
943 | ||
73b0ebdd LC |
944 | (define profiled? |
945 | (let ((profiled | |
946 | (or (and=> (getenv "GUIX_PROFILING") string-tokenize) | |
947 | '()))) | |
948 | (lambda (component) | |
949 | "Return true if COMPONENT profiling is active." | |
950 | (member component profiled)))) | |
951 | ||
f4453df9 LC |
952 | (define %rpc-calls |
953 | ;; Mapping from RPC names (symbols) to invocation counts. | |
954 | (make-hash-table)) | |
955 | ||
956 | (define* (show-rpc-profile #:optional (port (current-error-port))) | |
957 | "Write to PORT a summary of the RPCs that have been made." | |
958 | (let ((profile (sort (hash-fold alist-cons '() %rpc-calls) | |
959 | (lambda (rpc1 rpc2) | |
960 | (< (cdr rpc1) (cdr rpc2)))))) | |
961 | (format port "Remote procedure call summary: ~a RPCs~%" | |
962 | (match profile | |
963 | (((names . counts) ...) | |
964 | (reduce + 0 counts)))) | |
965 | (for-each (match-lambda | |
966 | ((rpc . count) | |
967 | (format port " ~30a ... ~5@a~%" rpc count))) | |
968 | profile))) | |
969 | ||
970 | (define record-operation | |
971 | ;; Optionally, increment the number of calls of the given RPC. | |
03870da8 LC |
972 | (if (profiled? "rpc") |
973 | (begin | |
974 | (register-profiling-hook! "rpc" show-rpc-profile) | |
975 | (lambda (name) | |
976 | (let ((count (or (hashq-ref %rpc-calls name) 0))) | |
977 | (hashq-set! %rpc-calls name (+ count 1))))) | |
978 | (lambda (_) | |
979 | #t))) | |
f4453df9 | 980 | |
fd060fd3 | 981 | (define-syntax operation |
77d3cf08 | 982 | (syntax-rules () |
fd060fd3 | 983 | "Define a client-side RPC stub for the given operation." |
3259877d | 984 | ((_ (name (type arg) ...) docstring return ...) |
fd060fd3 | 985 | (lambda (server arg ...) |
77d3cf08 | 986 | docstring |
de9fbe9c LC |
987 | (let* ((s (store-connection-socket server)) |
988 | (buffered (store-connection-output-port server))) | |
f4453df9 | 989 | (record-operation 'name) |
e037e9db LC |
990 | (write-int (operation-id name) buffered) |
991 | (write-arg type arg buffered) | |
77d3cf08 | 992 | ... |
e037e9db LC |
993 | (write-buffered-output server) |
994 | ||
dcee50c1 LC |
995 | ;; Loop until the server is done sending error output. |
996 | (let loop ((done? (process-stderr server))) | |
997 | (or done? (loop (process-stderr server)))) | |
3259877d | 998 | (values (read-arg return s) ...)))))) |
77d3cf08 | 999 | |
fd060fd3 LC |
1000 | (define-syntax-rule (define-operation (name args ...) |
1001 | docstring return ...) | |
1002 | (define name | |
1003 | (operation (name args ...) docstring return ...))) | |
1004 | ||
31ef99a8 | 1005 | (define-operation (valid-path? (string path)) |
06b76acc LC |
1006 | "Return #t when PATH designates a valid store item and #f otherwise (an |
1007 | invalid item may exist on disk but still be invalid, for instance because it | |
1008 | is the result of an aborted or failed build.) | |
1009 | ||
f9e8a123 | 1010 | A '&store-protocol-error' condition is raised if PATH is not prefixed by the |
06b76acc | 1011 | store directory (/gnu/store)." |
31ef99a8 LC |
1012 | boolean) |
1013 | ||
63193ebf | 1014 | (define-operation (query-path-hash (store-path path)) |
aa8fff0c | 1015 | "Return the SHA256 hash of the nar serialization of PATH as a bytevector." |
82058eff LC |
1016 | base16) |
1017 | ||
11e7a6cf LC |
1018 | (define hash-part->path |
1019 | (let ((query-path-from-hash-part | |
1020 | (operation (query-path-from-hash-part (string hash)) | |
1021 | #f | |
1022 | store-path))) | |
1023 | (lambda (server hash-part) | |
1024 | "Return the store path whose hash part is HASH-PART (a nix-base32 | |
7fcb86da | 1025 | string). Return the empty string if no such path exists." |
11e7a6cf LC |
1026 | ;; This RPC is primarily used by Hydra to reply to HTTP GETs of |
1027 | ;; /HASH.narinfo. | |
1028 | (query-path-from-hash-part server hash-part)))) | |
1029 | ||
533d1768 DT |
1030 | (define-operation (query-path-info (store-path path)) |
1031 | "Return the info (hash, references, etc.) for PATH." | |
1032 | path-info) | |
1033 | ||
0d268c5d | 1034 | (define add-data-to-store |
fd060fd3 LC |
1035 | ;; A memoizing version of `add-to-store', to avoid repeated RPCs with |
1036 | ;; the very same arguments during a given session. | |
1037 | (let ((add-text-to-store | |
0d268c5d | 1038 | (operation (add-text-to-store (string name) (bytevector text) |
fd060fd3 LC |
1039 | (string-list references)) |
1040 | #f | |
d1f7748a LC |
1041 | store-path)) |
1042 | (lookup (if (profiled? "add-data-to-store-cache") | |
1043 | (let ((lookups 0) | |
1044 | (hits 0) | |
1045 | (drv 0) | |
1046 | (scheme 0)) | |
1047 | (define (show-stats) | |
1048 | (define (% n) | |
1049 | (if (zero? lookups) | |
1050 | 100. | |
1051 | (* 100. (/ n lookups)))) | |
1052 | ||
1053 | (format (current-error-port) " | |
1054 | 'add-data-to-store' cache: | |
1055 | lookups: ~5@a | |
1056 | hits: ~5@a (~,1f%) | |
1057 | .drv files: ~5@a (~,1f%) | |
1058 | Scheme files: ~5@a (~,1f%)~%" | |
1059 | lookups hits (% hits) | |
1060 | drv (% drv) | |
1061 | scheme (% scheme))) | |
1062 | ||
1063 | (register-profiling-hook! "add-data-to-store-cache" | |
1064 | show-stats) | |
1065 | (lambda (cache args) | |
1066 | (let ((result (hash-ref cache args))) | |
1067 | (set! lookups (+ 1 lookups)) | |
1068 | (when result | |
1069 | (set! hits (+ 1 hits))) | |
1070 | (match args | |
1071 | ((_ name _) | |
1072 | (cond ((string-suffix? ".drv" name) | |
1073 | (set! drv (+ drv 1))) | |
1074 | ((string-suffix? "-builder" name) | |
1075 | (set! scheme (+ scheme 1))) | |
1076 | ((string-suffix? ".scm" name) | |
1077 | (set! scheme (+ scheme 1)))))) | |
1078 | result))) | |
1079 | hash-ref))) | |
0d268c5d LC |
1080 | (lambda* (server name bytes #:optional (references '())) |
1081 | "Add BYTES under file NAME in the store, and return its store path. | |
bdcf35a6 LC |
1082 | REFERENCES is the list of store paths referred to by the resulting store |
1083 | path." | |
0d268c5d | 1084 | (let* ((args `(,bytes ,name ,references)) |
de9fbe9c | 1085 | (cache (store-connection-add-text-to-store-cache server))) |
d1f7748a | 1086 | (or (lookup cache args) |
0d268c5d | 1087 | (let ((path (add-text-to-store server name bytes references))) |
bdcf35a6 LC |
1088 | (hash-set! cache args path) |
1089 | path)))))) | |
1090 | ||
0d268c5d LC |
1091 | (define* (add-text-to-store store name text #:optional (references '())) |
1092 | "Add TEXT under file NAME in the store, and return its store path. | |
1093 | REFERENCES is the list of store paths referred to by the resulting store | |
1094 | path." | |
1095 | (add-data-to-store store name (string->utf8 text) references)) | |
1096 | ||
1ec32f4a LC |
1097 | (define true |
1098 | ;; Define it once and for all since we use it as a default value for | |
1099 | ;; 'add-to-store' and want to make sure two default values are 'eq?' for the | |
1100 | ;; purposes or memoization. | |
1101 | (lambda (file stat) | |
1102 | #t)) | |
1103 | ||
fd060fd3 | 1104 | (define add-to-store |
a7b6ffee LC |
1105 | ;; A memoizing version of `add-to-store'. This is important because |
1106 | ;; `add-to-store' leads to huge data transfers to the server, and | |
1107 | ;; because it's often called many times with the very same argument. | |
1ec32f4a LC |
1108 | (let ((add-to-store |
1109 | (lambda* (server basename recursive? hash-algo file-name | |
1110 | #:key (select? true)) | |
1111 | ;; We don't use the 'operation' macro so we can pass SELECT? to | |
1112 | ;; 'write-file'. | |
0ca3d556 | 1113 | (record-operation 'add-to-store) |
7a45b5d5 LDB |
1114 | (let ((port (store-connection-socket server)) |
1115 | (buffered (store-connection-output-port server))) | |
1116 | (write-int (operation-id add-to-store) buffered) | |
1117 | (write-string basename buffered) | |
1118 | (write-int 1 buffered) ;obsolete, must be #t | |
1119 | (write-int (if recursive? 1 0) buffered) | |
1120 | (write-string hash-algo buffered) | |
1121 | (write-file file-name buffered #:select? select?) | |
38098241 | 1122 | (write-buffered-output server) |
1ec32f4a LC |
1123 | (let loop ((done? (process-stderr server))) |
1124 | (or done? (loop (process-stderr server)))) | |
1125 | (read-store-path port))))) | |
1126 | (lambda* (server basename recursive? hash-algo file-name | |
1127 | #:key (select? true)) | |
a9ebd9ef | 1128 | "Add the contents of FILE-NAME under BASENAME to the store. When |
69792b28 LC |
1129 | RECURSIVE? is false, FILE-NAME must designate a regular file--not a directory |
1130 | nor a symlink. When RECURSIVE? is true and FILE-NAME designates a directory, | |
1131 | the contents of FILE-NAME are added recursively; if FILE-NAME designates a | |
1132 | flat file and RECURSIVE? is true, its contents are added, and its permission | |
1ec32f4a LC |
1133 | bits are kept. HASH-ALGO must be a string such as \"sha256\". |
1134 | ||
1135 | When RECURSIVE? is true, call (SELECT? FILE STAT) for each directory entry, | |
1136 | where FILE is the entry's absolute file name and STAT is the result of | |
1137 | 'lstat'; exclude entries for which SELECT? does not return true." | |
b4671215 LC |
1138 | ;; Note: We don't stat FILE-NAME at each call, and thus we assume that |
1139 | ;; the file remains unchanged for the lifetime of SERVER. | |
1140 | (let* ((args `(,file-name ,basename ,recursive? ,hash-algo ,select?)) | |
de9fbe9c | 1141 | (cache (store-connection-add-to-store-cache server))) |
b4671215 | 1142 | (or (hash-ref cache args) |
1ec32f4a LC |
1143 | (let ((path (add-to-store server basename recursive? |
1144 | hash-algo file-name | |
1145 | #:select? select?))) | |
a7b6ffee LC |
1146 | (hash-set! cache args path) |
1147 | path)))))) | |
1148 | ||
7f11efba LC |
1149 | (define %not-slash |
1150 | (char-set-complement (char-set #\/))) | |
1151 | ||
1152 | (define* (add-file-tree-to-store server tree | |
1153 | #:key | |
1154 | (hash-algo "sha256") | |
1155 | (recursive? #t)) | |
1156 | "Add the given TREE to the store on SERVER. TREE must be an entry such as: | |
1157 | ||
1158 | (\"my-tree\" directory | |
1159 | (\"a\" regular (data \"hello\")) | |
1160 | (\"b\" symlink \"a\") | |
1161 | (\"c\" directory | |
1162 | (\"d\" executable (file \"/bin/sh\")))) | |
1163 | ||
1164 | This is a generalized version of 'add-to-store'. It allows you to reproduce | |
1165 | an arbitrary directory layout in the store without creating a derivation." | |
1166 | ||
1167 | ;; Note: The format of TREE was chosen to allow trees to be compared with | |
1168 | ;; 'equal?', which in turn allows us to memoize things. | |
1169 | ||
1170 | (define root | |
1171 | ;; TREE is a single entry. | |
1172 | (list tree)) | |
1173 | ||
1174 | (define basename | |
1175 | (match tree | |
1176 | ((name . _) name))) | |
1177 | ||
1178 | (define (lookup file) | |
1179 | (let loop ((components (string-tokenize file %not-slash)) | |
1180 | (tree root)) | |
1181 | (match components | |
1182 | ((basename) | |
1183 | (assoc basename tree)) | |
1184 | ((head . rest) | |
1185 | (loop rest | |
1186 | (match (assoc-ref tree head) | |
1187 | (('directory . entries) entries))))))) | |
1188 | ||
1189 | (define (file-type+size file) | |
1190 | (match (lookup file) | |
1191 | ((_ (and type (or 'directory 'symlink)) . _) | |
1192 | (values type 0)) | |
1193 | ((_ type ('file file)) | |
1194 | (values type (stat:size (stat file)))) | |
1195 | ((_ type ('data (? string? data))) | |
1196 | (values type (string-length data))) | |
1197 | ((_ type ('data (? bytevector? data))) | |
1198 | (values type (bytevector-length data))))) | |
1199 | ||
1200 | (define (file-port file) | |
1201 | (match (lookup file) | |
1202 | ((_ (or 'regular 'executable) content) | |
1203 | (match content | |
1204 | (('file (? string? file)) | |
1205 | (open-file file "r0b")) | |
1206 | (('data (? string? str)) | |
1207 | (open-input-string str)) | |
1208 | (('data (? bytevector? bv)) | |
1209 | (open-bytevector-input-port bv)))))) | |
1210 | ||
1211 | (define (symlink-target file) | |
1212 | (match (lookup file) | |
1213 | ((_ 'symlink target) target))) | |
1214 | ||
1215 | (define (directory-entries directory) | |
1216 | (match (lookup directory) | |
1217 | ((_ 'directory (names . _) ...) names))) | |
1218 | ||
1219 | (define cache | |
de9fbe9c | 1220 | (store-connection-add-to-store-cache server)) |
7f11efba LC |
1221 | |
1222 | (or (hash-ref cache tree) | |
1223 | (begin | |
1224 | ;; We don't use the 'operation' macro so we can use 'write-file-tree' | |
1225 | ;; instead of 'write-file'. | |
1226 | (record-operation 'add-to-store/tree) | |
7a45b5d5 LDB |
1227 | (let ((port (store-connection-socket server)) |
1228 | (buffered (store-connection-output-port server))) | |
1229 | (write-int (operation-id add-to-store) buffered) | |
1230 | (write-string basename buffered) | |
1231 | (write-int 1 buffered) ;obsolete, must be #t | |
1232 | (write-int (if recursive? 1 0) buffered) | |
1233 | (write-string hash-algo buffered) | |
1234 | (write-file-tree basename buffered | |
7f11efba LC |
1235 | #:file-type+size file-type+size |
1236 | #:file-port file-port | |
1237 | #:symlink-target symlink-target | |
1238 | #:directory-entries directory-entries) | |
000bbe02 | 1239 | (write-buffered-output server) |
7f11efba LC |
1240 | (let loop ((done? (process-stderr server))) |
1241 | (or done? (loop (process-stderr server)))) | |
1242 | (let ((result (read-store-path port))) | |
1243 | (hash-set! cache tree result) | |
1244 | result))))) | |
1245 | ||
68dbd5c9 LC |
1246 | (define (file-mapping->tree mapping) |
1247 | "Convert MAPPING, an alist like: | |
1248 | ||
1249 | ((\"guix/build/utils.scm\" . \"…/utils.scm\")) | |
1250 | ||
1251 | to a tree suitable for 'add-file-tree-to-store' and 'interned-file-tree'." | |
1252 | (let ((mapping (map (match-lambda | |
1253 | ((destination . source) | |
1254 | (cons (string-tokenize destination %not-slash) | |
1255 | source))) | |
1256 | mapping))) | |
1257 | (fold (lambda (pair result) | |
1258 | (match pair | |
1259 | ((destination . source) | |
1260 | (let loop ((destination destination) | |
1261 | (result result)) | |
1262 | (match destination | |
1263 | ((file) | |
1264 | (let* ((mode (stat:mode (stat source))) | |
1265 | (type (if (zero? (logand mode #o100)) | |
1266 | 'regular | |
1267 | 'executable))) | |
1268 | (alist-cons file | |
1269 | `(,type (file ,source)) | |
1270 | result))) | |
1271 | ((file rest ...) | |
1272 | (let ((directory (assoc-ref result file))) | |
1273 | (alist-cons file | |
1274 | `(directory | |
1275 | ,@(loop rest | |
1276 | (match directory | |
1277 | (('directory . entries) entries) | |
1278 | (#f '())))) | |
1279 | (if directory | |
1280 | (alist-delete file result) | |
1281 | result))))))))) | |
1282 | '() | |
1283 | mapping))) | |
1284 | ||
041b340d LC |
1285 | (define current-build-prompt |
1286 | ;; When true, this is the prompt to abort to when 'build-things' is called. | |
1287 | (make-parameter #f)) | |
1288 | ||
1289 | (define (call-with-build-handler handler thunk) | |
1290 | "Register HANDLER as a \"build handler\" and invoke THUNK." | |
1291 | (define tag | |
1292 | (make-prompt-tag "build handler")) | |
1293 | ||
1294 | (parameterize ((current-build-prompt tag)) | |
1295 | (call-with-prompt tag | |
1296 | thunk | |
1297 | (lambda (k . args) | |
1298 | ;; Since HANDLER may call K, which in turn may call 'build-things' | |
1299 | ;; again, reinstate a prompt (thus, it's not a tail call.) | |
1300 | (call-with-build-handler handler | |
1301 | (lambda () | |
1302 | (apply handler k args))))))) | |
1303 | ||
1304 | (define (invoke-build-handler store things mode) | |
1305 | "Abort to 'current-build-prompt' if it is set." | |
1306 | (or (not (current-build-prompt)) | |
1307 | (abort-to-prompt (current-build-prompt) store things mode))) | |
1308 | ||
1309 | (define-syntax-rule (with-build-handler handler exp ...) | |
1310 | "Register HANDLER as a \"build handler\" and invoke THUNK. When | |
1311 | 'build-things' is called within the dynamic extent of the call to THUNK, | |
1312 | HANDLER is invoked like so: | |
1313 | ||
1314 | (HANDLER CONTINUE STORE THINGS MODE) | |
1315 | ||
1316 | where CONTINUE is the continuation, and the remaining arguments are those that | |
1317 | were passed to 'build-things'. | |
1318 | ||
1319 | Build handlers are useful to announce a build plan with 'show-what-to-build' | |
1320 | and to implement dry runs (by not invoking CONTINUE) in a way that gracefully | |
1321 | deals with \"dynamic dependencies\" such as grafts---derivations that depend | |
1322 | on the build output of a previous derivation." | |
1323 | (call-with-build-handler handler (lambda () exp ...))) | |
1324 | ||
c40bf581 LC |
1325 | ;; Unresolved dynamic dependency. |
1326 | (define-record-type <unresolved> | |
1327 | (unresolved things continuation) | |
1328 | unresolved? | |
1329 | (things unresolved-things) | |
1330 | (continuation unresolved-continuation)) | |
1331 | ||
1332 | (define (build-accumulator continue store things mode) | |
1333 | "This build handler accumulates THINGS and returns an <unresolved> object." | |
1334 | (if (= mode (build-mode normal)) | |
1335 | (unresolved things continue) | |
1336 | (continue #t))) | |
1337 | ||
1338 | (define (map/accumulate-builds store proc lst) | |
1339 | "Apply PROC over each element of LST, accumulating 'build-things' calls and | |
1340 | coalescing them into a single call." | |
1341 | (define result | |
1342 | (map (lambda (obj) | |
1343 | (with-build-handler build-accumulator | |
1344 | (proc obj))) | |
1345 | lst)) | |
1346 | ||
1347 | (match (append-map (lambda (obj) | |
1348 | (if (unresolved? obj) | |
1349 | (unresolved-things obj) | |
1350 | '())) | |
1351 | result) | |
1352 | (() | |
1353 | result) | |
1354 | (to-build | |
1355 | ;; We've accumulated things TO-BUILD. Actually build them and resume the | |
1356 | ;; corresponding continuations. | |
1357 | (build-things store (delete-duplicates to-build)) | |
1358 | (map/accumulate-builds store | |
1359 | (lambda (obj) | |
1360 | (if (unresolved? obj) | |
1361 | ;; Pass #f because 'build-things' is now | |
1362 | ;; unnecessary. | |
1363 | ((unresolved-continuation obj) #f) | |
1364 | obj)) | |
1365 | result)))) | |
1366 | ||
07e70f48 LC |
1367 | (define build-things |
1368 | (let ((build (operation (build-things (string-list things) | |
1369 | (integer mode)) | |
1370 | "Do it!" | |
2734cbb8 LC |
1371 | boolean)) |
1372 | (build/old (operation (build-things (string-list things)) | |
1373 | "Do it!" | |
1374 | boolean))) | |
07e70f48 LC |
1375 | (lambda* (store things #:optional (mode (build-mode normal))) |
1376 | "Build THINGS, a list of store items which may be either '.drv' files or | |
abac874b LC |
1377 | outputs, and return when the worker is done building them. Elements of THINGS |
1378 | that are not derivations can only be substituted and not built locally. | |
f8a9f99c LC |
1379 | Alternately, an element of THING can be a derivation/output name pair, in |
1380 | which case the daemon will attempt to substitute just the requested output of | |
041b340d LC |
1381 | the derivation. Return #t on success. |
1382 | ||
1383 | When a handler is installed with 'with-build-handler', it is called any time | |
1384 | 'build-things' is called." | |
1385 | (or (not (invoke-build-handler store things mode)) | |
1386 | (let ((things (map (match-lambda | |
1387 | ((drv . output) (string-append drv "!" output)) | |
1388 | (thing thing)) | |
1389 | things))) | |
1390 | (parameterize ((current-store-protocol-version | |
1391 | (store-connection-version store))) | |
1392 | (if (>= (store-connection-minor-version store) 15) | |
1393 | (build store things mode) | |
1394 | (if (= mode (build-mode normal)) | |
1395 | (build/old store things) | |
1396 | (raise (condition (&store-protocol-error | |
1397 | (message "unsupported build mode") | |
1398 | (status 1)))))))))))) | |
26bbbb95 | 1399 | |
d3648e01 LC |
1400 | (define-operation (add-temp-root (store-path path)) |
1401 | "Make PATH a temporary root for the duration of the current session. | |
1402 | Return #t." | |
1403 | boolean) | |
1404 | ||
34811f02 | 1405 | (define-operation (add-indirect-root (string file-name)) |
a9d2a105 LC |
1406 | "Make the symlink FILE-NAME an indirect root for the garbage collector: |
1407 | whatever store item FILE-NAME points to will not be collected. Return #t on | |
1408 | success. | |
1409 | ||
1410 | FILE-NAME can be anywhere on the file system, but it must be an absolute file | |
1411 | name--it is the caller's responsibility to ensure that it is an absolute file | |
1412 | name." | |
34811f02 LC |
1413 | boolean) |
1414 | ||
a9d2a105 LC |
1415 | (define %gc-roots-directory |
1416 | ;; The place where garbage collector roots (symlinks) are kept. | |
1417 | (string-append %state-directory "/gcroots")) | |
1418 | ||
1419 | (define (add-permanent-root target) | |
1420 | "Add a garbage collector root pointing to TARGET, an element of the store, | |
1421 | preventing TARGET from even being collected. This can also be used if TARGET | |
1422 | does not exist yet. | |
1423 | ||
1424 | Raise an error if the caller does not have write access to the GC root | |
1425 | directory." | |
1426 | (let* ((root (string-append %gc-roots-directory "/" (basename target)))) | |
1427 | (catch 'system-error | |
1428 | (lambda () | |
1429 | (symlink target root)) | |
1430 | (lambda args | |
1431 | ;; If ROOT already exists, this is fine; otherwise, re-throw. | |
1432 | (unless (= EEXIST (system-error-errno args)) | |
1433 | (apply throw args)))))) | |
1434 | ||
1435 | (define (remove-permanent-root target) | |
1436 | "Remove the permanent garbage collector root pointing to TARGET. Raise an | |
1437 | error if there is no such root." | |
1438 | (delete-file (string-append %gc-roots-directory "/" (basename target)))) | |
1439 | ||
fae31edc LC |
1440 | (define references |
1441 | (operation (query-references (store-path path)) | |
1442 | "Return the list of references of PATH." | |
1443 | store-path-list)) | |
1444 | ||
f09aea1b LC |
1445 | (define %reference-cache |
1446 | ;; Brute-force cache mapping store items to their list of references. | |
1447 | ;; Caching matters because when building a profile in the presence of | |
1448 | ;; grafts, we keep calling 'graft-derivation', which in turn calls | |
1449 | ;; 'references/substitutes' many times with the same arguments. Ideally we | |
1450 | ;; would use a cache associated with the daemon connection instead (XXX). | |
1451 | (make-hash-table 100)) | |
1452 | ||
c70cf1a7 LC |
1453 | (define (references/cached store item) |
1454 | "Like 'references', but cache results." | |
1455 | (or (hash-ref %reference-cache item) | |
1456 | (let ((references (references store item))) | |
1457 | (hash-set! %reference-cache item references) | |
1458 | references))) | |
1459 | ||
6581ec9a LC |
1460 | (define (references/substitutes store items) |
1461 | "Return the list of list of references of ITEMS; the result has the same | |
1462 | length as ITEMS. Query substitute information for any item missing from the | |
f9e8a123 | 1463 | store at once. Raise a '&store-protocol-error' exception if reference |
6581ec9a | 1464 | information for one of ITEMS is missing." |
b2fde480 LC |
1465 | (let* ((requested items) |
1466 | (local-refs (map (lambda (item) | |
f09aea1b | 1467 | (or (hash-ref %reference-cache item) |
f9e8a123 | 1468 | (guard (c ((store-protocol-error? c) #f)) |
f09aea1b | 1469 | (references store item)))) |
6581ec9a LC |
1470 | items)) |
1471 | (missing (fold-right (lambda (item local-ref result) | |
1472 | (if local-ref | |
1473 | result | |
1474 | (cons item result))) | |
1475 | '() | |
1476 | items local-refs)) | |
1477 | ||
1478 | ;; Query all the substitutes at once to minimize the cost of | |
1479 | ;; launching 'guix substitute' and making HTTP requests. | |
2633bd32 LC |
1480 | (substs (if (null? missing) |
1481 | '() | |
1482 | (substitutable-path-info store missing)))) | |
6581ec9a | 1483 | (when (< (length substs) (length missing)) |
f9e8a123 | 1484 | (raise (condition (&store-protocol-error |
6581ec9a LC |
1485 | (message "cannot determine \ |
1486 | the list of references") | |
1487 | (status 1))))) | |
1488 | ||
1489 | ;; Intersperse SUBSTS and LOCAL-REFS. | |
dd78e90a LC |
1490 | (let loop ((items items) |
1491 | (local-refs local-refs) | |
6581ec9a | 1492 | (result '())) |
dd78e90a | 1493 | (match items |
6581ec9a | 1494 | (() |
f09aea1b LC |
1495 | (let ((result (reverse result))) |
1496 | (for-each (cut hash-set! %reference-cache <> <>) | |
b2fde480 | 1497 | requested result) |
f09aea1b | 1498 | result)) |
dd78e90a LC |
1499 | ((item items ...) |
1500 | (match local-refs | |
1501 | ((#f tail ...) | |
1502 | (loop items tail | |
1503 | (cons (any (lambda (subst) | |
1504 | (and (string=? (substitutable-path subst) item) | |
1505 | (substitutable-references subst))) | |
1506 | substs) | |
1507 | result))) | |
1508 | ((head tail ...) | |
1509 | (loop items tail | |
1510 | (cons head result))))))))) | |
6581ec9a | 1511 | |
f6fee16e | 1512 | (define* (fold-path store proc seed paths |
3f1e6939 | 1513 | #:optional (relatives (cut references store <>))) |
f6fee16e | 1514 | "Call PROC for each of the RELATIVES of PATHS, exactly once, and return the |
3f1e6939 LC |
1515 | result formed from the successive calls to PROC, the first of which is passed |
1516 | SEED." | |
f6fee16e | 1517 | (let loop ((paths paths) |
3f1e6939 LC |
1518 | (result seed) |
1519 | (seen vlist-null)) | |
1520 | (match paths | |
1521 | ((path rest ...) | |
1522 | (if (vhash-assoc path seen) | |
1523 | (loop rest result seen) | |
1524 | (let ((seen (vhash-cons path #t seen)) | |
1525 | (rest (append rest (relatives path))) | |
1526 | (result (proc path result))) | |
1527 | (loop rest result seen)))) | |
1528 | (() | |
1529 | result)))) | |
1530 | ||
f6fee16e LC |
1531 | (define (requisites store paths) |
1532 | "Return the requisites of PATHS, including PATHS---i.e., their closures (all | |
1533 | its references, recursively)." | |
1534 | (fold-path store cons '() paths)) | |
3f1e6939 | 1535 | |
50add477 LC |
1536 | (define (topologically-sorted store paths) |
1537 | "Return a list containing PATHS and all their references sorted in | |
1538 | topological order." | |
1539 | (define (traverse) | |
1540 | ;; Do a simple depth-first traversal of all of PATHS. | |
1541 | (let loop ((paths paths) | |
1542 | (visited vlist-null) | |
1543 | (result '())) | |
1544 | (define (visit n) | |
1545 | (vhash-cons n #t visited)) | |
1546 | ||
1547 | (define (visited? n) | |
1548 | (vhash-assoc n visited)) | |
1549 | ||
1550 | (match paths | |
1551 | ((head tail ...) | |
1552 | (if (visited? head) | |
1553 | (loop tail visited result) | |
1554 | (call-with-values | |
1555 | (lambda () | |
1556 | (loop (references store head) | |
1557 | (visit head) | |
1558 | result)) | |
1559 | (lambda (visited result) | |
1560 | (loop tail | |
1561 | visited | |
1562 | (cons head result)))))) | |
1563 | (() | |
1564 | (values visited result))))) | |
1565 | ||
1566 | (call-with-values traverse | |
1567 | (lambda (_ result) | |
1568 | (reverse result)))) | |
1569 | ||
fae31edc LC |
1570 | (define referrers |
1571 | (operation (query-referrers (store-path path)) | |
1572 | "Return the list of path that refer to PATH." | |
1573 | store-path-list)) | |
1574 | ||
1575 | (define valid-derivers | |
1576 | (operation (query-valid-derivers (store-path path)) | |
1577 | "Return the list of valid \"derivers\" of PATH---i.e., all the | |
1578 | .drv present in the store that have PATH among their outputs." | |
1579 | store-path-list)) | |
1580 | ||
1581 | (define query-derivation-outputs ; avoid name clash with `derivation-outputs' | |
1582 | (operation (query-derivation-outputs (store-path path)) | |
1583 | "Return the list of outputs of PATH, a .drv file." | |
1584 | store-path-list)) | |
1585 | ||
0f3d2504 LC |
1586 | (define-operation (has-substitutes? (store-path path)) |
1587 | "Return #t if binary substitutes are available for PATH, and #f otherwise." | |
1588 | boolean) | |
1589 | ||
1590 | (define substitutable-paths | |
1591 | (operation (query-substitutable-paths (store-path-list paths)) | |
1592 | "Return the subset of PATHS that is substitutable." | |
1593 | store-path-list)) | |
1594 | ||
1595 | (define substitutable-path-info | |
f65cf81a | 1596 | (operation (query-substitutable-path-infos (store-path-list paths)) |
0f3d2504 LC |
1597 | "Return information about the subset of PATHS that is |
1598 | substitutable. For each substitutable path, a `substitutable?' object is | |
dd78e90a LC |
1599 | returned; thus, the resulting list can be shorter than PATHS. Furthermore, |
1600 | that there is no guarantee that the order of the resulting list matches the | |
1601 | order of PATHS." | |
0f3d2504 LC |
1602 | substitutable-path-list)) |
1603 | ||
3961edf2 | 1604 | (define %built-in-builders |
f9aefa2d LC |
1605 | (let ((builders (operation (built-in-builders) |
1606 | "Return the built-in builders." | |
1607 | string-list))) | |
1608 | (lambda (store) | |
1609 | "Return the names of the supported built-in derivation builders | |
3961edf2 | 1610 | supported by STORE. The result is memoized for STORE." |
f9aefa2d LC |
1611 | ;; Check whether STORE's version supports this RPC and built-in |
1612 | ;; derivation builders in general, which appeared in Guix > 0.11.0. | |
1613 | ;; Return the empty list if it doesn't. Note that this RPC does not | |
1614 | ;; exist in 'nix-daemon'. | |
de9fbe9c LC |
1615 | (if (or (> (store-connection-major-version store) #x100) |
1616 | (and (= (store-connection-major-version store) #x100) | |
1617 | (>= (store-connection-minor-version store) #x60))) | |
f9aefa2d LC |
1618 | (builders store) |
1619 | '())))) | |
1620 | ||
3961edf2 LC |
1621 | (define (built-in-builders store) |
1622 | "Return the names of the supported built-in derivation builders | |
1623 | supported by STORE." | |
1624 | (force (store-connection-built-in-builders store))) | |
1625 | ||
e3fd0ce6 LC |
1626 | (define-operation (optimize-store) |
1627 | "Optimize the store by hard-linking identical files (\"deduplication\".) | |
1628 | Return #t on success." | |
1629 | ;; Note: the daemon in Guix <= 0.8.2 does not implement this RPC. | |
1630 | boolean) | |
1631 | ||
c63d9403 LC |
1632 | (define verify-store |
1633 | (let ((verify (operation (verify-store (boolean check-contents?) | |
1634 | (boolean repair?)) | |
1635 | "Verify the store." | |
1636 | boolean))) | |
1637 | (lambda* (store #:key check-contents? repair?) | |
1638 | "Verify the integrity of the store and return false if errors remain, | |
1639 | and true otherwise. When REPAIR? is true, repair any missing or altered store | |
1640 | items by substituting them (this typically requires root privileges because it | |
1641 | is not an atomic operation.) When CHECK-CONTENTS? is true, check the contents | |
1642 | of store items; this can take a lot of time." | |
1643 | (not (verify store check-contents? repair?))))) | |
1644 | ||
3259877d LC |
1645 | (define (run-gc server action to-delete min-freed) |
1646 | "Perform the garbage-collector operation ACTION, one of the | |
1647 | `gc-action' values. When ACTION is `delete-specific', the TO-DELETE is | |
1648 | the list of store paths to delete. IGNORE-LIVENESS? should always be | |
1649 | #f. MIN-FREED is the minimum amount of disk space to be freed, in | |
1650 | bytes, before the GC can stop. Return the list of store paths delete, | |
1651 | and the number of bytes freed." | |
7a45b5d5 LDB |
1652 | (let ((s (store-connection-socket server)) |
1653 | (buffered (store-connection-output-port server))) | |
1654 | (write-int (operation-id collect-garbage) buffered) | |
1655 | (write-int action buffered) | |
1656 | (write-store-path-list to-delete buffered) | |
1657 | (write-arg boolean #f buffered) ; ignore-liveness? | |
1658 | (write-long-long min-freed buffered) | |
1659 | (write-int 0 buffered) ; obsolete | |
de9fbe9c | 1660 | (when (>= (store-connection-minor-version server) 5) |
3259877d | 1661 | ;; Obsolete `use-atime' and `max-atime' parameters. |
7a45b5d5 LDB |
1662 | (write-int 0 buffered) |
1663 | (write-int 0 buffered)) | |
1664 | (write-buffered-output server) | |
3259877d LC |
1665 | |
1666 | ;; Loop until the server is done sending error output. | |
1667 | (let loop ((done? (process-stderr server))) | |
1668 | (or done? (loop (process-stderr server)))) | |
1669 | ||
1670 | (let ((paths (read-store-path-list s)) | |
1671 | (freed (read-long-long s)) | |
1672 | (obsolete (read-long-long s))) | |
000c59b6 LC |
1673 | (unless (null? paths) |
1674 | ;; To be on the safe side, completely invalidate both caches. | |
1675 | ;; Otherwise we could end up returning store paths that are no longer | |
1676 | ;; valid. | |
de9fbe9c LC |
1677 | (hash-clear! (store-connection-add-to-store-cache server)) |
1678 | (hash-clear! (store-connection-add-text-to-store-cache server))) | |
000c59b6 | 1679 | |
3259877d LC |
1680 | (values paths freed)))) |
1681 | ||
1682 | (define-syntax-rule (%long-long-max) | |
1683 | ;; Maximum unsigned 64-bit integer. | |
1684 | (- (expt 2 64) 1)) | |
1685 | ||
1686 | (define (live-paths server) | |
1687 | "Return the list of live store paths---i.e., store paths still | |
1688 | referenced, and thus not subject to being garbage-collected." | |
1689 | (run-gc server (gc-action return-live) '() (%long-long-max))) | |
1690 | ||
1691 | (define (dead-paths server) | |
1692 | "Return the list of dead store paths---i.e., store paths no longer | |
1693 | referenced, and thus subject to being garbage-collected." | |
1694 | (run-gc server (gc-action return-dead) '() (%long-long-max))) | |
1695 | ||
1696 | (define* (collect-garbage server #:optional (min-freed (%long-long-max))) | |
1697 | "Collect garbage from the store at SERVER. If MIN-FREED is non-zero, | |
1698 | then collect at least MIN-FREED bytes. Return the paths that were | |
1699 | collected, and the number of bytes freed." | |
1700 | (run-gc server (gc-action delete-dead) '() min-freed)) | |
1701 | ||
1702 | (define* (delete-paths server paths #:optional (min-freed (%long-long-max))) | |
1703 | "Delete PATHS from the store at SERVER, if they are no longer | |
1704 | referenced. If MIN-FREED is non-zero, then stop after at least | |
1705 | MIN-FREED bytes have been collected. Return the paths that were | |
1706 | collected, and the number of bytes freed." | |
1707 | (run-gc server (gc-action delete-specific) paths min-freed)) | |
1708 | ||
526382ff LC |
1709 | (define (import-paths server port) |
1710 | "Import the set of store paths read from PORT into SERVER's store. An error | |
1711 | is raised if the set of paths read from PORT is not signed (as per | |
1712 | 'export-path #:sign? #t'.) Return the list of store paths imported." | |
de9fbe9c | 1713 | (let ((s (store-connection-socket server))) |
526382ff LC |
1714 | (write-int (operation-id import-paths) s) |
1715 | (let loop ((done? (process-stderr server port))) | |
1716 | (or done? (loop (process-stderr server port)))) | |
1717 | (read-store-path-list s))) | |
1718 | ||
1719 | (define* (export-path server path port #:key (sign? #t)) | |
1720 | "Export PATH to PORT. When SIGN? is true, sign it." | |
7a45b5d5 LDB |
1721 | (let ((s (store-connection-socket server)) |
1722 | (buffered (store-connection-output-port server))) | |
1723 | (write-int (operation-id export-path) buffered) | |
1724 | (write-store-path path buffered) | |
1725 | (write-arg boolean sign? buffered) | |
1726 | (write-buffered-output server) | |
526382ff LC |
1727 | (let loop ((done? (process-stderr server port))) |
1728 | (or done? (loop (process-stderr server port)))) | |
1729 | (= 1 (read-int s)))) | |
1730 | ||
b03267df LC |
1731 | (define* (export-paths server paths port #:key (sign? #t) recursive? |
1732 | (start (const #f)) | |
1733 | (progress (const #f)) | |
1734 | (finish (const #f))) | |
99fbddf9 | 1735 | "Export the store paths listed in PATHS to PORT, in topological order, |
5b3d863f | 1736 | signing them if SIGN? is true. When RECURSIVE? is true, export the closure of |
b03267df LC |
1737 | PATHS---i.e., PATHS and all their dependencies. |
1738 | ||
1739 | START, PROGRESS, and FINISH are used to track progress of the data transfer. | |
1740 | START is a one-argument that is passed the list of store items that will be | |
1741 | transferred; it returns values that are then used as the initial state | |
1742 | threaded through PROGRESS calls. PROGRESS is passed the store item about to | |
1743 | be sent, along with the values previously return by START or by PROGRESS | |
1744 | itself. FINISH is called when the last store item has been called." | |
cafb92d8 | 1745 | (define ordered |
5b3d863f LC |
1746 | (let ((sorted (topologically-sorted server paths))) |
1747 | ;; When RECURSIVE? is #f, filter out the references of PATHS. | |
1748 | (if recursive? | |
1749 | sorted | |
1750 | (filter (cut member <> paths) sorted)))) | |
cafb92d8 | 1751 | |
b03267df LC |
1752 | (let loop ((paths ordered) |
1753 | (state (call-with-values (lambda () (start ordered)) | |
1754 | list))) | |
1d506993 LC |
1755 | (match paths |
1756 | (() | |
b03267df | 1757 | (apply finish state) |
1d506993 LC |
1758 | (write-int 0 port)) |
1759 | ((head tail ...) | |
1760 | (write-int 1 port) | |
1761 | (and (export-path server head port #:sign? sign?) | |
b03267df LC |
1762 | (loop tail |
1763 | (call-with-values | |
1764 | (lambda () (apply progress head state)) | |
1765 | list))))))) | |
526382ff | 1766 | |
16748d80 LC |
1767 | (define-operation (query-failed-paths) |
1768 | "Return the list of store items for which a build failure is cached. | |
1769 | ||
1770 | The result is always the empty list unless the daemon was started with | |
1771 | '--cache-failures'." | |
1772 | store-path-list) | |
1773 | ||
1774 | (define-operation (clear-failed-paths (store-path-list items)) | |
1775 | "Remove ITEMS from the list of cached build failures. | |
1776 | ||
1777 | This makes sense only when the daemon was started with '--cache-failures'." | |
1778 | boolean) | |
1779 | ||
26bbbb95 | 1780 | \f |
e87f0591 LC |
1781 | ;;; |
1782 | ;;; Store monad. | |
1783 | ;;; | |
1784 | ||
4e190c28 LC |
1785 | (define-syntax-rule (define-alias new old) |
1786 | (define-syntax new (identifier-syntax old))) | |
e87f0591 | 1787 | |
4e190c28 LC |
1788 | ;; The store monad allows us to (1) build sequences of operations in the |
1789 | ;; store, and (2) make the store an implicit part of the execution context, | |
1790 | ;; rather than a parameter of every single function. | |
1791 | (define-alias %store-monad %state-monad) | |
1792 | (define-alias store-return state-return) | |
1793 | (define-alias store-bind state-bind) | |
e87f0591 | 1794 | |
dcb95c1f LC |
1795 | ;; Instantiate templates for %STORE-MONAD since it's syntactically different |
1796 | ;; from %STATE-MONAD. | |
1797 | (template-directory instantiations %store-monad) | |
1798 | ||
c57e417e LC |
1799 | (define* (cache-object-mapping object keys result |
1800 | #:key (vhash-cons vhash-consq)) | |
c6080c32 LC |
1801 | "Augment the store's object cache with a mapping from OBJECT/KEYS to RESULT. |
1802 | KEYS is a list of additional keys to match against, for instance a (SYSTEM | |
c57e417e | 1803 | TARGET) tuple. Use VHASH-CONS to insert OBJECT into the cache. |
c6080c32 LC |
1804 | |
1805 | OBJECT is typically a high-level object such as a <package> or an <origin>, | |
1806 | and RESULT is typically its derivation." | |
1807 | (lambda (store) | |
1808 | (values result | |
de9fbe9c | 1809 | (store-connection |
c6080c32 | 1810 | (inherit store) |
c57e417e LC |
1811 | (object-cache (vhash-cons object (cons result keys) |
1812 | (store-connection-object-cache store))))))) | |
c6080c32 | 1813 | |
73b0ebdd LC |
1814 | (define record-cache-lookup! |
1815 | (if (profiled? "object-cache") | |
1816 | (let ((fresh 0) | |
1817 | (lookups 0) | |
1818 | (hits 0)) | |
1819 | (register-profiling-hook! | |
1820 | "object-cache" | |
1821 | (lambda () | |
1822 | (format (current-error-port) "Store object cache: | |
1823 | fresh caches: ~5@a | |
1824 | lookups: ~5@a | |
1825 | hits: ~5@a (~,1f%)~%" | |
1826 | fresh lookups hits | |
1827 | (if (zero? lookups) | |
1828 | 100. | |
1829 | (* 100. (/ hits lookups)))))) | |
1830 | ||
1831 | (lambda (hit? cache) | |
1832 | (set! fresh | |
1833 | (if (eq? cache vlist-null) | |
1834 | (+ 1 fresh) | |
1835 | fresh)) | |
1836 | (set! lookups (+ 1 lookups)) | |
1837 | (set! hits (if hit? (+ hits 1) hits)))) | |
1838 | (lambda (x y) | |
1839 | #t))) | |
1840 | ||
c57e417e LC |
1841 | (define* (lookup-cached-object object #:optional (keys '()) |
1842 | #:key (vhash-fold* vhash-foldq*)) | |
c6080c32 | 1843 | "Return the cached object in the store connection corresponding to OBJECT |
c57e417e LC |
1844 | and KEYS; use VHASH-FOLD* to look for OBJECT in the cache. KEYS is a list of |
1845 | additional keys to match against, and which are compared with 'equal?'. | |
1846 | Return #f on failure and the cached result otherwise." | |
c6080c32 | 1847 | (lambda (store) |
de9fbe9c | 1848 | (let* ((cache (store-connection-object-cache store)) |
73b0ebdd LC |
1849 | |
1850 | ;; Escape as soon as we find the result. This avoids traversing | |
1851 | ;; the whole vlist chain and significantly reduces the number of | |
1852 | ;; 'hashq' calls. | |
1853 | (value (let/ec return | |
c57e417e LC |
1854 | (vhash-fold* (lambda (item result) |
1855 | (match item | |
1856 | ((value . keys*) | |
1857 | (if (equal? keys keys*) | |
1858 | (return value) | |
1859 | result)))) | |
1860 | #f object | |
1861 | cache)))) | |
73b0ebdd LC |
1862 | (record-cache-lookup! value cache) |
1863 | (values value store)))) | |
c6080c32 | 1864 | |
c57e417e LC |
1865 | (define* (%mcached mthunk object #:optional (keys '()) |
1866 | #:key | |
1867 | (vhash-cons vhash-consq) | |
1868 | (vhash-fold* vhash-foldq*)) | |
c6080c32 | 1869 | "Bind the monadic value returned by MTHUNK, which supposedly corresponds to |
c57e417e LC |
1870 | OBJECT/KEYS, or return its cached value. Use VHASH-CONS to insert OBJECT into |
1871 | the cache, and VHASH-FOLD* to look it up." | |
1872 | (mlet %store-monad ((cached (lookup-cached-object object keys | |
1873 | #:vhash-fold* vhash-fold*))) | |
c6080c32 LC |
1874 | (if cached |
1875 | (return cached) | |
1876 | (>>= (mthunk) | |
1877 | (lambda (result) | |
c57e417e LC |
1878 | (cache-object-mapping object keys result |
1879 | #:vhash-cons vhash-cons)))))) | |
c6080c32 | 1880 | |
c57e417e LC |
1881 | (define-syntax mcached |
1882 | (syntax-rules (eq? equal?) | |
1883 | "Run MVALUE, which corresponds to OBJECT/KEYS, and cache it; or return the | |
c6080c32 LC |
1884 | value associated with OBJECT/KEYS in the store's object cache if there is |
1885 | one." | |
c57e417e LC |
1886 | ((_ eq? mvalue object keys ...) |
1887 | (%mcached (lambda () mvalue) | |
1888 | object (list keys ...) | |
1889 | #:vhash-cons vhash-consq | |
1890 | #:vhash-fold* vhash-foldq*)) | |
1891 | ((_ equal? mvalue object keys ...) | |
1892 | (%mcached (lambda () mvalue) | |
1893 | object (list keys ...) | |
1894 | #:vhash-cons vhash-cons | |
1895 | #:vhash-fold* vhash-fold*)) | |
1896 | ((_ mvalue object keys ...) | |
1897 | (mcached eq? mvalue object keys ...)))) | |
c6080c32 | 1898 | |
5808dcc2 LC |
1899 | (define (preserve-documentation original proc) |
1900 | "Return PROC with documentation taken from ORIGINAL." | |
1901 | (set-object-property! proc 'documentation | |
1902 | (procedure-property original 'documentation)) | |
1903 | proc) | |
1904 | ||
e87f0591 LC |
1905 | (define (store-lift proc) |
1906 | "Lift PROC, a procedure whose first argument is a connection to the store, | |
1907 | in the store monad." | |
5808dcc2 LC |
1908 | (preserve-documentation proc |
1909 | (lambda args | |
1910 | (lambda (store) | |
1911 | (values (apply proc store args) store))))) | |
e87f0591 | 1912 | |
023d9892 LC |
1913 | (define (store-lower proc) |
1914 | "Lower PROC, a monadic procedure in %STORE-MONAD, to a \"normal\" procedure | |
1915 | taking the store as its first argument." | |
5808dcc2 LC |
1916 | (preserve-documentation proc |
1917 | (lambda (store . args) | |
1918 | (run-with-store store (apply proc args))))) | |
023d9892 | 1919 | |
c40bf581 LC |
1920 | (define (mapm/accumulate-builds mproc lst) |
1921 | "Like 'mapm' in %STORE-MONAD, but accumulate 'build-things' calls and | |
1922 | coalesce them into a single call." | |
1923 | (lambda (store) | |
1924 | (values (map/accumulate-builds store | |
1925 | (lambda (obj) | |
1926 | (run-with-store store | |
80963744 LC |
1927 | (mproc obj) |
1928 | #:system (%current-system) | |
1929 | #:target (%current-target-system))) | |
c40bf581 LC |
1930 | lst) |
1931 | store))) | |
1932 | ||
1933 | ||
e87f0591 LC |
1934 | ;; |
1935 | ;; Store monad operators. | |
1936 | ;; | |
1937 | ||
f3a42251 JN |
1938 | (define* (binary-file name |
1939 | data ;bytevector | |
1940 | #:optional (references '())) | |
1941 | "Return as a monadic value the absolute file name in the store of the file | |
1942 | containing DATA, a bytevector. REFERENCES is a list of store items that the | |
1943 | resulting text file refers to; it defaults to the empty list." | |
1944 | (lambda (store) | |
1945 | (values (add-data-to-store store name data references) | |
1946 | store))) | |
1947 | ||
1948 | (define* (text-file name | |
1949 | text ;string | |
ad372953 | 1950 | #:optional (references '())) |
e87f0591 | 1951 | "Return as a monadic value the absolute file name in the store of the file |
ad372953 LC |
1952 | containing TEXT, a string. REFERENCES is a list of store items that the |
1953 | resulting text file refers to; it defaults to the empty list." | |
e87f0591 | 1954 | (lambda (store) |
ad372953 | 1955 | (values (add-text-to-store store name text references) |
4e190c28 | 1956 | store))) |
e87f0591 LC |
1957 | |
1958 | (define* (interned-file file #:optional name | |
1ec32f4a | 1959 | #:key (recursive? #t) (select? true)) |
e87f0591 LC |
1960 | "Return the name of FILE once interned in the store. Use NAME as its store |
1961 | name, or the basename of FILE if NAME is omitted. | |
1962 | ||
1963 | When RECURSIVE? is true, the contents of FILE are added recursively; if FILE | |
1964 | designates a flat file and RECURSIVE? is true, its contents are added, and its | |
1ec32f4a LC |
1965 | permission bits are kept. |
1966 | ||
1967 | When RECURSIVE? is true, call (SELECT? FILE STAT) for each directory entry, | |
1968 | where FILE is the entry's absolute file name and STAT is the result of | |
1969 | 'lstat'; exclude entries for which SELECT? does not return true." | |
e87f0591 | 1970 | (lambda (store) |
4e190c28 | 1971 | (values (add-to-store store (or name (basename file)) |
1ec32f4a LC |
1972 | recursive? "sha256" file |
1973 | #:select? select?) | |
4e190c28 | 1974 | store))) |
e87f0591 | 1975 | |
7f11efba LC |
1976 | (define interned-file-tree |
1977 | (store-lift add-file-tree-to-store)) | |
1978 | ||
abac874b LC |
1979 | (define build |
1980 | ;; Monadic variant of 'build-things'. | |
1981 | (store-lift build-things)) | |
1982 | ||
4f740b67 AK |
1983 | (define set-build-options* |
1984 | (store-lift set-build-options)) | |
1985 | ||
e74f64b9 LC |
1986 | (define references* |
1987 | (store-lift references)) | |
1988 | ||
0744a9f0 LC |
1989 | (define (query-path-info* item) |
1990 | "Monadic version of 'query-path-info' that returns #f when ITEM is not in | |
1991 | the store." | |
1992 | (lambda (store) | |
f9e8a123 | 1993 | (guard (c ((store-protocol-error? c) |
0744a9f0 LC |
1994 | ;; ITEM is not in the store; return #f. |
1995 | (values #f store))) | |
1996 | (values (query-path-info store item) store)))) | |
1997 | ||
98a7b528 LC |
1998 | (define-inlinable (current-system) |
1999 | ;; Consult the %CURRENT-SYSTEM fluid at bind time. This is equivalent to | |
2000 | ;; (lift0 %current-system %store-monad), but inlinable, thus avoiding | |
2001 | ;; closure allocation in some cases. | |
2002 | (lambda (state) | |
2003 | (values (%current-system) state))) | |
2004 | ||
2005 | (define-inlinable (set-current-system system) | |
2006 | ;; Set the %CURRENT-SYSTEM fluid at bind time. | |
2007 | (lambda (state) | |
2008 | (values (%current-system system) state))) | |
2009 | ||
91be09de MO |
2010 | (define-inlinable (current-target-system) |
2011 | ;; Consult the %CURRENT-TARGET-SYSTEM fluid at bind time. | |
2012 | (lambda (state) | |
2013 | (values (%current-target-system) state))) | |
2014 | ||
fdae0fa5 MO |
2015 | (define-inlinable (set-current-target target) |
2016 | ;; Set the %CURRENT-TARGET-SYSTEM fluid at bind time. | |
2017 | (lambda (state) | |
2018 | (values (%current-target-system target) state))) | |
2019 | ||
e87f0591 LC |
2020 | (define %guile-for-build |
2021 | ;; The derivation of the Guile to be used within the build environment, | |
2022 | ;; when using 'gexp->derivation' and co. | |
2023 | (make-parameter #f)) | |
2024 | ||
dd0ee954 LC |
2025 | (define set-store-connection-object-cache! |
2026 | (record-modifier <store-connection> 'object-cache)) | |
2027 | ||
e87f0591 LC |
2028 | (define* (run-with-store store mval |
2029 | #:key | |
2030 | (guile-for-build (%guile-for-build)) | |
45bba475 LC |
2031 | (system (%current-system)) |
2032 | (target #f)) | |
e87f0591 | 2033 | "Run MVAL, a monadic value in the store monad, in STORE, an open store |
3698f524 | 2034 | connection, and return the result." |
a8afb9ae LC |
2035 | ;; Initialize the dynamic bindings here to avoid bad surprises. The |
2036 | ;; difficulty lies in the fact that dynamic bindings are resolved at | |
2037 | ;; bind-time and not at call time, which can be disconcerting. | |
e87f0591 | 2038 | (parameterize ((%guile-for-build guile-for-build) |
a8afb9ae | 2039 | (%current-system system) |
45bba475 | 2040 | (%current-target-system target)) |
3698f524 LC |
2041 | (call-with-values (lambda () |
2042 | (run-with-state mval store)) | |
dd0ee954 | 2043 | (lambda (result new-store) |
728a4ab1 LC |
2044 | (when (and store new-store) |
2045 | ;; Copy the object cache from NEW-STORE so we don't fully discard | |
2046 | ;; the state. | |
2047 | (let ((cache (store-connection-object-cache new-store))) | |
2048 | (set-store-connection-object-cache! store cache))) | |
2049 | result)))) | |
e87f0591 LC |
2050 | |
2051 | \f | |
26bbbb95 LC |
2052 | ;;; |
2053 | ;;; Store paths. | |
2054 | ;;; | |
2055 | ||
2056 | (define %store-prefix | |
2057 | ;; Absolute path to the Nix store. | |
1d6816f9 | 2058 | (make-parameter %store-directory)) |
26bbbb95 | 2059 | |
cd041b26 LC |
2060 | (define (compressed-hash bv size) ; `compressHash' |
2061 | "Given the hash stored in BV, return a compressed version thereof that fits | |
2062 | in SIZE bytes." | |
2063 | (define new (make-bytevector size 0)) | |
2064 | (define old-size (bytevector-length bv)) | |
2065 | (let loop ((i 0)) | |
2066 | (if (= i old-size) | |
2067 | new | |
2068 | (let* ((j (modulo i size)) | |
2069 | (o (bytevector-u8-ref new j))) | |
2070 | (bytevector-u8-set! new j | |
2071 | (logxor o (bytevector-u8-ref bv i))) | |
2072 | (loop (+ 1 i)))))) | |
2073 | ||
2074 | (define (store-path type hash name) ; makeStorePath | |
2075 | "Return the store path for NAME/HASH/TYPE." | |
2076 | (let* ((s (string-append type ":sha256:" | |
2077 | (bytevector->base16-string hash) ":" | |
2078 | (%store-prefix) ":" name)) | |
2079 | (h (sha256 (string->utf8 s))) | |
2080 | (c (compressed-hash h 20))) | |
2081 | (string-append (%store-prefix) "/" | |
2082 | (bytevector->nix-base32-string c) "-" | |
2083 | name))) | |
2084 | ||
2085 | (define (output-path output hash name) ; makeOutputPath | |
2086 | "Return an output path for OUTPUT (the name of the output as a string) of | |
2087 | the derivation called NAME with hash HASH." | |
2088 | (store-path (string-append "output:" output) hash | |
2089 | (if (string=? output "out") | |
2090 | name | |
2091 | (string-append name "-" output)))) | |
2092 | ||
2093 | (define* (fixed-output-path name hash | |
2094 | #:key | |
2095 | (output "out") | |
2096 | (hash-algo 'sha256) | |
2097 | (recursive? #t)) | |
2098 | "Return an output path for the fixed output OUTPUT defined by HASH of type | |
2099 | HASH-ALGO, of the derivation NAME. RECURSIVE? has the same meaning as for | |
2100 | 'add-to-store'." | |
2101 | (if (and recursive? (eq? hash-algo 'sha256)) | |
2102 | (store-path "source" hash name) | |
2103 | (let ((tag (string-append "fixed:" output ":" | |
2104 | (if recursive? "r:" "") | |
2105 | (symbol->string hash-algo) ":" | |
2106 | (bytevector->base16-string hash) ":"))) | |
2107 | (store-path (string-append "output:" output) | |
2108 | (sha256 (string->utf8 tag)) | |
2109 | name)))) | |
2110 | ||
f39bd08a LC |
2111 | (define (store-path? path) |
2112 | "Return #t if PATH is a store path." | |
2113 | ;; This is a lightweight check, compared to using a regexp, but this has to | |
2114 | ;; be fast as it's called often in `derivation', for instance. | |
2115 | ;; `isStorePath' in Nix does something similar. | |
2116 | (string-prefix? (%store-prefix) path)) | |
26bbbb95 | 2117 | |
9336e5b5 LC |
2118 | (define (direct-store-path? path) |
2119 | "Return #t if PATH is a store path, and not a sub-directory of a store path. | |
2120 | This predicate is sometimes needed because files *under* a store path are not | |
2121 | valid inputs." | |
2122 | (and (store-path? path) | |
eee21271 | 2123 | (not (string=? path (%store-prefix))) |
9336e5b5 LC |
2124 | (let ((len (+ 1 (string-length (%store-prefix))))) |
2125 | (not (string-index (substring path len) #\/))))) | |
2126 | ||
cdb5b075 CS |
2127 | (define (direct-store-path path) |
2128 | "Return the direct store path part of PATH, stripping components after | |
2129 | '/gnu/store/xxxx-foo'." | |
2130 | (let ((prefix-length (+ (string-length (%store-prefix)) 35))) | |
2131 | (if (> (string-length path) prefix-length) | |
2132 | (let ((slash (string-index path #\/ prefix-length))) | |
2133 | (if slash (string-take path slash) path)) | |
2134 | path))) | |
2135 | ||
26bbbb95 LC |
2136 | (define (derivation-path? path) |
2137 | "Return #t if PATH is a derivation path." | |
2138 | (and (store-path? path) (string-suffix? ".drv" path))) | |
e3d74106 | 2139 | |
771c5e15 FP |
2140 | (define (store-path-base path) |
2141 | "Return the base path of a path in the store." | |
2142 | (and (string-prefix? (%store-prefix) path) | |
2143 | (let ((base (string-drop path (+ 1 (string-length (%store-prefix)))))) | |
2144 | (and (> (string-length base) 33) | |
2145 | (not (string-index base #\/)) | |
2146 | base)))) | |
5c0f1845 | 2147 | |
e3d74106 LC |
2148 | (define (store-path-package-name path) |
2149 | "Return the package name part of PATH, a file name in the store." | |
771c5e15 FP |
2150 | (let ((base (store-path-base path))) |
2151 | (string-drop base (+ 32 1)))) ;32 hash part + 1 hyphen | |
2c6ab6cc LC |
2152 | |
2153 | (define (store-path-hash-part path) | |
2154 | "Return the hash part of PATH as a base32 string, or #f if PATH is not a | |
2155 | syntactically valid store path." | |
771c5e15 FP |
2156 | (let* ((base (store-path-base path)) |
2157 | (hash (string-take base 32))) | |
2158 | (and (string-every %nix-base32-charset hash) | |
2159 | hash))) | |
eddd4077 | 2160 | |
fb94d82b LC |
2161 | (define (derivation-log-file drv) |
2162 | "Return the build log file for DRV, a derivation file name, or #f if it | |
2163 | could not be found." | |
2164 | (let* ((base (basename drv)) | |
7c4700e9 LC |
2165 | (log (string-append (or (getenv "GUIX_LOG_DIRECTORY") |
2166 | (string-append %localstatedir "/log/guix")) | |
2167 | "/drvs/" | |
fb94d82b LC |
2168 | (string-take base 2) "/" |
2169 | (string-drop base 2))) | |
2170 | (log.gz (string-append log ".gz")) | |
2171 | (log.bz2 (string-append log ".bz2"))) | |
2172 | (cond ((file-exists? log.gz) log.gz) | |
2173 | ((file-exists? log.bz2) log.bz2) | |
2174 | ((file-exists? log) log) | |
2175 | (else #f)))) | |
2176 | ||
eddd4077 LC |
2177 | (define (log-file store file) |
2178 | "Return the build log file for FILE, or #f if none could be found. FILE | |
2179 | must be an absolute store file name, or a derivation file name." | |
eddd4077 | 2180 | (cond ((derivation-path? file) |
fb94d82b | 2181 | (derivation-log-file file)) |
eddd4077 LC |
2182 | (else |
2183 | (match (valid-derivers store file) | |
2184 | ((derivers ...) | |
2185 | ;; Return the first that works. | |
2186 | (any (cut log-file store <>) derivers)) | |
2187 | (_ #f))))) | |
3b5cd17a LC |
2188 | |
2189 | ;;; Local Variables: | |
2190 | ;;; eval: (put 'system-error-to-connection-error 'scheme-indent-function 1) | |
2191 | ;;; End: |