Skip to content

Commit f1d1d3b

Browse files
committed
nix: make System, Version, SourceProfile public
As part of some cleanup for making the DetSys installer the default, move the code related to getting the Nix version and system to the non-internal go.jetpack.io/devbox/nix package. It also merges in some nearly-duplicate code from the search indexer's Nix code (which will eventually use this package instead). Some of the changes taken from the indexer are: - Calling any function or method automatically... - sources the Nix profile if necessary. - looks for Nix in some well-known places if it isn't in PATH. - `nix.Version` and `nix.System` are cached behind a `sync.Once` by default. All top-level functions map to a method on a default Nix struct, following the same pattern found in `flags`, `slog`, etc. const Version2_12 = "2.12.0" ... func AtLeast(version string) bool func SourceProfile() (sourced bool, err error) func System() string func Version() string type Info struct{ ... } type Nix struct{ ... } func Default() *Nix func LookPath() *Nix func (n *Nix) Info() (Info, error) func (n *Nix) System() string func (n *Nix) Version() string
1 parent 892add7 commit f1d1d3b

File tree

18 files changed

+671
-580
lines changed

18 files changed

+671
-580
lines changed

internal/devbox/devbox.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func Open(opts *devopt.Opts) (*Devbox, error) {
115115
cfg: cfg,
116116
env: opts.Env,
117117
environment: environment,
118-
nix: &nix.Nix{},
118+
nix: &nix.NixInstance{},
119119
projectDir: filepath.Dir(cfg.Root.AbsRootPath),
120120
pluginManager: plugin.NewManager(),
121121
stderr: opts.Stderr,

internal/devpkg/narinfo_cache.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"go.jetpack.io/devbox/internal/goutil"
1919
"go.jetpack.io/devbox/internal/lock"
2020
"go.jetpack.io/devbox/internal/nix"
21+
"go.jetpack.io/devbox/internal/redact"
2122
"golang.org/x/sync/errgroup"
2223
)
2324

@@ -73,14 +74,6 @@ func FillNarInfoCache(ctx context.Context, packages ...*Package) error {
7374
return nil
7475
}
7576

76-
// Pre-compute values read in fillNarInfoCache
77-
// so they can be read from multiple go-routines without locks
78-
_, err := nix.Version()
79-
if err != nil {
80-
return err
81-
}
82-
_ = nix.System()
83-
8477
group, _ := errgroup.WithContext(ctx)
8578
for _, p := range eligiblePackages {
8679
pkg := p // copy the loop variable since its used in a closure below
@@ -240,29 +233,23 @@ func (p *Package) sysInfoIfExists() (*lock.SystemInfo, error) {
240233
return nil, nil
241234
}
242235

243-
version, err := nix.Version()
244-
if err != nil {
245-
return nil, err
246-
}
247-
248236
// disable for nix < 2.17
249-
if !version.AtLeast(nix.Version2_17) {
250-
return nil, err
237+
minVersion := nix.Version2_17
238+
if !nix.AtLeast(minVersion) {
239+
return nil, redact.Errorf("nix version is older than %s", redact.Safe(minVersion))
251240
}
252241

253242
entry, err := p.lockfile.Resolve(p.Raw)
254243
if err != nil {
255244
return nil, err
256245
}
257246

258-
userSystem := nix.System()
259-
260247
if entry.Systems == nil {
261248
return nil, nil
262249
}
263250

264251
// Check if the user's system's info is present in the lockfile
265-
sysInfo, ok := entry.Systems[userSystem]
252+
sysInfo, ok := entry.Systems[nix.System()]
266253
if !ok {
267254
return nil, nil
268255
}

internal/devpkg/package.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ func (p *Package) normalizePackageAttributePath() (string, error) {
390390

391391
// We prefer nix.Search over just trying to parse the package's "URL" because
392392
// nix.Search will guarantee that the package exists for the current system.
393-
var infos map[string]*nix.Info
393+
var infos map[string]*nix.PkgInfo
394394
if p.IsDevboxPackage && !p.IsRunX() {
395395
// Perf optimization: For queries of the form nixpkgs/<commit>#foo, we can
396396
// use a nix.Search cache.

internal/nix/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"strings"
1717

1818
"go.jetpack.io/devbox/internal/redact"
19+
"go.jetpack.io/devbox/nix"
1920
)
2021

2122
// Config is a parsed Nix configuration.
@@ -106,7 +107,7 @@ func (c Config) IsUserTrusted(ctx context.Context, username string) (bool, error
106107
}
107108

108109
func IncludeDevboxConfig(ctx context.Context, username string) error {
109-
info, _ := versionInfo()
110+
info, _ := nix.Default().Info()
110111
path := cmp.Or(info.SystemConfig, "/etc/nix/nix.conf")
111112
includePath := filepath.Join(filepath.Dir(path), "devbox-nix.conf")
112113
b := fmt.Appendf(nil, "# This config was auto-generated by Devbox.\n\nextra-trusted-users = %s\n", username)

internal/nix/install.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import (
2121
"go.jetpack.io/devbox/internal/build"
2222
"go.jetpack.io/devbox/internal/cmdutil"
2323
"go.jetpack.io/devbox/internal/fileutil"
24-
"go.jetpack.io/devbox/internal/redact"
2524
"go.jetpack.io/devbox/internal/ux"
25+
"go.jetpack.io/devbox/nix"
2626
)
2727

2828
const rootError = "warning: installing Nix as root is not supported by this script!"
@@ -121,33 +121,23 @@ func EnsureNixInstalled(writer io.Writer, withDaemonFunc func() *bool) (err erro
121121
return
122122
}
123123

124-
var version VersionInfo
125-
version, err = Version()
126-
if err != nil {
127-
err = redact.Errorf("nix: ensure install: get version: %w", err)
128-
return
129-
}
130-
131124
// ensure minimum nix version installed
132-
if !version.AtLeast(MinVersion) {
125+
if !nix.AtLeast(MinVersion) {
133126
err = usererr.New(
134127
"Devbox requires nix of version >= %s. Your version is %s. "+
135128
"Please upgrade nix and try again.\n",
136129
MinVersion,
137-
version,
130+
nix.Version(),
138131
)
139132
return
140133
}
141-
// call ComputeSystem to ensure its value is internally cached so other
142-
// callers can rely on just calling System
143-
err = ComputeSystem()
144134
}()
145135

146136
if BinaryInstalled() {
147137
return nil
148138
}
149139
if dirExists() {
150-
if err = SourceNixEnv(); err != nil {
140+
if _, err = SourceProfile(); err != nil {
151141
return err
152142
} else if BinaryInstalled() {
153143
return nil
@@ -174,7 +164,7 @@ func EnsureNixInstalled(writer io.Writer, withDaemonFunc func() *bool) (err erro
174164
}
175165

176166
// Source again
177-
if err = SourceNixEnv(); err != nil {
167+
if _, err = SourceProfile(); err != nil {
178168
return err
179169
}
180170

internal/nix/instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package nix
66
import "context"
77

88
// These make it easier to stub out nix for testing
9-
type Nix struct{}
9+
type NixInstance struct{}
1010

1111
type Nixer interface {
1212
PrintDevEnv(ctx context.Context, args *PrintDevEnvArgs) (*PrintDevEnvOut, error)

0 commit comments

Comments
 (0)