Skip to content

Commit

Permalink
[WSL] Don't allow root nix instalation on WSL (#709)
Browse files Browse the repository at this point in the history
## Summary

This shows an error is user tries to install nix on WSL as root. cc:
@Lagoja

## How was it tested?

Untested
  • Loading branch information
mikeland73 committed Mar 1, 2023
1 parent cf22dcc commit 5158452
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
3 changes: 1 addition & 2 deletions internal/boxcli/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package boxcli
import (
"fmt"
"os"
"os/user"

"github.com/fatih/color"
"github.com/mattn/go-isatty"
Expand Down Expand Up @@ -93,7 +92,7 @@ func ensureNixInstalled(cmd *cobra.Command, args []string) error {

func nixDaemonFlagVal(cmd *cobra.Command) *bool {
if !cmd.Flags().Changed(nixDaemonFlag) {
if u, err := user.Current(); err == nil && u.Uid == "0" {
if os.Geteuid() == 0 {
ux.Fwarning(
cmd.ErrOrStderr(),
"Running as root. Installing Nix in multi-user mode.\n",
Expand Down
9 changes: 9 additions & 0 deletions internal/nix/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ import (

"github.com/fatih/color"
"github.com/pkg/errors"
"go.jetpack.io/devbox/internal/boxcli/usererr"
"go.jetpack.io/devbox/internal/telemetry"
)

const rootError = "warning: installing Nix as root is not supported by this script!"

// Install runs the install script for Nix. daemon has 3 states
// nil is unset. false is --no-daemon. true is --daemon.
func Install(writer io.Writer, daemon *bool) error {
if isRoot() && telemetry.IsWSL() {
return usererr.New("Nix cannot be installed as root on WSL. Please run as a normal user with sudo access.")
}
r, w, err := os.Pipe()
if err != nil {
return errors.WithStack(err)
Expand Down Expand Up @@ -79,3 +84,7 @@ func DirExists() bool {
_, err := os.Stat("/nix")
return err == nil
}

func isRoot() bool {
return os.Geteuid() == 0
}
4 changes: 4 additions & 0 deletions internal/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ func OS() string {

return os
}

func IsWSL() bool {
return OS() == "wsl"
}

0 comments on commit 5158452

Please sign in to comment.