Nix Archive (NAR) is the archive format of the Nix package manager. A NAR contains a single file system object: a regular file, a symbolic link, or a directory with everything in it. Besides names, file contents and symlink targets, it only stores which regular files are executable. There are no timestamps and no other permissions.
Unlike TAR or ZIP, the format is canonical: a given file system object always has the same NAR. For example, directory entries are always sorted by name. Nix identifies the contents of each store path by the SHA-256 hash of its NAR, and binary caches serve store paths as NARs, usually compressed. GNU Guix uses the same format.
This Kaitai Struct implementation rejects the same NARs as Nix 2.36.0
with the default settings on Linux, except that it accepts entry names
that contain /.
Older versions of Nix may accept more NARs. For example, Nix checks the order of directory entries only since version 2.25.0, and it limits the length of names and symlink targets and the nesting depth only since 2.35.0. Some of these checks were also backported to patch releases of older versions.
This page hosts a formal specification of Nix Archive (NAR) using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.
meta:
id: nix_nar
title: Nix Archive (NAR)
file-extension: nar
xref:
# Nix 2.36.0
# [sends](https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libstore/binary-cache-store.cc#L266-L271)
# this type for every NAR that it uploads to a binary cache, including
# [compressed](https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libstore/binary-cache-store.cc#L153-L155)
# ones, so a file of this type isn't necessarily a plain NAR.
mime: application/x-nix-nar
license: MIT
endian: le
doc: |
Nix Archive (NAR) is the archive format of the [Nix](https://nixos.org/)
package manager. A NAR contains a single file system object: a regular
file, a symbolic link, or a directory with everything in it. Besides
names, file contents and symlink targets, it only stores which regular
files are executable. There are
[no timestamps and no other permissions](https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/doc/manual/source/command-ref/nix-store/dump.md?plain=1#L15-L19).
Unlike TAR or ZIP, the format is
[canonical](https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/doc/manual/source/command-ref/nix-store/dump.md?plain=1#L21-L27):
a given file system object always has the same NAR. For example,
directory entries are always sorted by name. Nix identifies the contents
of each store path by the SHA-256 hash of its NAR, and
[binary caches](https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/doc/manual/source/protocols/binary-cache/index.md?plain=1#L8-L10)
serve store paths as NARs, usually compressed.
[GNU Guix](https://codeberg.org/guix/guix/src/commit/ab6a9f84e783710335a29f6591f570e5e2df143c/guix/serialization.scm#L441-L446)
uses the same format.
This Kaitai Struct implementation rejects the same NARs as Nix 2.36.0
with the default settings on Linux, except that it accepts entry names
that contain `/`.
Older versions of Nix may accept more NARs. For example, Nix checks the
order of directory entries only since version 2.25.0, and it limits the
length of names and symlink targets and the nesting depth only since
2.35.0. Some of these checks were also backported to patch releases of
older versions.
doc-ref: https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/doc/manual/source/protocols/nix-archive/index.md Git tag "2.36.0"
seq:
- id: magic
type: padded_str
valid:
expr: _.body == 'nix-archive-1'
-affected-by: 1260 # this `valid/expr` doesn't compile in C++11
doc: Magic string, must be `nix-archive-1`.
- id: root_node
type: node(0)
doc: The root of the archive, which is always a single node.
types:
padded_str:
doc: |
A string, prefixed with its length (u8le) and
padded with null bytes to the next 8-byte boundary.
Used for the tokens of the format, such as `nix-archive-1`, `(` and
`type`. They are all ASCII.
-webide-representation: '"{body}"'
seq:
- id: len_body
type: u8
- id: body
size: len_body
type: str
encoding: ASCII
- id: padding
size: (8 - (len_body % 8)) % 8
valid:
# https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libutil/serialise.cc#L586-L595 (Git tag "2.36.0")
expr: '_.size != 0 ? _.max == 0 : true'
padded_bytes:
doc: |
A byte sequence, prefixed with its length (u8le) and
padded with null bytes to the next 8-byte boundary.
Used for entry names and symlink targets, which Nix handles as raw
bytes, without decoding them as text.
-webide-representation: '{body:str}'
seq:
- id: len_body
type: u8
- id: body
size: len_body
- id: padding
size: (8 - (len_body % 8)) % 8
valid:
# https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libutil/serialise.cc#L586-L595 (Git tag "2.36.0")
expr: '_.size != 0 ? _.max == 0 : true'
node:
doc: A single file system node (regular file, directory, or symlink).
-webide-representation: '{type_val} {body}'
params:
- id: depth
type: s4
doc: Nesting depth of the node. The root node has depth 0.
seq:
- size: 0
# Checks the depth before any part of the node is read, like Nix does.
valid:
# Nix 2.35.0 added this limit (it was also backported to the 2.28
# to 2.34 branches):
# https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libutil/archive.cc#L176-L177 (Git tag "2.36.0")
# https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libutil/include/nix/util/archive.hh#L18 (Git tag "2.36.0")
expr: depth < 64
- id: open_paren
type: padded_str
valid:
expr: _.body == '('
-affected-by: 1260 # this `valid/expr` doesn't compile in C++11
doc: Must be `(`, a token starting the node definition.
- id: type_key
type: padded_str
valid:
expr: _.body == 'type'
-affected-by: 1260 # this `valid/expr` doesn't compile in C++11
doc: Must be `type`.
- id: type_val
type: padded_str
valid:
# https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libutil/archive.cc#L289 (Git tag "2.36.0")
expr: |
_.body == 'regular' or
_.body == 'directory' or
_.body == 'symlink'
-affected-by: 1260 # this `valid/expr` doesn't compile in C++11
doc: Type of the node (`regular`, `directory`, or `symlink`).
- id: body
type:
switch-on: type_val.body
cases:
"'directory'": type_directory
"'regular'": type_regular
"'symlink'": type_symlink
- id: close_paren
type: padded_str
valid:
expr: _.body == ')'
-affected-by: 1260 # this `valid/expr` doesn't compile in C++11
if: type_val.body != 'directory'
doc: |
Must be `)`, a token ending the node definition. A directory node
has no `close_paren`: its `)` is read as the terminator at the end
of `body.entries`.
type_directory:
doc: |
A directory node, containing a list of entries. Entries must be
ordered by their names. Each name must be greater than the previous
one when compared lexicographically as arrays of unsigned bytes.
seq:
- id: entries
type: dir_entry
valid:
# https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libutil/archive.cc#L249-L251 (Git tag "2.36.0")
expr: |
_index != 0 and not _.is_terminator
? _.body.name.body > entries[_index - 1].body.name.body
: true
-affected-by:
- 1260 # this `valid/expr` doesn't compile in C++11
- 1275 # this `valid/expr` doesn't compile in Java and C#
repeat: until
repeat-until: _.is_terminator
types:
dir_entry:
doc: |
A single entry within a directory, or the terminator, i.e. the `)`
token that ends the directory node.
-webide-representation: '{body}'
seq:
- id: kind
type: padded_str
valid:
# https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libutil/archive.cc#L235-L239 (Git tag "2.36.0")
expr: _.body == 'entry' or _.body == ')'
-affected-by: 1260 # this `valid/expr` doesn't compile in C++11
doc: Must be `entry` (for a child node) or `)` (for the terminator).
- id: body
type: dir_entry_body
if: not is_terminator
doc: The entry itself. The terminator has no `body`.
instances:
is_terminator:
value: kind.body == ')'
dir_entry_body:
-webide-representation: '{name}: {node}'
seq:
- id: open_paren
type: padded_str
valid:
expr: _.body == '('
-affected-by: 1260 # this `valid/expr` doesn't compile in C++11
- id: name_key
type: padded_str
valid:
expr: _.body == 'name'
-affected-by: 1260 # this `valid/expr` doesn't compile in C++11
- id: name
type: padded_bytes
valid:
# https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libutil/archive.cc#L245-L248 (Git tag "2.36.0")
#
# The 255-byte limit is new in Nix 2.35.0 (and backported to the
# 2.28 to 2.34 branches):
# https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libutil/archive.cc#L185 (Git tag "2.36.0")
expr: |
_.body.size != 0 and
_.body.size <= 255 and
_.body != [0x2e] and
_.body != [0x2e, 0x2e] and
_.body.min != 0
-affected-by: 1260 # this `valid/expr` doesn't compile in C++11
doc: |
Name of the entry, as raw bytes. Nix 2.36.0
[doesn't decode names as text](https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libutil/archive.cc#L245-L248)
either, and real NARs contain names that aren't valid UTF-8.
The name must be 1 to 255 bytes long, must not be `.` or `..`, and
must not contain a null byte. Nix also rejects names that contain
`/`, but this Kaitai Struct implementation doesn't check that: as
of KS 0.11, expressions can't search a byte array for a given
byte. A null byte can be detected anyway: 0 is the smallest
possible byte value, so a name contains a null byte exactly if its
smallest byte is 0.
- id: node_key
type: padded_str
valid:
expr: _.body == 'node'
-affected-by: 1260 # this `valid/expr` doesn't compile in C++11
- id: node
type: node(_parent._parent._parent.depth + 1)
doc: The child node.
- id: close_paren
type: padded_str
valid:
expr: _.body == ')'
-affected-by: 1260 # this `valid/expr` doesn't compile in C++11
type_regular:
doc: A regular file node.
-webide-representation: '{file_data}'
seq:
- id: first_key
type: padded_str
valid:
expr: _.body == 'executable' or _.body == 'contents'
-affected-by: 1260 # this `valid/expr` doesn't compile in C++11
doc: Either `executable` or `contents`.
- id: executable_val
type: padded_str
valid:
expr: _.body == ''
-affected-by: 1260 # this `valid/expr` doesn't compile in C++11
if: is_executable
doc: Must be empty.
- id: contents_key
type: padded_str
valid:
expr: _.body == 'contents'
-affected-by: 1260 # this `valid/expr` doesn't compile in C++11
if: is_executable
- id: file_data
type: file_content
instances:
is_executable:
value: first_key.body == 'executable'
doc: True if the file has the `executable` attribute.
types:
file_content:
doc: |
The contents of the file, prefixed with their length and padded
with null bytes to the next 8-byte boundary.
-webide-representation: '{len_contents:dec} bytes'
seq:
- id: len_contents
type: u8
# The `if` is always false, so this field reads nothing. Its only
# purpose is to evaluate `nar_offset` right before `contents`.
- size: 0
if: nar_offset < 0
- id: contents
size: len_contents
-affected-by: 133 # lazy parsing of `seq` fields
doc: |
This Kaitai Struct implementation keeps the contents of every
file in memory, so parsing a NAR needs at least as much memory
as all its files together.
- id: padding
size: (8 - (len_contents % 8)) % 8
valid:
# https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libutil/serialise.cc#L586-L595 (Git tag "2.36.0")
expr: '_.size != 0 ? _.max == 0 : true'
instances:
nar_offset:
value: _io.pos
doc: |
Offset of `contents` in the stream. It corresponds to
[`narOffset`](https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libutil/include/nix/util/source-accessor.hh#L134-L138)
in Nix.
type_symlink:
doc: A symbolic link node.
-webide-representation: '-> {target_val}'
seq:
- id: target_key
type: padded_str
valid:
expr: _.body == 'target'
-affected-by: 1260 # this `valid/expr` doesn't compile in C++11
doc: Must be `target`.
- id: target_val
type: padded_bytes
valid:
# Nix 2.35.0 added these checks (they were also backported to the
# 2.28 to 2.34 branches):
# https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libutil/archive.cc#L280-L282 (Git tag "2.36.0")
# https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libutil/archive.cc#L186 (Git tag "2.36.0")
expr: |
_.body.size != 0 and
_.body.size <= 4095 and
_.body.min != 0
-affected-by: 1260 # this `valid/expr` doesn't compile in C++11
doc: |
The destination path of the symlink, as raw bytes. Nix 2.36.0
[doesn't decode targets as text](https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libutil/archive.cc#L280-L282)
either.
The target must be 1 to 4095 bytes long and must not contain a null
byte.