Skip to content

Commit 4063fc5

Browse files
authored
Allow limited set of vars to leak into devbox run env (#498)
## Summary TSIA Study on how these vars were chosen: https://www.notion.so/jetpackio/Env-audit-nix-shell-vs-print-dev-env-bebe176d2a2b4796967df92f58201fef ## How was it tested? ``` DEVBOX_FEATURE_NIX_DEV_ENV_RUN=1 ./devbox run -- echo '$HOME' # my home, instead of /homeless-shelter DEVBOX_FEATURE_NIX_DEV_ENV_RUN=1 ./devbox run -- echo '$TMP' # not set in host, not set here DEVBOX_FEATURE_NIX_DEV_ENV_RUN=1 ./devbox run -- echo '$TMPDIR' # set in host, set here DEVBOX_FEATURE_NIX_DEV_ENV_RUN=1 ./devbox run lint # works without complaining about lockfile DEVBOX_FEATURE_NIX_DEV_ENV_RUN=1 ./devbox run build # works as well DEVBOX_FEATURE_NIX_DEV_ENV_RUN=1 ./devbox run curl https://www.google.com # works without ssl failure ```
1 parent 52cdb7a commit 4063fc5

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

internal/nix/run.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ func RunScript(nixShellFilePath string, projectDir string, cmdWithArgs string, a
2828
}
2929
}
3030

31+
// Overwrite/leak whitelisted vars into nixEnv:
32+
for name, leak := range leakVarsForRun {
33+
if leak {
34+
nixEnv = append(nixEnv, fmt.Sprintf("%s=%s", name, os.Getenv(name)))
35+
}
36+
}
37+
3138
cmd := exec.Command("sh", "-c", cmdWithArgs)
3239
cmd.Env = append(nixEnv, additionalEnv...)
3340
cmd.Dir = projectDir
@@ -43,3 +50,23 @@ func RunScript(nixShellFilePath string, projectDir string, cmdWithArgs string, a
4350
}
4451
return errors.WithStack(err)
4552
}
53+
54+
// leakVarsForRun contains a list of variables that, if set in the host, will be copied
55+
// to the environment of devbox run. If they're NOT set in the host, they will be set
56+
// to an empty value for devbox run. NOTE: we want to keep this list AS SMALL AS POSSIBLE.
57+
// The longer this list, the less "pure" devbox run becomes.
58+
//
59+
// In particular, this list should be much smaller than that of devbox shell, since we
60+
// do want to allow more parts of the host environment to leak into a shell session, so
61+
// that the shell session is easy to use for our users. However, in devbox run, we value
62+
// reproducibility above interactive ease-of-use.
63+
var leakVarsForRun = map[string]bool{
64+
"HOME": true, // Without this, HOME is set to /homeless-shelter and most programs fail.
65+
66+
// Where to write temporary files. nix print-dev-env sets these to an unwriteable path,
67+
// so we override that here with whatever the host has set.
68+
"TMP": true,
69+
"TEMP": true,
70+
"TMPDIR": true,
71+
"TEMPDIR": true,
72+
}

0 commit comments

Comments
 (0)