Skip to content
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

shell,nix: set original PATH at start of init file #57

Merged
merged 5 commits into from
Sep 2, 2022

Conversation

gcurtis
Copy link
Collaborator

@gcurtis gcurtis commented Sep 2, 2022

Summary

This fixes a bug that causes the devbox shell's init to fail when the user's rcfile tries to call a command outside of devbox. For example, the following line in a ~/.bashrc will trigger an error if the devbox doesn't have Go installed:

export PATH="$(go env GOPATH):$PATH"

This is because we're adding the ORIGINAL_PATH at the end of their shell init script, which means that anything before that only see Nix packages.

Fix this by adding a pre-init hook that lets us restore the PATH before the user's rcfile, while still forcing Nix packages to come first in the post-init hook.

How was it tested?

Added a command that's only available outside of devbox to my ~/.bashrc and made sure it was called successfully.

This fixes a bug that causes the devbox shell's init to fail when the
user's rcfile tries to call a command outside of devbox. For example,
the following line in a ~/.bashrc will trigger an error if the devbox
doesn't have Go installed:

	export PATH="$(go env GOPATH):$PATH"

This is because we're adding the ORIGINAL_PATH at the end of their shell
init script, which means that anything before that only see Nix
packages.

Fix this by adding a pre-init hook that lets us restore the PATH before
the user's rcfile, while still forcing Nix packages to come first in
the post-init hook.
@gcurtis
Copy link
Collaborator Author

gcurtis commented Sep 2, 2022

@LucilleH do you mind running this to verify that devbox shell is fixed for you?

# but prefer anything installed by Nix.
export PATH="$PURE_NIX_PATH:$ORIGINAL_PATH"
sh.PreInitHook = `
# Update the $PATH so that the user's init script has access to all of their
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should both the PreInit and PostInit hooks set the path to: $PURE_NIX_PATH:$ORIGINAL_PATH?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just switched both hooks to use the Nix packages.

I wasn't sure what the correct behavior should be:

  • Prefer the non-devbox PATH to guarantee compatibility with the user's rcfile.
  • Prefer the devbox PATH so that hooks use the Nix packages.

@loreto
Copy link
Contributor

loreto commented Sep 2, 2022

@LucilleH are you able to test again?

Copy link
Contributor

@Lagoja Lagoja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worked when I ran it!

Copy link
Contributor

@loreto loreto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@gcurtis gcurtis merged commit 26f5204 into main Sep 2, 2022
@gcurtis gcurtis deleted the gcurtis/shell-preinit-hook branch September 2, 2022 22:53
if runErr != nil && d.Debug() {
log.Printf("Error: %+v\n", runErr)
strVal := ""
if d.flag.Changed {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work. preRun runs before the command so the flag is not set yet

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well that explains it. I'll put up another PR to clean up the debug stuff and make the flag work.

var enabled bool

func init() {
enabled, _ = strconv.ParseBool(os.Getenv("DEBUG"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be DEVBOX_DEBUG ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants