Skip to content

Commit ef8218f

Browse files
authored
Merge pull request #14307 from NixOS/json-schema-hash
`nlohmann::json` instance and JSON Schema for `Hash`
2 parents ada008a + 5e7ee80 commit ef8218f

File tree

16 files changed

+252
-24
lines changed

16 files changed

+252
-24
lines changed

doc/manual/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ manual = custom_target(
8888
@0@ @INPUT0@ @CURRENT_SOURCE_DIR@ > @DEPFILE@
8989
@0@ @INPUT1@ summary @2@ < @CURRENT_SOURCE_DIR@/source/SUMMARY.md.in > @2@/source/SUMMARY.md
9090
sed -e 's|@version@|@3@|g' < @INPUT2@ > @2@/book.toml
91-
@4@ -r --include='*.md' @CURRENT_SOURCE_DIR@/ @2@/
91+
@4@ -r -L --include='*.md' @CURRENT_SOURCE_DIR@/ @2@/
9292
(cd @2@; RUST_LOG=warn @1@ build -d @2@ 3>&2 2>&1 1>&3) | { grep -Fv "because fragment resolution isn't implemented" || :; } 3>&2 2>&1 1>&3
9393
rm -rf @2@/manual
9494
mv @2@/html @2@/manual

doc/manual/package.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ mkMesonDerivation (finalAttrs: {
3333
fileset.difference
3434
(fileset.unions [
3535
../../.version
36+
# For example JSON
37+
../../src/libutil-tests/data/hash
3638
# Too many different types of files to filter for now
3739
../../doc/manual
3840
./.

doc/manual/source/protocols/json/hash.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
{{#include hash-v1-fixed.md}}
22

3+
## Examples
4+
5+
### SHA-256 with Base64 encoding
6+
7+
```json
8+
{{#include schema/hash-v1/sha256-base64.json}}
9+
```
10+
11+
### SHA-256 with Base16 (hexadecimal) encoding
12+
13+
```json
14+
{{#include schema/hash-v1/sha256-base16.json}}
15+
```
16+
17+
### SHA-256 with Nix32 encoding
18+
19+
```json
20+
{{#include schema/hash-v1/sha256-nix32.json}}
21+
```
22+
23+
### BLAKE3 with Base64 encoding
24+
25+
```json
26+
{{#include schema/hash-v1/blake3-base64.json}}
27+
```
28+
329
<!--
430
## Raw Schema
531
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../../../src/libutil-tests/data/hash/

doc/manual/source/protocols/json/schema/hash-v1.yaml

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,38 @@ description: |
55
A cryptographic hash value used throughout Nix for content addressing and integrity verification.
66
77
This schema describes the JSON representation of Nix's `Hash` type.
8-
9-
TODO Work in progress
108
type: object
119
properties:
1210
algorithm:
13-
title: Hash algorithm
1411
"$ref": "#/$defs/algorithm"
12+
format:
13+
type: string
14+
enum:
15+
- base64
16+
- nix32
17+
- base16
18+
- sri
19+
title: Hash format
20+
description: |
21+
The encoding format of the hash value.
22+
23+
- `base64` uses standard Base64 encoding [RFC 4648, section 4](https://datatracker.ietf.org/doc/html/rfc4648#section-4)
24+
- `nix32` is Nix-specific base-32 encoding
25+
- `base16` is lowercase hexadecimal
26+
- `sri` is the [Subresource Integrity format](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity).
27+
hash:
28+
type: string
29+
title: Hash
30+
description: |
31+
The encoded hash value, itself.
32+
33+
It is specified in the format specified by the `format` field.
34+
It must be the right length for the hash algorithm specified in the `algorithm` field, also.
35+
The hash value does not include any algorithm prefix.
1536
required:
1637
- algorithm
38+
- format
39+
- hash
1740
additionalProperties: false
1841
"$defs":
1942
algorithm:
@@ -24,6 +47,7 @@ additionalProperties: false
2447
- sha1
2548
- sha256
2649
- sha512
50+
title: Hash algorithm
2751
description: |
2852
The hash algorithm used to compute the hash value.
2953

src/json-schema-checks/hash

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../src/libutil-tests/data/hash

src/json-schema-checks/meson.build

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ schema_dir = meson.current_source_dir() / 'schema'
2020

2121
# Get all example files
2222
schemas = [
23+
{
24+
'stem' : 'hash',
25+
'schema' : schema_dir / 'hash-v1.yaml',
26+
'files' : [
27+
'sha256-base64.json',
28+
'sha256-base16.json',
29+
'sha256-nix32.json',
30+
'blake3-base64.json',
31+
],
32+
},
2333
{
2434
'stem' : 'derivation',
2535
'schema' : schema_dir / 'derivation-v3.yaml',

src/json-schema-checks/package.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mkMesonDerivation (finalAttrs: {
2020
fileset = lib.fileset.unions [
2121
../../.version
2222
../../doc/manual/source/protocols/json/schema
23+
../../src/libutil-tests/data/hash
2324
../../src/libstore-tests/data/derivation
2425
./.
2526
];
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"algorithm": "blake3",
3+
"format": "base64",
4+
"hash": "nnDuFEmWX7YtBJBAoe0G7Dd0MNpuwTFz58T//NKL6YA="
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"algorithm": "sha256",
3+
"format": "base16",
4+
"hash": "f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b"
5+
}

0 commit comments

Comments
 (0)