Skip to content

Commit 94b34ef

Browse files
authored
[nix] Check other locations for nix startup and better error (#702)
## Summary Tries $XDG_STATE_HOME in addition to previous paths. Shows better error message. ## How was it tested? - [x] docker ubuntu fresh install
1 parent ef3e4a2 commit 94b34ef

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

internal/boxcli/setup.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ func ensureNixInstalled(cmd *cobra.Command, args []string) error {
5353
return nil
5454
}
5555
if nix.DirExists() {
56-
if err := nix.SourceNixEnv(); err != nil || nix.BinaryInstalled() {
56+
if err := nix.SourceNixEnv(); err != nil {
5757
return err
58+
} else if nix.BinaryInstalled() {
59+
return nil
5860
}
5961

6062
return usererr.New(

internal/nix/source.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,36 @@ import (
1010
"github.com/MakeNowJust/heredoc/v2"
1111
"github.com/hashicorp/go-envparse"
1212
"github.com/pkg/errors"
13+
"go.jetpack.io/devbox/internal/boxcli/usererr"
14+
"go.jetpack.io/devbox/internal/xdg"
1315
)
1416

17+
func nixLinks() []string {
18+
return []string{
19+
"/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh",
20+
filepath.Join(os.Getenv("HOME"), ".nix-profile/etc/profile.d/nix.sh"),
21+
// logic introduced in https://github.com/NixOS/nix/pull/5588/files
22+
xdg.StateSubpath("nix/profile/etc/profile.d/nix.sh"),
23+
}
24+
}
25+
1526
func SourceNixEnv() error {
1627
// if command is not in path, the source the nix startup files and hopefully
1728
// the command will be found. (we should still check that nix is actually
1829
// installed before we get here)
19-
srcFile := "/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh"
20-
// if global (multi-user) daemon file is missing, try getting the single user
21-
// file.
22-
if _, err := os.Stat(srcFile); os.IsNotExist(err) {
23-
srcFile = filepath.Join(
24-
os.Getenv("HOME"),
25-
"/.nix-profile/etc/profile.d/nix.sh",
30+
srcFile := ""
31+
for _, f := range nixLinks() {
32+
if _, err := os.Stat(f); err == nil {
33+
srcFile = f
34+
break
35+
}
36+
}
37+
38+
if srcFile == "" {
39+
return usererr.New(
40+
"Unable to find nix startup file. If /nix directory exists it's " +
41+
"possible the installation did not complete successfully. Follow " +
42+
"instructions at https://nixos.org/download.html for manual install.",
2643
)
2744
}
2845

0 commit comments

Comments
 (0)