gonar

package module
v0.0.0-...-a7076e4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2026 License: Apache-2.0, MIT Imports: 15 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

gonar

A Go implementation of the NAR (Nix Archive) format — a Go port inspired by the Rust libnar implementation.

gonar reads and writes the stable Nix Archive (nix-archive-1) format used by Nix and Guix. It is cross-tested against real Nix and Guix archive data, producing byte-identical, reproducible NAR output for conforming archives.

Library

import "github.com/cpumichael/gonar"

// Pack a file, directory, or symlink into NAR format.
err := gonar.Pack(w, "/path/to/dir")
data, err := gonar.PackBytes("/path/to/dir")

// Read entries out of a NAR archive.
a := gonar.NewArchive(r)
for entry, err := range a.Entries() {
    if err != nil {
        // handle error
    }
    fmt.Println(entry.Name(), entry.IsDir())
    // For regular files, entry.Contents() streams the file data and is valid
    // until the next iteration step.
}

// Or unpack the whole archive to disk.
a := gonar.NewArchive(r)
err := a.Unpack("/path/to/dest")

By default, unpacked entries have their mtimes canonicalized to the Unix epoch and extended attributes stripped, matching Nix's deterministic store semantics. Use Archive.SetCanonicalizeMtime / Archive.SetRemoveXattrs (or the per-Entry equivalents) to change this.

Directories created during unpacking are made read-only (0555) once their contents have been written. Use Archive.SetReadOnlyDirectories(false) or the CLI's --writable-dirs flag to leave them writable.

CLI

go install github.com/cpumichael/gonar/cmd/gonar@latest
usage: gonar <command> [arguments]

commands:
  pack [-o out.nar] [--checksum] [--force-stdout] [--status-file f] <path>
                               serialize path into NAR format
  unpack [--no-over-write] [--writable-dirs] [--status-file f] <archive.nar|-> <dst>
                               extract a NAR archive into dst
  rmdir [--status-file f] <path>
                               recursively remove a directory unpacked by gonar
  list [-l|-j|--jsonl] [--status-file f] <archive.nar|->
                               print the entries in a NAR archive
                               (default: one name per line; -l: long form;
                               -j: pretty-printed JSON array document;
                               --jsonl: streaming JSON, one object per line)

  --checksum      (pack only) include archive's SHA-256 checksum in the json
                  status file
  --force-stdout  (pack only) write the archive to stdout even if stdout is
                  a terminal
  --no-over-write (unpack only) fail if dst already exists
  --writable-dirs (unpack only) leave created directories writable
  --status-file f (all commands) write a JSON {success, errors, ...} object
                  to f, decoupled from stdout/stderr so it composes with
                  pipelines like: gonar pack dir | zstd > out.nar.zst

flags must come before positional arguments.

Examples:

# Pack a directory to a file, or to stdout for piping.
gonar pack -o archive.nar ./some-dir
gonar pack ./some-dir | zstd > archive.nar.zst

# List entries from a file or stdin: short names (default), long ls -l-style, or JSON.
gonar list archive.nar
gonar list - < archive.nar
gonar list -l archive.nar
gonar list -j archive.nar        # pretty-printed JSON array document
gonar list --jsonl archive.nar   # streaming: one compact JSON object per line

# Unpack an archive from a file or stdin.
gonar unpack archive.nar ./dest
gonar unpack - ./dest < archive.nar

# Remove an unpacked, read-only directory tree.
gonar rmdir ./dest

# Write a machine-readable {command, success, errors, checksum} result to a
# file, independent of stdout/stderr -- useful when stdout is piped elsewhere.
gonar pack --checksum --status-file=status.json ./some-dir | zstd > archive.nar.zst

Requirements

Go 1.23+ (uses range-over-func iterators).

Documentation

Overview

Package gonar implements the NAR (Nix Archive) format: a self-describing, length-prefixed serialization of a file, directory, or symlink tree.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Pack

func Pack(w io.Writer, path string) error

Pack serializes the file, directory, or symlink at path into NAR format, writing the result to w.

func PackBytes

func PackBytes(path string) ([]byte, error)

PackBytes serializes the file, directory, or symlink at path into NAR format and returns the result as a byte slice.

Types

type Archive

type Archive struct {
	// contains filtered or unexported fields
}

Archive reads entries out of a NAR-formatted byte stream.

func NewArchive

func NewArchive(r io.Reader) *Archive

NewArchive wraps r as a NAR archive reader. By default, unpacked entries have their mtimes canonicalized to the Unix epoch and their extended attributes stripped.

func (*Archive) Entries

func (a *Archive) Entries() iter.Seq2[*Entry, error]

Entries lazily parses the archive and yields each entry in depth-first order, matching the on-disk layout. Regular file contents are streamed; read them from Entry.Contents before the next iteration step. If iteration continues without consuming all contents, Entries drains the unread bytes so parsing can advance. Stopping iteration early (via break) leaves the underlying reader at an unspecified position.

func (*Archive) SetCanonicalizeMtime

func (a *Archive) SetCanonicalizeMtime(canonicalize bool)

SetCanonicalizeMtime controls whether unpacked entries have their mtime reset to the Unix epoch.

func (*Archive) SetReadOnlyDirectories

func (a *Archive) SetReadOnlyDirectories(readOnly bool)

SetReadOnlyDirectories controls whether directories created during unpacking are made read-only after their contents have been written.

func (*Archive) SetRemoveXattrs

func (a *Archive) SetRemoveXattrs(remove bool)

SetRemoveXattrs controls whether unpacked entries have their extended attributes stripped.

func (*Archive) Unpack

func (a *Archive) Unpack(dst string) error

Unpack parses the archive and writes every entry into dst.

type Entry

type Entry struct {
	// contains filtered or unexported fields
}

Entry is a single file, directory, or symlink parsed out of an Archive.

func (*Entry) Contents

func (e *Entry) Contents() io.Reader

Contents returns a streaming reader for a regular file's contents. It returns nil for directories and symlinks. The reader is only valid during the current Entries iteration step.

func (*Entry) IsDir

func (e *Entry) IsDir() bool

func (*Entry) IsExecutable

func (e *Entry) IsExecutable() bool

func (*Entry) IsFile

func (e *Entry) IsFile() bool

IsFile reports whether the entry is a non-executable regular file.

func (e *Entry) IsSymlink() bool

func (*Entry) Name

func (e *Entry) Name() string

Name returns the entry's path relative to the archive root, using "/" as the separator regardless of host OS.

func (*Entry) SetCanonicalizeMtime

func (e *Entry) SetCanonicalizeMtime(canonicalize bool)

func (*Entry) SetRemoveXattrs

func (e *Entry) SetRemoveXattrs(remove bool)

func (*Entry) Size

func (e *Entry) Size() int64

Size returns the length of a regular file's contents in bytes. It is 0 for directories and symlinks.

func (*Entry) String

func (e *Entry) String() string

String formats the entry as a single ls-l-style line: permissions, size, and name (with "-> target" appended for symlinks).

func (*Entry) Target

func (e *Entry) Target() string

Target returns the symlink target. It is "" for non-symlink entries.

func (*Entry) UnpackIn

func (e *Entry) UnpackIn(dst string) error

UnpackIn writes the entry into dst, joining it with the entry's relative name.

Directories

Path Synopsis
cmd
gonar command
Command gonar packs and unpacks NAR (Nix Archive) files.
Command gonar packs and unpacks NAR (Nix Archive) files.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址