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.
digraph {
rankdir=LR;
node [shape=plaintext];
subgraph cluster__nix_nar {
label="NixNar";
graph[style=dotted];
nix_nar__seq [label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR><TD BGCOLOR="#E0FFE0">pos</TD><TD BGCOLOR="#E0FFE0">size</TD><TD BGCOLOR="#E0FFE0">type</TD><TD BGCOLOR="#E0FFE0">id</TD></TR>
<TR><TD PORT="magic_pos">0</TD><TD PORT="magic_size">...</TD><TD>PaddedStr</TD><TD PORT="magic_type">magic</TD></TR>
<TR><TD COLSPAN="4" PORT="magic__valid">must satisfy _.body == "nix-archive-1"</TD></TR>
<TR><TD PORT="root_node_pos">...</TD><TD PORT="root_node_size">...</TD><TD>Node</TD><TD PORT="root_node_type">root_node</TD></TR>
</TABLE>>];
subgraph cluster__node {
label="NixNar::Node";
graph[style=dotted];
node__seq [label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR><TD BGCOLOR="#E0FFE0">pos</TD><TD BGCOLOR="#E0FFE0">size</TD><TD BGCOLOR="#E0FFE0">type</TD><TD BGCOLOR="#E0FFE0">id</TD></TR>
<TR><TD PORT="_unnamed0_pos">0</TD><TD PORT="_unnamed0_size">0</TD><TD></TD><TD PORT="_unnamed0_type">_unnamed0</TD></TR>
<TR><TD COLSPAN="4" PORT="_unnamed0__valid">must satisfy depth < 64</TD></TR>
<TR><TD PORT="open_paren_pos">0</TD><TD PORT="open_paren_size">...</TD><TD>PaddedStr</TD><TD PORT="open_paren_type">open_paren</TD></TR>
<TR><TD COLSPAN="4" PORT="open_paren__valid">must satisfy _.body == "("</TD></TR>
<TR><TD PORT="type_key_pos">...</TD><TD PORT="type_key_size">...</TD><TD>PaddedStr</TD><TD PORT="type_key_type">type_key</TD></TR>
<TR><TD COLSPAN="4" PORT="type_key__valid">must satisfy _.body == "type"</TD></TR>
<TR><TD PORT="type_val_pos">...</TD><TD PORT="type_val_size">...</TD><TD>PaddedStr</TD><TD PORT="type_val_type">type_val</TD></TR>
<TR><TD COLSPAN="4" PORT="type_val__valid">must satisfy ((_.body == "regular") || (_.body == "directory") || (_.body == "symlink")) </TD></TR>
<TR><TD PORT="body_pos">...</TD><TD PORT="body_size">...</TD><TD>switch (type_val.body)</TD><TD PORT="body_type">body</TD></TR>
<TR><TD PORT="close_paren_pos">...</TD><TD PORT="close_paren_size">...</TD><TD>PaddedStr</TD><TD PORT="close_paren_type">close_paren</TD></TR>
<TR><TD COLSPAN="4" PORT="close_paren__valid">must satisfy _.body == ")"</TD></TR>
<TR><TD COLSPAN="4" PORT="close_paren__if">if type_val.body != "directory"</TD></TR>
</TABLE>>];
node__seq_body_switch [label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR><TD BGCOLOR="#F0F2E4">case</TD><TD BGCOLOR="#F0F2E4">type</TD></TR>
<TR><TD>"directory"</TD><TD PORT="case0">TypeDirectory</TD></TR>
<TR><TD>"regular"</TD><TD PORT="case1">TypeRegular</TD></TR>
<TR><TD>"symlink"</TD><TD PORT="case2">TypeSymlink</TD></TR>
</TABLE>>];
}
subgraph cluster__padded_bytes {
label="NixNar::PaddedBytes";
graph[style=dotted];
padded_bytes__seq [label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR><TD BGCOLOR="#E0FFE0">pos</TD><TD BGCOLOR="#E0FFE0">size</TD><TD BGCOLOR="#E0FFE0">type</TD><TD BGCOLOR="#E0FFE0">id</TD></TR>
<TR><TD PORT="len_body_pos">0</TD><TD PORT="len_body_size">8</TD><TD>u8le</TD><TD PORT="len_body_type">len_body</TD></TR>
<TR><TD PORT="body_pos">8</TD><TD PORT="body_size">len_body</TD><TD></TD><TD PORT="body_type">body</TD></TR>
<TR><TD PORT="padding_pos">...</TD><TD PORT="padding_size">(8 - len_body % 8) % 8</TD><TD></TD><TD PORT="padding_type">padding</TD></TR>
<TR><TD COLSPAN="4" PORT="padding__valid">must satisfy (_.size != 0 ? _.bytes.max == 0 : true)</TD></TR>
</TABLE>>];
}
subgraph cluster__padded_str {
label="NixNar::PaddedStr";
graph[style=dotted];
padded_str__seq [label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR><TD BGCOLOR="#E0FFE0">pos</TD><TD BGCOLOR="#E0FFE0">size</TD><TD BGCOLOR="#E0FFE0">type</TD><TD BGCOLOR="#E0FFE0">id</TD></TR>
<TR><TD PORT="len_body_pos">0</TD><TD PORT="len_body_size">8</TD><TD>u8le</TD><TD PORT="len_body_type">len_body</TD></TR>
<TR><TD PORT="body_pos">8</TD><TD PORT="body_size">len_body</TD><TD>str(ASCII)</TD><TD PORT="body_type">body</TD></TR>
<TR><TD PORT="padding_pos">...</TD><TD PORT="padding_size">(8 - len_body % 8) % 8</TD><TD></TD><TD PORT="padding_type">padding</TD></TR>
<TR><TD COLSPAN="4" PORT="padding__valid">must satisfy (_.size != 0 ? _.bytes.max == 0 : true)</TD></TR>
</TABLE>>];
}
subgraph cluster__type_directory {
label="NixNar::TypeDirectory";
graph[style=dotted];
type_directory__seq [label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR><TD BGCOLOR="#E0FFE0">pos</TD><TD BGCOLOR="#E0FFE0">size</TD><TD BGCOLOR="#E0FFE0">type</TD><TD BGCOLOR="#E0FFE0">id</TD></TR>
<TR><TD PORT="entries_pos">0</TD><TD PORT="entries_size">...</TD><TD>DirEntry</TD><TD PORT="entries_type">entries</TD></TR>
<TR><TD COLSPAN="4" PORT="entries__valid">must satisfy ( ((i != 0) && (!(_.is_terminator))) ? _.body.name.body > entries[i - 1].body.name.body : true)</TD></TR>
<TR><TD COLSPAN="4" PORT="entries__repeat">repeat until _.is_terminator</TD></TR>
</TABLE>>];
subgraph cluster__dir_entry {
label="NixNar::TypeDirectory::DirEntry";
graph[style=dotted];
dir_entry__seq [label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR><TD BGCOLOR="#E0FFE0">pos</TD><TD BGCOLOR="#E0FFE0">size</TD><TD BGCOLOR="#E0FFE0">type</TD><TD BGCOLOR="#E0FFE0">id</TD></TR>
<TR><TD PORT="kind_pos">0</TD><TD PORT="kind_size">...</TD><TD>PaddedStr</TD><TD PORT="kind_type">kind</TD></TR>
<TR><TD COLSPAN="4" PORT="kind__valid">must satisfy ((_.body == "entry") || (_.body == ")")) </TD></TR>
<TR><TD PORT="body_pos">...</TD><TD PORT="body_size">...</TD><TD>DirEntryBody</TD><TD PORT="body_type">body</TD></TR>
<TR><TD COLSPAN="4" PORT="body__if">if !(is_terminator)</TD></TR>
</TABLE>>];
dir_entry__inst__is_terminator [label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR><TD BGCOLOR="#E0FFE0">id</TD><TD BGCOLOR="#E0FFE0">value</TD></TR>
<TR><TD>is_terminator</TD><TD>kind.body == ")"</TD></TR>
</TABLE>>];
}
subgraph cluster__dir_entry_body {
label="NixNar::TypeDirectory::DirEntryBody";
graph[style=dotted];
dir_entry_body__seq [label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR><TD BGCOLOR="#E0FFE0">pos</TD><TD BGCOLOR="#E0FFE0">size</TD><TD BGCOLOR="#E0FFE0">type</TD><TD BGCOLOR="#E0FFE0">id</TD></TR>
<TR><TD PORT="open_paren_pos">0</TD><TD PORT="open_paren_size">...</TD><TD>PaddedStr</TD><TD PORT="open_paren_type">open_paren</TD></TR>
<TR><TD COLSPAN="4" PORT="open_paren__valid">must satisfy _.body == "("</TD></TR>
<TR><TD PORT="name_key_pos">...</TD><TD PORT="name_key_size">...</TD><TD>PaddedStr</TD><TD PORT="name_key_type">name_key</TD></TR>
<TR><TD COLSPAN="4" PORT="name_key__valid">must satisfy _.body == "name"</TD></TR>
<TR><TD PORT="name_pos">...</TD><TD PORT="name_size">...</TD><TD>PaddedBytes</TD><TD PORT="name_type">name</TD></TR>
<TR><TD COLSPAN="4" PORT="name__valid">must satisfy ((_.body.size != 0) && (_.body.size <= 255) && (_.body != [46].pack('C*')) && (_.body != [46, 46].pack('C*')) && (_.body.bytes.min != 0)) </TD></TR>
<TR><TD PORT="node_key_pos">...</TD><TD PORT="node_key_size">...</TD><TD>PaddedStr</TD><TD PORT="node_key_type">node_key</TD></TR>
<TR><TD COLSPAN="4" PORT="node_key__valid">must satisfy _.body == "node"</TD></TR>
<TR><TD PORT="node_pos">...</TD><TD PORT="node_size">...</TD><TD>Node</TD><TD PORT="node_type">node</TD></TR>
<TR><TD PORT="close_paren_pos">...</TD><TD PORT="close_paren_size">...</TD><TD>PaddedStr</TD><TD PORT="close_paren_type">close_paren</TD></TR>
<TR><TD COLSPAN="4" PORT="close_paren__valid">must satisfy _.body == ")"</TD></TR>
</TABLE>>];
}
}
subgraph cluster__type_regular {
label="NixNar::TypeRegular";
graph[style=dotted];
type_regular__seq [label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR><TD BGCOLOR="#E0FFE0">pos</TD><TD BGCOLOR="#E0FFE0">size</TD><TD BGCOLOR="#E0FFE0">type</TD><TD BGCOLOR="#E0FFE0">id</TD></TR>
<TR><TD PORT="first_key_pos">0</TD><TD PORT="first_key_size">...</TD><TD>PaddedStr</TD><TD PORT="first_key_type">first_key</TD></TR>
<TR><TD COLSPAN="4" PORT="first_key__valid">must satisfy ((_.body == "executable") || (_.body == "contents")) </TD></TR>
<TR><TD PORT="executable_val_pos">...</TD><TD PORT="executable_val_size">...</TD><TD>PaddedStr</TD><TD PORT="executable_val_type">executable_val</TD></TR>
<TR><TD COLSPAN="4" PORT="executable_val__valid">must satisfy _.body == ""</TD></TR>
<TR><TD COLSPAN="4" PORT="executable_val__if">if is_executable</TD></TR>
<TR><TD PORT="contents_key_pos">...</TD><TD PORT="contents_key_size">...</TD><TD>PaddedStr</TD><TD PORT="contents_key_type">contents_key</TD></TR>
<TR><TD COLSPAN="4" PORT="contents_key__valid">must satisfy _.body == "contents"</TD></TR>
<TR><TD COLSPAN="4" PORT="contents_key__if">if is_executable</TD></TR>
<TR><TD PORT="file_data_pos">...</TD><TD PORT="file_data_size">...</TD><TD>FileContent</TD><TD PORT="file_data_type">file_data</TD></TR>
</TABLE>>];
type_regular__inst__is_executable [label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR><TD BGCOLOR="#E0FFE0">id</TD><TD BGCOLOR="#E0FFE0">value</TD></TR>
<TR><TD>is_executable</TD><TD>first_key.body == "executable"</TD></TR>
</TABLE>>];
subgraph cluster__file_content {
label="NixNar::TypeRegular::FileContent";
graph[style=dotted];
file_content__seq [label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR><TD BGCOLOR="#E0FFE0">pos</TD><TD BGCOLOR="#E0FFE0">size</TD><TD BGCOLOR="#E0FFE0">type</TD><TD BGCOLOR="#E0FFE0">id</TD></TR>
<TR><TD PORT="len_contents_pos">0</TD><TD PORT="len_contents_size">8</TD><TD>u8le</TD><TD PORT="len_contents_type">len_contents</TD></TR>
<TR><TD PORT="_unnamed1_pos">8</TD><TD PORT="_unnamed1_size">0</TD><TD></TD><TD PORT="_unnamed1_type">_unnamed1</TD></TR>
<TR><TD COLSPAN="4" PORT="_unnamed1__if">if nar_offset < 0</TD></TR>
<TR><TD PORT="contents_pos">8</TD><TD PORT="contents_size">len_contents</TD><TD></TD><TD PORT="contents_type">contents</TD></TR>
<TR><TD PORT="padding_pos">...</TD><TD PORT="padding_size">(8 - len_contents % 8) % 8</TD><TD></TD><TD PORT="padding_type">padding</TD></TR>
<TR><TD COLSPAN="4" PORT="padding__valid">must satisfy (_.size != 0 ? _.bytes.max == 0 : true)</TD></TR>
</TABLE>>];
file_content__inst__nar_offset [label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR><TD BGCOLOR="#E0FFE0">id</TD><TD BGCOLOR="#E0FFE0">value</TD></TR>
<TR><TD>nar_offset</TD><TD>_io.pos</TD></TR>
</TABLE>>];
}
}
subgraph cluster__type_symlink {
label="NixNar::TypeSymlink";
graph[style=dotted];
type_symlink__seq [label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR><TD BGCOLOR="#E0FFE0">pos</TD><TD BGCOLOR="#E0FFE0">size</TD><TD BGCOLOR="#E0FFE0">type</TD><TD BGCOLOR="#E0FFE0">id</TD></TR>
<TR><TD PORT="target_key_pos">0</TD><TD PORT="target_key_size">...</TD><TD>PaddedStr</TD><TD PORT="target_key_type">target_key</TD></TR>
<TR><TD COLSPAN="4" PORT="target_key__valid">must satisfy _.body == "target"</TD></TR>
<TR><TD PORT="target_val_pos">...</TD><TD PORT="target_val_size">...</TD><TD>PaddedBytes</TD><TD PORT="target_val_type">target_val</TD></TR>
<TR><TD COLSPAN="4" PORT="target_val__valid">must satisfy ((_.body.size != 0) && (_.body.size <= 4095) && (_.body.bytes.min != 0)) </TD></TR>
</TABLE>>];
}
}
nix_nar__seq:magic_type -> padded_str__seq [style=bold];
padded_str__seq:body_type -> nix_nar__seq:magic__valid [color="#404040"];
nix_nar__seq:root_node_type -> node__seq [style=bold];
node__params:depth_type -> node__seq:_unnamed0__valid [color="#404040"];
node__seq:open_paren_type -> padded_str__seq [style=bold];
padded_str__seq:body_type -> node__seq:open_paren__valid [color="#404040"];
node__seq:type_key_type -> padded_str__seq [style=bold];
padded_str__seq:body_type -> node__seq:type_key__valid [color="#404040"];
node__seq:type_val_type -> padded_str__seq [style=bold];
padded_str__seq:body_type -> node__seq:type_val__valid [color="#404040"];
node__seq:body_type -> node__seq_body_switch [style=bold];
node__seq_body_switch:case0 -> type_directory__seq [style=bold];
node__seq_body_switch:case1 -> type_regular__seq [style=bold];
node__seq_body_switch:case2 -> type_symlink__seq [style=bold];
padded_str__seq:body_type -> node__seq:body_type [color="#404040"];
node__seq:close_paren_type -> padded_str__seq [style=bold];
padded_str__seq:body_type -> node__seq:close_paren__valid [color="#404040"];
padded_str__seq:body_type -> node__seq:close_paren__if [color="#404040"];
padded_bytes__seq:len_body_type -> padded_bytes__seq:body_size [color="#404040"];
padded_bytes__seq:len_body_type -> padded_bytes__seq:padding_size [color="#404040"];
padded_str__seq:len_body_type -> padded_str__seq:body_size [color="#404040"];
padded_str__seq:len_body_type -> padded_str__seq:padding_size [color="#404040"];
type_directory__seq:entries_type -> dir_entry__seq [style=bold];
dir_entry__inst__is_terminator:is_terminator_type -> type_directory__seq:entries__valid [color="#404040"];
padded_bytes__seq:body_type -> type_directory__seq:entries__valid [color="#404040"];
dir_entry__inst__is_terminator:is_terminator_type -> type_directory__seq:entries__repeat [color="#404040"];
dir_entry__seq:kind_type -> padded_str__seq [style=bold];
padded_str__seq:body_type -> dir_entry__seq:kind__valid [color="#404040"];
dir_entry__seq:body_type -> dir_entry_body__seq [style=bold];
dir_entry__inst__is_terminator:is_terminator_type -> dir_entry__seq:body__if [color="#404040"];
padded_str__seq:body_type -> dir_entry__inst__is_terminator [color="#404040"];
dir_entry_body__seq:open_paren_type -> padded_str__seq [style=bold];
padded_str__seq:body_type -> dir_entry_body__seq:open_paren__valid [color="#404040"];
dir_entry_body__seq:name_key_type -> padded_str__seq [style=bold];
padded_str__seq:body_type -> dir_entry_body__seq:name_key__valid [color="#404040"];
dir_entry_body__seq:name_type -> padded_bytes__seq [style=bold];
padded_bytes__seq:body_type -> dir_entry_body__seq:name__valid [color="#404040"];
dir_entry_body__seq:node_key_type -> padded_str__seq [style=bold];
padded_str__seq:body_type -> dir_entry_body__seq:node_key__valid [color="#404040"];
dir_entry_body__seq:node_type -> node__seq [style=bold];
dir_entry_body__seq:close_paren_type -> padded_str__seq [style=bold];
padded_str__seq:body_type -> dir_entry_body__seq:close_paren__valid [color="#404040"];
type_regular__seq:first_key_type -> padded_str__seq [style=bold];
padded_str__seq:body_type -> type_regular__seq:first_key__valid [color="#404040"];
type_regular__seq:executable_val_type -> padded_str__seq [style=bold];
padded_str__seq:body_type -> type_regular__seq:executable_val__valid [color="#404040"];
type_regular__inst__is_executable:is_executable_type -> type_regular__seq:executable_val__if [color="#404040"];
type_regular__seq:contents_key_type -> padded_str__seq [style=bold];
padded_str__seq:body_type -> type_regular__seq:contents_key__valid [color="#404040"];
type_regular__inst__is_executable:is_executable_type -> type_regular__seq:contents_key__if [color="#404040"];
type_regular__seq:file_data_type -> file_content__seq [style=bold];
padded_str__seq:body_type -> type_regular__inst__is_executable [color="#404040"];
file_content__inst__nar_offset:nar_offset_type -> file_content__seq:_unnamed1__if [color="#404040"];
file_content__seq:len_contents_type -> file_content__seq:contents_size [color="#404040"];
file_content__seq:len_contents_type -> file_content__seq:padding_size [color="#404040"];
type_symlink__seq:target_key_type -> padded_str__seq [style=bold];
padded_str__seq:body_type -> type_symlink__seq:target_key__valid [color="#404040"];
type_symlink__seq:target_val_type -> padded_bytes__seq [style=bold];
padded_bytes__seq:body_type -> type_symlink__seq:target_val__valid [color="#404040"];
}