# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild
# type: ignore

import kaitaistruct
from kaitaistruct import ReadWriteKaitaiStruct, KaitaiStream, BytesIO


if getattr(kaitaistruct, 'API_VERSION', (0, 9)) < (0, 11):
    raise Exception("Incompatible Kaitai Struct Python API: 0.11 or later is required, but you have %s" % (kaitaistruct.__version__))

class NixNar(ReadWriteKaitaiStruct):
    """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.
    
    .. seealso::
       Git tag "2.36.0" - https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/doc/manual/source/protocols/nix-archive/index.md
    """
    def __init__(self, _io=None, _parent=None, _root=None):
        super(NixNar, self).__init__(_io)
        self._parent = _parent
        self._root = _root or self

    def _read(self):
        self.magic = NixNar.PaddedStr(self._io, self, self._root)
        self.magic._read()
        _ = self.magic
        if not _.body == u"nix-archive-1":
            raise kaitaistruct.ValidationExprError(self.magic, self._io, u"/seq/0")
        self.root_node = NixNar.Node(0, self._io, self, self._root)
        self.root_node._read()
        self._dirty = False


    def _fetch_instances(self):
        pass
        self.magic._fetch_instances()
        self.root_node._fetch_instances()


    def _write__seq(self, io=None):
        super(NixNar, self)._write__seq(io)
        self.magic._write__seq(self._io)
        self.root_node._write__seq(self._io)


    def _check(self):
        if self.magic._root != self._root:
            raise kaitaistruct.ConsistencyError(u"magic", self._root, self.magic._root)
        if self.magic._parent != self:
            raise kaitaistruct.ConsistencyError(u"magic", self, self.magic._parent)
        _ = self.magic
        if not _.body == u"nix-archive-1":
            raise kaitaistruct.ValidationExprError(self.magic, None, u"/seq/0")
        if self.root_node._root != self._root:
            raise kaitaistruct.ConsistencyError(u"root_node", self._root, self.root_node._root)
        if self.root_node._parent != self:
            raise kaitaistruct.ConsistencyError(u"root_node", self, self.root_node._parent)
        if self.root_node.depth != 0:
            raise kaitaistruct.ConsistencyError(u"root_node", 0, self.root_node.depth)
        self._dirty = False

    class Node(ReadWriteKaitaiStruct):
        """A single file system node (regular file, directory, or symlink)."""
        def __init__(self, depth, _io=None, _parent=None, _root=None):
            super(NixNar.Node, self).__init__(_io)
            self._parent = _parent
            self._root = _root
            self.depth = depth

        def _read(self):
            self._unnamed0 = self._io.read_bytes(0)
            _ = self._unnamed0
            if not self.depth < 64:
                raise kaitaistruct.ValidationExprError(self._unnamed0, self._io, u"/types/node/seq/0")
            self.open_paren = NixNar.PaddedStr(self._io, self, self._root)
            self.open_paren._read()
            _ = self.open_paren
            if not _.body == u"(":
                raise kaitaistruct.ValidationExprError(self.open_paren, self._io, u"/types/node/seq/1")
            self.type_key = NixNar.PaddedStr(self._io, self, self._root)
            self.type_key._read()
            _ = self.type_key
            if not _.body == u"type":
                raise kaitaistruct.ValidationExprError(self.type_key, self._io, u"/types/node/seq/2")
            self.type_val = NixNar.PaddedStr(self._io, self, self._root)
            self.type_val._read()
            _ = self.type_val
            if not  ((_.body == u"regular") or (_.body == u"directory") or (_.body == u"symlink")) :
                raise kaitaistruct.ValidationExprError(self.type_val, self._io, u"/types/node/seq/3")
            _on = self.type_val.body
            if _on == u"directory":
                pass
                self.body = NixNar.TypeDirectory(self._io, self, self._root)
                self.body._read()
            elif _on == u"regular":
                pass
                self.body = NixNar.TypeRegular(self._io, self, self._root)
                self.body._read()
            elif _on == u"symlink":
                pass
                self.body = NixNar.TypeSymlink(self._io, self, self._root)
                self.body._read()
            if self.type_val.body != u"directory":
                pass
                self.close_paren = NixNar.PaddedStr(self._io, self, self._root)
                self.close_paren._read()
                _ = self.close_paren
                if not _.body == u")":
                    raise kaitaistruct.ValidationExprError(self.close_paren, self._io, u"/types/node/seq/5")

            self._dirty = False


        def _fetch_instances(self):
            pass
            self.open_paren._fetch_instances()
            self.type_key._fetch_instances()
            self.type_val._fetch_instances()
            _on = self.type_val.body
            if _on == u"directory":
                pass
                self.body._fetch_instances()
            elif _on == u"regular":
                pass
                self.body._fetch_instances()
            elif _on == u"symlink":
                pass
                self.body._fetch_instances()
            if self.type_val.body != u"directory":
                pass
                self.close_paren._fetch_instances()



        def _write__seq(self, io=None):
            super(NixNar.Node, self)._write__seq(io)
            self._io.write_bytes(self._unnamed0)
            self.open_paren._write__seq(self._io)
            self.type_key._write__seq(self._io)
            self.type_val._write__seq(self._io)
            _on = self.type_val.body
            if _on == u"directory":
                pass
                self.body._write__seq(self._io)
            elif _on == u"regular":
                pass
                self.body._write__seq(self._io)
            elif _on == u"symlink":
                pass
                self.body._write__seq(self._io)
            if self.type_val.body != u"directory":
                pass
                self.close_paren._write__seq(self._io)



        def _check(self):
            if len(self._unnamed0) != 0:
                raise kaitaistruct.ConsistencyError(u"_unnamed0", 0, len(self._unnamed0))
            _ = self._unnamed0
            if not self.depth < 64:
                raise kaitaistruct.ValidationExprError(self._unnamed0, None, u"/types/node/seq/0")
            if self.open_paren._root != self._root:
                raise kaitaistruct.ConsistencyError(u"open_paren", self._root, self.open_paren._root)
            if self.open_paren._parent != self:
                raise kaitaistruct.ConsistencyError(u"open_paren", self, self.open_paren._parent)
            _ = self.open_paren
            if not _.body == u"(":
                raise kaitaistruct.ValidationExprError(self.open_paren, None, u"/types/node/seq/1")
            if self.type_key._root != self._root:
                raise kaitaistruct.ConsistencyError(u"type_key", self._root, self.type_key._root)
            if self.type_key._parent != self:
                raise kaitaistruct.ConsistencyError(u"type_key", self, self.type_key._parent)
            _ = self.type_key
            if not _.body == u"type":
                raise kaitaistruct.ValidationExprError(self.type_key, None, u"/types/node/seq/2")
            if self.type_val._root != self._root:
                raise kaitaistruct.ConsistencyError(u"type_val", self._root, self.type_val._root)
            if self.type_val._parent != self:
                raise kaitaistruct.ConsistencyError(u"type_val", self, self.type_val._parent)
            _ = self.type_val
            if not  ((_.body == u"regular") or (_.body == u"directory") or (_.body == u"symlink")) :
                raise kaitaistruct.ValidationExprError(self.type_val, None, u"/types/node/seq/3")
            _on = self.type_val.body
            if _on == u"directory":
                pass
                if self.body._root != self._root:
                    raise kaitaistruct.ConsistencyError(u"body", self._root, self.body._root)
                if self.body._parent != self:
                    raise kaitaistruct.ConsistencyError(u"body", self, self.body._parent)
            elif _on == u"regular":
                pass
                if self.body._root != self._root:
                    raise kaitaistruct.ConsistencyError(u"body", self._root, self.body._root)
                if self.body._parent != self:
                    raise kaitaistruct.ConsistencyError(u"body", self, self.body._parent)
            elif _on == u"symlink":
                pass
                if self.body._root != self._root:
                    raise kaitaistruct.ConsistencyError(u"body", self._root, self.body._root)
                if self.body._parent != self:
                    raise kaitaistruct.ConsistencyError(u"body", self, self.body._parent)
            if self.type_val.body != u"directory":
                pass
                if self.close_paren._root != self._root:
                    raise kaitaistruct.ConsistencyError(u"close_paren", self._root, self.close_paren._root)
                if self.close_paren._parent != self:
                    raise kaitaistruct.ConsistencyError(u"close_paren", self, self.close_paren._parent)
                _ = self.close_paren
                if not _.body == u")":
                    raise kaitaistruct.ValidationExprError(self.close_paren, None, u"/types/node/seq/5")

            self._dirty = False


    class PaddedBytes(ReadWriteKaitaiStruct):
        """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.
        """
        def __init__(self, _io=None, _parent=None, _root=None):
            super(NixNar.PaddedBytes, self).__init__(_io)
            self._parent = _parent
            self._root = _root

        def _read(self):
            self.len_body = self._io.read_u8le()
            self.body = self._io.read_bytes(self.len_body)
            self.padding = self._io.read_bytes((8 - self.len_body % 8) % 8)
            _ = self.padding
            if not (KaitaiStream.byte_array_max(_) == 0 if len(_) != 0 else True):
                raise kaitaistruct.ValidationExprError(self.padding, self._io, u"/types/padded_bytes/seq/2")
            self._dirty = False


        def _fetch_instances(self):
            pass


        def _write__seq(self, io=None):
            super(NixNar.PaddedBytes, self)._write__seq(io)
            self._io.write_u8le(self.len_body)
            self._io.write_bytes(self.body)
            self._io.write_bytes(self.padding)


        def _check(self):
            if len(self.body) != self.len_body:
                raise kaitaistruct.ConsistencyError(u"body", self.len_body, len(self.body))
            if len(self.padding) != (8 - self.len_body % 8) % 8:
                raise kaitaistruct.ConsistencyError(u"padding", (8 - self.len_body % 8) % 8, len(self.padding))
            _ = self.padding
            if not (KaitaiStream.byte_array_max(_) == 0 if len(_) != 0 else True):
                raise kaitaistruct.ValidationExprError(self.padding, None, u"/types/padded_bytes/seq/2")
            self._dirty = False


    class PaddedStr(ReadWriteKaitaiStruct):
        """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.
        """
        def __init__(self, _io=None, _parent=None, _root=None):
            super(NixNar.PaddedStr, self).__init__(_io)
            self._parent = _parent
            self._root = _root

        def _read(self):
            self.len_body = self._io.read_u8le()
            self.body = (self._io.read_bytes(self.len_body)).decode(u"ASCII")
            self.padding = self._io.read_bytes((8 - self.len_body % 8) % 8)
            _ = self.padding
            if not (KaitaiStream.byte_array_max(_) == 0 if len(_) != 0 else True):
                raise kaitaistruct.ValidationExprError(self.padding, self._io, u"/types/padded_str/seq/2")
            self._dirty = False


        def _fetch_instances(self):
            pass


        def _write__seq(self, io=None):
            super(NixNar.PaddedStr, self)._write__seq(io)
            self._io.write_u8le(self.len_body)
            self._io.write_bytes((self.body).encode(u"ASCII"))
            self._io.write_bytes(self.padding)


        def _check(self):
            if len((self.body).encode(u"ASCII")) != self.len_body:
                raise kaitaistruct.ConsistencyError(u"body", self.len_body, len((self.body).encode(u"ASCII")))
            if len(self.padding) != (8 - self.len_body % 8) % 8:
                raise kaitaistruct.ConsistencyError(u"padding", (8 - self.len_body % 8) % 8, len(self.padding))
            _ = self.padding
            if not (KaitaiStream.byte_array_max(_) == 0 if len(_) != 0 else True):
                raise kaitaistruct.ValidationExprError(self.padding, None, u"/types/padded_str/seq/2")
            self._dirty = False


    class TypeDirectory(ReadWriteKaitaiStruct):
        """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.
        """
        def __init__(self, _io=None, _parent=None, _root=None):
            super(NixNar.TypeDirectory, self).__init__(_io)
            self._parent = _parent
            self._root = _root

        def _read(self):
            self.entries = []
            i = 0
            while True:
                _t_entries = NixNar.TypeDirectory.DirEntry(self._io, self, self._root)
                try:
                    _t_entries._read()
                finally:
                    _ = _t_entries
                    self.entries.append(_)
                _ = self.entries[i]
                if not (_.body.name.body > self.entries[i - 1].body.name.body if  ((i != 0) and ((not (_.is_terminator))))  else True):
                    raise kaitaistruct.ValidationExprError(self.entries[i], self._io, u"/types/type_directory/seq/0")
                if _.is_terminator:
                    break
                i += 1
            self._dirty = False


        def _fetch_instances(self):
            pass
            for i in range(len(self.entries)):
                pass
                self.entries[i]._fetch_instances()



        def _write__seq(self, io=None):
            super(NixNar.TypeDirectory, self)._write__seq(io)
            for i in range(len(self.entries)):
                pass
                self.entries[i]._write__seq(self._io)



        def _check(self):
            if len(self.entries) == 0:
                raise kaitaistruct.ConsistencyError(u"entries", 0, len(self.entries))
            for i in range(len(self.entries)):
                pass
                if self.entries[i]._root != self._root:
                    raise kaitaistruct.ConsistencyError(u"entries", self._root, self.entries[i]._root)
                if self.entries[i]._parent != self:
                    raise kaitaistruct.ConsistencyError(u"entries", self, self.entries[i]._parent)
                _ = self.entries[i]
                if not (_.body.name.body > self.entries[i - 1].body.name.body if  ((i != 0) and ((not (_.is_terminator))))  else True):
                    raise kaitaistruct.ValidationExprError(self.entries[i], None, u"/types/type_directory/seq/0")
                _ = self.entries[i]
                if _.is_terminator != (i == len(self.entries) - 1):
                    raise kaitaistruct.ConsistencyError(u"entries", i == len(self.entries) - 1, _.is_terminator)

            self._dirty = False

        class DirEntry(ReadWriteKaitaiStruct):
            """A single entry within a directory, or the terminator, i.e. the `)`
            token that ends the directory node.
            """
            def __init__(self, _io=None, _parent=None, _root=None):
                super(NixNar.TypeDirectory.DirEntry, self).__init__(_io)
                self._parent = _parent
                self._root = _root

            def _read(self):
                self.kind = NixNar.PaddedStr(self._io, self, self._root)
                self.kind._read()
                _ = self.kind
                if not  ((_.body == u"entry") or (_.body == u")")) :
                    raise kaitaistruct.ValidationExprError(self.kind, self._io, u"/types/type_directory/types/dir_entry/seq/0")
                if (not (self.is_terminator)):
                    pass
                    self.body = NixNar.TypeDirectory.DirEntryBody(self._io, self, self._root)
                    self.body._read()

                self._dirty = False


            def _fetch_instances(self):
                pass
                self.kind._fetch_instances()
                if (not (self.is_terminator)):
                    pass
                    self.body._fetch_instances()



            def _write__seq(self, io=None):
                super(NixNar.TypeDirectory.DirEntry, self)._write__seq(io)
                self.kind._write__seq(self._io)
                if (not (self.is_terminator)):
                    pass
                    self.body._write__seq(self._io)



            def _check(self):
                if self.kind._root != self._root:
                    raise kaitaistruct.ConsistencyError(u"kind", self._root, self.kind._root)
                if self.kind._parent != self:
                    raise kaitaistruct.ConsistencyError(u"kind", self, self.kind._parent)
                _ = self.kind
                if not  ((_.body == u"entry") or (_.body == u")")) :
                    raise kaitaistruct.ValidationExprError(self.kind, None, u"/types/type_directory/types/dir_entry/seq/0")
                if (not (self.is_terminator)):
                    pass
                    if self.body._root != self._root:
                        raise kaitaistruct.ConsistencyError(u"body", self._root, self.body._root)
                    if self.body._parent != self:
                        raise kaitaistruct.ConsistencyError(u"body", self, self.body._parent)

                self._dirty = False

            @property
            def is_terminator(self):
                if hasattr(self, '_m_is_terminator'):
                    return self._m_is_terminator

                self._m_is_terminator = self.kind.body == u")"
                return getattr(self, '_m_is_terminator', None)

            def _invalidate_is_terminator(self):
                del self._m_is_terminator

        class DirEntryBody(ReadWriteKaitaiStruct):
            def __init__(self, _io=None, _parent=None, _root=None):
                super(NixNar.TypeDirectory.DirEntryBody, self).__init__(_io)
                self._parent = _parent
                self._root = _root

            def _read(self):
                self.open_paren = NixNar.PaddedStr(self._io, self, self._root)
                self.open_paren._read()
                _ = self.open_paren
                if not _.body == u"(":
                    raise kaitaistruct.ValidationExprError(self.open_paren, self._io, u"/types/type_directory/types/dir_entry_body/seq/0")
                self.name_key = NixNar.PaddedStr(self._io, self, self._root)
                self.name_key._read()
                _ = self.name_key
                if not _.body == u"name":
                    raise kaitaistruct.ValidationExprError(self.name_key, self._io, u"/types/type_directory/types/dir_entry_body/seq/1")
                self.name = NixNar.PaddedBytes(self._io, self, self._root)
                self.name._read()
                _ = self.name
                if not  ((len(_.body) != 0) and (len(_.body) <= 255) and (_.body != b"\x2E") and (_.body != b"\x2E\x2E") and (KaitaiStream.byte_array_min(_.body) != 0)) :
                    raise kaitaistruct.ValidationExprError(self.name, self._io, u"/types/type_directory/types/dir_entry_body/seq/2")
                self.node_key = NixNar.PaddedStr(self._io, self, self._root)
                self.node_key._read()
                _ = self.node_key
                if not _.body == u"node":
                    raise kaitaistruct.ValidationExprError(self.node_key, self._io, u"/types/type_directory/types/dir_entry_body/seq/3")
                self.node = NixNar.Node(self._parent._parent._parent.depth + 1, self._io, self, self._root)
                self.node._read()
                self.close_paren = NixNar.PaddedStr(self._io, self, self._root)
                self.close_paren._read()
                _ = self.close_paren
                if not _.body == u")":
                    raise kaitaistruct.ValidationExprError(self.close_paren, self._io, u"/types/type_directory/types/dir_entry_body/seq/5")
                self._dirty = False


            def _fetch_instances(self):
                pass
                self.open_paren._fetch_instances()
                self.name_key._fetch_instances()
                self.name._fetch_instances()
                self.node_key._fetch_instances()
                self.node._fetch_instances()
                self.close_paren._fetch_instances()


            def _write__seq(self, io=None):
                super(NixNar.TypeDirectory.DirEntryBody, self)._write__seq(io)
                self.open_paren._write__seq(self._io)
                self.name_key._write__seq(self._io)
                self.name._write__seq(self._io)
                self.node_key._write__seq(self._io)
                self.node._write__seq(self._io)
                self.close_paren._write__seq(self._io)


            def _check(self):
                if self.open_paren._root != self._root:
                    raise kaitaistruct.ConsistencyError(u"open_paren", self._root, self.open_paren._root)
                if self.open_paren._parent != self:
                    raise kaitaistruct.ConsistencyError(u"open_paren", self, self.open_paren._parent)
                _ = self.open_paren
                if not _.body == u"(":
                    raise kaitaistruct.ValidationExprError(self.open_paren, None, u"/types/type_directory/types/dir_entry_body/seq/0")
                if self.name_key._root != self._root:
                    raise kaitaistruct.ConsistencyError(u"name_key", self._root, self.name_key._root)
                if self.name_key._parent != self:
                    raise kaitaistruct.ConsistencyError(u"name_key", self, self.name_key._parent)
                _ = self.name_key
                if not _.body == u"name":
                    raise kaitaistruct.ValidationExprError(self.name_key, None, u"/types/type_directory/types/dir_entry_body/seq/1")
                if self.name._root != self._root:
                    raise kaitaistruct.ConsistencyError(u"name", self._root, self.name._root)
                if self.name._parent != self:
                    raise kaitaistruct.ConsistencyError(u"name", self, self.name._parent)
                _ = self.name
                if not  ((len(_.body) != 0) and (len(_.body) <= 255) and (_.body != b"\x2E") and (_.body != b"\x2E\x2E") and (KaitaiStream.byte_array_min(_.body) != 0)) :
                    raise kaitaistruct.ValidationExprError(self.name, None, u"/types/type_directory/types/dir_entry_body/seq/2")
                if self.node_key._root != self._root:
                    raise kaitaistruct.ConsistencyError(u"node_key", self._root, self.node_key._root)
                if self.node_key._parent != self:
                    raise kaitaistruct.ConsistencyError(u"node_key", self, self.node_key._parent)
                _ = self.node_key
                if not _.body == u"node":
                    raise kaitaistruct.ValidationExprError(self.node_key, None, u"/types/type_directory/types/dir_entry_body/seq/3")
                if self.node._root != self._root:
                    raise kaitaistruct.ConsistencyError(u"node", self._root, self.node._root)
                if self.node._parent != self:
                    raise kaitaistruct.ConsistencyError(u"node", self, self.node._parent)
                if self.node.depth != self._parent._parent._parent.depth + 1:
                    raise kaitaistruct.ConsistencyError(u"node", self._parent._parent._parent.depth + 1, self.node.depth)
                if self.close_paren._root != self._root:
                    raise kaitaistruct.ConsistencyError(u"close_paren", self._root, self.close_paren._root)
                if self.close_paren._parent != self:
                    raise kaitaistruct.ConsistencyError(u"close_paren", self, self.close_paren._parent)
                _ = self.close_paren
                if not _.body == u")":
                    raise kaitaistruct.ValidationExprError(self.close_paren, None, u"/types/type_directory/types/dir_entry_body/seq/5")
                self._dirty = False



    class TypeRegular(ReadWriteKaitaiStruct):
        """A regular file node."""
        def __init__(self, _io=None, _parent=None, _root=None):
            super(NixNar.TypeRegular, self).__init__(_io)
            self._parent = _parent
            self._root = _root

        def _read(self):
            self.first_key = NixNar.PaddedStr(self._io, self, self._root)
            self.first_key._read()
            _ = self.first_key
            if not  ((_.body == u"executable") or (_.body == u"contents")) :
                raise kaitaistruct.ValidationExprError(self.first_key, self._io, u"/types/type_regular/seq/0")
            if self.is_executable:
                pass
                self.executable_val = NixNar.PaddedStr(self._io, self, self._root)
                self.executable_val._read()
                _ = self.executable_val
                if not _.body == u"":
                    raise kaitaistruct.ValidationExprError(self.executable_val, self._io, u"/types/type_regular/seq/1")

            if self.is_executable:
                pass
                self.contents_key = NixNar.PaddedStr(self._io, self, self._root)
                self.contents_key._read()
                _ = self.contents_key
                if not _.body == u"contents":
                    raise kaitaistruct.ValidationExprError(self.contents_key, self._io, u"/types/type_regular/seq/2")

            self.file_data = NixNar.TypeRegular.FileContent(self._io, self, self._root)
            self.file_data._read()
            self._dirty = False


        def _fetch_instances(self):
            pass
            self.first_key._fetch_instances()
            if self.is_executable:
                pass
                self.executable_val._fetch_instances()

            if self.is_executable:
                pass
                self.contents_key._fetch_instances()

            self.file_data._fetch_instances()


        def _write__seq(self, io=None):
            super(NixNar.TypeRegular, self)._write__seq(io)
            self.first_key._write__seq(self._io)
            if self.is_executable:
                pass
                self.executable_val._write__seq(self._io)

            if self.is_executable:
                pass
                self.contents_key._write__seq(self._io)

            self.file_data._write__seq(self._io)


        def _check(self):
            if self.first_key._root != self._root:
                raise kaitaistruct.ConsistencyError(u"first_key", self._root, self.first_key._root)
            if self.first_key._parent != self:
                raise kaitaistruct.ConsistencyError(u"first_key", self, self.first_key._parent)
            _ = self.first_key
            if not  ((_.body == u"executable") or (_.body == u"contents")) :
                raise kaitaistruct.ValidationExprError(self.first_key, None, u"/types/type_regular/seq/0")
            if self.is_executable:
                pass
                if self.executable_val._root != self._root:
                    raise kaitaistruct.ConsistencyError(u"executable_val", self._root, self.executable_val._root)
                if self.executable_val._parent != self:
                    raise kaitaistruct.ConsistencyError(u"executable_val", self, self.executable_val._parent)
                _ = self.executable_val
                if not _.body == u"":
                    raise kaitaistruct.ValidationExprError(self.executable_val, None, u"/types/type_regular/seq/1")

            if self.is_executable:
                pass
                if self.contents_key._root != self._root:
                    raise kaitaistruct.ConsistencyError(u"contents_key", self._root, self.contents_key._root)
                if self.contents_key._parent != self:
                    raise kaitaistruct.ConsistencyError(u"contents_key", self, self.contents_key._parent)
                _ = self.contents_key
                if not _.body == u"contents":
                    raise kaitaistruct.ValidationExprError(self.contents_key, None, u"/types/type_regular/seq/2")

            if self.file_data._root != self._root:
                raise kaitaistruct.ConsistencyError(u"file_data", self._root, self.file_data._root)
            if self.file_data._parent != self:
                raise kaitaistruct.ConsistencyError(u"file_data", self, self.file_data._parent)
            self._dirty = False

        class FileContent(ReadWriteKaitaiStruct):
            """The contents of the file, prefixed with their length and padded
            with null bytes to the next 8-byte boundary.
            """
            def __init__(self, _io=None, _parent=None, _root=None):
                super(NixNar.TypeRegular.FileContent, self).__init__(_io)
                self._parent = _parent
                self._root = _root

            def _read(self):
                self.len_contents = self._io.read_u8le()
                if self.nar_offset < 0:
                    pass
                    self._unnamed1 = self._io.read_bytes(0)

                self.contents = self._io.read_bytes(self.len_contents)
                self.padding = self._io.read_bytes((8 - self.len_contents % 8) % 8)
                _ = self.padding
                if not (KaitaiStream.byte_array_max(_) == 0 if len(_) != 0 else True):
                    raise kaitaistruct.ValidationExprError(self.padding, self._io, u"/types/type_regular/types/file_content/seq/3")
                self._dirty = False


            def _fetch_instances(self):
                pass
                if self.nar_offset < 0:
                    pass



            def _write__seq(self, io=None):
                super(NixNar.TypeRegular.FileContent, self)._write__seq(io)
                self._io.write_u8le(self.len_contents)
                if self.nar_offset < 0:
                    pass
                    if len(self._unnamed1) != 0:
                        raise kaitaistruct.ConsistencyError(u"_unnamed1", 0, len(self._unnamed1))
                    self._io.write_bytes(self._unnamed1)

                self._io.write_bytes(self.contents)
                self._io.write_bytes(self.padding)


            def _check(self):
                if len(self.contents) != self.len_contents:
                    raise kaitaistruct.ConsistencyError(u"contents", self.len_contents, len(self.contents))
                if len(self.padding) != (8 - self.len_contents % 8) % 8:
                    raise kaitaistruct.ConsistencyError(u"padding", (8 - self.len_contents % 8) % 8, len(self.padding))
                _ = self.padding
                if not (KaitaiStream.byte_array_max(_) == 0 if len(_) != 0 else True):
                    raise kaitaistruct.ValidationExprError(self.padding, None, u"/types/type_regular/types/file_content/seq/3")
                self._dirty = False

            @property
            def nar_offset(self):
                """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.
                """
                if hasattr(self, '_m_nar_offset'):
                    return self._m_nar_offset

                self._m_nar_offset = self._io.pos()
                return getattr(self, '_m_nar_offset', None)

            def _invalidate_nar_offset(self):
                del self._m_nar_offset

        @property
        def is_executable(self):
            """True if the file has the `executable` attribute."""
            if hasattr(self, '_m_is_executable'):
                return self._m_is_executable

            self._m_is_executable = self.first_key.body == u"executable"
            return getattr(self, '_m_is_executable', None)

        def _invalidate_is_executable(self):
            del self._m_is_executable

    class TypeSymlink(ReadWriteKaitaiStruct):
        """A symbolic link node."""
        def __init__(self, _io=None, _parent=None, _root=None):
            super(NixNar.TypeSymlink, self).__init__(_io)
            self._parent = _parent
            self._root = _root

        def _read(self):
            self.target_key = NixNar.PaddedStr(self._io, self, self._root)
            self.target_key._read()
            _ = self.target_key
            if not _.body == u"target":
                raise kaitaistruct.ValidationExprError(self.target_key, self._io, u"/types/type_symlink/seq/0")
            self.target_val = NixNar.PaddedBytes(self._io, self, self._root)
            self.target_val._read()
            _ = self.target_val
            if not  ((len(_.body) != 0) and (len(_.body) <= 4095) and (KaitaiStream.byte_array_min(_.body) != 0)) :
                raise kaitaistruct.ValidationExprError(self.target_val, self._io, u"/types/type_symlink/seq/1")
            self._dirty = False


        def _fetch_instances(self):
            pass
            self.target_key._fetch_instances()
            self.target_val._fetch_instances()


        def _write__seq(self, io=None):
            super(NixNar.TypeSymlink, self)._write__seq(io)
            self.target_key._write__seq(self._io)
            self.target_val._write__seq(self._io)


        def _check(self):
            if self.target_key._root != self._root:
                raise kaitaistruct.ConsistencyError(u"target_key", self._root, self.target_key._root)
            if self.target_key._parent != self:
                raise kaitaistruct.ConsistencyError(u"target_key", self, self.target_key._parent)
            _ = self.target_key
            if not _.body == u"target":
                raise kaitaistruct.ValidationExprError(self.target_key, None, u"/types/type_symlink/seq/0")
            if self.target_val._root != self._root:
                raise kaitaistruct.ConsistencyError(u"target_val", self._root, self.target_val._root)
            if self.target_val._parent != self:
                raise kaitaistruct.ConsistencyError(u"target_val", self, self.target_val._parent)
            _ = self.target_val
            if not  ((len(_.body) != 0) and (len(_.body) <= 4095) and (KaitaiStream.byte_array_min(_.body) != 0)) :
                raise kaitaistruct.ValidationExprError(self.target_val, None, u"/types/type_symlink/seq/1")
            self._dirty = False



