nix

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: AGPL-3.0 Imports: 11 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.

Documentation

Overview

Package nix implements the parts of the Nix binary cache protocol the server needs: the nixbase32 encoding, store path names, the narinfo file format and the ed25519 signatures narinfo carry.

Index

Constants

View Source
const MaxNarInfoBytes = 1 << 20

MaxNarInfoBytes bounds a narinfo file. Real ones are a few KiB; the largest grow with the number of references.

View Source
const StoreDir = "/nix/store"

StoreDir is the store directory of the binary caches the server hosts. A store with another prefix has other store paths, so its paths cannot be substituted by a client using /nix/store.

Variables

View Source
var (
	ErrInvalidNarInfo = errors.New("invalid narinfo")
	ErrInvalidHash    = errors.New("invalid sha256 hash")
	ErrInvalidNarFile = errors.New("invalid nar file name")
)

Errors of ParseNarInfo, ParseSHA256 and the narinfo accessors.

View Source
var ErrInvalidBase32 = errors.New("invalid nixbase32")

ErrInvalidBase32 is returned for text that is not nixbase32.

View Source
var ErrInvalidKey = errors.New("invalid nix signing key")

ErrInvalidKey is returned for a key that is not in Nix's "<name>:<base64>" format or does not hold a consistent ed25519 key pair.

View Source
var ErrInvalidStorePath = errors.New("invalid store path")

ErrInvalidStorePath is returned for text that is not a store path or the base name of one.

Functions

func DecodeBase32

func DecodeBase32(s string, size int) ([]byte, error)

DecodeBase32 decodes nixbase32 text into size bytes. The text must have the exact length of such an encoding and no bits may be set beyond the last byte, so a value has exactly one spelling.

func EncodeBase32

func EncodeBase32(b []byte) string

EncodeBase32 returns the nixbase32 encoding of b. Unlike RFC 4648 Nix emits the least significant digit last, i.e. it encodes the bytes in reverse.

func SplitBaseName

func SplitBaseName(base string) (string, string, error)

SplitBaseName splits "<hash>-<name>" into its parts.

func SplitStorePath

func SplitStorePath(path string) (string, string, error)

SplitStorePath splits "/nix/store/<hash>-<name>" into its parts.

func ValidKeyName

func ValidKeyName(s string) bool

ValidKeyName reports whether s can name a key: it is the part before the colon in trusted-public-keys and signatures.

func ValidName

func ValidName(s string) bool

ValidName reports whether s is acceptable as the name part of a store path.

func ValidStoreHash

func ValidStoreHash(s string) bool

ValidStoreHash reports whether s is the hash part of a store path.

Types

type NarInfo

type NarInfo struct {
	// StorePath is the full path, /nix/store/<hash>-<name>.
	StorePath string
	// URL locates the NAR file relative to the cache, nar/<filehash>.nar.xz.
	URL string
	// Compression is the compression of the file ("none" when it is the bare NAR).
	Compression string
	// FileHash and FileSize describe the file at URL; they are absent from some
	// narinfo of uncompressed NARs.
	FileHash string
	FileSize int64
	// NarHash and NarSize describe the NAR after decompression.
	NarHash string
	NarSize int64
	// References are the base names of the paths this one refers to.
	References []string
	// Deriver is the base name of the .drv that built the path, "" when unknown.
	Deriver string
	// Sigs are the signatures, each "<keyname>:<base64>".
	Sigs []string
	// CA is the content address of a content-addressed path.
	CA string
}

NarInfo is the description of one store path in a binary cache.

func ParseNarInfo

func ParseNarInfo(data []byte) (*NarInfo, error)

ParseNarInfo parses and validates the text of a .narinfo file. Unknown fields are ignored, a repeated field (other than Sig) is an error.

func (*NarInfo) Fingerprint

func (n *NarInfo) Fingerprint() (string, error)

Fingerprint is the string a signature covers: the store path, the NAR hash and size and the references as full paths in name order.

func (*NarInfo) Name

func (n *NarInfo) Name() string

Name returns the name part of the store path.

func (*NarInfo) NarFile

func (n *NarInfo) NarFile() (string, SHA256, error)

NarFile validates URL as the location the server accepts for a NAR file, nar/<nixbase32 of the file's sha256>.nar[.<ext>], and returns the file name below nar/ and the hash it embeds.

func (*NarInfo) StoreHash

func (n *NarInfo) StoreHash() string

StoreHash returns the hash part of the store path.

func (*NarInfo) String

func (n *NarInfo) String() string

String renders the narinfo file. Hashes are written in the nixbase32 form Nix expects, whatever spelling the narinfo was parsed from.

type SHA256

type SHA256 [sha256.Size]byte

SHA256 is a SHA-256 digest.

func ParseNarFile

func ParseNarFile(file string) (SHA256, error)

ParseNarFile checks a file name below nar/ (<nixbase32 sha256>.nar[.<ext>]) and returns the hash it names.

func ParseSHA256

func ParseSHA256(s string) (SHA256, error)

ParseSHA256 reads a SHA-256 hash in any of the spellings Nix tools write: "sha256:<nixbase32>", "sha256:<hex>" and the SRI form "sha256-<base64>".

func (SHA256) Base32

func (h SHA256) Base32() string

Base32 returns the nixbase32 digits alone, the form used in NAR file names.

func (SHA256) Hex

func (h SHA256) Hex() string

Hex returns the lower case hex digits, the form of a blob id.

func (SHA256) String

func (h SHA256) String() string

String returns the narinfo spelling "sha256:<nixbase32>".

type SecretKey

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

SecretKey is an ed25519 key that signs narinfo. Its text form is the one "nix-store --generate-binary-cache-key" writes: "<name>:<base64 of seed and public key>".

func GenerateKey

func GenerateKey(name string) (*SecretKey, error)

GenerateKey creates a key with a fresh random pair.

func ParseSecretKey

func ParseSecretKey(s string) (*SecretKey, error)

ParseSecretKey reads the text form of a secret key.

func (*SecretKey) Name

func (k *SecretKey) Name() string

Name returns the key name.

func (*SecretKey) PublicKey

func (k *SecretKey) PublicKey() string

PublicKey returns the key to put into trusted-public-keys: "<name>:<base64 of the public key>".

func (*SecretKey) Renamed

func (k *SecretKey) Renamed(name string) (*SecretKey, error)

Renamed returns the same key pair under another name.

func (*SecretKey) Sign

func (k *SecretKey) Sign(n *NarInfo) (string, error)

Sign returns the signature of the narinfo, "<name>:<base64>".

func (*SecretKey) String

func (k *SecretKey) String() string

String returns the secret key in its text form. Callers seal it before storing it and never log it.

Jump to

Keyboard shortcuts

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