Skip to content

Commit 5158452

Browse files
authored
[WSL] Don't allow root nix instalation on WSL (#709)
## Summary This shows an error is user tries to install nix on WSL as root. cc: @Lagoja ## How was it tested? Untested
1 parent cf22dcc commit 5158452

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

internal/boxcli/setup.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package boxcli
66
import (
77
"fmt"
88
"os"
9-
"os/user"
109

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

9493
func nixDaemonFlagVal(cmd *cobra.Command) *bool {
9594
if !cmd.Flags().Changed(nixDaemonFlag) {
96-
if u, err := user.Current(); err == nil && u.Uid == "0" {
95+
if os.Geteuid() == 0 {
9796
ux.Fwarning(
9897
cmd.ErrOrStderr(),
9998
"Running as root. Installing Nix in multi-user mode.\n",

internal/nix/install.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@ import (
1111

1212
"github.com/fatih/color"
1313
"github.com/pkg/errors"
14+
"go.jetpack.io/devbox/internal/boxcli/usererr"
15+
"go.jetpack.io/devbox/internal/telemetry"
1416
)
1517

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

1820
// Install runs the install script for Nix. daemon has 3 states
1921
// nil is unset. false is --no-daemon. true is --daemon.
2022
func Install(writer io.Writer, daemon *bool) error {
23+
if isRoot() && telemetry.IsWSL() {
24+
return usererr.New("Nix cannot be installed as root on WSL. Please run as a normal user with sudo access.")
25+
}
2126
r, w, err := os.Pipe()
2227
if err != nil {
2328
return errors.WithStack(err)
@@ -79,3 +84,7 @@ func DirExists() bool {
7984
_, err := os.Stat("/nix")
8085
return err == nil
8186
}
87+
88+
func isRoot() bool {
89+
return os.Geteuid() == 0
90+
}

internal/telemetry/telemetry.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,7 @@ func OS() string {
5555

5656
return os
5757
}
58+
59+
func IsWSL() bool {
60+
return OS() == "wsl"
61+
}

0 commit comments

Comments
 (0)