|
| 1 | +meta: |
| 2 | + id: nix_nar |
| 3 | + title: Nix Archive (NAR) |
| 4 | + file-extension: nar |
| 5 | + endian: le |
| 6 | +doc: | |
| 7 | + Nix Archive (NAR) format. A simple, reproducible binary archive |
| 8 | + format used by the Nix package manager to serialize file system objects. |
| 9 | +doc-ref: 'https://nixos.org/manual/nix/stable/command-ref/nix-store.html#nar-format' |
| 10 | + |
| 11 | +seq: |
| 12 | + - id: magic |
| 13 | + type: padded_str |
| 14 | + doc: "Magic string, must be 'nix-archive-1'." |
| 15 | + valid: |
| 16 | + expr: _.body == 'nix-archive-1' |
| 17 | + - id: root_node |
| 18 | + type: node |
| 19 | + doc: "The root of the archive, which is always a single node." |
| 20 | + |
| 21 | +types: |
| 22 | + padded_str: |
| 23 | + doc: | |
| 24 | + A string, prefixed with its length (u8le) and |
| 25 | + padded with null bytes to the next 8-byte boundary. |
| 26 | + seq: |
| 27 | + - id: len_str |
| 28 | + type: u8 |
| 29 | + - id: body |
| 30 | + type: str |
| 31 | + size: len_str |
| 32 | + encoding: 'ascii' |
| 33 | + - id: padding |
| 34 | + size: (8 - (len_str % 8)) % 8 |
| 35 | + |
| 36 | + node: |
| 37 | + doc: "A single filesystem node (file, directory, or symlink)." |
| 38 | + seq: |
| 39 | + - id: open_paren |
| 40 | + type: padded_str |
| 41 | + doc: "Must be '(', a token starting the node definition." |
| 42 | + valid: |
| 43 | + expr: _.body == '(' |
| 44 | + - id: type_key |
| 45 | + type: padded_str |
| 46 | + doc: "Must be 'type'." |
| 47 | + valid: |
| 48 | + expr: _.body == 'type' |
| 49 | + - id: type_val |
| 50 | + type: padded_str |
| 51 | + doc: "The type of the node: 'regular', 'directory', or 'symlink'." |
| 52 | + - id: body |
| 53 | + type: |
| 54 | + switch-on: type_val.body |
| 55 | + cases: |
| 56 | + "'directory'": type_directory |
| 57 | + "'regular'": type_regular |
| 58 | + "'symlink'": type_symlink |
| 59 | + - id: close_paren |
| 60 | + type: padded_str |
| 61 | + valid: |
| 62 | + expr: _.body == ')' |
| 63 | + if: "type_val.body != 'directory'" |
| 64 | + doc: "Must be ')', a token ending the node definition." |
| 65 | + |
| 66 | + type_directory: |
| 67 | + doc: "A directory node, containing a list of entries. Entries must be ordered by their names." |
| 68 | + seq: |
| 69 | + - id: entries |
| 70 | + type: dir_entry |
| 71 | + repeat: until |
| 72 | + repeat-until: _.kind.body == ')' |
| 73 | + types: |
| 74 | + dir_entry: |
| 75 | + doc: "A single entry within a directory, or a terminator." |
| 76 | + seq: |
| 77 | + - id: kind |
| 78 | + type: padded_str |
| 79 | + valid: |
| 80 | + expr: _.body == 'entry' or _.body == ')' |
| 81 | + doc: "Must be 'entry' (for a child node) or '' (for terminator)." |
| 82 | + - id: open_paren |
| 83 | + type: padded_str |
| 84 | + valid: |
| 85 | + expr: _.body == '(' |
| 86 | + if: 'kind.body == "entry"' |
| 87 | + - id: name_key |
| 88 | + type: padded_str |
| 89 | + valid: |
| 90 | + expr: _.body == 'name' |
| 91 | + if: 'kind.body == "entry"' |
| 92 | + - id: name |
| 93 | + type: padded_str |
| 94 | + if: 'kind.body == "entry"' |
| 95 | + - id: node_key |
| 96 | + type: padded_str |
| 97 | + valid: |
| 98 | + expr: _.body == 'node' |
| 99 | + if: 'kind.body == "entry"' |
| 100 | + - id: node |
| 101 | + type: node |
| 102 | + if: 'kind.body == "entry"' |
| 103 | + doc: "The child node, present only if kind is 'entry'." |
| 104 | + - id: close_paren |
| 105 | + type: padded_str |
| 106 | + valid: |
| 107 | + expr: _.body == ')' |
| 108 | + if: 'kind.body == "entry"' |
| 109 | + instances: |
| 110 | + is_terminator: |
| 111 | + value: kind.body == ')' |
| 112 | + |
| 113 | + type_regular: |
| 114 | + doc: "A regular file node." |
| 115 | + seq: |
| 116 | + # Read attributes (like 'executable') until we hit 'contents' |
| 117 | + - id: attributes |
| 118 | + type: reg_attribute |
| 119 | + repeat: until |
| 120 | + repeat-until: _.key.body == "contents" |
| 121 | + # After the 'contents' token, read the file data |
| 122 | + - id: file_data |
| 123 | + type: file_content |
| 124 | + instances: |
| 125 | + is_executable: |
| 126 | + value: 'attributes[0].key.body == "executable"' |
| 127 | + doc: "True if the file has the 'executable' attribute." |
| 128 | + types: |
| 129 | + reg_attribute: |
| 130 | + doc: "An attribute of the file, e.g., 'executable' or 'contents'." |
| 131 | + seq: |
| 132 | + - id: key |
| 133 | + type: padded_str |
| 134 | + doc: "Attribute key, e.g., 'executable' or 'contents'." |
| 135 | + valid: |
| 136 | + expr: _.body == 'executable' or _.body == 'contents' |
| 137 | + - id: value |
| 138 | + type: padded_str |
| 139 | + if: 'key.body == "executable"' |
| 140 | + valid: |
| 141 | + expr: _.body == '' |
| 142 | + doc: "Must be '' if key is 'executable'." |
| 143 | + file_content: |
| 144 | + doc: "The raw data of the file, prefixed by length." |
| 145 | + seq: |
| 146 | + - id: len_contents |
| 147 | + type: u8 |
| 148 | + # # This relies on the property of instances that they are lazily evaluated and cached. |
| 149 | + - size: 0 |
| 150 | + if: nar_offset < 0 |
| 151 | + - id: contents |
| 152 | + size: len_contents |
| 153 | + - id: padding |
| 154 | + size: (8 - (len_contents % 8)) % 8 |
| 155 | + instances: |
| 156 | + nar_offset: |
| 157 | + value: _io.pos |
| 158 | + |
| 159 | + type_symlink: |
| 160 | + doc: "A symbolic link node." |
| 161 | + seq: |
| 162 | + - id: target_key |
| 163 | + type: padded_str |
| 164 | + doc: "Must be 'target'." |
| 165 | + valid: |
| 166 | + expr: _.body == 'target' |
| 167 | + - id: target_val |
| 168 | + type: padded_str |
| 169 | + doc: "The destination path of the symlink." |
0 commit comments