nix

package module
v0.0.0-...-0734e13 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2024 License: Apache-2.0 Imports: 20 Imported by: 0

README

go-nix - Nix library for Go

Go Reference

STATUS: Experimental

This repository is a streamlined fork of github.com/nix-community/go-nix, a library to interoperate with Nix. I am currently working on upstreaming these changes. Until then, I am making my development branch available as zombiezen.com/go/nix.

Install

go get zombiezen.com/go/nix

License

Apache 2.0

Documentation

Overview

Package nix provides functions and types for interoperating with Nix.

Index

Examples

Constants

View Source
const CacheInfoMIMEType = "text/x-nix-cache-info"

CacheInfoMIMEType is the MIME content type for the nix-cache-info file.

View Source
const CacheInfoName = "nix-cache-info"

CacheInfoName is the name of the binary cache resource that contains its CacheInfo.

View Source
const NARInfoExtension = ".narinfo"

NARInfoExtension is the file extension for a file containing NAR information.

View Source
const NARInfoMIMEType = "text/x-nix-narinfo"

NARInfoMIMEType is the MIME content type for a .narinfo file.

Variables

This section is empty.

Functions

func CompressHash

func CompressHash(dst, src []byte)

CompressHash compresses the src byte slice (usually a hash digest) into the given dst byte slice by cyclically XORing bytes together. If len(dst) >= len(src), dst[:len(src)] will be a copy of src and dst[len(src):] will not be modified.

func GenerateKey

func GenerateKey(name string, rand io.Reader) (*PublicKey, *PrivateKey, error)

GenerateKey generates a Nix signing key using entropy from rand. If rand is nil, crypto/rand.Reader will be used.

func VerifyNARInfo

func VerifyNARInfo(trusted []*PublicKey, info *NARInfo, sig *Signature) error

VerifyNARInfo verifies that a signature for a NARInfo matches the signature of the same name in a list of trusted keys. The trusted key list should not contain more than one key with the same name.

Types

type CacheInfo

type CacheInfo struct {
	// StoreDirectory is the location of the store.
	// Defaults to [DefaultStoreDirectory] if empty.
	StoreDirectory StoreDirectory
	// Priority is the priority of the store when used as a substituter.
	// Lower values mean higher priority.
	Priority int
	// WantMassQuery indicates whether this store (when used as a substituter)
	// can be queried efficiently for path validity.
	WantMassQuery bool
}

CacheInfo holds various settings about a Nix binary cache.

func (*CacheInfo) MarshalText

func (info *CacheInfo) MarshalText() ([]byte, error)

MarshalText formats the binary cache information in the format of a nix-cache-info file.

func (*CacheInfo) UnmarshalText

func (info *CacheInfo) UnmarshalText(data []byte) error

UnmarshalText parses the binary cache information from a nix-cache-info file.

type CompressionType

type CompressionType string

CompressionType is an enumeration of compression algorithms used in NARInfo.

const (
	NoCompression CompressionType = "none"
	Gzip          CompressionType = "gzip"
	Bzip2         CompressionType = "bzip2"
	XZ            CompressionType = "xz"
	Zstandard     CompressionType = "zstd"
	Lzip          CompressionType = "lzip"
	LZ4           CompressionType = "lz4"
	Brotli        CompressionType = "br"
)

Compression types.

func (CompressionType) IsKnown

func (ct CompressionType) IsKnown() bool

IsKnown reports whether ct is one of the known compression types.

type ContentAddress

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

A ContentAddress is a content-addressability assertion.

func FlatFileContentAddress

func FlatFileContentAddress(h Hash) ContentAddress

FlatFileContentAddress returns a content address for a flat, fixed-output derivation with the given hash.

func ParseContentAddress

func ParseContentAddress(s string) (ContentAddress, error)

ParseContentAddress parses a content address in the form of "text:<ht>:<sha256 hash of file contents>" or "fixed<:r?>:<ht>:<h>".

func RecursiveFileContentAddress

func RecursiveFileContentAddress(h Hash) ContentAddress

RecursiveFileContentAddress returns a content address for a recursive (NAR), fixed-output derivation with the given hash.

func TextContentAddress

func TextContentAddress(h Hash) ContentAddress

TextContentAddress returns a content address for a "text" filesystem object with the given hash.

func (ContentAddress) Equal

func (ca ContentAddress) Equal(ca2 ContentAddress) bool

Equal reports whether ca == ca2.

func (ContentAddress) Hash

func (ca ContentAddress) Hash() Hash

Hash returns the hash part of the content address.

func (ContentAddress) IsFixed

func (ca ContentAddress) IsFixed() bool

IsFixed reports whether the content address is for a fixed-output derivation.

func (ContentAddress) IsRecursiveFile

func (ca ContentAddress) IsRecursiveFile() bool

IsRecursiveFile reports whether the content address is for a fixed-output derivation with recursive (NAR) hashing.

func (ContentAddress) IsText

func (ca ContentAddress) IsText() bool

IsText reports whether the content address is for a "text" filesystem object.

func (ContentAddress) IsZero

func (ca ContentAddress) IsZero() bool

IsZero reports whether the content address is the zero value.

func (ContentAddress) MarshalText

func (ca ContentAddress) MarshalText() ([]byte, error)

MarshalText formats the content address in the same way as ContentAddress.String. It returns an error if ca is the zero value.

func (ContentAddress) String

func (ca ContentAddress) String() string

String formats the content address as either "text:<ht>:<sha256 hash of file contents>" or "fixed<:r?>:<ht>:<h>". It returns the empty string if ca is the zero value.

func (*ContentAddress) UnmarshalText

func (ca *ContentAddress) UnmarshalText(data []byte) error

UnmarshalText parses a string in the same way as ParseContentAddress.

type Hash

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

A Hash is an output of a hash algorithm. The zero value is an empty hash with no type.

func NewHash

func NewHash(typ HashType, bits []byte) Hash

NewHash returns a new hash with the given type and raw bytes. NewHash panics if the type is invalid or the length of the raw bytes do not match the type's length.

func ParseHash

func ParseHash(s string) (Hash, error)

ParseHash parses a hash in the format "<type>:<base16|base32|base64>" or "<type>-<base64>" (a Subresource Integrity hash expression). It is a wrapper around Hash.UnmarshalText.

func (Hash) Base16

func (h Hash) Base16() string

Base16 encodes the hash with base16 (i.e. hex) prefixed by the hash type separated by a colon.

func (Hash) Base32

func (h Hash) Base32() string

Base32 encodes the hash with base32 prefixed by the hash type separated by a colon.

func (Hash) Base64

func (h Hash) Base64() string

Base64 encodes the hash with base64 prefixed by the hash type separated by a colon.

func (Hash) Bytes

func (h Hash) Bytes(dst []byte) []byte

Bytes appends the raw bytes of the hash to dst and returns the resulting slice.

func (Hash) Equal

func (h Hash) Equal(h2 Hash) bool

Equal reports whether h == h2.

func (Hash) IsZero

func (h Hash) IsZero() bool

IsZero reports whether the hash is the zero hash.

func (Hash) MarshalText

func (h Hash) MarshalText() ([]byte, error)

MarshalText formats the hash as a Subresource Integrity hash expression (e.g. "sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="). It returns an error if h is the zero Hash.

func (Hash) RawBase16

func (h Hash) RawBase16() string

RawBase16 encodes the hash with base16 (i.e. hex).

func (Hash) RawBase32

func (h Hash) RawBase32() string

RawBase32 encodes the hash with base32.

func (Hash) RawBase64

func (h Hash) RawBase64() string

RawBase64 encodes the hash with base64.

func (Hash) SRI

func (h Hash) SRI() string

SRI returns the hash in the format of a Subresource Integrity hash expression (e.g. "sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=").

func (Hash) String

func (h Hash) String() string

String returns the result of Hash.SRI or "<nil>" if the hash is the zero Hash.

func (Hash) Type

func (h Hash) Type() HashType

Type returns the hash's algorithm. It returns zero for a zero Hash.

func (*Hash) UnmarshalText

func (h *Hash) UnmarshalText(s []byte) error

UnmarshalText parses a hash in the format "<type>:<base16|base32|base64>" or "<type>-<base64>" (a Subresource Integrity hash expression).

type HashType

type HashType int8

HashType is an enumeration of algorithms supported by Hash.

const (
	MD5 HashType = 1 + iota
	SHA1
	SHA256
	SHA512
)

Hash algorithms. Applications must not depend on the exact numeric values.

func ParseHashType

func ParseHashType(s string) (HashType, error)

ParseHashType matches a string to its hash type, returning an error if the string does not name a hash type.

func (HashType) IsValid

func (typ HashType) IsValid() bool

IsValid reports whether typ is one of the known hash algorithms.

func (HashType) Size

func (typ HashType) Size() int

Size returns the size of a hash produced by this type in bytes.

func (HashType) String

func (typ HashType) String() string

String returns the name of the hash algorithm.

type Hasher

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

Hasher is a common interface for hash algorithms to produce Hash values from byte streams. Hasher implements hash.Hash.

Example
package main

import (
	"fmt"
	"io"

	"zombiezen.com/go/nix"
)

func main() {
	h := nix.NewHasher(nix.SHA256)
	io.WriteString(h, "Hello, World!\n")
	fmt.Println(h.SumHash())
}
Output:

sha256-yYwktnfv9Ehgr+pvSTu67FuxxMuyCcb8K7tH9m/yrTE=

func NewHasher

func NewHasher(typ HashType) *Hasher

NewHasher returns a new Hasher object for the given algorithm. NewHasher panics if the hash type is invalid.

func (*Hasher) BlockSize

func (h *Hasher) BlockSize() int

BlockSize returns the hash's underlying block size.

func (*Hasher) Reset

func (h *Hasher) Reset()

Reset resets the hasher to its initial state.

func (*Hasher) Size

func (h *Hasher) Size() int

Size returns the number of bytes Hasher.Sum will return.

func (*Hasher) Sum

func (h *Hasher) Sum(b []byte) []byte

Sum appends the current hash to b and returns the resulting slice. It does not change the underlying hash state.

func (*Hasher) SumHash

func (h *Hasher) SumHash() Hash

SumHash returns the current Hash value. It does not change the underlying hash state.

func (*Hasher) Type

func (h *Hasher) Type() HashType

Type returns the type passed into NewHasher.

func (*Hasher) Write

func (h *Hasher) Write(p []byte) (n int, err error)

Write adds more data to the running hash. It never returns an error.

type NARInfo

type NARInfo struct {
	// StorePath is the absolute path of this store object
	// (e.g. "/nix/store/s66mzxpvicwk07gjbjfw9izjfa797vsw-hello-2.12.1").
	// Nix requires this field to be set.
	StorePath StorePath
	// URL is the path to download to the (possibly compressed) .nar file,
	// relative to the .narinfo file's directory.
	// Nix requires this field to be set.
	URL string
	// Compression is the algorithm used for the file referenced by URL.
	// If empty, defaults to "bzip2".
	Compression CompressionType
	// FileHash is the hash of the file referenced by URL.
	// If Compression is [NoCompression] and FileHash is the zero hash,
	// then NARHash will be used.
	FileHash Hash
	// FileSize is the size of the file referenced by URL in bytes.
	// If Compression is [NoCompression] and FileSize is zero,
	// then NARSize will be used.
	FileSize int64
	// NARHash is the hash of the decompressed .nar file.
	// Nix requires this field to be set.
	NARHash Hash
	// NARSize is the size of the decompressed .nar file in bytes.
	// Nix requires this field to be set.
	NARSize int64
	// References is the set of other store objects that this store object references.
	References []StorePath
	// Deriver is the name of the store object that is the store derivation
	// of this store object.
	Deriver StorePath
	// System is a deprecated field.
	//
	// Deprecated: Ignore this field.
	System string
	// Sig is a set of signatures for this object.
	Sig []*Signature
	// CA is an optional content-addressability assertion.
	CA ContentAddress
}

NARInfo represents a parsed .narinfo file.

func (*NARInfo) AddSignatures

func (info *NARInfo) AddSignatures(sigs ...*Signature)

AddSignatures adds signatures that are not already present in info.

func (*NARInfo) Clone

func (info *NARInfo) Clone() *NARInfo

Clone returns a deep copy of an info struct.

func (*NARInfo) IsValid

func (info *NARInfo) IsValid() bool

IsValid reports whether the NAR information fields are valid.

func (*NARInfo) MarshalText

func (info *NARInfo) MarshalText() ([]byte, error)

MarshalText encodes the information as a .narinfo file.

func (*NARInfo) StoreDirectory

func (info *NARInfo) StoreDirectory() StoreDirectory

Directory returns the store directory of the store object.

func (*NARInfo) UnmarshalText

func (info *NARInfo) UnmarshalText(src []byte) (err error)

UnmarshalText decodes a .narinfo file.

func (*NARInfo) WriteFingerprint

func (info *NARInfo) WriteFingerprint(w io.Writer) error

WriteFingerprint writes the store object's "fingerprint" to the given writer. The fingerprint is the string used for signing.

type PrivateKey

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

A PrivateKey is a Nix private signing key. It is used to produce a Signature for a Nix store object (represented by NARInfo).

func ParsePrivateKey

func ParsePrivateKey(s string) (*PrivateKey, error)

ParsePrivateKey parses the string encoding of a private key. It is a wrapper around PrivateKey.UnmarshalText.

func (*PrivateKey) MarshalText

func (pk *PrivateKey) MarshalText() ([]byte, error)

MarshalText formats the private key as "<name>:<base64 data>". It returns an error if pub is nil.

func (*PrivateKey) Name

func (pk *PrivateKey) Name() string

Name returns the private key's identifier.

func (*PrivateKey) PublicKey

func (pk *PrivateKey) PublicKey() *PublicKey

PublicKey returns the public portion of the private key.

func (*PrivateKey) String

func (pk *PrivateKey) String() string

String formats the private key as "<name>:<base64 data>".

func (*PrivateKey) UnmarshalText

func (pk *PrivateKey) UnmarshalText(data []byte) error

UnmarshalText parses the string encoding of a private key.

type PublicKey

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

A PublicKey is a Nix public signing key.

func ParsePublicKey

func ParsePublicKey(s string) (*PublicKey, error)

ParsePublicKey parses the string encoding of a public key. It is a wrapper around PublicKey.UnmarshalText.

func (*PublicKey) MarshalText

func (pub *PublicKey) MarshalText() ([]byte, error)

MarshalText formats the public key as "<name>:<base64 data>". It returns an error if pub is nil.

func (*PublicKey) Name

func (pub *PublicKey) Name() string

Name returns the public key's identifier.

func (*PublicKey) String

func (pub *PublicKey) String() string

String formats the public key as "<name>:<base64 data>".

func (*PublicKey) UnmarshalText

func (pub *PublicKey) UnmarshalText(data []byte) error

UnmarshalText parses the string encoding of a public key.

type Signature

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

A Signature is a signature of a Nix store object created by a PrivateKey.

func ParseSignature

func ParseSignature(s string) (*Signature, error)

ParseSignature parses the string encoding of a signature. It is a wrapper around Signature.UnmarshalText.

func SignNARInfo

func SignNARInfo(pk *PrivateKey, info *NARInfo) (*Signature, error)

SignNARInfo signs the given NARInfo with the private key.

func (*Signature) MarshalText

func (sig *Signature) MarshalText() ([]byte, error)

MarshalText formats the signature as "<key name>:<base64 data>". It returns an error if pub is nil.

func (*Signature) Name

func (sig *Signature) Name() string

Name returns the identifier of the PrivateKey used to produce this signature.

func (*Signature) String

func (sig *Signature) String() string

String formats the signature as "<key name>:<base64 data>".

func (*Signature) UnmarshalText

func (sig *Signature) UnmarshalText(data []byte) error

UnmarshalText parses the string encoding of a signature.

type StoreDirectory

type StoreDirectory string

StoreDirectory is the absolute path of a Nix store in the local filesystem.

const DefaultStoreDirectory StoreDirectory = "/nix/store"

DefaultStoreDirectory is the default Nix store directory.

func CleanStoreDirectory

func CleanStoreDirectory(path string) (StoreDirectory, error)

CleanStoreDirectory cleans an absolute slash-separated path as a StoreDirectory. It returns an error if the path is not absolute.

func StoreDirectoryFromEnvironment

func StoreDirectoryFromEnvironment() (StoreDirectory, error)

StoreDirectoryFromEnvironment returns the Nix store directory in use based on the NIX_STORE_DIR environment variable, falling back to DefaultStoreDirectory if not set.

func (StoreDirectory) Join

func (dir StoreDirectory) Join(elem ...string) string

Join joins any number of path elements to the store directory separated by slashes.

func (StoreDirectory) Object

func (dir StoreDirectory) Object(name string) (StorePath, error)

Object returns the store path for the given store object name.

Example
package main

import (
	"fmt"

	"zombiezen.com/go/nix"
)

func main() {
	const objectName = "s66mzxpvicwk07gjbjfw9izjfa797vsw-hello-2.12.1"
	storePath, err := nix.DefaultStoreDirectory.Object(objectName)
	if err != nil {
		panic(err)
	}
	fmt.Println(storePath)
}
Output:

/nix/store/s66mzxpvicwk07gjbjfw9izjfa797vsw-hello-2.12.1

func (StoreDirectory) ParsePath

func (dir StoreDirectory) ParsePath(path string) (storePath StorePath, sub string, err error)

ParsePath verifies that a given absolute slash-separated path begins with the store directory and names either a store object or a file inside a store object. On success, it returns the store object's name and the relative path inside the store object, if any.

Example
package main

import (
	"fmt"

	"zombiezen.com/go/nix"
)

func main() {
	const path = "/nix/store/s66mzxpvicwk07gjbjfw9izjfa797vsw-hello-2.12.1/bin/hello"
	storePath, subPath, err := nix.DefaultStoreDirectory.ParsePath(path)
	if err != nil {
		panic(err)
	}
	fmt.Println(storePath)
	fmt.Println(subPath)
}
Output:

/nix/store/s66mzxpvicwk07gjbjfw9izjfa797vsw-hello-2.12.1
bin/hello

type StorePath

type StorePath string

StorePath is a Nix store path: the absolute path of a Nix store object in the filesystem. For example: "/nix/store/s66mzxpvicwk07gjbjfw9izjfa797vsw-hello-2.12.1".

func ParseStorePath

func ParseStorePath(path string) (StorePath, error)

ParseStorePath parses an absolute slash-separated path as a store path (i.e. an immediate child of a Nix store directory).

func (StorePath) Base

func (path StorePath) Base() string

Base returns the last element of the path.

func (StorePath) Digest

func (path StorePath) Digest() string

Digest returns the digest part of the name.

Example
package main

import (
	"fmt"

	"zombiezen.com/go/nix"
)

func main() {
	path, err := nix.ParseStorePath("/nix/store/s66mzxpvicwk07gjbjfw9izjfa797vsw-hello-2.12.1")
	if err != nil {
		panic(err)
	}
	fmt.Println(path.Digest())
}
Output:

s66mzxpvicwk07gjbjfw9izjfa797vsw

func (StorePath) Dir

func (path StorePath) Dir() StoreDirectory

Dir returns the path's directory.

func (StorePath) IsDerivation

func (path StorePath) IsDerivation() bool

IsDerivation reports whether the name ends in ".drv".

func (StorePath) MarshalText

func (path StorePath) MarshalText() ([]byte, error)

MarshalText returns a byte slice of the path or an error if it's empty.

func (StorePath) Name

func (path StorePath) Name() string

Name returns the part of the name after the digest.

Example
package main

import (
	"fmt"

	"zombiezen.com/go/nix"
)

func main() {
	path, err := nix.ParseStorePath("/nix/store/s66mzxpvicwk07gjbjfw9izjfa797vsw-hello-2.12.1")
	if err != nil {
		panic(err)
	}
	fmt.Println(path.Name())
}
Output:

hello-2.12.1

func (*StorePath) UnmarshalText

func (path *StorePath) UnmarshalText(data []byte) error

UnmarshalText validates and cleans the path in the same way as ParseStorePath and stores it into *path.

Directories

Path Synopsis
cmd
Package nar implements access to Nix Archive (NAR) files.
Package nar implements access to Nix Archive (NAR) files.
Package nixbase32 implements the slightly odd "base32" encoding that's used in Nix.
Package nixbase32 implements the slightly odd "base32" encoding that's used in Nix.

Jump to

Keyboard shortcuts

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