Skip to content

Commit

Permalink
Document store object content addressing & improve JSON format
Browse files Browse the repository at this point in the history
The JSON format no longer uses the legacy ATerm `r:` prefixing nonsese,
but separate fields.

Progress on #9866
  • Loading branch information
Ericson2314 committed May 17, 2024
1 parent ba2911b commit e0c9e21
Show file tree
Hide file tree
Showing 20 changed files with 286 additions and 63 deletions.
1 change: 1 addition & 0 deletions doc/manual/src/SUMMARY.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [File System Object](store/file-system-object.md)
- [Content-Addressing File System Objects](store/file-system-object/content-address.md)
- [Store Object](store/store-object.md)
- [Content-Addressing Store Objects](store/store-object/content-address.md)
- [Store Path](store/store-path.md)
- [Store Types](store/types/index.md)
{{#include ./store/types/SUMMARY.md}}
Expand Down
36 changes: 19 additions & 17 deletions doc/manual/src/language/advanced-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,30 +197,32 @@ Derivations can declare some infrequently used optional attributes.
`outputHashAlgo` can only be `null` when `outputHash` follows the SRI format.

The `outputHashMode` attribute determines how the hash is computed.
It must be one of the following two values:
It must be one of the following values:

<!-- FIXME link to store object content-addressing not file system object content addressing once we have the page for that. -->
- [`"flat"`](@docroot@/store/store-object/content-address.md#method-flat)

- `"flat"`
This is the default.

The output must be a non-executable regular file; if it isn’t, the build fails.
The hash is
[simply computed over the contents of that file](@docroot@/store/file-system-object/content-address.md#serial-flat)
(so it’s equal to what Unix commands like `sha256sum` or `sha1sum` produce).
- [`"recursive"` or `"nar"`](@docroot@/store/store-object/content-address.md#method-nix-archive)

This is the default.
> **Compatibility**
>
> `"recursive"` is the traditional way of indicating this,
> and is supported since 2005 (virtually the entire history of Nix).
> `"nar"` is more clear, and consistent with other parts of Nix (such as the CLI),
> however support for it is only added in Nix version 2.21.
- [`"text"`](@docroot@/store/store-object/content-address.md#method-text)

- `"recursive"` or `"nar"`
> **Warning**
>
> The use of this method for derivation outputs is part of the [`dynamic-derivations`][xp-feature-dynamic-derivations] experimental feature.
The hash is computed over the
[Nix Archive (NAR)](@docroot@/store/file-system-object/content-address.md#serial-nix-archive)
dump of the output (i.e., the result of [`nix-store --dump`](@docroot@/command-ref/nix-store/dump.md)).
In this case, the output is allowed to be any [file system object], including directories and more.
- [`"git"`](@docroot@/store/store-object/content-address.md#method-git)

`"recursive"` is the traditional way of indicating this,
and is supported since 2005 (virtually the entire history of Nix).
`"nar"` is more clear, and consistent with other parts of Nix (such as the CLI),
however support for it is only added in Nix version 2.21.
> **Warning**
>
> This method is part of the [`git-hashing`][xp-feature-git-hashing] experimental feature.
- [`__contentAddressed`]{#adv-attr-__contentAddressed}
> **Warning**
Expand Down
27 changes: 24 additions & 3 deletions doc/manual/src/protocols/json/derivation.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,30 @@ is a JSON object with the following fields:
Information about the output paths of the derivation.
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:

* `path`: The output path.
* `path`:
The output path, if it is known in advanced.
Otherwise, `null`.


* `method`:
For an output which will be [content addresed], a string representing the [method](@docroot@/store/store-object/content-address.md) of content addressing that is chosen.
Valid method strings are:

- [`flat`](@docroot@/store/store-object/content-address.md#method-flat)
- [`nar`](@docroot@/store/store-object/content-address.md#method-nix-archive)
- [`text`](@docroot@/store/store-object/content-address.md#method-text)
- [`git`](@docroot@/store/store-object/content-address.md#method-git)

Otherwise, `null`.

* `hashAlgo`:
For fixed-output derivations, the hashing algorithm (e.g. `sha256`), optionally prefixed by `r:` if `hash` denotes a NAR hash rather than a flat file hash.
For an output which will be [content addresed], the name of the hash algorithm used.
Valid algorithm strings are:

- `md5`
- `sha1`
- `sha256`
- `sha512`

* `hash`:
For fixed-output derivations, the expected content hash in base-16.
Expand All @@ -32,7 +52,8 @@ is a JSON object with the following fields:
> "outputs": {
> "out": {
> "path": "/nix/store/2543j7c6jn75blc3drf4g5vhb1rhdq29-source",
> "hashAlgo": "r:sha256",
> "method": "nar",
> "hashAlgo": "sha256",
> "hash": "6fc80dcc62179dbc12fc0b5881275898f93444833d21b89dfe5f7fbcbb1d0d62"
> }
> }
Expand Down
25 changes: 17 additions & 8 deletions doc/manual/src/protocols/store-path.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,36 @@ where
- `type` = one of:

- ```ebnf
| "text" ( ":" store-path )*
| "text" { ":" store-path }
```

for encoded derivations written to the store.
This is for the
["Text"](@docroot@/store/store-object/content-address.md#method-text)
method of content addressing store objects.
The optional trailing store paths are the references of the store object.

- ```ebnf
| "source" ( ":" store-path )*
| "source" { ":" store-path } [ ":self" ]
```

For paths copied to the store and hashed via a [Nix Archive (NAR)] and [SHA-256][sha-256].
Just like in the text case, we can have the store objects referenced by their paths.
This is for the
["Nix Archive"](@docroot@/store/store-object/content-address.md#method-nix-archive)
method of content addressing store objects,
if the hash algorithm is [SHA-256].
Just like in the "Text" case, we can have the store objects referenced by their paths.
Additionally, we can have an optional `:self` label to denote self reference.

- ```ebnf
| "output:" id
```

For either the outputs built from derivations,
paths copied to the store hashed that area single file hashed directly, or the via a hash algorithm other than [SHA-256][sha-256].
(in that case "source" is used; this is only necessary for compatibility).
or content-addressed store objects that are not using one of the two above cases.
To be explicit about the latter, that is currently these methods:

- ["Flat"](@docroot@/store/store-object/content-address.md#method-flat)
- ["Git"](@docroot@/store/store-object/content-address.md#method-git)
- ["Nix Archive"](@docroot@/store/store-object/content-address.md#method-nix-archive) if the hash algorithm is not [SHA-256].

`id` is the name of the output (usually, "out").
For content-addressed store objects, `id`, is always "out".
Expand Down Expand Up @@ -116,7 +125,7 @@ where
Also note that NAR + SHA-256 must not use this case, and instead must use the `type` = `"source:" ...` case.

[Nix Archive (NAR)]: @docroot@/store/file-system-object/content-address.md#serial-nix-archive
[sha-256]: https://en.m.wikipedia.org/wiki/SHA-256
[SHA-256]: https://en.m.wikipedia.org/wiki/SHA-256

### Historical Note

Expand Down
9 changes: 7 additions & 2 deletions doc/manual/src/store/file-system-object/content-address.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Content-Addressing File System Objects

For many operations, Nix needs to calculate [a content addresses](@docroot@/glossary.md#gloss-content-address) of [a file system object][file system object].
Usually this is needed as part of content addressing [store objects], since store objects always have a root file system object.
Usually this is needed as part of
[content addressing store objects](../store-object/content-address.md),
since store objects always have a root file system object.
But some command-line utilities also just work on "raw" file system objects, not part of any store object.

Every content addressing scheme Nix uses ultimately involves feeding data into a [hash function](https://en.wikipedia.org/wiki/Hash_function), and getting back an opaque fixed-size digest which is deemed a content address.
Expand All @@ -18,6 +20,9 @@ A single file object can just be hashed by its contents.
This is not enough information to encode the fact that the file system object is a file,
but if we *already* know that the FSO is a single non-executable file by other means, it is sufficient.

Because the hashed data is just the raw file, as is, this choice is good for compatibility with other systems.
For example, Unix commands like `sha256sum` or `sha1sum` will produce hashes for single files that match this.

### Nix Archive (NAR) { #serial-nix-archive }

For the other cases of [file system objects][file system object], especially directories with arbitrary descendents, we need a more complex serialisation format.
Expand Down Expand Up @@ -69,7 +74,7 @@ every non-directory object is owned by a parent directory, and the entry that re
However, if the root object is not a directory, then we have no way of knowing which one of an executable file, non-executable file, or symlink it is supposed to be.

In response to this, we have decided to treat a bare file as non-executable file.
This is similar to do what we do with [flat serialisation](#flat), which also lacks this information.
This is similar to do what we do with [flat serialisation](#serial-flat), which also lacks this information.
To avoid an address collision, attempts to hash a bare executable file or symlink will result in an error (just as would happen for flat serialisation also).
Thus, Git can encode some, but not all of Nix's "File System Objects", and this sort of content-addressing is likewise partial.

Expand Down
160 changes: 160 additions & 0 deletions doc/manual/src/store/store-object/content-address.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# Content-Addressing Store Objects

Just [like][fso-ca] [File System Objects][File System Object],
[Store Objects][Store Object] can also be [content addressed](@docroot@/glossary.md#gloss-content-addressed).

For store objects, the content address we produce will take the form of a [Store Path] rather than regular hash.
In particular, the content-addressing scheme will ensure that the digest of the store path is solely computed from the

- file system object graph (the root one and its children, if it has any)
- references
- [store directory](../store-path.md#store-directory)
- name

of the store object, and not any other information, which would not be an intrinsic property of that store object.

For the full specification of the algorithms involved, see the [specification of store path digests][sp-spec].

[File System Object]: ../file-system-object.md
[Store Object]: ../store-object.md
[Store Path]: ../store-path.md

## Content addressing each part of a store object

### File System Objects

With all currently supported store object content addressing methods, the file system object is always content-addressed first, and then that hash incorporated in further calculations.
As such, the conte-addressing of the file system objects part of a store object is exactly as described in the [section on content-addressing file system objects][fso-ca].

### References

With all currently supported store object content addressing methods,
other objects are referred to by their regular (string-encoded-) [store paths][Store Path].

Self-references however cannot be referred to by their path, because we are in the midst of describing how to compute that path!

> The alternative would require finding as hash function fixed point, i.e. the solution to an equation in the form
> ```
> digest = hash(..... || digest || ....)
> ```
> which is computationally infeasible, being no easier than finding a hash collision.
Instead we just have a "has self reference" boolean, which will end up effecting the digest.

### Name and Store Directory

These two items affect the digest in a way that is standard for store path digest computations and not specific to content-addressing.
Consult the [specification of store path digests][sp-spec] for further details.

## Content addressing Methods

For historical reasons, we don't support all features in all combinations.
Each currently supported method of content addressing chooses a single method of file system object hashing, and may offer some restrictions on references.
The names and store directories are unrestricted however.

### Flat { #method-flat }

This uses the corresponding [Flat](../file-system-object/content-address.md#serial-flat) method of file system object content addressing.

References are not supported.

### Text { #method-text }

This also uses the corresponding [Flat](../file-system-object/content-address.md#serial-flat) method of file system object content addressing.

References to other store objects are supported, but not self reference are not.

This is the only store-object content-addressing method that is not named identically with a corresponding file system object method.
It is somewhat obscure, mainly used for "drv files"
(derivations serialized as store objects in their ["ATerm" file format](@docroot@/protocols/derivation-aterm.md)).
Prefer using another method if possible.

### Nix Archive { #method-nix-archive }

This uses the corresponding [Nix Archive](../file-system-object/content-address.md#serial-nix-archive) method of file system object content addressing.

References (to other store objects and self references alike) are supported so long as the hash algorithm in use is [SHA-256], but not (neither kind) otherwise.

[SHA-256]: https://en.m.wikipedia.org/wiki/SHA-256

### Git { #method-git }

> **Warning**
>
> This method is part of the [`git-hashing`][xp-feature-git-hashing] experimental feature.
This uses the corresponding [Git](../file-system-object/content-address.md#serial-git) method of file system object content addressing.

References are not supported.

Only [SHA-1] is supported at this time.
If SHA-256-based Git becomes more widespread, this restriction would be revisited.

[SHA-1]: https://en.m.wikipedia.org/wiki/SHA-1

The resulting abstract syntax is this:

```idris
data HashType
= MD5
| SHA-1
| SHA-256
| SHA-512
record Hash where
type : HashType
hash : Bytes[hashSize type]
data Method
= NixArchive
| FlatFile
data ContentAddress
= Text {
hash : Hash,
references : Set StorePath
}
| Fixed {
method : Method,
hash : Hash,
references : Set StorePath
hasSelfReference : Bool
} -- has side condition
```

Firstly, note that "text" hashing supports references to other paths, but no "has self reference" boolean:
texted-hashed store object must not have a self reference.
Only regular "fixed" hashing supports the boolean is thus allowed to represent store paths.

There is an additional side condition that a regular "fixed" output addressing only supports references (to self or other objects) if the method is NAR and the hash type is SHA-256.

### Reproducibility

The above system is rather more complex than it needs to be, owning to accretion of features over time.
Still, the fundamental property remains that if one knows how a store object is supposed to be hashed
--- all the non-Hash, non-references information above
--- one can recompute a store object's store path just from that metadata and its content proper (its references and file system objects).
Collectively, we can call this information the "content address method".

By storing the "Content address method" extra information as part of store object
--- making it data not metadata
--- we achieve the key property of making content-addressed store objects *trustless*.

What this is means is that they are just plain old data, not containing any "claim" that could be false.
All this information is free to vary, and if any of it varies one gets (ignoring the possibility of hash collisions, as usual) a different store path.
Store paths referring to content-addressed store objects uniquely identify a store object, and given that object, one can recompute the store path.
Any content-addressed store object purporting to be the referee of a store object can be readily verified to see whether it in fact does without any extra information.
No other party claiming a store object corresponds to a store path need be trusted because this verification can be done instead.

Content addressing currently is used when adding data like source code to the store.
Such data are "basal inputs", not produced from any other derivation (to our knowledge).
Content addressing is thus the only way to address them of our two options.
([Input addressing](@docroot@/glossary.md#gloss-input-addressed-store-object), is only valid for store paths produced from derivations.)

Additionally, content addressing is also used for the outputs of certain sorts of derivations.
It is very nice to be able to uniformly content-address all data rather than rely on a mix of content addressing and input addressing.
This however, is in some cases still experimental, so in practice input addressing is still (as of 2022) widely used.

[fso-ca]: ../file-system-object/content-address.md

[sp-spec]: @docroot@/protocols/store-path.md
12 changes: 6 additions & 6 deletions src/libcmd/misc-store-flags.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,27 @@ Args::Flag contentAddressMethod(ContentAddressMethod * method)
return Args::Flag {
.longName = "mode",
// FIXME indentation carefully made for context, this is messed up.
/* FIXME link to store object content-addressing not file system
object content addressing once we have that page. */
.description = R"(
How to compute the content-address of the store object.
One of:
- `nar` (the default):
- [`nar`](@docroot@/store/store-object/content-address.md#method-nix-archive)
(the default):
Serialises the input as a
[Nix Archive](@docroot@/store/file-system-object/content-address.md#serial-nix-archive)
and passes that to the hash function.
- `flat`:
- [`flat`](@docroot@/store/store-object/content-address.md#method-flat):
Assumes that the input is a single file and
[directly passes](@docroot@/store/file-system-object/content-address.md#serial-flat)
it to the hash function.
- `text`: Like `flat`, but used for
- [`text`](@docroot@/store/store-object/content-address.md#method-text):
Like `flat`, but used for
[derivations](@docroot@/glossary.md#store-derivation) serialized in store object and
[`builtins.toFile`](@docroot@/language/builtins.html#builtins-toFile).
For advanced use-cases only;
for regular usage prefer `nar` and `flat.
for regular usage prefer `nar` and `flat`.
)",
.labels = {"content-address-method"},
.handler = {[method](std::string s) {
Expand Down
Loading

0 comments on commit e0c9e21

Please sign in to comment.