Skip to content

Commit fd2fab6

Browse files
committed
[nix.System] call EnsureNixInstalled from devbox.Open, and cleanup function API
1 parent 1678c13 commit fd2fab6

File tree

11 files changed

+23
-57
lines changed

11 files changed

+23
-57
lines changed

internal/devconfig/packages.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func NewPackage(name string, values map[string]any) Package {
243243
// If the package has a list of excluded platforms, it is enabled on all platforms
244244
// except those.
245245
func (p *Package) IsEnabledOnPlatform() bool {
246-
platform := nix.MustGetSystem()
246+
platform := nix.System()
247247
if len(p.Platforms) > 0 {
248248
for _, plt := range p.Platforms {
249249
if plt == platform {

internal/devpkg/package.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,8 @@ func (p *Package) NormalizedDevboxPackageReference() (string, error) {
227227
}
228228

229229
if path != "" {
230-
s, err := nix.System()
231-
if err != nil {
232-
return "", err
233-
}
234230
url, fragment, _ := strings.Cut(path, "#")
235-
return fmt.Sprintf("%s#legacyPackages.%s.%s", url, s, fragment), nil
231+
return fmt.Sprintf("%s#legacyPackages.%s.%s", url, nix.System(), fragment), nil
236232
}
237233

238234
return "", nil
@@ -480,17 +476,12 @@ func (p *Package) IsInBinaryCache() (bool, error) {
480476
return false, err
481477
}
482478

483-
userSystem, err := nix.System()
484-
if err != nil {
485-
return false, err
486-
}
487-
488479
if entry.Systems == nil {
489480
return false, nil
490481
}
491482

492483
// Check if the user's system's info is present in the lockfile
493-
_, ok := entry.Systems[userSystem]
484+
_, ok := entry.Systems[nix.System()]
494485
if !ok {
495486
return false, nil
496487
}
@@ -519,12 +510,7 @@ func (p *Package) InputAddressedPath() (string, error) {
519510
return "", err
520511
}
521512

522-
userSystem, err := nix.System()
523-
if err != nil {
524-
return "", err
525-
}
526-
527-
sysInfo := entry.Systems[userSystem]
513+
sysInfo := entry.Systems[nix.System()]
528514
return sysInfo.StorePath, nil
529515
}
530516

internal/impl/devbox.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ type Devbox struct {
7373
var legacyPackagesWarningHasBeenShown = false
7474

7575
func Open(opts *devopt.Opts) (*Devbox, error) {
76+
7677
projectDir, err := findProjectDir(opts.Dir)
7778
if err != nil {
7879
return nil, err
@@ -128,6 +129,10 @@ func Open(opts *devopt.Opts) (*Devbox, error) {
128129
)
129130
}
130131

132+
if err := nix.EnsureNixInstalled(box.writer, nil); err != nil {
133+
return nil, err
134+
}
135+
131136
return box, nil
132137
}
133138

internal/impl/update.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,12 @@ func (d *Devbox) mergeResolvedPackageToLockfile(
126126

127127
// Add any missing system infos for packages whose versions did not change.
128128
if featureflag.RemoveNixpkgs.Enabled() {
129-
userSystem, err := nix.System()
130-
if err != nil {
131-
return err
132-
}
133129

134130
if lockfile.Packages[pkg.Raw].Systems == nil {
135131
lockfile.Packages[pkg.Raw].Systems = map[string]*lock.SystemInfo{}
136132
}
137133

134+
userSystem := nix.System()
138135
updated := false
139136
for sysName, newSysInfo := range resolved.Systems {
140137
if sysName == userSystem {

internal/impl/update_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ func TestUpdateOtherSysInfoIsReplaced(t *testing.T) {
157157
}
158158

159159
func currentSystem(t *testing.T) string {
160-
sys, err := nix.System() // NOTE: we could mock this too, if it helps.
161-
require.NoError(t, err)
160+
sys := nix.System() // NOTE: we could mock this too, if it helps.
162161
return sys
163162
}

internal/lock/resolve.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,7 @@ func (f *File) FetchResolvedPackage(pkg string) (*Package, error) {
6161
}
6262

6363
func selectForSystem(pkg *searcher.PackageVersion) (searcher.PackageInfo, error) {
64-
currentSystem, err := nix.System()
65-
if err != nil {
66-
return searcher.PackageInfo{}, err
67-
}
68-
if pi, ok := pkg.Systems[currentSystem]; ok {
64+
if pi, ok := pkg.Systems[nix.System()]; ok {
6965
return pi, nil
7066
}
7167
if pi, ok := pkg.Systems["x86_64-linux"]; ok {

internal/nix/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func EnsureNixInstalled(writer io.Writer, withDaemonFunc func() *bool) (err erro
9999
defer func() {
100100
if err == nil {
101101
// call System to ensure its value is internally cached so we can rely on MustGetSystem
102-
_, err = System()
102+
_ = System()
103103
}
104104
}()
105105

internal/nix/nix.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,21 @@ func ExperimentalFlags() []string {
118118
}
119119
}
120120

121-
// TODO: rename to System
122-
func MustGetSystem() string {
121+
func System() string {
123122
if cachedSystem == "" {
124-
// For internal calls (during tests), this may not be initialized properly
125-
// so do a best-effort attempt to initialize it.
126-
_, err := System()
123+
// While this should have been initialized, we do a best-effort to avoid
124+
// a panic.
125+
_, err := ComputeSystem()
127126
if err != nil {
128-
panic("MustGetSystem called before being initialized by System")
127+
panic("System called before being initialized by ComputeSystem")
129128
}
130129
}
131130
return cachedSystem
132131
}
133132

134133
var cachedSystem string
135134

136-
// TODO: rename to ComputeSystem or ComputePlatform?
137-
func System() (string, error) {
135+
func ComputeSystem() (string, error) {
138136
// For Savil to debug "remove nixpkgs" feature. The Search api lacks x86-darwin info.
139137
// So, I need to fake that I am x86-linux and inspect the output in generated devbox.lock
140138
// and flake.nix files.
@@ -151,7 +149,7 @@ func System() (string, error) {
151149
cmd.Args = append(cmd.Args, ExperimentalFlags()...)
152150
out, err := cmd.Output()
153151
if err != nil {
154-
return "", errors.WithStack(err)
152+
return "", err
155153
}
156154
cachedSystem = string(out)
157155
}

internal/nix/nixprofile/profile.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,7 @@ func ProfileInstall(args *ProfileInstallArgs) error {
265265
if exists, err := input.ValidateInstallsOnSystem(); err != nil {
266266
return err
267267
} else if !exists {
268-
platform, err := nix.System()
269-
if err != nil {
270-
platform = ""
271-
} else {
272-
platform = " " + platform
273-
}
268+
platform := " " + nix.System()
274269
return usererr.New(
275270
"package %s cannot be installed on your platform%s. "+
276271
"Run `devbox add %[1]s --exclude-platform%[2]s` and re-try.",

internal/plugin/plugin.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,6 @@ func (m *Manager) createFile(
142142
return errors.WithStack(err)
143143
}
144144

145-
system, err := nix.System()
146-
if err != nil {
147-
return err
148-
}
149-
150145
var urlForInput, attributePath string
151146

152147
if pkg, ok := pkg.(*devpkg.Package); ok {
@@ -164,7 +159,7 @@ func (m *Manager) createFile(
164159
"DevboxProfileDefault": filepath.Join(m.ProjectDir(), nix.ProfilePath),
165160
"PackageAttributePath": attributePath,
166161
"Packages": m.PackageNames(),
167-
"System": system,
162+
"System": nix.System(),
168163
"URLForInput": urlForInput,
169164
"Virtenv": filepath.Join(virtenvPath, name),
170165
}); err != nil {

0 commit comments

Comments
 (0)