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.
<?php
// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild
/**
* 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.
*/
namespace {
class NixNar extends \Kaitai\Struct\Struct {
public function __construct(\Kaitai\Struct\Stream $_io, ?\Kaitai\Struct\Struct $_parent = null, ?\NixNar $_root = null) {
parent::__construct($_io, $_parent, $_root === null ? $this : $_root);
$this->_read();
}
private function _read() {
$this->_m_magic = new \NixNar\PaddedStr($this->_io, $this, $this->_root);
$_ = $this->_m_magic;
if (!($_->body() == "nix-archive-1")) {
throw new \Kaitai\Struct\Error\ValidationExprError($this->_m_magic, $this->_io, "/seq/0");
}
$this->_m_rootNode = new \NixNar\Node(0, $this->_io, $this, $this->_root);
}
protected $_m_magic;
protected $_m_rootNode;
/**
* Magic string, must be `nix-archive-1`.
*/
public function magic() { return $this->_m_magic; }
/**
* The root of the archive, which is always a single node.
*/
public function rootNode() { return $this->_m_rootNode; }
}
}
/**
* A single file system node (regular file, directory, or symlink).
*/
namespace NixNar {
class Node extends \Kaitai\Struct\Struct {
public function __construct(int $depth, \Kaitai\Struct\Stream $_io, ?\Kaitai\Struct\Struct $_parent = null, ?\NixNar $_root = null) {
parent::__construct($_io, $_parent, $_root);
$this->_m_depth = $depth;
$this->_read();
}
private function _read() {
$this->_m__unnamed0 = $this->_io->readBytes(0);
$_ = $this->_m__unnamed0;
if (!($this->depth() < 64)) {
throw new \Kaitai\Struct\Error\ValidationExprError($this->_m__unnamed0, $this->_io, "/types/node/seq/0");
}
$this->_m_openParen = new \NixNar\PaddedStr($this->_io, $this, $this->_root);
$_ = $this->_m_openParen;
if (!($_->body() == "(")) {
throw new \Kaitai\Struct\Error\ValidationExprError($this->_m_openParen, $this->_io, "/types/node/seq/1");
}
$this->_m_typeKey = new \NixNar\PaddedStr($this->_io, $this, $this->_root);
$_ = $this->_m_typeKey;
if (!($_->body() == "type")) {
throw new \Kaitai\Struct\Error\ValidationExprError($this->_m_typeKey, $this->_io, "/types/node/seq/2");
}
$this->_m_typeVal = new \NixNar\PaddedStr($this->_io, $this, $this->_root);
$_ = $this->_m_typeVal;
if (!( (($_->body() == "regular") || ($_->body() == "directory") || ($_->body() == "symlink")) )) {
throw new \Kaitai\Struct\Error\ValidationExprError($this->_m_typeVal, $this->_io, "/types/node/seq/3");
}
switch ($this->typeVal()->body()) {
case "directory":
$this->_m_body = new \NixNar\TypeDirectory($this->_io, $this, $this->_root);
break;
case "regular":
$this->_m_body = new \NixNar\TypeRegular($this->_io, $this, $this->_root);
break;
case "symlink":
$this->_m_body = new \NixNar\TypeSymlink($this->_io, $this, $this->_root);
break;
}
if ($this->typeVal()->body() != "directory") {
$this->_m_closeParen = new \NixNar\PaddedStr($this->_io, $this, $this->_root);
$_ = $this->_m_closeParen;
if (!($_->body() == ")")) {
throw new \Kaitai\Struct\Error\ValidationExprError($this->_m_closeParen, $this->_io, "/types/node/seq/5");
}
}
}
protected $_m__unnamed0;
protected $_m_openParen;
protected $_m_typeKey;
protected $_m_typeVal;
protected $_m_body;
protected $_m_closeParen;
protected $_m_depth;
public function _unnamed0() { return $this->_m__unnamed0; }
/**
* Must be `(`, a token starting the node definition.
*/
public function openParen() { return $this->_m_openParen; }
/**
* Must be `type`.
*/
public function typeKey() { return $this->_m_typeKey; }
/**
* Type of the node (`regular`, `directory`, or `symlink`).
*/
public function typeVal() { return $this->_m_typeVal; }
public function body() { return $this->_m_body; }
/**
* 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`.
*/
public function closeParen() { return $this->_m_closeParen; }
/**
* Nesting depth of the node. The root node has depth 0.
*/
public function depth() { return $this->_m_depth; }
}
}
/**
* 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.
*/
namespace NixNar {
class PaddedBytes extends \Kaitai\Struct\Struct {
public function __construct(\Kaitai\Struct\Stream $_io, ?\Kaitai\Struct\Struct $_parent = null, ?\NixNar $_root = null) {
parent::__construct($_io, $_parent, $_root);
$this->_read();
}
private function _read() {
$this->_m_lenBody = $this->_io->readU8le();
$this->_m_body = $this->_io->readBytes($this->lenBody());
$this->_m_padding = $this->_io->readBytes(\Kaitai\Struct\Stream::mod(8 - \Kaitai\Struct\Stream::mod($this->lenBody(), 8), 8));
$_ = $this->_m_padding;
if (!((strlen($_) != 0 ? \Kaitai\Struct\Stream::byteArrayMax($_) == 0 : true))) {
throw new \Kaitai\Struct\Error\ValidationExprError($this->_m_padding, $this->_io, "/types/padded_bytes/seq/2");
}
}
protected $_m_lenBody;
protected $_m_body;
protected $_m_padding;
public function lenBody() { return $this->_m_lenBody; }
public function body() { return $this->_m_body; }
public function padding() { return $this->_m_padding; }
}
}
/**
* 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.
*/
namespace NixNar {
class PaddedStr extends \Kaitai\Struct\Struct {
public function __construct(\Kaitai\Struct\Stream $_io, ?\Kaitai\Struct\Struct $_parent = null, ?\NixNar $_root = null) {
parent::__construct($_io, $_parent, $_root);
$this->_read();
}
private function _read() {
$this->_m_lenBody = $this->_io->readU8le();
$this->_m_body = \Kaitai\Struct\Stream::bytesToStr($this->_io->readBytes($this->lenBody()), "ASCII");
$this->_m_padding = $this->_io->readBytes(\Kaitai\Struct\Stream::mod(8 - \Kaitai\Struct\Stream::mod($this->lenBody(), 8), 8));
$_ = $this->_m_padding;
if (!((strlen($_) != 0 ? \Kaitai\Struct\Stream::byteArrayMax($_) == 0 : true))) {
throw new \Kaitai\Struct\Error\ValidationExprError($this->_m_padding, $this->_io, "/types/padded_str/seq/2");
}
}
protected $_m_lenBody;
protected $_m_body;
protected $_m_padding;
public function lenBody() { return $this->_m_lenBody; }
public function body() { return $this->_m_body; }
public function padding() { return $this->_m_padding; }
}
}
/**
* 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.
*/
namespace NixNar {
class TypeDirectory extends \Kaitai\Struct\Struct {
public function __construct(\Kaitai\Struct\Stream $_io, ?\NixNar\Node $_parent = null, ?\NixNar $_root = null) {
parent::__construct($_io, $_parent, $_root);
$this->_read();
}
private function _read() {
$this->_m_entries = [];
$i = 0;
do {
$_ = new \NixNar\TypeDirectory\DirEntry($this->_io, $this, $this->_root);
$this->_m_entries[] = $_;
$_ = $this->_m_entries[$i];
if (!(( (($i != 0) && (!($_->isTerminator()))) ? $_->body()->name()->body() > $this->entries()[$i - 1]->body()->name()->body() : true))) {
throw new \Kaitai\Struct\Error\ValidationExprError($this->_m_entries[$i], $this->_io, "/types/type_directory/seq/0");
}
$i++;
} while (!($_->isTerminator()));
}
protected $_m_entries;
public function entries() { return $this->_m_entries; }
}
}
/**
* A single entry within a directory, or the terminator, i.e. the `)`
* token that ends the directory node.
*/
namespace NixNar\TypeDirectory {
class DirEntry extends \Kaitai\Struct\Struct {
public function __construct(\Kaitai\Struct\Stream $_io, ?\NixNar\TypeDirectory $_parent = null, ?\NixNar $_root = null) {
parent::__construct($_io, $_parent, $_root);
$this->_read();
}
private function _read() {
$this->_m_kind = new \NixNar\PaddedStr($this->_io, $this, $this->_root);
$_ = $this->_m_kind;
if (!( (($_->body() == "entry") || ($_->body() == ")")) )) {
throw new \Kaitai\Struct\Error\ValidationExprError($this->_m_kind, $this->_io, "/types/type_directory/types/dir_entry/seq/0");
}
if (!($this->isTerminator())) {
$this->_m_body = new \NixNar\TypeDirectory\DirEntryBody($this->_io, $this, $this->_root);
}
}
protected $_m_isTerminator;
public function isTerminator() {
if ($this->_m_isTerminator !== null)
return $this->_m_isTerminator;
$this->_m_isTerminator = $this->kind()->body() == ")";
return $this->_m_isTerminator;
}
protected $_m_kind;
protected $_m_body;
/**
* Must be `entry` (for a child node) or `)` (for the terminator).
*/
public function kind() { return $this->_m_kind; }
/**
* The entry itself. The terminator has no `body`.
*/
public function body() { return $this->_m_body; }
}
}
namespace NixNar\TypeDirectory {
class DirEntryBody extends \Kaitai\Struct\Struct {
public function __construct(\Kaitai\Struct\Stream $_io, ?\NixNar\TypeDirectory\DirEntry $_parent = null, ?\NixNar $_root = null) {
parent::__construct($_io, $_parent, $_root);
$this->_read();
}
private function _read() {
$this->_m_openParen = new \NixNar\PaddedStr($this->_io, $this, $this->_root);
$_ = $this->_m_openParen;
if (!($_->body() == "(")) {
throw new \Kaitai\Struct\Error\ValidationExprError($this->_m_openParen, $this->_io, "/types/type_directory/types/dir_entry_body/seq/0");
}
$this->_m_nameKey = new \NixNar\PaddedStr($this->_io, $this, $this->_root);
$_ = $this->_m_nameKey;
if (!($_->body() == "name")) {
throw new \Kaitai\Struct\Error\ValidationExprError($this->_m_nameKey, $this->_io, "/types/type_directory/types/dir_entry_body/seq/1");
}
$this->_m_name = new \NixNar\PaddedBytes($this->_io, $this, $this->_root);
$_ = $this->_m_name;
if (!( ((strlen($_->body()) != 0) && (strlen($_->body()) <= 255) && ($_->body() != "\x2E") && ($_->body() != "\x2E\x2E") && (\Kaitai\Struct\Stream::byteArrayMin($_->body()) != 0)) )) {
throw new \Kaitai\Struct\Error\ValidationExprError($this->_m_name, $this->_io, "/types/type_directory/types/dir_entry_body/seq/2");
}
$this->_m_nodeKey = new \NixNar\PaddedStr($this->_io, $this, $this->_root);
$_ = $this->_m_nodeKey;
if (!($_->body() == "node")) {
throw new \Kaitai\Struct\Error\ValidationExprError($this->_m_nodeKey, $this->_io, "/types/type_directory/types/dir_entry_body/seq/3");
}
$this->_m_node = new \NixNar\Node($this->_parent()->_parent()->_parent()->depth() + 1, $this->_io, $this, $this->_root);
$this->_m_closeParen = new \NixNar\PaddedStr($this->_io, $this, $this->_root);
$_ = $this->_m_closeParen;
if (!($_->body() == ")")) {
throw new \Kaitai\Struct\Error\ValidationExprError($this->_m_closeParen, $this->_io, "/types/type_directory/types/dir_entry_body/seq/5");
}
}
protected $_m_openParen;
protected $_m_nameKey;
protected $_m_name;
protected $_m_nodeKey;
protected $_m_node;
protected $_m_closeParen;
public function openParen() { return $this->_m_openParen; }
public function nameKey() { return $this->_m_nameKey; }
/**
* 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.
*/
public function name() { return $this->_m_name; }
public function nodeKey() { return $this->_m_nodeKey; }
/**
* The child node.
*/
public function node() { return $this->_m_node; }
public function closeParen() { return $this->_m_closeParen; }
}
}
/**
* A regular file node.
*/
namespace NixNar {
class TypeRegular extends \Kaitai\Struct\Struct {
public function __construct(\Kaitai\Struct\Stream $_io, ?\NixNar\Node $_parent = null, ?\NixNar $_root = null) {
parent::__construct($_io, $_parent, $_root);
$this->_read();
}
private function _read() {
$this->_m_firstKey = new \NixNar\PaddedStr($this->_io, $this, $this->_root);
$_ = $this->_m_firstKey;
if (!( (($_->body() == "executable") || ($_->body() == "contents")) )) {
throw new \Kaitai\Struct\Error\ValidationExprError($this->_m_firstKey, $this->_io, "/types/type_regular/seq/0");
}
if ($this->isExecutable()) {
$this->_m_executableVal = new \NixNar\PaddedStr($this->_io, $this, $this->_root);
$_ = $this->_m_executableVal;
if (!($_->body() == "")) {
throw new \Kaitai\Struct\Error\ValidationExprError($this->_m_executableVal, $this->_io, "/types/type_regular/seq/1");
}
}
if ($this->isExecutable()) {
$this->_m_contentsKey = new \NixNar\PaddedStr($this->_io, $this, $this->_root);
$_ = $this->_m_contentsKey;
if (!($_->body() == "contents")) {
throw new \Kaitai\Struct\Error\ValidationExprError($this->_m_contentsKey, $this->_io, "/types/type_regular/seq/2");
}
}
$this->_m_fileData = new \NixNar\TypeRegular\FileContent($this->_io, $this, $this->_root);
}
protected $_m_isExecutable;
/**
* True if the file has the `executable` attribute.
*/
public function isExecutable() {
if ($this->_m_isExecutable !== null)
return $this->_m_isExecutable;
$this->_m_isExecutable = $this->firstKey()->body() == "executable";
return $this->_m_isExecutable;
}
protected $_m_firstKey;
protected $_m_executableVal;
protected $_m_contentsKey;
protected $_m_fileData;
/**
* Either `executable` or `contents`.
*/
public function firstKey() { return $this->_m_firstKey; }
/**
* Must be empty.
*/
public function executableVal() { return $this->_m_executableVal; }
public function contentsKey() { return $this->_m_contentsKey; }
public function fileData() { return $this->_m_fileData; }
}
}
/**
* The contents of the file, prefixed with their length and padded
* with null bytes to the next 8-byte boundary.
*/
namespace NixNar\TypeRegular {
class FileContent extends \Kaitai\Struct\Struct {
public function __construct(\Kaitai\Struct\Stream $_io, ?\NixNar\TypeRegular $_parent = null, ?\NixNar $_root = null) {
parent::__construct($_io, $_parent, $_root);
$this->_read();
}
private function _read() {
$this->_m_lenContents = $this->_io->readU8le();
if ($this->narOffset() < 0) {
$this->_m__unnamed1 = $this->_io->readBytes(0);
}
$this->_m_contents = $this->_io->readBytes($this->lenContents());
$this->_m_padding = $this->_io->readBytes(\Kaitai\Struct\Stream::mod(8 - \Kaitai\Struct\Stream::mod($this->lenContents(), 8), 8));
$_ = $this->_m_padding;
if (!((strlen($_) != 0 ? \Kaitai\Struct\Stream::byteArrayMax($_) == 0 : true))) {
throw new \Kaitai\Struct\Error\ValidationExprError($this->_m_padding, $this->_io, "/types/type_regular/types/file_content/seq/3");
}
}
protected $_m_narOffset;
/**
* 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.
*/
public function narOffset() {
if ($this->_m_narOffset !== null)
return $this->_m_narOffset;
$this->_m_narOffset = $this->_io()->pos();
return $this->_m_narOffset;
}
protected $_m_lenContents;
protected $_m__unnamed1;
protected $_m_contents;
protected $_m_padding;
public function lenContents() { return $this->_m_lenContents; }
public function _unnamed1() { return $this->_m__unnamed1; }
/**
* 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.
*/
public function contents() { return $this->_m_contents; }
public function padding() { return $this->_m_padding; }
}
}
/**
* A symbolic link node.
*/
namespace NixNar {
class TypeSymlink extends \Kaitai\Struct\Struct {
public function __construct(\Kaitai\Struct\Stream $_io, ?\NixNar\Node $_parent = null, ?\NixNar $_root = null) {
parent::__construct($_io, $_parent, $_root);
$this->_read();
}
private function _read() {
$this->_m_targetKey = new \NixNar\PaddedStr($this->_io, $this, $this->_root);
$_ = $this->_m_targetKey;
if (!($_->body() == "target")) {
throw new \Kaitai\Struct\Error\ValidationExprError($this->_m_targetKey, $this->_io, "/types/type_symlink/seq/0");
}
$this->_m_targetVal = new \NixNar\PaddedBytes($this->_io, $this, $this->_root);
$_ = $this->_m_targetVal;
if (!( ((strlen($_->body()) != 0) && (strlen($_->body()) <= 4095) && (\Kaitai\Struct\Stream::byteArrayMin($_->body()) != 0)) )) {
throw new \Kaitai\Struct\Error\ValidationExprError($this->_m_targetVal, $this->_io, "/types/type_symlink/seq/1");
}
}
protected $_m_targetKey;
protected $_m_targetVal;
/**
* Must be `target`.
*/
public function targetKey() { return $this->_m_targetKey; }
/**
* 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.
*/
public function targetVal() { return $this->_m_targetVal; }
}
}