Create CBOR representation of derivations - #16429
Conversation
|
Is there a way we can reuse more of the definition between the JSON and CBOR formats? My ideal would be a single "aeson instance", using an AST with CBOR's two types of strings (binary and unicode), and the former when translated to JSON uses non-standard I'm still trying to land this monster #10760 and so having fewer format implementations to keep in sync really does help not make that implementation metastasize further. I do need to write down all the derivation plans in my head regardless, but also it would probably good to discuss live in a Nix meeting. |
|
I think the consensus is that CBOR is not suitable for derivations because it doesn't have a canonical representation? On those grounds of course JSON can be considered unsuitable too and should be considered a convenience for CLI/internals tests only. |
You have to define the representation, which this PR does. |
|
I'm referring more to:
Does this PR enforce a particular way to serialise integers in structured attrs? If not then we'd be kinda in the same hole of having equivalent codings of the same thing which doesn't seem good for derivations that are content addressed (well, if the intention is to have NG derivations and addressing schemes). It's also quite hard to read, so forgive me if I'm missing the particulars of how structured attrs are handled - I've only skimmed the code here. There's of course other concerns that would have to be included in the ng format (like @Ericson2314 mentioned above). For one, that would be better handling of placeholders that doesn't necessitate resorting to string rewriting and such. Taking an opportunity to collect design considerations for such seems very prudent |
Add deterministic CBOR import and export with arbitrary byte support for builder, arguments, and environment names and values. Share derivation field definitions with the JSON codec while preserving the existing JSON format and the original structured-attribute JSON bytes. Distinguish byte 0xff from EOF in the ATerm parser so stored derivations round-trip correctly. Document the CBOR schema and normalization rules, and test byte preservation, malformed inputs, and derivation identity. Assisted-by: Codex (GPT-6)
427b0f5 to
72dc19c
Compare
|
Thanks for the feedback. JSON and CBOR now share derivation field definitions and input/output conversion logic. The JSON wire format remains unchanged; this doesn’t implement the suggested CBOR output uses deterministic ordering and shortest integer/length encodings. Import accepts alternative encodings and normalizes them on export, with tests confirming identical normalized bytes and derivation paths. Structured attributes retain their original JSON bytes, including number spellings. Their numbers aren’t encoded as CBOR integers or floats, and preserving those bytes preserves existing derivation identity. This PR provides interchange for existing derivations; CBOR bytes aren’t hashed directly. A new addressing scheme and placeholder redesign would need a separate design discussion. |
Add CBOR version 1 import and export for existing derivations through
nix derivation show --cborandnix derivation add --cbor. Builder paths, arguments, and environment names and values can contain arbitrary bytes, including values that the current JSON format cannot represent.JSON and CBOR share derivation field definitions and output/input conversion logic. The existing JSON wire format remains unchanged; this does not introduce non-standard JSON
\xescapes.The encoder emits deterministic CBOR with shortest integer and length encodings, definite lengths, ordered map keys, and sorted sets. The decoder accepts nonminimal encodings and unsorted maps and sets, then normalizes them on export. Different accepted encodings produce the same normalized CBOR and derivation path.
Structured attributes are carried as the original JSON bytes, preserving whitespace, escaping, key order, and number spellings. Their numbers are not converted to CBOR integers or floats. This preserves existing derivation identity: import uses the current validation, ATerm storage, and hashing rules, and CBOR bytes are not hashed directly. A new derivation addressing scheme, semantic JSON canonicalization, and placeholder redesign are outside this PR's scope.
The manual documents the schema, normalization rules, and identity guarantees. Regression tests cover arbitrary bytes, malformed input, deterministic encoding, normalization without identity changes, and verbatim structured attributes.
Validation with rebuilt store library, CLI, and unit-test binary: all 28 focused CBOR/ATerm/JSON tests pass, and both main and CA
derivation-jsonfunctional tests pass. Formatting andgit diff --checkpass. The broader store suite passed 796 of 808 tests; the remaining 12 failed during OpenSSL setup or because/bin/shwas unavailable in the test build environment.Related: JSON byte limitation discussion and the corresponding Rust nix-derivation PR.
Assisted-by: Codex (GPT-6)