-
Notifications
You must be signed in to change notification settings - Fork 263
[nix] attempt to source nix startup files if not in path #426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
0d358aa
to
7ada3b3
Compare
internal/boxcli/setup.go
Outdated
|
||
color.Yellow("\nNix is not installed. Devbox will attempt to install it.\n\n") | ||
|
||
if skipPrompts, _ := cmd.Flags().GetBool(globalYesFlag); !skipPrompts { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we simply skip this if the terminal is non-interactive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will do!
internal/nix/source.go
Outdated
"bash", | ||
"-c", | ||
fmt.Sprintf("source %s ; echo '<<<ENVIRONMENT>>>' ; env", srcFile), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this work with /bin/sh
? bash isn't available in Alpine images, for example.
I'd also suggest clearing the environment to try and grab just the environment variables exported by the script (maybe even do a before-and-after diff). Otherwise we may end up reverting env vars that the user changed since starting their terminal.
"bash", | |
"-c", | |
fmt.Sprintf("source %s ; echo '<<<ENVIRONMENT>>>' ; env", srcFile), | |
"env", | |
"-i", | |
"/bin/sh", | |
"-c", | |
fmt.Sprintf(`. "%s" ; echo '<<<ENVIRONMENT>>>' ; env`, srcFile), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good calls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gcurtis one issue your solution runs into is that the nix script itself uses __ETC_PROFILE_NIX_SOURCED
to determine if it should run again. I can always pass that variable manually, but that's a bit fragile.
Question about variables changing, the commands Env should be exactly the same as the current process, from the docs:
// If Env is nil, the new process uses the current process's
// environment.
so the diff should in theory be just the loaded ones right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm switching bash to sh, but I don't think we can use a fresh environment. The reason is that the sourced files use environment variables (PATH and __ETC_PROFILE_NIX_SOURCED) this means that we would get bad values.
I actually think the risk of a stale variable is minimal because how this is used. It only gets called at pre-command time if /nix
directory is found but nix is not in path. It never gets called again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, good point. Alright, all sounds good to me!
Summary
This change allows devbox to work even if nix is not in path. This is useful for:
/etc
rc files) devbox should continue to work.I tried a bunch of other approaches, but this one seems like most robust and least prone to errors because it does't actually change the commands. If we find a
/nix
dir butnix-shell
is not in path, we attempt to source the nix startup files (multi and single user). If we still fail to find the binaries we show an error.Edit:
Additional changes:
-y
flag to avoid promptcc: @LucilleH
How was it tested?
nix version
did not work. Devbox continued to work.