Skip to content

Commit

Permalink
[nix] Check other locations for nix startup and better error (#702)
Browse files Browse the repository at this point in the history
## Summary

Tries $XDG_STATE_HOME in addition to previous paths. Shows better error
message.

## How was it tested?

- [x] docker ubuntu fresh install
  • Loading branch information
mikeland73 committed Feb 28, 2023
1 parent ef3e4a2 commit 94b34ef
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
4 changes: 3 additions & 1 deletion internal/boxcli/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ func ensureNixInstalled(cmd *cobra.Command, args []string) error {
return nil
}
if nix.DirExists() {
if err := nix.SourceNixEnv(); err != nil || nix.BinaryInstalled() {
if err := nix.SourceNixEnv(); err != nil {
return err
} else if nix.BinaryInstalled() {
return nil
}

return usererr.New(
Expand Down
31 changes: 24 additions & 7 deletions internal/nix/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,36 @@ import (
"github.com/MakeNowJust/heredoc/v2"
"github.com/hashicorp/go-envparse"
"github.com/pkg/errors"
"go.jetpack.io/devbox/internal/boxcli/usererr"
"go.jetpack.io/devbox/internal/xdg"
)

func nixLinks() []string {
return []string{
"/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh",
filepath.Join(os.Getenv("HOME"), ".nix-profile/etc/profile.d/nix.sh"),
// logic introduced in https://github.com/NixOS/nix/pull/5588/files
xdg.StateSubpath("nix/profile/etc/profile.d/nix.sh"),
}
}

func SourceNixEnv() error {
// if command is not in path, the source the nix startup files and hopefully
// the command will be found. (we should still check that nix is actually
// installed before we get here)
srcFile := "/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh"
// if global (multi-user) daemon file is missing, try getting the single user
// file.
if _, err := os.Stat(srcFile); os.IsNotExist(err) {
srcFile = filepath.Join(
os.Getenv("HOME"),
"/.nix-profile/etc/profile.d/nix.sh",
srcFile := ""
for _, f := range nixLinks() {
if _, err := os.Stat(f); err == nil {
srcFile = f
break
}
}

if srcFile == "" {
return usererr.New(
"Unable to find nix startup file. If /nix directory exists it's " +
"possible the installation did not complete successfully. Follow " +
"instructions at https://nixos.org/download.html for manual install.",
)
}

Expand Down

0 comments on commit 94b34ef

Please sign in to comment.