Documentation
¶
Overview ¶
Package nix validates Nix binary-cache metadata and archive integrity: narinfo files (Parse, Info) and the NAR archives they name, compressed or not (ArchivePath, ReadArchive, Archive). It signs and verifies a narinfo with Ed25519 over Nix's fingerprint of its store path, and its Handler keeps each store path's narinfo and archive consistent in a repository, where a hosted repository stores both in the store path's folder, binary-cache/<store hash>/, as Artifactory 7.x does (InfoItem, ArchiveItem). It also names and checks a channel's snapshots: the path of each snapshot's nixexprs.tar.xz (Snapshot, ParseSnapshotDir, ParseSnapshotPath) and the tarball itself (ReadChannel).
Role ¶
artifacts module, ecosystem layer: the Nix narinfo metadata and archive rules of the Nix slice. Its Handler plugs into the item service through the format hooks (decision 0032); the slice's http package (artifacts/ecosystem/nix/http) serves the binary-cache protocol and channels over what it keeps.
Inputs ¶
- Driving: the format registry (artifacts/ecosystem), which hands the item service's hooks for nix repositories to Handler, and the work queue, which runs every format's reindex through it; and the protocol, which parses, signs and verifies narinfo, checks archives, and reads a stored narinfo and its archive (ReadInfo, ReferencedArchive).
- Driven: the format hooks' environment (format.Env): its blob store, from which every narinfo and archive is read again and checked; its store, in which Handler finds a narinfo's archive and keeps the package index (artifacts/core/pkgindex); and its lease store, which serialises competing first publications of a store path. And the generic deploy policy (artifacts/ecosystem/generic), which Handler embeds and applies first.
- Settings: none.
Outputs ¶
- Handler: the hooks that admit nix-cache-info only with the content the cache serves, a narinfo only where the repository's layout stores it and when it parses (and, when a client deploys it, its archive is already stored), and an archive only where the layout stores it, or in a hosted repository at nar/<file> where the client uploads it, and when it decompresses to a valid NAR and hashes to its path (Inspect, Admit, GuardWrite); keep a store path's signed identity and an archive's bytes immutable; index a store path while its narinfo and archive are both stored and agree, and release the upload held at nar/<file> once its narinfo is published (Deployed, Deleted, Reindex); refuse a generic download from a remote and check a narinfo's archive before a generic download serves the narinfo (Resolve); and class each path (Classify). A channel snapshot's tarball is admitted only when ReadChannel passes it, and is not indexed.
- The package index: a package per store path name, with a version per store hash whose metadata is its narinfo's Info as JSON and whose files are the narinfo and its archive.
- The rules the protocol shares: Info, Parse, IsInfoPath, ArchivePath, the layout (BinaryCache, InfoItem, ArchiveItem, IsInfoItem, IsArchiveItem), Nix32, Archive, ReadArchive, ReadInfo, ReferencedArchive, CacheInfo, ErrInvalid and the size limits; and for channels Snapshot, ValidChannel, ValidFlavor, ParseSnapshotDir, ParseSnapshotPath, IsChannelPath, ReadChannel, ChannelFile and MaxChannelBytes.
Invariants ¶
- A narinfo is checked whole, and its CA field is dropped: no signature covers it, and a client could use it to skip signature trust.
- An archive is admitted only when it decompresses to exactly one well-formed NAR within the limits and its SHA-256 is the one its path names. The zstd decoder holds at most 64 MiB of history. An xz file is read whole, as liblzma reads it: every stream must end in an index that lists the blocks read and a footer that matches it, and an xz block header that names a dictionary above 1.5 GiB is refused, as XZ for Java refuses it (platform/xzfile).
- A hosted repository stores a store path's narinfo and archive only in its folder under binary-cache, and an archive at nar/<file> only until the narinfo that names it is published; a remote's cache keeps the client's paths.
- A published store path keeps its signed fingerprint (store path, NAR hash and size, and references), and an archive never changes.
- The index lists a store path only while its narinfo and archive are both stored and agree.
- A channel snapshot is stored only as channels/<channel>/<nixpkgs|nixos>/<snapshot>/nixexprs.tar.xz, and only as an xz stream of one tar archive with an entry, nothing after it but zero padding.
References ¶
- Decision 0032: docs/research/decisions/0032-package-format-handlers.md (the format hooks; the protocol and storage decision); decision 0007: docs/research/decisions/0007-remote-repositories.md (remote repositories).
- docs/testing/nix.md (connecting Nix, signatures, the limits, remote and virtual caches, the lifecycle and the evidence).
- JFrog Nix repositories: https://docs.jfrog.com/artifactory/docs/nix-repositories, and the Nix narinfo format: https://nix.dev/manual/nix/2.35/protocols/binary-cache/narinfo.html.
- Target layout: docs/research/architecture-options/5-modular-monolith.md, line 236 (ecosystem/nix), line 385 (the slice row) and line 997 (the mapping row).
- Plan: docs/implementation/plan.md#p33-automation-and-source-repositories and #p9d-modular-monolith-migration, epic ECO (https://github.com/weaveplatform/artifactweave/issues/943).
- The binary-cache folder: the JFrog CLI's jf nix, jfrog-cli-artifactory artifactory/commands/nix/command.go (binaryCacheDirPrefix, lines 53-55, and its AQL, lines 526-580).
- The .xz file format 1.2.1: https://tukaani.org/xz/xz-file-format.txt.
- https://github.com/weaveplatform/artifactweave/issues/788, https://github.com/weaveplatform/artifactweave/issues/1037, https://github.com/weaveplatform/artifactweave/issues/1263, https://github.com/weaveplatform/artifactweave/issues/1296 and https://github.com/weaveplatform/artifactweave/issues/1297
Status ¶
Implemented P9c, which delivered the plan's P33 group (issues #782 to #788). Moved here in P9d. Channels added by #1263; the binary-cache layout by #1296 and the whole xz stream by #1297, whose reader #1331 moved to platform/xzfile, where #1332 bounded its dictionary.
Testing ¶
- metadata_test.go: narinfo round trips, signing and verification, hashes and paths, optional fields and bounds, and ambiguous or unsafe metadata.
- archive_test.go: the native client's cache fixture (testdata/README.md records how it was made), each compression, and malformed, unsafe or oversized NARs.
- handler_test.go: admission, the generic read fences, storage failures and the publication lock's failures.
- layout_test.go: the binary-cache layout's paths, inspection by layout, indexing a store path's folder, releasing the held upload, and a remote cache's paths.
- xz_test.go: a NAR and a channel tarball cut by their index and footer are refused, and a NAR whose xz block header names a 4 GiB dictionary is refused without allocating it. platform/xzfile tests the container itself.
- xz_budget_test.go: those NARs and tarballs, whole and cut, answer the same under the xz dictionary budget as without it (#1354).
- contract_test.go: the format's lifecycle through the item service, and concurrent first publications of one store path.
- channel_test.go: snapshot paths, channel tarballs and their faults, and a tarball's admission without indexing.
Index ¶
- Constants
- Variables
- func ArchiveItem(hash, url string, cached bool) string
- func ArchivePath(path string) (string, bool)
- func InfoItem(hash string, cached bool) string
- func IsArchiveItem(path string, cached bool) (string, bool)
- func IsChannelPath(path string) bool
- func IsInfoItem(path string, cached bool) bool
- func IsInfoPath(path string) bool
- func Nix32(raw []byte) string
- func ReadChannel(ctx context.Context, input io.Reader, size int64) error
- func ReferencedArchive(ctx context.Context, env format.Env, c repo.Config, info Info, cached bool) (model.Item, error)
- func Serves(t repo.PackageType) bool
- func ValidChannel(name string) bool
- func ValidFlavor(flavor string) bool
- type Archive
- type Handler
- func (Handler) Admit(ctx context.Context, env format.Env, c repo.Config, w format.Write) (format.Write, error)
- func (Handler) Classify(_ repo.Config, path string) format.Class
- func (Handler) Deleted(ctx context.Context, env format.Env, c repo.Config, _ []model.Item) error
- func (Handler) Deployed(ctx context.Context, env format.Env, c repo.Config, it model.Item) error
- func (Handler) GuardWrite(ctx context.Context, env format.Env, c repo.Config, w format.Write) error
- func (Handler) Inspect(ctx context.Context, env format.Env, c repo.Config, w format.Write) (any, error)
- func (Handler) Reindex(ctx context.Context, env format.Env, c repo.Config) error
- func (Handler) Resolve(ctx context.Context, env format.Env, c repo.Config, d format.Download) (string, error)
- type Info
- type Snapshot
Constants ¶
const ( // ChannelFile is the one file a channel snapshot holds. The JFrog // documentation calls the name "mandatory and enforced" because // nix-channel --update asks for it under the URL its channel redirects // to. ChannelFile = "nixexprs.tar.xz" // MaxChannelBytes is the largest snapshot a channel's tarball may // expand to, 2 GiB; the compressed tarball shares MaxArchiveBytes. MaxChannelBytes = MaxNarBytes )
const ( // MaxMetadataBytes is the largest narinfo, 1 MiB. MaxMetadataBytes = 1 << 20 // MaxArchiveBytes is the largest archive as stored and transferred, // 512 MiB. MaxArchiveBytes int64 = 512 << 20 // MaxNarBytes is the largest NAR an archive may decompress to, 2 GiB. MaxNarBytes int64 = 2 << 30 // CacheInfo is the nix-cache-info every repository serves, and the only // content a write to that path may carry. Its store directory is fixed // because Parse accepts only store paths under /nix/store. CacheInfo = "StoreDir: /nix/store\nWantMassQuery: 1\nPriority: 30\n" )
const BinaryCache = "binary-cache/"
BinaryCache is the folder of a hosted repository that holds one folder per store path, named for its store hash, with the store path's narinfo and archive in it. The JFrog CLI's jf nix command names it as 7.x lays it out: "binaryCacheDirPrefix is the directory under an Artifactory Nix repo where each store-path's artifacts (*.nar.xz, *.narinfo) live" (jfrog-cli-artifactory artifactory/commands/nix/command.go, lines 53-55), and it finds a NAR by AQL in binary-cache/<store hash> and records the narinfo at binary-cache/<store hash>/<store hash>.narinfo.
Variables ¶
var ( // ErrInvalid is returned, unwrapped, for narinfo or archive content that // fails a check: by Parse, ReadInfo, ReadArchive, ReferencedArchive and // Info.Sign. ErrInvalid = errors.New("nix: invalid binary cache content") )
Functions ¶
func ArchiveItem ¶
ArchiveItem is where the archive a narinfo names by url, nar/<file>, is stored for the store path whose hash is hash: binary-cache/<hash>/<file> in a hosted repository, under the client's own file name, which jf nix matches as *.nar.xz [I]; url itself in a remote's cache.
func ArchivePath ¶
ArchivePath admits only relative content-addressed archive names, never URLs. A name is nar/, a SHA-256 in hex or Nix base-32, then .nar with an optional .xz, .bz2, .gz or .zst suffix. It returns the compression the suffix names (none, xz, bzip2, gzip or zstd) and whether path is a name.
func InfoItem ¶
InfoItem is where the narinfo of the store path whose hash is hash is stored: in its binary-cache folder in a hosted repository, at the root the client asks for in a remote's cache (cached). No source shows a 7.x remote's cache, which keeps the upstream's own layout here [I].
func IsArchiveItem ¶
IsArchiveItem reports whether path is where an archive is stored, an ArchiveItem, and returns the compression its name gives.
func IsChannelPath ¶
IsChannelPath reports whether path is a channel snapshot's tarball.
func IsInfoItem ¶
IsInfoItem reports whether path is where a narinfo is stored: InfoItem of some store hash, in a remote's cache when cached is set.
func IsInfoPath ¶
IsInfoPath reports whether path is a narinfo path at the cache's root: a 32-character store hash in Nix's base-32 alphabet, then .narinfo. It is the path the Nix client asks for and uploads to, and where a remote repository caches the narinfo; a hosted repository stores it at InfoItem.
func ReadChannel ¶
ReadChannel checks that input, exactly size bytes, is a whole xz file, every stream ending in its index and footer, holding one tar archive and nothing more, as nix-channel unpacks it: the documentation requires nixexprs.tar.xz, so a gzip or bzip2 tarball, or an xz stream of anything else, is refused. The tarball must have an entry, stay within MaxChannelBytes expanded, and size must be 1 to MaxArchiveBytes; its block headers may name no dictionary above xzfile.MaxDict. Every refusal is ErrInvalid. ctx ends a wait for the xz dictionary budget, which is then a refusal too.
func ReferencedArchive ¶
func ReferencedArchive( ctx context.Context, env format.Env, c repo.Config, info Info, cached bool, ) (model.Item, error)
ReferencedArchive verifies the complete compressed and uncompressed identity. It returns the archive item info names in repository c, at its ArchiveItem (in its cache when cached), once its size and SHA-256 match and its content, read again with ReadArchive, matches info. A missing item is the store's error, wrapped, so callers can tell it from a mismatch, which is ErrInvalid.
func Serves ¶
func Serves(t repo.PackageType) bool
Serves reports whether t is the Nix package type.
func ValidChannel ¶
ValidChannel reports whether name can name a channel.
func ValidFlavor ¶
ValidFlavor reports whether flavor is one the JFrog documentation lists for a channel upload, nixpkgs or nixos: the first segment of the releases.nixos.org URL an official channel redirects to.
Types ¶
type Archive ¶
Archive describes both the transport bytes and the uncompressed NAR.
func ReadArchive ¶
func ReadArchive( ctx context.Context, input io.Reader, size int64, compression string, ) (Archive, error)
ReadArchive checks that input, exactly size bytes compressed as compression names (none, xz, bzip2, gzip or zstd, as ArchivePath spells them), decompresses to one well-formed NAR and nothing more, and returns the SHA-256, as lowercase hex, and size of both. The NAR must keep the grammar and zero padding, name its directory entries safely and in strictly increasing order, and stay within MaxNarBytes, 100,000 nodes and a depth of 128. The zstd decoder holds at most 64 MiB of history. An xz file must be whole, every stream ending in its index and footer, as Nix's decompressor requires (#1297), and its block headers may name no dictionary above xzfile.MaxDict. Every refusal, a size outside 1 to MaxArchiveBytes included, is ErrInvalid. ctx ends a wait for the xz dictionary budget, which is then a refusal too.
type Handler ¶
Handler is the format of Nix repositories, which the item service calls on every write and delete in them, whichever API carried it. It admits nix-cache-info, narinfo files and the archives they name only once it has read them whole, keeps a published store path's signed identity and an archive's bytes immutable, and indexes a store path only while its narinfo and archive are both stored and agree.
A hosted repository keeps each store path's narinfo and archive in its folder under BinaryCache, as 7.x does, and holds an archive the client uploads to nar/<file> there only until the narinfo that names it is published, since the archive alone does not say which store path it belongs to. A remote's cache keeps the paths the client asks for.
func (Handler) Admit ¶
func (Handler) Admit( ctx context.Context, env format.Env, c repo.Config, w format.Write, ) (format.Write, error)
Admit applies the generic deploy policy, then refuses (400) a write to a repository that is not local or federated, unless it is a remote's cached write, and passes a checksum write. Any other write's content is checked as Inspect checks it, reusing Inspect's result for the same content. A narinfo a client deploys needs its archive already stored at its ArchiveItem and matching (409), and a write over an existing item may change neither an archive's bytes nor a store path's signed fingerprint (409).
func (Handler) Classify ¶
Classify reports narinfo files, at the root or an InfoItem, and nix-cache-info as metadata, since a narinfo's signatures, deriver and archive encoding may be replaced under the same path, and every archive and other file as unique.
func (Handler) Deleted ¶
Deleted rebuilds the repository's index with Reindex, so a store path whose narinfo or archive was removed leaves it.
func (Handler) Deployed ¶
Deployed checks a stored file's content as Inspect does, then indexes each store path the file completes: its own narinfo once the archive it names is stored and matches, or, for an archive, the narinfo that names it. A version is the store hash, under a package named for the rest of the store path's name, with the narinfo's Info as JSON metadata and both items as its files. Folders are skipped, and nix-cache-info, a channel snapshot's tarball and an archive a hosted repository holds until its narinfo is published are checked but not indexed. A narinfo published in a hosted repository takes the place of the archive held for it: that copy, whose content is now stored at the narinfo's ArchiveItem, goes.
func (Handler) GuardWrite ¶
func (Handler) GuardWrite( ctx context.Context, env format.Env, c repo.Config, w format.Write, ) error
GuardWrite serializes competing first publications at the same store hash. A transfer is admitted while planning, so repeat the fingerprint check under the transaction lock before its destination is replaced.
func (Handler) Inspect ¶
func (Handler) Inspect( ctx context.Context, env format.Env, c repo.Config, w format.Write, ) (any, error)
Inspect reads a write's content before the write's transaction, so Admit and Deployed decide without parsing under the write lock: nix-cache-info must be exactly CacheInfo, a narinfo at an InfoItem must parse against its path, a channel snapshot's tarball (IsChannelPath) must pass ReadChannel, and any other path must be an ArchiveItem, or in a hosted repository an archive path where the client uploads an archive, whose content passes ReadArchive. It answers nil for a checksum write. Content that fails is refused with 400, as is any other path, such as a narinfo at the root of a hosted repository; an archive whose SHA-256 is not the one its path names is refused with 409, and a blob that cannot be opened is an error.
func (Handler) Reindex ¶
Reindex rebuilds the package index of repository c from its stored narinfo files (a remote's cached ones), skipping a store path whose archive is missing; a narinfo or archive that no longer verifies fails the reindex. It checks env.CheckReindex, when set, before it writes, and then reconciles the index to the store paths it found.
func (Handler) Resolve ¶
func (Handler) Resolve( ctx context.Context, env format.Env, c repo.Config, d format.Download, ) (string, error)
Resolve guards a generic download without rewriting its path. It applies the repository's download policy and refuses every remote read (400), since only the native endpoint verifies upstream content. For a narinfo it also requires that the caller may read its archive (404 otherwise) and that the archive is stored and matches.
type Info ¶
type Info struct {
StorePath string `json:"store_path"`
URL string `json:"url"`
Compression string `json:"compression"`
FileHash string `json:"file_hash"`
FileSize int64 `json:"file_size"`
NarHash string `json:"nar_hash"`
NarSize int64 `json:"nar_size"`
References []string `json:"references"`
Deriver string `json:"deriver"`
Signatures []string `json:"signatures"`
}
Info holds the fields covered by a Nix signature and the archive's transport identity.
func Parse ¶
Parse reads the narinfo stored at path, a root narinfo path or an InfoItem, and checks it whole: known fields only, each once, and at most 32 well-formed Ed25519 signatures; a store path under /nix/store whose hash is path's; an archive URL that ArchivePath accepts, whose compression is the Compression field's (bzip2 when absent) and whose hash is the FileHash; positive sizes within MaxNarBytes and MaxArchiveBytes; at most 4,096 distinct valid references, which it sorts; and a valid deriver, if any. Hashes may be hex or Nix base-32 and come back as lowercase hex. Every refusal is ErrInvalid.
func ReadInfo ¶
ReadInfo parses the narinfo stored as item it, against its full path, as Parse does. A blob that cannot be opened or read is a wrapped error, and content that does not parse is ErrInvalid.
func (Info) Encode ¶
Encode can rewrite the relative archive URL without changing the signed fingerprint.
func (Info) Fingerprint ¶
Fingerprint is the message a Nix signature covers, version 1 of Nix's narinfo fingerprint: "1;", the store path, the NAR hash as sha256:<Nix base-32>, the NAR size and the references as full store paths joined by commas, separated by semicolons. The URL, compression, file hash and size and the deriver are outside it, so they may change without invalidating a signature.
func (*Info) Sign ¶
func (i *Info) Sign(alias string, key ed25519.PrivateKey) error
Sign adds alias's Ed25519 signature of the fingerprint to i's signatures unless i already carries it: Ed25519 signatures are deterministic, so signing a narinfo again on every read adds nothing. An alias other than 1 to 200 ASCII letters, digits, dots, underscores and hyphens, or a key of the wrong size, is ErrInvalid.
type Snapshot ¶
type Snapshot struct {
Channel, Flavor, ID string
}
Snapshot is one published snapshot of a channel: the channel's name, the flavor (nixpkgs or nixos) and the snapshot's identifier, the three segments a channel upload's path names after channels/.
func ParseSnapshotDir ¶
ParseSnapshotDir returns the snapshot whose folder, Dir, is dir.
func ParseSnapshotPath ¶
ParseSnapshotPath returns the snapshot whose tarball, Path, is path.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package nixhttp serves the native Nix HTTP binary-cache protocol under /artifactory/api/nix/{repoKey}: nix-cache-info, narinfo files and the NAR archives they name for GET and HEAD, publication and deletion of each with PUT and DELETE, and a rebuild of the repository's index with POST reindex.
|
Package nixhttp serves the native Nix HTTP binary-cache protocol under /artifactory/api/nix/{repoKey}: nix-cache-info, narinfo files and the NAR archives they name for GET and HEAD, publication and deletion of each with PUT and DELETE, and a rebuild of the repository's index with POST reindex. |