nix

package
v0.0.0-...-3f9dac1 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2025 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Version2_12 = "2.12.0"
	Version2_13 = "2.13.0"
	Version2_14 = "2.14.0"
	Version2_15 = "2.15.0"
	Version2_16 = "2.16.0"
	Version2_17 = "2.17.0"
	Version2_18 = "2.18.0"
	Version2_19 = "2.19.0"
	Version2_20 = "2.20.0"
	Version2_21 = "2.21.0"
	Version2_22 = "2.22.0"
	Version2_23 = "2.23.0"
	Version2_24 = "2.24.0"
	Version2_25 = "2.25.0"

	MinVersion = Version2_12
)

All major Nix versions supported by Devbox.

Variables

View Source
var Default = &Nix{}

Default is the default Nix installation.

Functions

func AtLeast

func AtLeast(version string) bool

AtLeast reports if the default Nix installation's version is equal to or newer than the given version. It returns false if it cannot determine the Nix version.

func SourceProfile

func SourceProfile() (sourced bool, err error)

SourceProfile adds environment variables from the Nix profile shell scripts to the current process's environment. This ensures that PATH contains the nix bin directory and that NIX_PROFILES and NIX_SSL_CERT_FILE are set.

For properly configured Nix installations, the user's login shell handles sourcing the profile and SourceProfile has no effect.

func System

func System() string

System calls Nix.System on the default Nix installation.

func Version

func Version() string

Version calls Nix.Version on the default Nix installation.

Types

type Args

type Args []any

Args is a slice of Cmd arguments.

func (Args) String

func (a Args) String() string

String returns the arguments as a shell command, quoting arguments with spaces.

func (Args) StringSlice

func (a Args) StringSlice() []string

StringSlice formats each argument using fmt.Sprint.

type Cmd

type Cmd struct {
	// Path is the absolute path to the nix executable. It is the only
	// mandatory field and must not be empty.
	Path string

	// Args are the command line arguments, including the command name in
	// Args[0]. Run formats each argument using [fmt.Sprint] before passing
	// them to Nix.
	Args Args

	Env    []string
	Stdin  io.Reader
	Stdout io.Writer
	Stderr io.Writer

	// Logger emits debug logs when the command starts and exits. If nil, it
	// defaults to [slog.Default].
	Logger *slog.Logger
	// contains filtered or unexported fields
}

Cmd is an external command that invokes a *Nix executable. It provides improved error messages, graceful cancellation, and debug logging via log/slog. Although it's possible to initialize a Cmd directly, calling the Command function or Nix.Command method is more typical.

Most methods and fields correspond to their exec.Cmd equivalent. See its documentation for more details.

func Command

func Command(args ...any) *Cmd

Command creates an arbitrary command using the Nix executable found in $PATH. It's the same as calling Nix.Command on the default Nix installation.

func (*Cmd) CombinedOutput

func (c *Cmd) CombinedOutput(ctx context.Context) ([]byte, error)

func (*Cmd) LogValue

func (c *Cmd) LogValue() slog.Value

func (*Cmd) Output

func (c *Cmd) Output(ctx context.Context) ([]byte, error)

func (*Cmd) Run

func (c *Cmd) Run(ctx context.Context) error

func (*Cmd) String

func (c *Cmd) String() string

String returns c as a shell-quoted string.

type Info

type Info struct {
	// Name identifies the Nix implementation. It is usually "nix" but may
	// also be a fork like "lix".
	Name string

	// Version is the semantic Nix version string.
	Version string

	// System is the Nix system tuple. It follows the pattern <arch>-<os>
	// and does not use the same values as GOOS or GOARCH. Note that the Nix
	// system is configurable and may not represent the actual operating
	// system or architecture.
	System string

	// ExtraSystems are other systems that the current machine supports.
	// Usually set by the extra-platforms setting in nix.conf.
	ExtraSystems []string

	// Features are the capabilities that the Nix binary was compiled with.
	Features []string

	// SystemConfig is the path to the Nix system configuration file,
	// usually /etc/nix/nix.conf.
	SystemConfig string

	// UserConfigs is a list of paths to the user's Nix configuration files.
	UserConfigs []string

	// StoreDir is the path to the Nix store directory, usually /nix/store.
	StoreDir string

	// StateDir is the path to the Nix state directory, usually
	// /nix/var/nix.
	StateDir string

	// DataDir is the path to the Nix data directory, usually somewhere
	// within the Nix store. This field is empty for Nix versions <= 2.12.
	DataDir string
}

Info contains information about a Nix installation.

func (Info) AtLeast

func (i Info) AtLeast(version string) bool

AtLeast returns true if i.Version is >= version per semantic versioning. It always returns false if i.Version is empty or invalid, such as when the current Nix version cannot be parsed. It panics if version is an invalid semver.

type Installer

type Installer struct {
	// Path is the path to the Nix installer. If it's empty, Download and
	// Install will automatically download the installer and set Path to the
	// downloaded file before returning.
	Path string
}

Installer downloads and installs Nix.

func (*Installer) Download

func (i *Installer) Download(ctx context.Context) error

Download downloads the Nix installer without running it.

func (*Installer) Run

func (i *Installer) Run(ctx context.Context) error

Run downloads and installs Nix.

type Nix

type Nix struct {
	// Path is the absolute path to the nix executable. If it is empty,
	// nix commands use the executable found in $PATH.
	Path string

	// ExtraArgs are command line arguments to pass to every Nix command.
	ExtraArgs Args

	// Logger logs information at [slog.LevelDebug] about Nix command
	// starts and exits. If nil, it defaults to [slog.Default].
	Logger *slog.Logger
	// contains filtered or unexported fields
}

Nix provides an interface for interacting with Nix. The zero-value is valid and uses the first Nix executable found in $PATH.

func (*Nix) Command

func (n *Nix) Command(args ...any) *Cmd

Command creates an arbitrary Nix command that uses the Path, ExtraArgs, Logger and other defaults from n.

func (*Nix) Info

func (n *Nix) Info() (Info, error)

Info returns Nix version information. It caches the result after the first call, which means it won't reflect any configuration changes to Nix. Create a new Nix instance to retrieve uncached information.

func (*Nix) System

func (n *Nix) System() string

System returns the system from Nix.Info or an empty string if there was an error.

func (*Nix) Version

func (n *Nix) Version() string

Version returns the version from Nix.Info or an empty string if there was an error.

Directories

Path Synopsis
Package flake parses and formats Nix flake references.
Package flake parses and formats Nix flake references.

Jump to

Keyboard shortcuts

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