Nix Archive (NAR): Java (read-write) parsing library

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.

File extension

nar

KS implementation details

License: MIT

References

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.

Java (read-write) source code to parse Nix Archive (NAR)

NixNar.java

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

import io.kaitai.struct.ByteBufferKaitaiStream;
import io.kaitai.struct.KaitaiStruct;
import io.kaitai.struct.KaitaiStream;
import java.io.IOException;
import java.util.Objects;
import io.kaitai.struct.ConsistencyError;
import java.nio.charset.StandardCharsets;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 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.
 * @see <a href="https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/doc/manual/source/protocols/nix-archive/index.md">Git tag "2.36.0"</a>
 */
public class NixNar extends KaitaiStruct.ReadWrite {
    public static NixNar fromFile(String fileName) throws IOException {
        return new NixNar(new ByteBufferKaitaiStream(fileName));
    }
    public NixNar() {
        this(null, null, null);
    }

    public NixNar(KaitaiStream _io) {
        this(_io, null, null);
    }

    public NixNar(KaitaiStream _io, KaitaiStruct.ReadWrite _parent) {
        this(_io, _parent, null);
    }

    public NixNar(KaitaiStream _io, KaitaiStruct.ReadWrite _parent, NixNar _root) {
        super(_io);
        this._parent = _parent;
        this._root = _root == null ? this : _root;
    }
    public void _read() {
        this.magic = new PaddedStr(this._io, this, _root);
        this.magic._read();
        {
            PaddedStr _it = this.magic;
            if (!(_it.body().equals("nix-archive-1"))) {
                throw new KaitaiStream.ValidationExprError(this.magic, this._io, "/seq/0");
            }
        }
        this.rootNode = new Node(this._io, this, _root, 0);
        this.rootNode._read();
        _dirty = false;
    }

    public void _fetchInstances() {
        this.magic._fetchInstances();
        this.rootNode._fetchInstances();
    }

    public void _write_Seq() {
        _assertNotDirty();
        this.magic._write_Seq(this._io);
        this.rootNode._write_Seq(this._io);
    }

    public void _check() {
        if (!Objects.equals(this.magic._root(), _root()))
            throw new ConsistencyError("magic", _root(), this.magic._root());
        if (!Objects.equals(this.magic._parent(), this))
            throw new ConsistencyError("magic", this, this.magic._parent());
        {
            PaddedStr _it = this.magic;
            if (!(_it.body().equals("nix-archive-1"))) {
                throw new KaitaiStream.ValidationExprError(this.magic, null, "/seq/0");
            }
        }
        if (!Objects.equals(this.rootNode._root(), _root()))
            throw new ConsistencyError("root_node", _root(), this.rootNode._root());
        if (!Objects.equals(this.rootNode._parent(), this))
            throw new ConsistencyError("root_node", this, this.rootNode._parent());
        if (this.rootNode.depth() != 0)
            throw new ConsistencyError("root_node", 0, this.rootNode.depth());
        _dirty = false;
    }

    /**
     * A single file system node (regular file, directory, or symlink).
     */
    public static class Node extends KaitaiStruct.ReadWrite {
        public Node(int depth) {
            this(null, null, null, depth);
        }

        public Node(KaitaiStream _io, int depth) {
            this(_io, null, null, depth);
        }

        public Node(KaitaiStream _io, KaitaiStruct.ReadWrite _parent, int depth) {
            this(_io, _parent, null, depth);
        }

        public Node(KaitaiStream _io, KaitaiStruct.ReadWrite _parent, NixNar _root, int depth) {
            super(_io);
            this._parent = _parent;
            this._root = _root;
            this.depth = depth;
        }
        public void _read() {
            this._unnamed0 = this._io.readBytes(0);
            {
                byte[] _it = this._unnamed0;
                if (!(depth() < 64)) {
                    throw new KaitaiStream.ValidationExprError(this._unnamed0, this._io, "/types/node/seq/0");
                }
            }
            this.openParen = new PaddedStr(this._io, this, _root);
            this.openParen._read();
            {
                PaddedStr _it = this.openParen;
                if (!(_it.body().equals("("))) {
                    throw new KaitaiStream.ValidationExprError(this.openParen, this._io, "/types/node/seq/1");
                }
            }
            this.typeKey = new PaddedStr(this._io, this, _root);
            this.typeKey._read();
            {
                PaddedStr _it = this.typeKey;
                if (!(_it.body().equals("type"))) {
                    throw new KaitaiStream.ValidationExprError(this.typeKey, this._io, "/types/node/seq/2");
                }
            }
            this.typeVal = new PaddedStr(this._io, this, _root);
            this.typeVal._read();
            {
                PaddedStr _it = this.typeVal;
                if (!( ((_it.body().equals("regular")) || (_it.body().equals("directory")) || (_it.body().equals("symlink"))) )) {
                    throw new KaitaiStream.ValidationExprError(this.typeVal, this._io, "/types/node/seq/3");
                }
            }
            switch (typeVal().body()) {
            case "directory": {
                this.body = new TypeDirectory(this._io, this, _root);
                ((TypeDirectory) (this.body))._read();
                break;
            }
            case "regular": {
                this.body = new TypeRegular(this._io, this, _root);
                ((TypeRegular) (this.body))._read();
                break;
            }
            case "symlink": {
                this.body = new TypeSymlink(this._io, this, _root);
                ((TypeSymlink) (this.body))._read();
                break;
            }
            }
            if (!typeVal().body().equals("directory")) {
                this.closeParen = new PaddedStr(this._io, this, _root);
                this.closeParen._read();
                {
                    PaddedStr _it = this.closeParen;
                    if (!(_it.body().equals(")"))) {
                        throw new KaitaiStream.ValidationExprError(this.closeParen, this._io, "/types/node/seq/5");
                    }
                }
            }
            _dirty = false;
        }

        public void _fetchInstances() {
            this.openParen._fetchInstances();
            this.typeKey._fetchInstances();
            this.typeVal._fetchInstances();
            switch (typeVal().body()) {
            case "directory": {
                ((TypeDirectory) (this.body))._fetchInstances();
                break;
            }
            case "regular": {
                ((TypeRegular) (this.body))._fetchInstances();
                break;
            }
            case "symlink": {
                ((TypeSymlink) (this.body))._fetchInstances();
                break;
            }
            }
            if (!typeVal().body().equals("directory")) {
                this.closeParen._fetchInstances();
            }
        }

        public void _write_Seq() {
            _assertNotDirty();
            this._io.writeBytes(this._unnamed0);
            this.openParen._write_Seq(this._io);
            this.typeKey._write_Seq(this._io);
            this.typeVal._write_Seq(this._io);
            switch (typeVal().body()) {
            case "directory": {
                ((TypeDirectory) (this.body))._write_Seq(this._io);
                break;
            }
            case "regular": {
                ((TypeRegular) (this.body))._write_Seq(this._io);
                break;
            }
            case "symlink": {
                ((TypeSymlink) (this.body))._write_Seq(this._io);
                break;
            }
            }
            if (!typeVal().body().equals("directory")) {
                this.closeParen._write_Seq(this._io);
            }
        }

        public void _check() {
            if (this._unnamed0.length != 0)
                throw new ConsistencyError("_unnamed0", 0, this._unnamed0.length);
            {
                byte[] _it = this._unnamed0;
                if (!(depth() < 64)) {
                    throw new KaitaiStream.ValidationExprError(this._unnamed0, null, "/types/node/seq/0");
                }
            }
            if (!Objects.equals(this.openParen._root(), _root()))
                throw new ConsistencyError("open_paren", _root(), this.openParen._root());
            if (!Objects.equals(this.openParen._parent(), this))
                throw new ConsistencyError("open_paren", this, this.openParen._parent());
            {
                PaddedStr _it = this.openParen;
                if (!(_it.body().equals("("))) {
                    throw new KaitaiStream.ValidationExprError(this.openParen, null, "/types/node/seq/1");
                }
            }
            if (!Objects.equals(this.typeKey._root(), _root()))
                throw new ConsistencyError("type_key", _root(), this.typeKey._root());
            if (!Objects.equals(this.typeKey._parent(), this))
                throw new ConsistencyError("type_key", this, this.typeKey._parent());
            {
                PaddedStr _it = this.typeKey;
                if (!(_it.body().equals("type"))) {
                    throw new KaitaiStream.ValidationExprError(this.typeKey, null, "/types/node/seq/2");
                }
            }
            if (!Objects.equals(this.typeVal._root(), _root()))
                throw new ConsistencyError("type_val", _root(), this.typeVal._root());
            if (!Objects.equals(this.typeVal._parent(), this))
                throw new ConsistencyError("type_val", this, this.typeVal._parent());
            {
                PaddedStr _it = this.typeVal;
                if (!( ((_it.body().equals("regular")) || (_it.body().equals("directory")) || (_it.body().equals("symlink"))) )) {
                    throw new KaitaiStream.ValidationExprError(this.typeVal, null, "/types/node/seq/3");
                }
            }
            switch (typeVal().body()) {
            case "directory": {
                if (!Objects.equals(((NixNar.TypeDirectory) (this.body))._root(), _root()))
                    throw new ConsistencyError("body", _root(), ((NixNar.TypeDirectory) (this.body))._root());
                if (!Objects.equals(((NixNar.TypeDirectory) (this.body))._parent(), this))
                    throw new ConsistencyError("body", this, ((NixNar.TypeDirectory) (this.body))._parent());
                break;
            }
            case "regular": {
                if (!Objects.equals(((NixNar.TypeRegular) (this.body))._root(), _root()))
                    throw new ConsistencyError("body", _root(), ((NixNar.TypeRegular) (this.body))._root());
                if (!Objects.equals(((NixNar.TypeRegular) (this.body))._parent(), this))
                    throw new ConsistencyError("body", this, ((NixNar.TypeRegular) (this.body))._parent());
                break;
            }
            case "symlink": {
                if (!Objects.equals(((NixNar.TypeSymlink) (this.body))._root(), _root()))
                    throw new ConsistencyError("body", _root(), ((NixNar.TypeSymlink) (this.body))._root());
                if (!Objects.equals(((NixNar.TypeSymlink) (this.body))._parent(), this))
                    throw new ConsistencyError("body", this, ((NixNar.TypeSymlink) (this.body))._parent());
                break;
            }
            }
            if (!typeVal().body().equals("directory")) {
                if (!Objects.equals(this.closeParen._root(), _root()))
                    throw new ConsistencyError("close_paren", _root(), this.closeParen._root());
                if (!Objects.equals(this.closeParen._parent(), this))
                    throw new ConsistencyError("close_paren", this, this.closeParen._parent());
                {
                    PaddedStr _it = this.closeParen;
                    if (!(_it.body().equals(")"))) {
                        throw new KaitaiStream.ValidationExprError(this.closeParen, null, "/types/node/seq/5");
                    }
                }
            }
            _dirty = false;
        }
        private byte[] _unnamed0;
        private PaddedStr openParen;
        private PaddedStr typeKey;
        private PaddedStr typeVal;
        private KaitaiStruct.ReadWrite body;
        private PaddedStr closeParen;
        private int depth;
        private NixNar _root;
        private KaitaiStruct.ReadWrite _parent;
        public byte[] _unnamed0() { return _unnamed0; }
        public void set_unnamed0(byte[] _v) { _dirty = true; _unnamed0 = _v; }

        /**
         * Must be `(`, a token starting the node definition.
         */
        public PaddedStr openParen() { return openParen; }
        public void setOpenParen(PaddedStr _v) { _dirty = true; openParen = _v; }

        /**
         * Must be `type`.
         */
        public PaddedStr typeKey() { return typeKey; }
        public void setTypeKey(PaddedStr _v) { _dirty = true; typeKey = _v; }

        /**
         * Type of the node (`regular`, `directory`, or `symlink`).
         */
        public PaddedStr typeVal() { return typeVal; }
        public void setTypeVal(PaddedStr _v) { _dirty = true; typeVal = _v; }
        public KaitaiStruct.ReadWrite body() { return body; }
        public void setBody(KaitaiStruct.ReadWrite _v) { _dirty = true; body = _v; }

        /**
         * 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 PaddedStr closeParen() { return closeParen; }
        public void setCloseParen(PaddedStr _v) { _dirty = true; closeParen = _v; }

        /**
         * Nesting depth of the node. The root node has depth 0.
         */
        public int depth() { return depth; }
        public void setDepth(int _v) { _dirty = true; depth = _v; }
        public NixNar _root() { return _root; }
        public void set_root(NixNar _v) { _dirty = true; _root = _v; }
        public KaitaiStruct.ReadWrite _parent() { return _parent; }
        public void set_parent(KaitaiStruct.ReadWrite _v) { _dirty = true; _parent = _v; }
    }

    /**
     * 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.
     */
    public static class PaddedBytes extends KaitaiStruct.ReadWrite {
        public static PaddedBytes fromFile(String fileName) throws IOException {
            return new PaddedBytes(new ByteBufferKaitaiStream(fileName));
        }
        public PaddedBytes() {
            this(null, null, null);
        }

        public PaddedBytes(KaitaiStream _io) {
            this(_io, null, null);
        }

        public PaddedBytes(KaitaiStream _io, KaitaiStruct.ReadWrite _parent) {
            this(_io, _parent, null);
        }

        public PaddedBytes(KaitaiStream _io, KaitaiStruct.ReadWrite _parent, NixNar _root) {
            super(_io);
            this._parent = _parent;
            this._root = _root;
        }
        public void _read() {
            this.lenBody = this._io.readU8le();
            this.body = this._io.readBytes(lenBody());
            this.padding = this._io.readBytes(KaitaiStream.mod(8 - KaitaiStream.mod(lenBody(), 8), 8));
            {
                byte[] _it = this.padding;
                if (!((_it.length != 0 ? KaitaiStream.byteArrayMax(_it) == 0 : true))) {
                    throw new KaitaiStream.ValidationExprError(this.padding, this._io, "/types/padded_bytes/seq/2");
                }
            }
            _dirty = false;
        }

        public void _fetchInstances() {
        }

        public void _write_Seq() {
            _assertNotDirty();
            this._io.writeU8le(this.lenBody);
            this._io.writeBytes(this.body);
            this._io.writeBytes(this.padding);
        }

        public void _check() {
            if (this.body.length != lenBody())
                throw new ConsistencyError("body", lenBody(), this.body.length);
            if (this.padding.length != KaitaiStream.mod(8 - KaitaiStream.mod(lenBody(), 8), 8))
                throw new ConsistencyError("padding", KaitaiStream.mod(8 - KaitaiStream.mod(lenBody(), 8), 8), this.padding.length);
            {
                byte[] _it = this.padding;
                if (!((_it.length != 0 ? KaitaiStream.byteArrayMax(_it) == 0 : true))) {
                    throw new KaitaiStream.ValidationExprError(this.padding, null, "/types/padded_bytes/seq/2");
                }
            }
            _dirty = false;
        }
        private long lenBody;
        private byte[] body;
        private byte[] padding;
        private NixNar _root;
        private KaitaiStruct.ReadWrite _parent;
        public long lenBody() { return lenBody; }
        public void setLenBody(long _v) { _dirty = true; lenBody = _v; }
        public byte[] body() { return body; }
        public void setBody(byte[] _v) { _dirty = true; body = _v; }
        public byte[] padding() { return padding; }
        public void setPadding(byte[] _v) { _dirty = true; padding = _v; }
        public NixNar _root() { return _root; }
        public void set_root(NixNar _v) { _dirty = true; _root = _v; }
        public KaitaiStruct.ReadWrite _parent() { return _parent; }
        public void set_parent(KaitaiStruct.ReadWrite _v) { _dirty = true; _parent = _v; }
    }

    /**
     * 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.
     */
    public static class PaddedStr extends KaitaiStruct.ReadWrite {
        public static PaddedStr fromFile(String fileName) throws IOException {
            return new PaddedStr(new ByteBufferKaitaiStream(fileName));
        }
        public PaddedStr() {
            this(null, null, null);
        }

        public PaddedStr(KaitaiStream _io) {
            this(_io, null, null);
        }

        public PaddedStr(KaitaiStream _io, KaitaiStruct.ReadWrite _parent) {
            this(_io, _parent, null);
        }

        public PaddedStr(KaitaiStream _io, KaitaiStruct.ReadWrite _parent, NixNar _root) {
            super(_io);
            this._parent = _parent;
            this._root = _root;
        }
        public void _read() {
            this.lenBody = this._io.readU8le();
            this.body = new String(this._io.readBytes(lenBody()), StandardCharsets.US_ASCII);
            this.padding = this._io.readBytes(KaitaiStream.mod(8 - KaitaiStream.mod(lenBody(), 8), 8));
            {
                byte[] _it = this.padding;
                if (!((_it.length != 0 ? KaitaiStream.byteArrayMax(_it) == 0 : true))) {
                    throw new KaitaiStream.ValidationExprError(this.padding, this._io, "/types/padded_str/seq/2");
                }
            }
            _dirty = false;
        }

        public void _fetchInstances() {
        }

        public void _write_Seq() {
            _assertNotDirty();
            this._io.writeU8le(this.lenBody);
            this._io.writeBytes((this.body).getBytes(Charset.forName("ASCII")));
            this._io.writeBytes(this.padding);
        }

        public void _check() {
            if ((this.body).getBytes(Charset.forName("ASCII")).length != lenBody())
                throw new ConsistencyError("body", lenBody(), (this.body).getBytes(Charset.forName("ASCII")).length);
            if (this.padding.length != KaitaiStream.mod(8 - KaitaiStream.mod(lenBody(), 8), 8))
                throw new ConsistencyError("padding", KaitaiStream.mod(8 - KaitaiStream.mod(lenBody(), 8), 8), this.padding.length);
            {
                byte[] _it = this.padding;
                if (!((_it.length != 0 ? KaitaiStream.byteArrayMax(_it) == 0 : true))) {
                    throw new KaitaiStream.ValidationExprError(this.padding, null, "/types/padded_str/seq/2");
                }
            }
            _dirty = false;
        }
        private long lenBody;
        private String body;
        private byte[] padding;
        private NixNar _root;
        private KaitaiStruct.ReadWrite _parent;
        public long lenBody() { return lenBody; }
        public void setLenBody(long _v) { _dirty = true; lenBody = _v; }
        public String body() { return body; }
        public void setBody(String _v) { _dirty = true; body = _v; }
        public byte[] padding() { return padding; }
        public void setPadding(byte[] _v) { _dirty = true; padding = _v; }
        public NixNar _root() { return _root; }
        public void set_root(NixNar _v) { _dirty = true; _root = _v; }
        public KaitaiStruct.ReadWrite _parent() { return _parent; }
        public void set_parent(KaitaiStruct.ReadWrite _v) { _dirty = true; _parent = _v; }
    }

    /**
     * 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.
     */
    public static class TypeDirectory extends KaitaiStruct.ReadWrite {
        public static TypeDirectory fromFile(String fileName) throws IOException {
            return new TypeDirectory(new ByteBufferKaitaiStream(fileName));
        }
        public TypeDirectory() {
            this(null, null, null);
        }

        public TypeDirectory(KaitaiStream _io) {
            this(_io, null, null);
        }

        public TypeDirectory(KaitaiStream _io, NixNar.Node _parent) {
            this(_io, _parent, null);
        }

        public TypeDirectory(KaitaiStream _io, NixNar.Node _parent, NixNar _root) {
            super(_io);
            this._parent = _parent;
            this._root = _root;
        }
        public void _read() {
            this.entries = new ArrayList<DirEntry>();
            {
                DirEntry _it;
                int i = 0;
                do {
                    DirEntry _t_entries = new DirEntry(this._io, this, _root);
                    try {
                        _t_entries._read();
                    } finally {
                        _it = _t_entries;
                        this.entries.add(_it);
                    }
                    {
                        DirEntry _it = this.entries.get(((Number) (i)).intValue());
                        if (!(( ((i != 0) && (!(_it.isTerminator())))  ? (KaitaiStream.byteArrayCompare(_it.body().name().body(), entries().get(((Number) (i - 1)).intValue()).body().name().body()) > 0) : true))) {
                            throw new KaitaiStream.ValidationExprError(this.entries.get(((Number) (i)).intValue()), this._io, "/types/type_directory/seq/0");
                        }
                    }
                    i++;
                } while (!(_it.isTerminator()));
            }
            _dirty = false;
        }

        public void _fetchInstances() {
            for (int i = 0; i < this.entries.size(); i++) {
                this.entries.get(((Number) (i)).intValue())._fetchInstances();
            }
        }

        public void _write_Seq() {
            _assertNotDirty();
            for (int i = 0; i < this.entries.size(); i++) {
                this.entries.get(((Number) (i)).intValue())._write_Seq(this._io);
            }
        }

        public void _check() {
            if (this.entries.size() == 0)
                throw new ConsistencyError("entries", 0, this.entries.size());
            for (int i = 0; i < this.entries.size(); i++) {
                if (!Objects.equals(this.entries.get(((Number) (i)).intValue())._root(), _root()))
                    throw new ConsistencyError("entries", _root(), this.entries.get(((Number) (i)).intValue())._root());
                if (!Objects.equals(this.entries.get(((Number) (i)).intValue())._parent(), this))
                    throw new ConsistencyError("entries", this, this.entries.get(((Number) (i)).intValue())._parent());
                {
                    DirEntry _it = this.entries.get(((Number) (i)).intValue());
                    if (!(( ((i != 0) && (!(_it.isTerminator())))  ? (KaitaiStream.byteArrayCompare(_it.body().name().body(), entries().get(((Number) (i - 1)).intValue()).body().name().body()) > 0) : true))) {
                        throw new KaitaiStream.ValidationExprError(this.entries.get(((Number) (i)).intValue()), null, "/types/type_directory/seq/0");
                    }
                }
                {
                    DirEntry _it = this.entries.get(((Number) (i)).intValue());
                    if (_it.isTerminator() != (i == this.entries.size() - 1))
                        throw new ConsistencyError("entries", i == this.entries.size() - 1, _it.isTerminator());
                }
            }
            _dirty = false;
        }

        /**
         * A single entry within a directory, or the terminator, i.e. the `)`
         * token that ends the directory node.
         */
        public static class DirEntry extends KaitaiStruct.ReadWrite {
            public static DirEntry fromFile(String fileName) throws IOException {
                return new DirEntry(new ByteBufferKaitaiStream(fileName));
            }
            public DirEntry() {
                this(null, null, null);
            }

            public DirEntry(KaitaiStream _io) {
                this(_io, null, null);
            }

            public DirEntry(KaitaiStream _io, NixNar.TypeDirectory _parent) {
                this(_io, _parent, null);
            }

            public DirEntry(KaitaiStream _io, NixNar.TypeDirectory _parent, NixNar _root) {
                super(_io);
                this._parent = _parent;
                this._root = _root;
            }
            public void _read() {
                this.kind = new PaddedStr(this._io, this, _root);
                this.kind._read();
                {
                    PaddedStr _it = this.kind;
                    if (!( ((_it.body().equals("entry")) || (_it.body().equals(")"))) )) {
                        throw new KaitaiStream.ValidationExprError(this.kind, this._io, "/types/type_directory/types/dir_entry/seq/0");
                    }
                }
                if (!(isTerminator())) {
                    this.body = new DirEntryBody(this._io, this, _root);
                    this.body._read();
                }
                _dirty = false;
            }

            public void _fetchInstances() {
                this.kind._fetchInstances();
                if (!(isTerminator())) {
                    this.body._fetchInstances();
                }
            }

            public void _write_Seq() {
                _assertNotDirty();
                this.kind._write_Seq(this._io);
                if (!(isTerminator())) {
                    this.body._write_Seq(this._io);
                }
            }

            public void _check() {
                if (!Objects.equals(this.kind._root(), _root()))
                    throw new ConsistencyError("kind", _root(), this.kind._root());
                if (!Objects.equals(this.kind._parent(), this))
                    throw new ConsistencyError("kind", this, this.kind._parent());
                {
                    PaddedStr _it = this.kind;
                    if (!( ((_it.body().equals("entry")) || (_it.body().equals(")"))) )) {
                        throw new KaitaiStream.ValidationExprError(this.kind, null, "/types/type_directory/types/dir_entry/seq/0");
                    }
                }
                if (!(isTerminator())) {
                    if (!Objects.equals(this.body._root(), _root()))
                        throw new ConsistencyError("body", _root(), this.body._root());
                    if (!Objects.equals(this.body._parent(), this))
                        throw new ConsistencyError("body", this, this.body._parent());
                }
                _dirty = false;
            }
            private Boolean isTerminator;
            public Boolean isTerminator() {
                if (this.isTerminator != null)
                    return this.isTerminator;
                this.isTerminator = kind().body().equals(")");
                return this.isTerminator;
            }
            public void _invalidateIsTerminator() { this.isTerminator = null; }
            private PaddedStr kind;
            private DirEntryBody body;
            private NixNar _root;
            private NixNar.TypeDirectory _parent;

            /**
             * Must be `entry` (for a child node) or `)` (for the terminator).
             */
            public PaddedStr kind() { return kind; }
            public void setKind(PaddedStr _v) { _dirty = true; kind = _v; }

            /**
             * The entry itself. The terminator has no `body`.
             */
            public DirEntryBody body() { return body; }
            public void setBody(DirEntryBody _v) { _dirty = true; body = _v; }
            public NixNar _root() { return _root; }
            public void set_root(NixNar _v) { _dirty = true; _root = _v; }
            public NixNar.TypeDirectory _parent() { return _parent; }
            public void set_parent(NixNar.TypeDirectory _v) { _dirty = true; _parent = _v; }
        }
        public static class DirEntryBody extends KaitaiStruct.ReadWrite {
            public static DirEntryBody fromFile(String fileName) throws IOException {
                return new DirEntryBody(new ByteBufferKaitaiStream(fileName));
            }
            public DirEntryBody() {
                this(null, null, null);
            }

            public DirEntryBody(KaitaiStream _io) {
                this(_io, null, null);
            }

            public DirEntryBody(KaitaiStream _io, NixNar.TypeDirectory.DirEntry _parent) {
                this(_io, _parent, null);
            }

            public DirEntryBody(KaitaiStream _io, NixNar.TypeDirectory.DirEntry _parent, NixNar _root) {
                super(_io);
                this._parent = _parent;
                this._root = _root;
            }
            public void _read() {
                this.openParen = new PaddedStr(this._io, this, _root);
                this.openParen._read();
                {
                    PaddedStr _it = this.openParen;
                    if (!(_it.body().equals("("))) {
                        throw new KaitaiStream.ValidationExprError(this.openParen, this._io, "/types/type_directory/types/dir_entry_body/seq/0");
                    }
                }
                this.nameKey = new PaddedStr(this._io, this, _root);
                this.nameKey._read();
                {
                    PaddedStr _it = this.nameKey;
                    if (!(_it.body().equals("name"))) {
                        throw new KaitaiStream.ValidationExprError(this.nameKey, this._io, "/types/type_directory/types/dir_entry_body/seq/1");
                    }
                }
                this.name = new PaddedBytes(this._io, this, _root);
                this.name._read();
                {
                    PaddedBytes _it = this.name;
                    if (!( ((_it.body().length != 0) && (_it.body().length <= 255) && (!Arrays.equals(_it.body(), new byte[] { 46 })) && (!Arrays.equals(_it.body(), new byte[] { 46, 46 })) && (KaitaiStream.byteArrayMin(_it.body()) != 0)) )) {
                        throw new KaitaiStream.ValidationExprError(this.name, this._io, "/types/type_directory/types/dir_entry_body/seq/2");
                    }
                }
                this.nodeKey = new PaddedStr(this._io, this, _root);
                this.nodeKey._read();
                {
                    PaddedStr _it = this.nodeKey;
                    if (!(_it.body().equals("node"))) {
                        throw new KaitaiStream.ValidationExprError(this.nodeKey, this._io, "/types/type_directory/types/dir_entry_body/seq/3");
                    }
                }
                this.node = new Node(this._io, this, _root, _parent()._parent()._parent().depth() + 1);
                this.node._read();
                this.closeParen = new PaddedStr(this._io, this, _root);
                this.closeParen._read();
                {
                    PaddedStr _it = this.closeParen;
                    if (!(_it.body().equals(")"))) {
                        throw new KaitaiStream.ValidationExprError(this.closeParen, this._io, "/types/type_directory/types/dir_entry_body/seq/5");
                    }
                }
                _dirty = false;
            }

            public void _fetchInstances() {
                this.openParen._fetchInstances();
                this.nameKey._fetchInstances();
                this.name._fetchInstances();
                this.nodeKey._fetchInstances();
                this.node._fetchInstances();
                this.closeParen._fetchInstances();
            }

            public void _write_Seq() {
                _assertNotDirty();
                this.openParen._write_Seq(this._io);
                this.nameKey._write_Seq(this._io);
                this.name._write_Seq(this._io);
                this.nodeKey._write_Seq(this._io);
                this.node._write_Seq(this._io);
                this.closeParen._write_Seq(this._io);
            }

            public void _check() {
                if (!Objects.equals(this.openParen._root(), _root()))
                    throw new ConsistencyError("open_paren", _root(), this.openParen._root());
                if (!Objects.equals(this.openParen._parent(), this))
                    throw new ConsistencyError("open_paren", this, this.openParen._parent());
                {
                    PaddedStr _it = this.openParen;
                    if (!(_it.body().equals("("))) {
                        throw new KaitaiStream.ValidationExprError(this.openParen, null, "/types/type_directory/types/dir_entry_body/seq/0");
                    }
                }
                if (!Objects.equals(this.nameKey._root(), _root()))
                    throw new ConsistencyError("name_key", _root(), this.nameKey._root());
                if (!Objects.equals(this.nameKey._parent(), this))
                    throw new ConsistencyError("name_key", this, this.nameKey._parent());
                {
                    PaddedStr _it = this.nameKey;
                    if (!(_it.body().equals("name"))) {
                        throw new KaitaiStream.ValidationExprError(this.nameKey, null, "/types/type_directory/types/dir_entry_body/seq/1");
                    }
                }
                if (!Objects.equals(this.name._root(), _root()))
                    throw new ConsistencyError("name", _root(), this.name._root());
                if (!Objects.equals(this.name._parent(), this))
                    throw new ConsistencyError("name", this, this.name._parent());
                {
                    PaddedBytes _it = this.name;
                    if (!( ((_it.body().length != 0) && (_it.body().length <= 255) && (!Arrays.equals(_it.body(), new byte[] { 46 })) && (!Arrays.equals(_it.body(), new byte[] { 46, 46 })) && (KaitaiStream.byteArrayMin(_it.body()) != 0)) )) {
                        throw new KaitaiStream.ValidationExprError(this.name, null, "/types/type_directory/types/dir_entry_body/seq/2");
                    }
                }
                if (!Objects.equals(this.nodeKey._root(), _root()))
                    throw new ConsistencyError("node_key", _root(), this.nodeKey._root());
                if (!Objects.equals(this.nodeKey._parent(), this))
                    throw new ConsistencyError("node_key", this, this.nodeKey._parent());
                {
                    PaddedStr _it = this.nodeKey;
                    if (!(_it.body().equals("node"))) {
                        throw new KaitaiStream.ValidationExprError(this.nodeKey, null, "/types/type_directory/types/dir_entry_body/seq/3");
                    }
                }
                if (!Objects.equals(this.node._root(), _root()))
                    throw new ConsistencyError("node", _root(), this.node._root());
                if (!Objects.equals(this.node._parent(), this))
                    throw new ConsistencyError("node", this, this.node._parent());
                if (this.node.depth() != _parent()._parent()._parent().depth() + 1)
                    throw new ConsistencyError("node", _parent()._parent()._parent().depth() + 1, this.node.depth());
                if (!Objects.equals(this.closeParen._root(), _root()))
                    throw new ConsistencyError("close_paren", _root(), this.closeParen._root());
                if (!Objects.equals(this.closeParen._parent(), this))
                    throw new ConsistencyError("close_paren", this, this.closeParen._parent());
                {
                    PaddedStr _it = this.closeParen;
                    if (!(_it.body().equals(")"))) {
                        throw new KaitaiStream.ValidationExprError(this.closeParen, null, "/types/type_directory/types/dir_entry_body/seq/5");
                    }
                }
                _dirty = false;
            }
            private PaddedStr openParen;
            private PaddedStr nameKey;
            private PaddedBytes name;
            private PaddedStr nodeKey;
            private Node node;
            private PaddedStr closeParen;
            private NixNar _root;
            private NixNar.TypeDirectory.DirEntry _parent;
            public PaddedStr openParen() { return openParen; }
            public void setOpenParen(PaddedStr _v) { _dirty = true; openParen = _v; }
            public PaddedStr nameKey() { return nameKey; }
            public void setNameKey(PaddedStr _v) { _dirty = true; nameKey = _v; }

            /**
             * 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 PaddedBytes name() { return name; }
            public void setName(PaddedBytes _v) { _dirty = true; name = _v; }
            public PaddedStr nodeKey() { return nodeKey; }
            public void setNodeKey(PaddedStr _v) { _dirty = true; nodeKey = _v; }

            /**
             * The child node.
             */
            public Node node() { return node; }
            public void setNode(Node _v) { _dirty = true; node = _v; }
            public PaddedStr closeParen() { return closeParen; }
            public void setCloseParen(PaddedStr _v) { _dirty = true; closeParen = _v; }
            public NixNar _root() { return _root; }
            public void set_root(NixNar _v) { _dirty = true; _root = _v; }
            public NixNar.TypeDirectory.DirEntry _parent() { return _parent; }
            public void set_parent(NixNar.TypeDirectory.DirEntry _v) { _dirty = true; _parent = _v; }
        }
        private List<DirEntry> entries;
        private NixNar _root;
        private NixNar.Node _parent;
        public List<DirEntry> entries() { return entries; }
        public void setEntries(List<DirEntry> _v) { _dirty = true; entries = _v; }
        public NixNar _root() { return _root; }
        public void set_root(NixNar _v) { _dirty = true; _root = _v; }
        public NixNar.Node _parent() { return _parent; }
        public void set_parent(NixNar.Node _v) { _dirty = true; _parent = _v; }
    }

    /**
     * A regular file node.
     */
    public static class TypeRegular extends KaitaiStruct.ReadWrite {
        public static TypeRegular fromFile(String fileName) throws IOException {
            return new TypeRegular(new ByteBufferKaitaiStream(fileName));
        }
        public TypeRegular() {
            this(null, null, null);
        }

        public TypeRegular(KaitaiStream _io) {
            this(_io, null, null);
        }

        public TypeRegular(KaitaiStream _io, NixNar.Node _parent) {
            this(_io, _parent, null);
        }

        public TypeRegular(KaitaiStream _io, NixNar.Node _parent, NixNar _root) {
            super(_io);
            this._parent = _parent;
            this._root = _root;
        }
        public void _read() {
            this.firstKey = new PaddedStr(this._io, this, _root);
            this.firstKey._read();
            {
                PaddedStr _it = this.firstKey;
                if (!( ((_it.body().equals("executable")) || (_it.body().equals("contents"))) )) {
                    throw new KaitaiStream.ValidationExprError(this.firstKey, this._io, "/types/type_regular/seq/0");
                }
            }
            if (isExecutable()) {
                this.executableVal = new PaddedStr(this._io, this, _root);
                this.executableVal._read();
                {
                    PaddedStr _it = this.executableVal;
                    if (!(_it.body().equals(""))) {
                        throw new KaitaiStream.ValidationExprError(this.executableVal, this._io, "/types/type_regular/seq/1");
                    }
                }
            }
            if (isExecutable()) {
                this.contentsKey = new PaddedStr(this._io, this, _root);
                this.contentsKey._read();
                {
                    PaddedStr _it = this.contentsKey;
                    if (!(_it.body().equals("contents"))) {
                        throw new KaitaiStream.ValidationExprError(this.contentsKey, this._io, "/types/type_regular/seq/2");
                    }
                }
            }
            this.fileData = new FileContent(this._io, this, _root);
            this.fileData._read();
            _dirty = false;
        }

        public void _fetchInstances() {
            this.firstKey._fetchInstances();
            if (isExecutable()) {
                this.executableVal._fetchInstances();
            }
            if (isExecutable()) {
                this.contentsKey._fetchInstances();
            }
            this.fileData._fetchInstances();
        }

        public void _write_Seq() {
            _assertNotDirty();
            this.firstKey._write_Seq(this._io);
            if (isExecutable()) {
                this.executableVal._write_Seq(this._io);
            }
            if (isExecutable()) {
                this.contentsKey._write_Seq(this._io);
            }
            this.fileData._write_Seq(this._io);
        }

        public void _check() {
            if (!Objects.equals(this.firstKey._root(), _root()))
                throw new ConsistencyError("first_key", _root(), this.firstKey._root());
            if (!Objects.equals(this.firstKey._parent(), this))
                throw new ConsistencyError("first_key", this, this.firstKey._parent());
            {
                PaddedStr _it = this.firstKey;
                if (!( ((_it.body().equals("executable")) || (_it.body().equals("contents"))) )) {
                    throw new KaitaiStream.ValidationExprError(this.firstKey, null, "/types/type_regular/seq/0");
                }
            }
            if (isExecutable()) {
                if (!Objects.equals(this.executableVal._root(), _root()))
                    throw new ConsistencyError("executable_val", _root(), this.executableVal._root());
                if (!Objects.equals(this.executableVal._parent(), this))
                    throw new ConsistencyError("executable_val", this, this.executableVal._parent());
                {
                    PaddedStr _it = this.executableVal;
                    if (!(_it.body().equals(""))) {
                        throw new KaitaiStream.ValidationExprError(this.executableVal, null, "/types/type_regular/seq/1");
                    }
                }
            }
            if (isExecutable()) {
                if (!Objects.equals(this.contentsKey._root(), _root()))
                    throw new ConsistencyError("contents_key", _root(), this.contentsKey._root());
                if (!Objects.equals(this.contentsKey._parent(), this))
                    throw new ConsistencyError("contents_key", this, this.contentsKey._parent());
                {
                    PaddedStr _it = this.contentsKey;
                    if (!(_it.body().equals("contents"))) {
                        throw new KaitaiStream.ValidationExprError(this.contentsKey, null, "/types/type_regular/seq/2");
                    }
                }
            }
            if (!Objects.equals(this.fileData._root(), _root()))
                throw new ConsistencyError("file_data", _root(), this.fileData._root());
            if (!Objects.equals(this.fileData._parent(), this))
                throw new ConsistencyError("file_data", this, this.fileData._parent());
            _dirty = false;
        }

        /**
         * The contents of the file, prefixed with their length and padded
         * with null bytes to the next 8-byte boundary.
         */
        public static class FileContent extends KaitaiStruct.ReadWrite {
            public static FileContent fromFile(String fileName) throws IOException {
                return new FileContent(new ByteBufferKaitaiStream(fileName));
            }
            public FileContent() {
                this(null, null, null);
            }

            public FileContent(KaitaiStream _io) {
                this(_io, null, null);
            }

            public FileContent(KaitaiStream _io, NixNar.TypeRegular _parent) {
                this(_io, _parent, null);
            }

            public FileContent(KaitaiStream _io, NixNar.TypeRegular _parent, NixNar _root) {
                super(_io);
                this._parent = _parent;
                this._root = _root;
            }
            public void _read() {
                this.lenContents = this._io.readU8le();
                if (narOffset() < 0) {
                    this._unnamed1 = this._io.readBytes(0);
                }
                this.contents = this._io.readBytes(lenContents());
                this.padding = this._io.readBytes(KaitaiStream.mod(8 - KaitaiStream.mod(lenContents(), 8), 8));
                {
                    byte[] _it = this.padding;
                    if (!((_it.length != 0 ? KaitaiStream.byteArrayMax(_it) == 0 : true))) {
                        throw new KaitaiStream.ValidationExprError(this.padding, this._io, "/types/type_regular/types/file_content/seq/3");
                    }
                }
                _dirty = false;
            }

            public void _fetchInstances() {
                if (narOffset() < 0) {
                }
            }

            public void _write_Seq() {
                _assertNotDirty();
                this._io.writeU8le(this.lenContents);
                if (narOffset() < 0) {
                    if (this._unnamed1.length != 0)
                        throw new ConsistencyError("_unnamed1", 0, this._unnamed1.length);
                    this._io.writeBytes(this._unnamed1);
                }
                this._io.writeBytes(this.contents);
                this._io.writeBytes(this.padding);
            }

            public void _check() {
                if (this.contents.length != lenContents())
                    throw new ConsistencyError("contents", lenContents(), this.contents.length);
                if (this.padding.length != KaitaiStream.mod(8 - KaitaiStream.mod(lenContents(), 8), 8))
                    throw new ConsistencyError("padding", KaitaiStream.mod(8 - KaitaiStream.mod(lenContents(), 8), 8), this.padding.length);
                {
                    byte[] _it = this.padding;
                    if (!((_it.length != 0 ? KaitaiStream.byteArrayMax(_it) == 0 : true))) {
                        throw new KaitaiStream.ValidationExprError(this.padding, null, "/types/type_regular/types/file_content/seq/3");
                    }
                }
                _dirty = false;
            }
            private Integer 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 Integer narOffset() {
                if (this.narOffset != null)
                    return this.narOffset;
                this.narOffset = ((Number) (_io().pos())).intValue();
                return this.narOffset;
            }
            public void _invalidateNarOffset() { this.narOffset = null; }
            private long lenContents;
            private byte[] _unnamed1;
            private byte[] contents;
            private byte[] padding;
            private NixNar _root;
            private NixNar.TypeRegular _parent;
            public long lenContents() { return lenContents; }
            public void setLenContents(long _v) { _dirty = true; lenContents = _v; }
            public byte[] _unnamed1() { return _unnamed1; }
            public void set_unnamed1(byte[] _v) { _dirty = true; _unnamed1 = _v; }

            /**
             * 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 byte[] contents() { return contents; }
            public void setContents(byte[] _v) { _dirty = true; contents = _v; }
            public byte[] padding() { return padding; }
            public void setPadding(byte[] _v) { _dirty = true; padding = _v; }
            public NixNar _root() { return _root; }
            public void set_root(NixNar _v) { _dirty = true; _root = _v; }
            public NixNar.TypeRegular _parent() { return _parent; }
            public void set_parent(NixNar.TypeRegular _v) { _dirty = true; _parent = _v; }
        }
        private Boolean isExecutable;

        /**
         * True if the file has the `executable` attribute.
         */
        public Boolean isExecutable() {
            if (this.isExecutable != null)
                return this.isExecutable;
            this.isExecutable = firstKey().body().equals("executable");
            return this.isExecutable;
        }
        public void _invalidateIsExecutable() { this.isExecutable = null; }
        private PaddedStr firstKey;
        private PaddedStr executableVal;
        private PaddedStr contentsKey;
        private FileContent fileData;
        private NixNar _root;
        private NixNar.Node _parent;

        /**
         * Either `executable` or `contents`.
         */
        public PaddedStr firstKey() { return firstKey; }
        public void setFirstKey(PaddedStr _v) { _dirty = true; firstKey = _v; }

        /**
         * Must be empty.
         */
        public PaddedStr executableVal() { return executableVal; }
        public void setExecutableVal(PaddedStr _v) { _dirty = true; executableVal = _v; }
        public PaddedStr contentsKey() { return contentsKey; }
        public void setContentsKey(PaddedStr _v) { _dirty = true; contentsKey = _v; }
        public FileContent fileData() { return fileData; }
        public void setFileData(FileContent _v) { _dirty = true; fileData = _v; }
        public NixNar _root() { return _root; }
        public void set_root(NixNar _v) { _dirty = true; _root = _v; }
        public NixNar.Node _parent() { return _parent; }
        public void set_parent(NixNar.Node _v) { _dirty = true; _parent = _v; }
    }

    /**
     * A symbolic link node.
     */
    public static class TypeSymlink extends KaitaiStruct.ReadWrite {
        public static TypeSymlink fromFile(String fileName) throws IOException {
            return new TypeSymlink(new ByteBufferKaitaiStream(fileName));
        }
        public TypeSymlink() {
            this(null, null, null);
        }

        public TypeSymlink(KaitaiStream _io) {
            this(_io, null, null);
        }

        public TypeSymlink(KaitaiStream _io, NixNar.Node _parent) {
            this(_io, _parent, null);
        }

        public TypeSymlink(KaitaiStream _io, NixNar.Node _parent, NixNar _root) {
            super(_io);
            this._parent = _parent;
            this._root = _root;
        }
        public void _read() {
            this.targetKey = new PaddedStr(this._io, this, _root);
            this.targetKey._read();
            {
                PaddedStr _it = this.targetKey;
                if (!(_it.body().equals("target"))) {
                    throw new KaitaiStream.ValidationExprError(this.targetKey, this._io, "/types/type_symlink/seq/0");
                }
            }
            this.targetVal = new PaddedBytes(this._io, this, _root);
            this.targetVal._read();
            {
                PaddedBytes _it = this.targetVal;
                if (!( ((_it.body().length != 0) && (_it.body().length <= 4095) && (KaitaiStream.byteArrayMin(_it.body()) != 0)) )) {
                    throw new KaitaiStream.ValidationExprError(this.targetVal, this._io, "/types/type_symlink/seq/1");
                }
            }
            _dirty = false;
        }

        public void _fetchInstances() {
            this.targetKey._fetchInstances();
            this.targetVal._fetchInstances();
        }

        public void _write_Seq() {
            _assertNotDirty();
            this.targetKey._write_Seq(this._io);
            this.targetVal._write_Seq(this._io);
        }

        public void _check() {
            if (!Objects.equals(this.targetKey._root(), _root()))
                throw new ConsistencyError("target_key", _root(), this.targetKey._root());
            if (!Objects.equals(this.targetKey._parent(), this))
                throw new ConsistencyError("target_key", this, this.targetKey._parent());
            {
                PaddedStr _it = this.targetKey;
                if (!(_it.body().equals("target"))) {
                    throw new KaitaiStream.ValidationExprError(this.targetKey, null, "/types/type_symlink/seq/0");
                }
            }
            if (!Objects.equals(this.targetVal._root(), _root()))
                throw new ConsistencyError("target_val", _root(), this.targetVal._root());
            if (!Objects.equals(this.targetVal._parent(), this))
                throw new ConsistencyError("target_val", this, this.targetVal._parent());
            {
                PaddedBytes _it = this.targetVal;
                if (!( ((_it.body().length != 0) && (_it.body().length <= 4095) && (KaitaiStream.byteArrayMin(_it.body()) != 0)) )) {
                    throw new KaitaiStream.ValidationExprError(this.targetVal, null, "/types/type_symlink/seq/1");
                }
            }
            _dirty = false;
        }
        private PaddedStr targetKey;
        private PaddedBytes targetVal;
        private NixNar _root;
        private NixNar.Node _parent;

        /**
         * Must be `target`.
         */
        public PaddedStr targetKey() { return targetKey; }
        public void setTargetKey(PaddedStr _v) { _dirty = true; targetKey = _v; }

        /**
         * 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 PaddedBytes targetVal() { return targetVal; }
        public void setTargetVal(PaddedBytes _v) { _dirty = true; targetVal = _v; }
        public NixNar _root() { return _root; }
        public void set_root(NixNar _v) { _dirty = true; _root = _v; }
        public NixNar.Node _parent() { return _parent; }
        public void set_parent(NixNar.Node _v) { _dirty = true; _parent = _v; }
    }
    private PaddedStr magic;
    private Node rootNode;
    private NixNar _root;
    private KaitaiStruct.ReadWrite _parent;

    /**
     * Magic string, must be `nix-archive-1`.
     */
    public PaddedStr magic() { return magic; }
    public void setMagic(PaddedStr _v) { _dirty = true; magic = _v; }

    /**
     * The root of the archive, which is always a single node.
     */
    public Node rootNode() { return rootNode; }
    public void setRootNode(Node _v) { _dirty = true; rootNode = _v; }
    public NixNar _root() { return _root; }
    public void set_root(NixNar _v) { _dirty = true; _root = _v; }
    public KaitaiStruct.ReadWrite _parent() { return _parent; }
    public void set_parent(KaitaiStruct.ReadWrite _v) { _dirty = true; _parent = _v; }
}