Home Explore Blog CI



nix

1st chunk of `doc/manual/source/protocols/store-path.md`
ffc4982ef3298d98a6bedc8833ed708eb4f559f6615c0e8b0000000100000c3c
# Complete Store Path Calculation

This is the complete specification for how [store path]s are calculated.

The format of this specification is close to [Extended Backus–Naur form](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form), but must deviate for a few things such as hash functions which we treat as bidirectional for specification purposes.

Regular users do *not* need to know this information --- store paths can be treated as black boxes computed from the properties of the store objects they refer to.
But for those interested in exactly how Nix works, e.g. if they are reimplementing it, this information can be useful.


## Store path proper

```ebnf
store-path = store-dir "/" digest "-" name
```
where

- `name` = the name of the store object.

- `store-dir` = the [store directory](@docroot@/store/store-path.md#store-directory)

- `digest` = base-32 representation of the compressed to 160 bits [SHA-256] hash of `fingerprint`

For the definition of the hash compression algorithm, please refer to the section 5.1 of
the [Nix thesis](https://edolstra.github.io/pubs/phd-thesis.pdf), which also defines the
specifics of base-32 encoding. Note that base-32 encoding processes the hash bytestring from
the end, while base-16 processes in from the beginning.

## Fingerprint

- ```ebnf
  fingerprint = type ":sha256:" inner-digest ":" store ":" name
  ```

  Note that it includes the location of the store as well as the name to make sure that changes to either of those are reflected in the hash
  (e.g. you won't get `/nix/store/<digest>-name1` and `/nix/store/<digest>-name2`, or `/gnu/store/<digest>-name1`, with equal hash parts).

- `type` = one of:

  - ```ebnf
    | "text" { ":" store-path }
    ```

    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 } [ ":self" ]
    ```

    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,
    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".

- `inner-digest` = base-16 representation of a SHA-256 hash of `inner-fingerprint`.

Title: Complete Specification for Store Path Calculation
Summary
This section provides a detailed specification for how store paths are calculated in Nix, using a format similar to Extended Backus–Naur form. It covers the structure of a store path, including the store directory, digest (SHA-256 hash of the fingerprint), and name. The specification also defines the fingerprint, which includes the type (text, source, or output), inner digest, store location, and name, ensuring that changes to any of these components are reflected in the hash. Different content addressing methods, such as Text, Nix Archive, Flat, and Git, are considered in determining the fingerprint.