(zilch): document most exported symbols

Change-Id: I6a6a6964d3be7b8c6306a21d810c639f30253d38
This commit is contained in:
puck 2025-06-23 12:22:20 +00:00
parent 6a1efc6a92
commit a80266d9d8
13 changed files with 186 additions and 81 deletions

View file

@ -16,22 +16,24 @@
daemon-write-bytevector daemon-read-bytevector
daemon-write-string daemon-read-string
*logger*
daemon-wop-handshake daemon-wop-set-options
daemon-wop-add-text-to-store daemon-wop-build-paths
daemon-wop-query-derivation-output-map
daemon-wop-query-path-info
daemon-wop-nar-from-path
daemon-wop-add-to-store-nar
<nix-activity> nix-activity?
nix-activity-id nix-activity-log-level nix-activity-type
nix-activity-string nix-activity-fields nix-activity-parent-id
*logger*
daemon-wop-handshake daemon-wop-set-options
daemon-wop-add-text-to-store daemon-wop-build-paths
daemon-wop-query-derivation-output-map
daemon-wop-nar-from-path
daemon-wop-add-to-store-nar
<valid-path-info> valid-path-info?
valid-path-info-deriver valid-path-info-nar-hash valid-path-info-references
valid-path-info-registration-time valid-path-info-nar-size valid-path-info-ultimate
valid-path-info-sigs valid-path-info-ca)
valid-path-info-sigs valid-path-info-ca
daemon-wop-query-path-info)
(begin
@ -51,6 +53,8 @@
(daemon-version daemon-link-daemon-version set-daemon-link-daemon-version!)
(settings daemon-link-settings))
;; Creates a new `<daemon-link>`, setting the internal settings to default
;; (verbosity at 3, job count at 32, use-substitutes `#t`)
(define (make-daemon-link in-port out-port)
(internal-make-daemon-link in-port out-port #f #f (make-daemon-link-settings 3 32 #t)))
@ -62,6 +66,8 @@
(define (daemon-read-u64 link) (port-read-u64 (daemon-link-in-port link)))
(define (daemon-read-bytevector link) (port-read-bytevector (daemon-link-in-port link)))
(define (daemon-read-string link) (port-read-string (daemon-link-in-port link)))
;; Flushes the ``<daemon-link>``'s output port.
(define (daemon-flush link) (flush-output-port (daemon-link-out-port link)))
(define build-activity #f)
@ -85,9 +91,9 @@
(when (or (> done-builds 0) (> total-builds 1) (> running-builds 0))
(printf "[~S/~S builds, ~S running]\n" done-builds total-builds running-builds))))))))
;; Reads a list of log events until STDERR_LAST is called.
;; This is the client-side equivalent of startWorking / stopWorking on the
;; server.
;; Reads a list of log events until `STDERR_LAST` is seen.
;; This is the client-side equivalent of `startWorking` / `stopWorking` in the
;; Nix daemon.
(define (daemon-read-log-events link)
(define val (daemon-read-u64 link))
(case val
@ -175,7 +181,8 @@
(#x1f . "Nix 2.4pre")
(#x1f . "Nix 2.4pre")
(#x20 . "Nix 2.4-2.6")))
;; Send a Nix worker protocol handshake.
;; Sends the Nix worker protocol handshake, then sends the default options.
(define (daemon-wop-handshake link)
(daemon-write-u64 link #x6e697863)
(daemon-flush link)
@ -199,6 +206,11 @@
(daemon-read-log-events link)
(daemon-wop-set-options link))
;; Sets some of the daemon's settings.
;;
;; - `verbosity` is the verbosity (amount of `-v` arguments in Nix)
;; - `max-build-jobs` is the amount of concurrent build jobs for this connection.
;; - `use-substitutes` defines whether the Nix daemon should check if the output of a Derivation is available in the binary cache.
(define daemon-wop-set-options
(case-lambda
((link)
@ -250,8 +262,8 @@
(daemon-read-log-events link)
(daemon-read-u64 link)))
;; Write a simple text file to the store. REFS is expected to be sorted.
;; Returns the store path at which the file has been created.
;; Write a simple text file to the store. `refs` is expected to be sorted.
;; Returns the (string) store path at which the file has been created.
(define (daemon-wop-add-text-to-store link suffix s refs)
(daemon-write-u64 link 8)
(daemon-write-string link suffix)
@ -262,6 +274,7 @@
(daemon-read-log-events link)
(daemon-read-string link))
;; Contains the information Nix stores about a valid store path.
(define-record-type <valid-path-info>
(make-valid-path-info deriver nar-hash references registration-time nar-size ultimate sigs ca)
valid-path-info?
@ -296,6 +309,7 @@
references registration-time nar-size (= ultimate 1) sigs
(if (string=? ca "") #f ca)))
;; Requests the information the Nix daemon has about a specified store path. Returns a `<valid-path-info>`.
(define (daemon-wop-query-path-info link store-path)
(daemon-write-u64 link 26)
(daemon-write-string link store-path)
@ -306,16 +320,25 @@
(daemon-read-valid-path-info link)
#f))
; You are responsible for reading exactly the right amount of bytes from
; the daemon after this. My condolences.
;; Requests the daemon send over the contents of a NAR file. The file is not multiplexed, and of unspecified size.
;; When you call this, you take responsibility to read exactly `nar-size` (from `<valid-path-info>`) bytes, or one valid NAR file,
;; from the `daemon-link-in-port`.
(define (daemon-wop-nar-from-path link store-path)
(daemon-write-u64 link 38)
(daemon-write-string link store-path)
(daemon-flush link)
(daemon-read-log-events link))
; `proc` is a procedure taking one argument, which is used to write data into the daemon.
; The write-blob procedure passed to `proc` looks like (write-blob bv [start [end]]).
;; Adds a NAR to the daemon's Nix store.
;;
;; - `store-path` is the path this nar file should be stored at, and must correspond to the rest of the information provided.
;; - `deriver` is the (optional) store path containing the information used to derive this store path (usually a `.drv`).
;; - `nar-hash` is a string containing the nixbase16 representation of the sha256 hash of the NAR.
;; - `references` is a (sorted) list of store paths that this nar depends on.
;; - `nar-size` is the amountt of bytes this nar takes up.
;; - `ca`, if not `#f` is a Nix-style hash describing the content-addressed hash type and hash value.
;; - `proc` is a procedure taking one argument (`write-blob`), which is used to write data into the daemon.
;; `write-blob` takes three arguments: a bytevector, and an optional start and end index into it.
(define (daemon-wop-add-to-store-nar link store-path deriver nar-hash references nar-size ca proc)
(daemon-write-u64 link 39)
(daemon-write-string link store-path)
@ -348,6 +371,7 @@
(daemon-read-log-events link)
(thread-join! data-thread))
;; Requests an alist of output name to output store path for the derivation at `store-path`.
(define (daemon-wop-query-derivation-output-map link store-path)
(daemon-write-u64 link 41)
(daemon-write-string link store-path)

View file

@ -7,20 +7,17 @@
(chicken base) (chicken format))
(export
<derivation> derivation?
derivation-name derivation-outputs derivation-input-drvs
derivation-input-src derivation-system derivation-builder
derivation-args derivation-env derivation-equal?
derivation-meta set-derivation-meta!
%derivation-compatible
<derivation-output> derivation-output?
derivation-output-path derivation-output-hash
derivation-output-algo derivation-output-recursive
derivation-output-placeholder? derivation-output-path-length
write-quoted-string
<derivation> derivation?
derivation-name derivation-outputs derivation-input-drvs
derivation-input-src derivation-system derivation-builder
derivation-args derivation-env derivation-equal?
derivation-meta set-derivation-meta!
drv-is-fod
derivation-serialize derivation-path-references derivation-path derivation-read read-drv-path
@ -28,7 +25,7 @@
modulo-hash-drv-contents)
(begin
;; If `#t`, outputs environment variables not used by Nix, but required for compatibility with Nix's output.
;; If `#t`, `make-[..]-derivation` will output environment variables not used by Nix, but required for compatibility with Nix's output.
;; This adds `name`, `builder`, and `system` to the environment; as well as `outputHash`, `outputHashAlgo`,
;; and `outputHashMode` for fixed-output derivations.
(define %derivation-compatible (make-parameter #t))
@ -37,11 +34,10 @@
;; whether or not it the hash is of the NAR file, if it is a content-addressed output.
;; The path can be read using `(derivation-output-path)`.
;;
;; - `(path #u8() "" #f)` is an input-addressed derivation output. TODO(puck): empty bytevector?
;; - `(path #f #f #f)` is an input-addressed derivation output. TODO(puck): empty bytevector?
;; - `(path #u8() "" #f)` is an input-addressed derivation output.
;; - `(path #f #f #f)` is an input-addressed derivation output.
;; - `(path hash-value hash-algo rec)` is a content-addressed derivation output.
;; - `(#f 'floating hash-algo rec)` is a floating content-addressed derivation output.
;; - `(#f 'impure hash-algo rec)` is an impure content-addressed derivation output.
(define-record-type <derivation-output>
(make-derivation-output path hash algo recursive)
derivation-output?
@ -57,9 +53,11 @@
(derivation-output-algo drvout)
(derivation-output-recursive drvout)))
;; Returns whether the `derivation-output-path` of this output is a placeholder (floating hash)
(define (derivation-output-placeholder? drvout)
(member (derivation-output-hash drvout) '(floating)))
;; Returns the final (post-placeholder substitution) length of a derivation's output.
(define (derivation-output-path-length drv output-name)
; /nix/store/a0a3n97c93ckfg3a920aqnycxdznbbmi-module-output
(+ (string-length (%store-dir)) 34 (string-length (derivation-name drv)) (if (string=? output-name "out") 0 (+ 1 (string-length output-name)))))
@ -74,9 +72,10 @@
(serialized derivation-metadata-serialized set-derivation-metadata-serialized!)
(meta derivation-metadata-meta set-derivation-metadata-meta!))
;; An arbitrary Scheme object stored in the `<derivation>`.
(define (derivation-meta drv)
(derivation-metadata-meta (derivation-metadata drv)))
;; Sets the object stored within the `<derivation>`.
(define (set-derivation-meta! drv meta)
(set-derivation-metadata-meta! (derivation-metadata drv) meta))
@ -88,9 +87,11 @@
(not (not (derivation-metadata-serialized drv)))))
;; An entire derivation.
;; `outputs` is stored as an alist of output name to `<derivation-output>` object.
;; `input-drvs` is stored as an alist of `<derivation>` to a (sorted) list of its outputs that are used.
;; The `outputs`, `input-drvs`, `input-src`, and `env` are expected to be sorted.
;;
;; - `outputs` is an alist of output name to `<derivation-output>` record.
;; - `input-drvs` is an alist of `<derivation>` to a (sorted) list of the outputs of said derivation that are depended on.
;;
;; `outputs`, `input-drvs`, `input-src`, and `env` are expected to be sorted.
(define-record-type <derivation>
(make-derivation name outputs input-drvs input-src system builder args env metadata)
derivation?
@ -182,7 +183,7 @@
(define (env-pair< left right)
(string<? (car left) (car right)))
;; Calculate the "modulo" contents (that will have to be hashed) of a derivation.
;; Calculate the "modulo" contents of a derivation. The modulo hash of a derivation is used in the store paths of the outputs of said derivation.
(define (modulo-hash-drv-contents drv)
(cond
((drv-is-fod drv)
@ -310,6 +311,7 @@
path)
(derivation-metadata-path (derivation-metadata drv))))
;; Equality comparison for a `<derivation>`. Checks whether the argument represent the same derivation, not just referential equality.
(define (derivation-equal? left right)
(define left-cached-path (derivation-metadata-path (derivation-metadata left)))
(define right-cached-path (derivation-metadata-path (derivation-metadata right)))
@ -494,8 +496,9 @@
(write-bracket-list (lambda (l) (write-paren-list write-quoted-string (list (car l) (cdr l)))) (derivation-env drv))
(write-u8 #x29)))
;; Writes the derivation to the specified port, or current-output-port if none is supplied.
;; If masked is set, writes the derivation using the passed-in input derivations, rather than the default one.
;; Writes the derivation to the specified port, or `(current-output-port)` if none is supplied.
;; If `masked` is set, writes the derivation using it as `input-drvs` value, rather than the one stored in the `<derivation>`.
;; This is used for generating the modulo-hashed derivation.
(define derivation-serialize
(case-lambda
((drv) (derivation-serialize drv (current-output-port)))

View file

@ -13,7 +13,7 @@
((= i (bytevector-length hash)) output-hash)
(bytevector-u8-set! output-hash (floor-remainder i 20) (bitwise-xor (bytevector-u8-ref output-hash (floor-remainder i 20)) (bytevector-u8-ref hash i)))))
;; Turns bytevector HASH to a Nix-style (reversed base32) format.
;; Turns bytevector `hash` to a Nix-style (reversed base32, custom alphabet) string.
(define (as-base32 hash)
(do ((len (+ (floor-quotient (- (* 8 (bytevector-length hash)) 1) 5) 1)) (tail '()) (i 0 (+ i 1)))
((= i len) (list->string tail))
@ -31,7 +31,7 @@
(when (= i 32) (error "unknown character in nixbase32 string" chr))
i)))
;; Returns a nix-base32 string decoded into a bytevector.
;; Returns a bytevector containing `hash` decoded (using reversed base32, custom alphabet)
(define (from-base32 hash)
(do ((i 0 (+ i 1))
(strlen (string-length hash))

View file

@ -1,6 +1,7 @@
;; A series of helpers that help create store paths.
;;
;; These helpers all use the `%store-dir` parameter as base store directory.
;; Where `hash-value` is used, a bytevector containing the raw hash is expected.
(define-library (zilch nix path)
(import
(scheme base) (srfi 152)
@ -8,7 +9,7 @@
(export
%store-dir
impure-placeholder make-upstream-output-placeholder make-placeholder
make-upstream-output-placeholder make-placeholder
make-store-path-from-parts make-text-path make-fixed-output-path make-output-path
make-fixed-output-with-references)
@ -16,12 +17,15 @@
;; The path to the store dir, as a parameter.
(define %store-dir (make-parameter "/nix/store"))
(define impure-placeholder (sha256 "impure"))
;; Calculates a string placeholder for a derivation.
;;
;; - `drv-hash-string` is the hash part of the derivation's store path
;; - `drv-name` is the name of the derivation
;; - `output-name` is the output to calculuate the placeholder for
(define (make-upstream-output-placeholder drv-hash-string drv-name output-name)
(string-append "/" (as-base32 (sha256 (string-append "nix-upstream-output:" drv-hash-string ":" drv-name (if (string=? output-name "out") "" (string-append "-" output-name)))))))
;; Makes a placeholder path, which is substituted with the path of the output.
;; Makes a placeholder path, which is substituted at build-time to be the corresponding output store path for that derivation.
(define (make-placeholder output-name)
(string-append "/" (as-base32 (sha256 (string->utf8 (string-append "nix-output:" output-name))))))
@ -32,15 +36,15 @@
((eqv? references '()) collected)
(else (fold-references (cdr references) (string-append collected ":" (car references))))))
;; Creates an arbitrary Nix store path.
;; Creates an arbitrary Nix store path from its constituent parts.
(define (make-store-path-from-parts type hash-algo hash-val name)
(let*
((inner (string-append type ":" hash-algo ":" (hex hash-val) ":" (%store-dir) ":" name))
(hashed (as-base32 (hash-compress (sha256 (string->utf8 inner))))))
(string-append (%store-dir) "/" hashed "-" name)))
;; Creates a store path belonging to a derivation output. HASH-ALGO and
;; HASH-VAL encode the (masked) modulo hash of the derivation.
;; Creates a store path belonging to a derivation output. `hash-algo` and
;; `hash-val` encode the (masked) modulo hash of the derivation.
(define (make-output-path hash-algo hash-val output-name name)
(make-store-path-from-parts
(string-append "output:" output-name)
@ -49,7 +53,7 @@
;; Creates a store path belonging to a text file. Text files may only
;; depend on other text files, and are used in input-srcs rather than
;; input-drvs. refs is expected to be sorted.
;; input-drvs. `refs` is expected to be sorted.
(define (make-text-path hash-algo hash-value name refs)
(make-store-path-from-parts (fold-references refs "text") hash-algo hash-value name))
@ -64,6 +68,7 @@
(string-append "fixed:out:" (if recursive "r:" "") hash-algo ":" (hex hash-value) ":")))
name)))
;; Creates a fixed-output store path, that has references to other store paths.
(define (make-fixed-output-with-references hash-value name references self-references)
(make-store-path-from-parts
(string-join (append (cons "source" references) (if self-references '("self") '())) ":")