2020-07-24 13:56:19 +03:00
|
|
|
|
# Name
|
2020-07-23 13:58:42 +03:00
|
|
|
|
|
2020-07-24 13:56:19 +03:00
|
|
|
|
`nix-hash` - compute the cryptographic hash of a path
|
2020-07-23 13:58:42 +03:00
|
|
|
|
|
2020-07-24 13:56:19 +03:00
|
|
|
|
# Synopsis
|
2020-07-23 13:58:42 +03:00
|
|
|
|
|
2020-07-24 13:56:19 +03:00
|
|
|
|
`nix-hash` [`--flat`] [`--base32`] [`--truncate`] [`--type` *hashAlgo*] *path…*
|
2020-07-23 13:58:42 +03:00
|
|
|
|
|
2023-03-15 18:33:58 +02:00
|
|
|
|
`nix-hash` [`--to-base16`|`--to-base32`|`--to-base64`|`--to-sri`] [`--type` *hashAlgo*] *hash…*
|
2020-07-23 13:58:42 +03:00
|
|
|
|
|
|
|
|
|
# Description
|
|
|
|
|
|
|
|
|
|
The command `nix-hash` computes the cryptographic hash of the contents
|
2020-07-23 15:28:05 +03:00
|
|
|
|
of each *path* and prints it on standard output. By default, it computes
|
2020-07-23 13:58:42 +03:00
|
|
|
|
an MD5 hash, but other hash algorithms are available as well. The hash
|
|
|
|
|
is printed in hexadecimal. To generate the same hash as
|
|
|
|
|
`nix-prefetch-url` you have to specify multiple arguments, see below for
|
|
|
|
|
an example.
|
|
|
|
|
|
2020-07-24 16:46:16 +03:00
|
|
|
|
The hash is computed over a *serialisation* of each path: a dump of
|
|
|
|
|
the file system tree rooted at the path. This allows directories and
|
|
|
|
|
symlinks to be hashed as well as regular files. The dump is in the
|
2024-04-10 00:07:39 +03:00
|
|
|
|
*[Nix Archive (NAR)][Nix Archive] format* produced by [`nix-store
|
2023-03-23 17:27:41 +02:00
|
|
|
|
--dump`](@docroot@/command-ref/nix-store/dump.md). Thus, `nix-hash path`
|
2020-07-24 16:46:16 +03:00
|
|
|
|
yields the same cryptographic hash as `nix-store --dump path |
|
|
|
|
|
md5sum`.
|
2020-07-23 13:58:42 +03:00
|
|
|
|
|
2024-04-10 00:07:39 +03:00
|
|
|
|
[Nix Archive]: @docroot@/store/file-system-object/content-address.md#serial-nix-archive
|
|
|
|
|
|
2020-07-23 13:58:42 +03:00
|
|
|
|
# Options
|
|
|
|
|
|
2021-04-23 15:30:42 +03:00
|
|
|
|
- `--flat`\
|
2024-04-10 00:07:39 +03:00
|
|
|
|
Print the cryptographic hash of the contents of each regular file *path*.
|
|
|
|
|
That is, instead of computing
|
|
|
|
|
the hash of the [Nix Archive (NAR)](@docroot@/store/file-system-object/content-address.md#serial-nix-archive) of *path*,
|
|
|
|
|
just [directly hash]((@docroot@/store/file-system-object/content-address.md#serial-flat) *path* as is.
|
|
|
|
|
This requires *path* to resolve to a regular file rather than directory.
|
2020-07-23 15:28:05 +03:00
|
|
|
|
The result is identical to that produced by the GNU commands
|
|
|
|
|
`md5sum` and `sha1sum`.
|
2020-07-23 13:58:42 +03:00
|
|
|
|
|
2023-03-15 18:33:58 +02:00
|
|
|
|
- `--base16`\
|
|
|
|
|
Print the hash in a hexadecimal representation (default).
|
|
|
|
|
|
2021-04-23 15:30:42 +03:00
|
|
|
|
- `--base32`\
|
2020-07-23 13:58:42 +03:00
|
|
|
|
Print the hash in a base-32 representation rather than hexadecimal.
|
|
|
|
|
This base-32 representation is more compact and can be used in Nix
|
|
|
|
|
expressions (such as in calls to `fetchurl`).
|
|
|
|
|
|
2023-03-15 18:33:58 +02:00
|
|
|
|
- `--base64`\
|
|
|
|
|
Similar to --base32, but print the hash in a base-64 representation,
|
|
|
|
|
which is more compact than the base-32 one.
|
|
|
|
|
|
|
|
|
|
- `--sri`\
|
|
|
|
|
Print the hash in SRI format with base-64 encoding.
|
|
|
|
|
The type of hash algorithm will be prepended to the hash string,
|
|
|
|
|
followed by a hyphen (-) and the base-64 hash body.
|
|
|
|
|
|
2021-04-23 15:30:42 +03:00
|
|
|
|
- `--truncate`\
|
2020-07-23 13:58:42 +03:00
|
|
|
|
Truncate hashes longer than 160 bits (such as SHA-256) to 160 bits.
|
|
|
|
|
|
2021-04-23 15:30:42 +03:00
|
|
|
|
- `--type` *hashAlgo*\
|
2020-07-23 13:58:42 +03:00
|
|
|
|
Use the specified cryptographic hash algorithm, which can be one of
|
2020-10-26 00:16:53 +02:00
|
|
|
|
`md5`, `sha1`, `sha256`, and `sha512`.
|
2020-07-23 13:58:42 +03:00
|
|
|
|
|
2021-04-23 15:30:42 +03:00
|
|
|
|
- `--to-base16`\
|
2020-07-23 13:58:42 +03:00
|
|
|
|
Don’t hash anything, but convert the base-32 hash representation
|
2020-07-23 15:28:05 +03:00
|
|
|
|
*hash* to hexadecimal.
|
2020-07-23 13:58:42 +03:00
|
|
|
|
|
2021-04-23 15:30:42 +03:00
|
|
|
|
- `--to-base32`\
|
2020-07-23 13:58:42 +03:00
|
|
|
|
Don’t hash anything, but convert the hexadecimal hash representation
|
2020-07-23 15:28:05 +03:00
|
|
|
|
*hash* to base-32.
|
2020-07-23 13:58:42 +03:00
|
|
|
|
|
2023-03-15 18:33:58 +02:00
|
|
|
|
- `--to-base64`\
|
|
|
|
|
Don’t hash anything, but convert the hexadecimal hash representation
|
|
|
|
|
*hash* to base-64.
|
|
|
|
|
|
|
|
|
|
- `--to-sri`\
|
|
|
|
|
Don’t hash anything, but convert the hexadecimal hash representation
|
|
|
|
|
*hash* to SRI.
|
|
|
|
|
|
2020-07-23 13:58:42 +03:00
|
|
|
|
# Examples
|
|
|
|
|
|
|
|
|
|
Computing the same hash as `nix-prefetch-url`:
|
|
|
|
|
|
2020-07-31 16:43:25 +03:00
|
|
|
|
```console
|
|
|
|
|
$ nix-prefetch-url file://<(echo test)
|
|
|
|
|
1lkgqb6fclns49861dwk9rzb6xnfkxbpws74mxnx01z9qyv1pjpj
|
|
|
|
|
$ nix-hash --type sha256 --flat --base32 <(echo test)
|
|
|
|
|
1lkgqb6fclns49861dwk9rzb6xnfkxbpws74mxnx01z9qyv1pjpj
|
|
|
|
|
```
|
2020-07-23 13:58:42 +03:00
|
|
|
|
|
|
|
|
|
Computing hashes:
|
|
|
|
|
|
2020-07-31 16:43:25 +03:00
|
|
|
|
```console
|
|
|
|
|
$ mkdir test
|
|
|
|
|
$ echo "hello" > test/world
|
2020-07-24 13:56:19 +03:00
|
|
|
|
|
2020-07-31 16:43:25 +03:00
|
|
|
|
$ nix-hash test/ (MD5 hash; default)
|
|
|
|
|
8179d3caeff1869b5ba1744e5a245c04
|
2020-07-24 13:56:19 +03:00
|
|
|
|
|
2020-07-31 16:43:25 +03:00
|
|
|
|
$ nix-store --dump test/ | md5sum (for comparison)
|
|
|
|
|
8179d3caeff1869b5ba1744e5a245c04 -
|
2020-07-24 13:56:19 +03:00
|
|
|
|
|
2020-07-31 16:43:25 +03:00
|
|
|
|
$ nix-hash --type sha1 test/
|
|
|
|
|
e4fd8ba5f7bbeaea5ace89fe10255536cd60dab6
|
2020-07-24 13:56:19 +03:00
|
|
|
|
|
2023-03-15 18:33:58 +02:00
|
|
|
|
$ nix-hash --type sha1 --base16 test/
|
|
|
|
|
e4fd8ba5f7bbeaea5ace89fe10255536cd60dab6
|
|
|
|
|
|
2020-07-31 16:43:25 +03:00
|
|
|
|
$ nix-hash --type sha1 --base32 test/
|
|
|
|
|
nvd61k9nalji1zl9rrdfmsmvyyjqpzg4
|
2020-07-24 13:56:19 +03:00
|
|
|
|
|
2023-03-15 18:33:58 +02:00
|
|
|
|
$ nix-hash --type sha1 --base64 test/
|
|
|
|
|
5P2Lpfe76upazon+ECVVNs1g2rY=
|
|
|
|
|
|
|
|
|
|
$ nix-hash --type sha1 --sri test/
|
|
|
|
|
sha1-5P2Lpfe76upazon+ECVVNs1g2rY=
|
|
|
|
|
|
2020-07-31 16:43:25 +03:00
|
|
|
|
$ nix-hash --type sha256 --flat test/
|
|
|
|
|
error: reading file `test/': Is a directory
|
2020-07-24 13:56:19 +03:00
|
|
|
|
|
2020-07-31 16:43:25 +03:00
|
|
|
|
$ nix-hash --type sha256 --flat test/world
|
|
|
|
|
5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03
|
|
|
|
|
```
|
2020-07-23 13:58:42 +03:00
|
|
|
|
|
2023-03-15 18:33:58 +02:00
|
|
|
|
Converting between hexadecimal, base-32, base-64, and SRI:
|
2020-07-23 13:58:42 +03:00
|
|
|
|
|
2020-07-31 16:43:25 +03:00
|
|
|
|
```console
|
|
|
|
|
$ nix-hash --type sha1 --to-base32 e4fd8ba5f7bbeaea5ace89fe10255536cd60dab6
|
|
|
|
|
nvd61k9nalji1zl9rrdfmsmvyyjqpzg4
|
2020-07-24 13:56:19 +03:00
|
|
|
|
|
2020-07-31 16:43:25 +03:00
|
|
|
|
$ nix-hash --type sha1 --to-base16 nvd61k9nalji1zl9rrdfmsmvyyjqpzg4
|
|
|
|
|
e4fd8ba5f7bbeaea5ace89fe10255536cd60dab6
|
2023-03-15 18:33:58 +02:00
|
|
|
|
|
|
|
|
|
$ nix-hash --type sha1 --to-base64 e4fd8ba5f7bbeaea5ace89fe10255536cd60dab6
|
|
|
|
|
5P2Lpfe76upazon+ECVVNs1g2rY=
|
|
|
|
|
|
|
|
|
|
$ nix-hash --type sha1 --to-sri nvd61k9nalji1zl9rrdfmsmvyyjqpzg4
|
|
|
|
|
sha1-5P2Lpfe76upazon+ECVVNs1g2rY=
|
|
|
|
|
|
|
|
|
|
$ nix-hash --to-base16 sha1-5P2Lpfe76upazon+ECVVNs1g2rY=
|
|
|
|
|
e4fd8ba5f7bbeaea5ace89fe10255536cd60dab6
|
2020-07-31 16:43:25 +03:00
|
|
|
|
```
|