nar

package
v0.10.0-rc14 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const HashPattern = `(?:(` + narinfo.HashPattern + `[-_]))?` + NormalizedHashPattern

HashPattern is the strict validation pattern for complete nar hashes. It matches an optional prefix (narinfo hash + separator) followed by exactly a 52-character normalized hash. Used with anchors (^...$) to validate the full input. For extraction and lenient parsing, use HashPatternLenient instead.

View Source
const HashPatternLenient = `(?:(` + narinfo.HashPattern + `[-_]))?(.+)`

HashPatternLenient is used for parsing/extraction. It matches optional prefix followed by anything, allowing us to extract and validate parts separately.

View Source
const NormalizedHashPattern = `(?:[0-9a-df-np-sv-z]{52}|[0-9a-f]{64})`

NormalizedHashPattern defines the valid patterns for Nix store hashes. It supports two primary formats:

  1. Nix32 (Base32): A custom 32-character alphabet (0-9, a-z excluding 'e', 'o', 'u', 't'). Used for truncated SHA-256 (52 chars).
  2. Hexadecimal (Base16): Standard 0-9, a-f. Used for full SHA-256 digests (64 chars).

Variables

View Source
var (
	// ErrInvalidHash is returned if the hash is not valid.
	ErrInvalidHash = errors.New("invalid nar hash")
)
View Source
var ErrInvalidURL = errors.New("invalid nar URL")

ErrInvalidURL is returned if the regexp did not match the given URL.

View Source
var ErrUnknownFileExtension = errors.New("file extension is not known")

ErrUnknownFileExtension is returned if the file extension is not known.

View Source
var ErrUnsupportedCompressionType = errors.New("unsupported compression type")

ErrUnsupportedCompressionType is returned when an unsupported compression type is encountered.

Functions

func DecompressReader added in v0.9.0

func DecompressReader(ctx context.Context, r io.Reader, comp CompressionType) (io.ReadCloser, error)

DecompressReader returns a Reader that decompresses the data from r using the given compression type. The caller is responsible for closing the returned ReadCloser.

func FilePath added in v0.8.5

func FilePath(hash, compression string) (string, error)

FilePath returns the path of the nar file given a hash and an optional compression.

func ValidateHash added in v0.8.5

func ValidateHash(hash string) error

ValidateHash validates a Nix archive (nar) hash string. It returns ErrInvalidHash if the hash does not match the expected pattern. The function accepts both the optional narinfo hash prefix and the 52‑ or 64‑character normalized hash value, following the definitions in NormalizedHashPattern and HashPattern.

Types

type CompressionType

type CompressionType string

CompressionType represents the compression types supported by Nix. See: https://github.com/NixOS/nix/blob/f1187cb696584739884687d788a6fbb4dd36c61c/src/libstore/binary-cache-store.cc#L166

const (
	CompressionTypeNone  CompressionType = "none"
	CompressionTypeBzip2 CompressionType = "bzip2"
	CompressionTypeZstd  CompressionType = "zstd"
	CompressionTypeLzip  CompressionType = "lzip"
	CompressionTypeLz4   CompressionType = "lz4"
	CompressionTypeBr    CompressionType = "br"
	CompressionTypeXz    CompressionType = "xz"
)

func CompressionTypeFromExtension

func CompressionTypeFromExtension(ext string) (CompressionType, error)

CompressionTypeFromExtension returns the compression type given an extension.

func CompressionTypeFromString

func CompressionTypeFromString(ct string) CompressionType

CompressionTypeFromString returns the string compression type as CompressionType.

func (CompressionType) String

func (ct CompressionType) String() string

String returns the CompressionType as a string.

func (CompressionType) ToFileExtension

func (ct CompressionType) ToFileExtension() string

ToFileExtension returns the file extensions associated with the compression type.

type URL

type URL struct {
	Hash            string
	Compression     CompressionType
	Query           url.Values
	TransparentZstd bool
	// contains filtered or unexported fields
}

URL represents a nar URL.

func ParseURL

func ParseURL(u string) (URL, error)

ParseURL parses a nar URL (as present in narinfo) and returns its components. It accepts URLs in the format: [path/]<hash>.nar[.<compression>][?query] The hash must match HashPattern. This implementation is flexible about the directory structure - only the filename matters, not the "nar/" prefix.

func ParseUpstreamURL

func ParseUpstreamURL(u, fallbackHash string) (URL, error)

ParseUpstreamURL parses a nar URL taken from an upstream narinfo. For conventional hash-named URLs it is identical to ParseURL. For opaque URLs — where the filename before ".nar" is not a valid Nix hash, as served by cachix (e.g. "nar/<uuidv4>.nar.zst") — it preserves the original path for the upstream GET and uses fallbackHash (the narinfo's NarHash) as ncps's internal storage key. fallbackHash must be a valid hash; otherwise ErrInvalidHash is returned.

The Nix binary-cache protocol treats the narinfo URL field as an opaque path relative to the cache root, so a pull-through proxy must tolerate non hash-named URLs rather than reject them.

func (URL) IsOpaque

func (u URL) IsOpaque() bool

IsOpaque reports whether the URL carries a preserved opaque upstream path (i.e. the narinfo URL was not hash-named and the storage key was derived from the narinfo NarHash instead).

func (URL) JoinURL

func (u URL) JoinURL(uri *url.URL) *url.URL

JoinURL returns a new URL combined with the given URL.

func (URL) NewLogger

func (u URL) NewLogger(log zerolog.Logger) zerolog.Logger

NewLogger returns a new logger with the right fields.

func (URL) Normalize added in v0.8.5

func (u URL) Normalize() (URL, error)

Normalize returns a new URL with the narinfo hash prefix trimmed from the Hash. nix-serve serves NAR URLs with the narinfo hash as a prefix (e.g., "narinfo-hash-actual-hash"). This method removes that prefix to standardize the hash for storage.

func (URL) OpaquePath

func (u URL) OpaquePath() string

OpaquePath returns the preserved opaque upstream path, or "" for a conventional hash-named URL. Persist this so the NAR can be re-fetched from upstream after the local copy is evicted.

func (URL) String

func (u URL) String() string

String returns the URL as a string.

func (URL) ToFilePath

func (u URL) ToFilePath() (string, error)

ToFilePath returns the filepath in the store for a given nar URL.

func (URL) WithOpaquePath

func (u URL) WithOpaquePath(path string) URL

WithOpaquePath returns a copy of the URL with the given opaque upstream path attached, used when restoring a persisted opaque path so the upstream GET targets the original path while local storage stays keyed off Hash.

Jump to

Keyboard shortcuts

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