Skip to content

Commit b56e456

Browse files
authored
Merge pull request #14269 from roberth/json-schema
Add a JSON Schema for `Derivation`
2 parents ad2360c + c92ba4b commit b56e456

25 files changed

+480
-124
lines changed

ci/gha/tests/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ rec {
116116
) nixComponentsInstrumented)
117117
// lib.optionalAttrs (pkgs.stdenv.hostPlatform == pkgs.stdenv.buildPlatform) {
118118
"${componentTestsPrefix}nix-functional-tests" = nixComponentsInstrumented.nix-functional-tests;
119+
"${componentTestsPrefix}nix-json-schema-checks" = nixComponentsInstrumented.nix-json-schema-checks;
119120
};
120121

121122
codeCoverage =

doc/manual/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ manual = custom_target(
115115
builtins_md,
116116
rl_next_generated,
117117
summary_rl_next,
118+
json_schema_generated_files,
118119
nix_input,
119120
],
120121
output : [

doc/manual/package.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
rsync,
1313
nix-cli,
1414
changelog-d,
15+
json-schema-for-humans,
1516
officialRelease,
1617

1718
# Configuration Options
@@ -55,6 +56,7 @@ mkMesonDerivation (finalAttrs: {
5556
jq
5657
python3
5758
rsync
59+
json-schema-for-humans
5860
changelog-d
5961
]
6062
++ lib.optionals (!officialRelease) [

doc/manual/source/SUMMARY.md.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
- [Architecture and Design](architecture/architecture.md)
118118
- [Formats and Protocols](protocols/index.md)
119119
- [JSON Formats](protocols/json/index.md)
120+
- [Hash](protocols/json/hash.md)
120121
- [Store Object Info](protocols/json/store-object-info.md)
121122
- [Derivation](protocols/json/derivation.md)
122123
- [Serving Tarball Flakes](protocols/tarball-fetcher.md)

doc/manual/source/meson.build

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Process JSON schema documentation
2+
subdir('protocols')
3+
14
summary_rl_next = custom_target(
25
command : [
36
bash,
Lines changed: 5 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,7 @@
1-
# Derivation JSON Format
1+
{{#include derivation-v3-fixed.md}}
22

3-
> **Warning**
4-
>
5-
> This JSON format is currently
6-
> [**experimental**](@docroot@/development/experimental-features.md#xp-feature-nix-command)
7-
> and subject to change.
3+
<!--
4+
## Raw Schema
85
9-
The JSON serialization of a
10-
[derivations](@docroot@/glossary.md#gloss-store-derivation)
11-
is a JSON object with the following fields:
12-
13-
* `name`:
14-
The name of the derivation.
15-
This is used when calculating the store paths of the derivation's outputs.
16-
17-
* `version`:
18-
Must be `3`.
19-
This is a guard that allows us to continue evolving this format.
20-
The choice of `3` is fairly arbitrary, but corresponds to this informal version:
21-
22-
- Version 0: A-Term format
23-
24-
- Version 1: Original JSON format, with ugly `"r:sha256"` inherited from A-Term format.
25-
26-
- Version 2: Separate `method` and `hashAlgo` fields in output specs
27-
28-
- Version 3: Drop store dir from store paths, just include base name.
29-
30-
Note that while this format is experimental, the maintenance of versions is best-effort, and not promised to identify every change.
31-
32-
* `outputs`:
33-
Information about the output paths of the derivation.
34-
This is a JSON object with one member per output, where the key is the output name and the value is a JSON object with these fields:
35-
36-
* `path`:
37-
The output path, if it is known in advanced.
38-
Otherwise, `null`.
39-
40-
41-
* `method`:
42-
For an output which will be [content addressed], a string representing the [method](@docroot@/store/store-object/content-address.md) of content addressing that is chosen.
43-
Valid method strings are:
44-
45-
- [`flat`](@docroot@/store/store-object/content-address.md#method-flat)
46-
- [`nar`](@docroot@/store/store-object/content-address.md#method-nix-archive)
47-
- [`text`](@docroot@/store/store-object/content-address.md#method-text)
48-
- [`git`](@docroot@/store/store-object/content-address.md#method-git)
49-
50-
Otherwise, `null`.
51-
52-
* `hashAlgo`:
53-
For an output which will be [content addressed], the name of the hash algorithm used.
54-
Valid algorithm strings are:
55-
56-
- `blake3`
57-
- `md5`
58-
- `sha1`
59-
- `sha256`
60-
- `sha512`
61-
62-
* `hash`:
63-
For fixed-output derivations, the expected content hash in base-16.
64-
65-
> **Example**
66-
>
67-
> ```json
68-
> "outputs": {
69-
> "out": {
70-
> "method": "nar",
71-
> "hashAlgo": "sha256",
72-
> "hash": "6fc80dcc62179dbc12fc0b5881275898f93444833d21b89dfe5f7fbcbb1d0d62"
73-
> }
74-
> }
75-
> ```
76-
77-
* `inputSrcs`:
78-
A list of store paths on which this derivation depends.
79-
80-
> **Example**
81-
>
82-
> ```json
83-
> "inputSrcs": [
84-
> "47y241wqdhac3jm5l7nv0x4975mb1975-separate-debug-info.sh",
85-
> "56d0w71pjj9bdr363ym3wj1zkwyqq97j-fix-pop-var-context-error.patch"
86-
> ]
87-
> ```
88-
89-
* `inputDrvs`:
90-
A JSON object specifying the derivations on which this derivation depends, and what outputs of those derivations.
91-
92-
> **Example**
93-
>
94-
> ```json
95-
> "inputDrvs": {
96-
> "6lkh5yi7nlb7l6dr8fljlli5zfd9hq58-curl-7.73.0.drv": ["dev"],
97-
> "fn3kgnfzl5dzym26j8g907gq3kbm8bfh-unzip-6.0.drv": ["out"]
98-
> }
99-
> ```
100-
101-
specifies that this derivation depends on the `dev` output of `curl`, and the `out` output of `unzip`.
102-
103-
* `system`:
104-
The system type on which this derivation is to be built
105-
(e.g. `x86_64-linux`).
106-
107-
* `builder`:
108-
The absolute path of the program to be executed to run the build.
109-
Typically this is the `bash` shell
110-
(e.g. `/nix/store/r3j288vpmczbl500w6zz89gyfa4nr0b1-bash-4.4-p23/bin/bash`).
111-
112-
* `args`:
113-
The command-line arguments passed to the `builder`.
114-
115-
* `env`:
116-
The environment passed to the `builder`.
117-
118-
* `structuredAttrs`:
119-
[Structured Attributes](@docroot@/store/derivation/index.md#structured-attrs), only defined if the derivation contains them.
120-
Structured attributes are JSON, and thus embedded as-is.
6+
[JSON Schema for Derivation v3](schema/derivation-v3.json)
7+
-->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# For some reason, backticks in the JSON schema are being escaped rather
2+
# than being kept as intentional code spans. This removes all backtick
3+
# escaping, which is an ugly solution, but one that is fine, because we
4+
# are not using backticks for any other purpose.
5+
s/\\`/`/g
6+
7+
# The way that semi-external references are rendered (i.e. ones to
8+
# sibling schema files, as opposed to separate website ones, is not nice
9+
# for humans. Replace it with a nice relative link within the manual
10+
# instead.
11+
#
12+
# As we have more such relative links, more replacements of this nature
13+
# should appear below.
14+
s^\(./hash-v1.yaml\)\?#/$defs/algorithm^[JSON format for `Hash`](./hash.html#algorithm)^g
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{{#include hash-v1-fixed.md}}
2+
3+
<!--
4+
## Raw Schema
5+
6+
[JSON Schema for Hash v1](schema/hash-v1.json)
7+
-->
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Configuration file for json-schema-for-humans
2+
#
3+
# https://github.com/coveooss/json-schema-for-humans/blob/main/docs/examples/examples_md_default/Configuration.md
4+
5+
template_name: md
6+
show_toc: true
7+
# impure timestamp and distracting
8+
with_footer: false
9+
recursive_detection_depth: 3
10+
show_breadcrumbs: false
11+
description_is_markdown: true
12+
template_md_options:
13+
properties_table_columns:
14+
- Property
15+
- Type
16+
- Pattern
17+
- Title/Description
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Tests in: ../../../../src/json-schema-checks
2+
3+
fs = import('fs')
4+
5+
# Find json-schema-for-humans if available
6+
json_schema_for_humans = find_program('generate-schema-doc', required : false)
7+
8+
# Configuration for json-schema-for-humans
9+
json_schema_config = files('json-schema-for-humans-config.yaml')
10+
11+
schemas = [
12+
'hash-v1',
13+
'derivation-v3',
14+
]
15+
16+
schema_files = files()
17+
foreach schema_name : schemas
18+
schema_files += files('schema' / schema_name + '.yaml')
19+
endforeach
20+
21+
22+
schema_outputs = []
23+
foreach schema_name : schemas
24+
schema_outputs += schema_name + '.md'
25+
endforeach
26+
27+
json_schema_generated_files = []
28+
29+
# Generate markdown documentation from JSON schema
30+
# Note: output must be just a filename, not a path
31+
gen_file = custom_target(
32+
schema_name + '-schema-docs.tmp',
33+
command : [
34+
json_schema_for_humans,
35+
'--config-file',
36+
json_schema_config,
37+
meson.current_source_dir() / 'schema',
38+
meson.current_build_dir(),
39+
],
40+
input : schema_files + [
41+
json_schema_config,
42+
],
43+
output : schema_outputs,
44+
capture : false,
45+
build_by_default : true,
46+
)
47+
48+
idx = 0
49+
if json_schema_for_humans.found()
50+
foreach schema_name : schemas
51+
#schema_file = 'schema' / schema_name + '.yaml'
52+
53+
# There is one so-so hack, and one horrible hack being done here.
54+
sedded_file = custom_target(
55+
schema_name + '-schema-docs',
56+
command : [
57+
'sed',
58+
'-f',
59+
# Out of line to avoid https://github.com/mesonbuild/meson/issues/1564
60+
files('fixup-json-schema-generated-doc.sed'),
61+
'@INPUT@',
62+
],
63+
capture : true,
64+
input : gen_file[idx],
65+
output : schema_name + '-fixed.md',
66+
)
67+
idx += 1
68+
json_schema_generated_files += [ sedded_file ]
69+
endforeach
70+
else
71+
warning(
72+
'json-schema-for-humans not found, skipping JSON schema documentation generation',
73+
)
74+
endif

0 commit comments

Comments
 (0)