From 1ad13a1423a8ed342140e57701d3ef24ceb3d3ca Mon Sep 17 00:00:00 2001 From: John Ericson Date: Wed, 3 Dec 2025 17:07:00 -0500 Subject: [PATCH] Introduce `--json-format` for `nix path-info` As discussed today at great length in the Nix meeting, we don't want to break the format, but we also don't want to impede the improvement of JSON formats. The solution is to add a new flag for control the output format. Note that prior to the release, we may want to replace `--json --json-format N` with `--json=N`, but this is being left for a separate PR, as we don't yet have `=` support for CLI flags. --- doc/manual/rl-next/json-format-changes.md | 35 ++++++- .../source/protocols/json/schema/nar-info-v1 | 1 - .../source/protocols/json/schema/nar-info-v2 | 1 + .../json/schema/store-object-info-v2 | 2 +- .../json/schema/store-object-info-v2.yaml | 8 +- .../protocols/json/store-object-info.md | 4 +- src/json-schema-checks/meson.build | 24 ++--- .../data/nar-info/json-1/impure.json | 21 ++++ .../data/nar-info/json-1/pure.json | 10 ++ .../data/nar-info/json-1/pure_noversion.json | 9 ++ .../data/nar-info/{ => json-2}/impure.json | 6 +- .../{path-info => nar-info/json-2}/pure.json | 4 +- .../data/path-info/json-1/empty_impure.json | 11 +++ .../data/path-info/json-1/empty_pure.json | 7 ++ .../data/path-info/json-1/impure.json | 17 ++++ .../data/path-info/json-1/pure.json | 10 ++ .../data/path-info/json-1/pure_noversion.json | 9 ++ .../path-info/{ => json-2}/empty_impure.json | 0 .../path-info/{ => json-2}/empty_pure.json | 0 .../data/path-info/{ => json-2}/impure.json | 6 +- .../{nar-info => path-info/json-2}/pure.json | 4 +- src/libstore-tests/nar-info.cc | 90 ++++++++++++----- src/libstore-tests/path-info.cc | 87 ++++++++++++---- src/libstore/include/nix/store/nar-info.hh | 3 +- src/libstore/include/nix/store/path-info.hh | 27 ++++- src/libstore/nar-info.cc | 29 ++++-- src/libstore/path-info.cc | 99 ++++++++++++++----- src/nix/path-info.cc | 38 +++++-- tests/functional/binary-cache.sh | 2 +- tests/functional/fixed.sh | 2 +- tests/functional/git-hashing/simple-common.sh | 2 +- tests/functional/impure-derivations.sh | 2 +- tests/functional/nix-profile.sh | 2 +- tests/functional/path-info.sh | 8 +- tests/functional/signing.sh | 10 +- tests/nixos/fetchers-substitute.nix | 6 +- 36 files changed, 464 insertions(+), 132 deletions(-) delete mode 120000 doc/manual/source/protocols/json/schema/nar-info-v1 create mode 120000 doc/manual/source/protocols/json/schema/nar-info-v2 create mode 100644 src/libstore-tests/data/nar-info/json-1/impure.json create mode 100644 src/libstore-tests/data/nar-info/json-1/pure.json create mode 100644 src/libstore-tests/data/nar-info/json-1/pure_noversion.json rename src/libstore-tests/data/nar-info/{ => json-2}/impure.json (81%) rename src/libstore-tests/data/{path-info => nar-info/json-2}/pure.json (78%) create mode 100644 src/libstore-tests/data/path-info/json-1/empty_impure.json create mode 100644 src/libstore-tests/data/path-info/json-1/empty_pure.json create mode 100644 src/libstore-tests/data/path-info/json-1/impure.json create mode 100644 src/libstore-tests/data/path-info/json-1/pure.json create mode 100644 src/libstore-tests/data/path-info/json-1/pure_noversion.json rename src/libstore-tests/data/path-info/{ => json-2}/empty_impure.json (100%) rename src/libstore-tests/data/path-info/{ => json-2}/empty_pure.json (100%) rename src/libstore-tests/data/path-info/{ => json-2}/impure.json (73%) rename src/libstore-tests/data/{nar-info => path-info/json-2}/pure.json (78%) diff --git a/doc/manual/rl-next/json-format-changes.md b/doc/manual/rl-next/json-format-changes.md index 009c1639c..ce280a305 100644 --- a/doc/manual/rl-next/json-format-changes.md +++ b/doc/manual/rl-next/json-format-changes.md @@ -6,13 +6,29 @@ issues: [] JSON formats for store path info and derivations have been updated with new versions and structured fields. -## Store Path Info JSON (Version 2) +## Store Path Info JSON -The store path info JSON format has been updated from version 1 to version 2: +`nix path-info --json` now requires a `--json-format` flag to specify the output format version. +Using `--json` without `--json-format` is deprecated and will become an error in a future release. +For now, it defaults to version 1 with a warning, for a smoother migration. -- **Added `version` field**: +### Version 1 (`--json-format 1`) - All store path info JSON now includes `"version": 2`. +This is the legacy format, preserved for backwards compatibility: + +- String-based hash values (e.g., `"narHash": "sha256:FePFYIlM..."`) +- String-based content addresses (e.g., `"ca": "fixed:r:sha256:1abc..."`) +- Full store paths in references (e.g., `"/nix/store/abc...-foo"`) +- Now includes `"storeDir"` field at the top level + +### Version 2 (`--json-format 2`) + +The new structured format with the following changes: + +- **Store path base names in references**: + + References use store path base names (e.g., `"abc...-foo"`) instead of full paths. + Combined with `storeDir`, the full path can be reconstructed. - **Structured `ca` field**: @@ -33,7 +49,16 @@ The store path info JSON format has been updated from version 1 to version 2: Nix currently only produces, and doesn't consume this format. -**Affected command**: `nix path-info --json` +Additionally the following field is added to both formats. +(The `version` tracks breaking changes, and adding fields to outputted JSON is not a breaking change.) + +- **`version` field**: + + All store path info JSON now includes `"version": <1|2>`. + +- **`storeDir` field**: + + Top-level `"storeDir"` field contains the store directory path (e.g., `"/nix/store"`). ## Derivation JSON (Version 4) diff --git a/doc/manual/source/protocols/json/schema/nar-info-v1 b/doc/manual/source/protocols/json/schema/nar-info-v1 deleted file mode 120000 index ded866b6f..000000000 --- a/doc/manual/source/protocols/json/schema/nar-info-v1 +++ /dev/null @@ -1 +0,0 @@ -../../../../../../src/libstore-tests/data/nar-info \ No newline at end of file diff --git a/doc/manual/source/protocols/json/schema/nar-info-v2 b/doc/manual/source/protocols/json/schema/nar-info-v2 new file mode 120000 index 000000000..cb08fe781 --- /dev/null +++ b/doc/manual/source/protocols/json/schema/nar-info-v2 @@ -0,0 +1 @@ +../../../../../../src/libstore-tests/data/nar-info/json-2 \ No newline at end of file diff --git a/doc/manual/source/protocols/json/schema/store-object-info-v2 b/doc/manual/source/protocols/json/schema/store-object-info-v2 index fc6f5c3f0..36ca7f13d 120000 --- a/doc/manual/source/protocols/json/schema/store-object-info-v2 +++ b/doc/manual/source/protocols/json/schema/store-object-info-v2 @@ -1 +1 @@ -../../../../../../src/libstore-tests/data/path-info \ No newline at end of file +../../../../../../src/libstore-tests/data/path-info/json-2 \ No newline at end of file diff --git a/doc/manual/source/protocols/json/schema/store-object-info-v2.yaml b/doc/manual/source/protocols/json/schema/store-object-info-v2.yaml index 44d9e5eae..fb63795be 100644 --- a/doc/manual/source/protocols/json/schema/store-object-info-v2.yaml +++ b/doc/manual/source/protocols/json/schema/store-object-info-v2.yaml @@ -63,7 +63,7 @@ $defs: - Version 2: Use structured JSON type for `ca` path: - type: string + "$ref": "./store-path-v1.yaml" title: Store Path description: | [Store path](@docroot@/store/store-path.md) to the given store object. @@ -89,7 +89,7 @@ $defs: description: | An array of [store paths](@docroot@/store/store-path.md), possibly including this one. items: - type: string + "$ref": "./store-path-v1.yaml" ca: oneOf: @@ -128,7 +128,9 @@ $defs: references: { $ref: "#/$defs/base/properties/references" } ca: { $ref: "#/$defs/base/properties/ca" } deriver: - type: ["string", "null"] + oneOf: + - "$ref": "./store-path-v1.yaml" + - type: "null" title: Deriver description: | If known, the path to the [store derivation](@docroot@/glossary.md#gloss-store-derivation) from which this store object was produced. diff --git a/doc/manual/source/protocols/json/store-object-info.md b/doc/manual/source/protocols/json/store-object-info.md index 6a101ab0f..4ad83de00 100644 --- a/doc/manual/source/protocols/json/store-object-info.md +++ b/doc/manual/source/protocols/json/store-object-info.md @@ -29,13 +29,13 @@ ### NAR info (minimal) ```json -{{#include schema/nar-info-v1/pure.json}} +{{#include schema/nar-info-v2/pure.json}} ``` ### NAR info (with binary cache fields) ```json -{{#include schema/nar-info-v1/impure.json}} +{{#include schema/nar-info-v2/impure.json}} ```