Skip to content

Conversation

savil
Copy link
Collaborator

@savil savil commented Sep 29, 2022

Summary

Motivation:
We can define a custom nix-profile, and install packages scoped to this profile.

The goal is to make it easier to add packages when within devbox shell. Currently,
users need to exit the shell and restart it for the nix package to be installed - clearly
not ideal. With a nix-profile, if we install the package using that profile, then the
package's binary should be downloaded and visible within the shell.

Within devbox shell:

  • previously, we would manually add the path to each package to PATH.
  • now, we can add the path to the nix-profile to PATH and skip manually adding each package.

Limitations:

  1. For devbox add within a devbox shell, the user needs to manually execute hash -r to ensure the latest binaries are visible.
  2. For devbox rm within a devbox shell, the user still needs to restart the shell. I'm not quite sure why the nix-env --profile <profile dir> --uninstall <pkg> isn't working as intended. Fixed via reapplying nix-env --profile <profile> -if development.nix
  3. devbox add and rm when in a shell started from a parent directory will not work.
    • A possible fix is to set DEVBOX_CONFIG_DIR env-var when inside a shell, and use that value in devbox add and rm.
    • Example:
> cd testdata/rust/
> devbox shell rust-stable
(devbox)> devbox add go_1_18
Error: No devbox.json found in this directory, or any parent directories. Did you run `devbox init` yet?

How was it tested?

  • in testdata/rust/rust-stable:
    • open devbox shell
    • verify openssl not installed via which openssl and getting /usr/bin/openssl
    • do devbox add openssl
      • note that which openssl still reflects the non nix-store location. It has NOT yet been updated.
    • do hash -r and then which openssl and see it point to a nix-store location
    • do devbox rm openssl
      • note that which openssl still reflects the nix-store location. It has NOT yet been updated.
    • restart devbox shell and do which openssl to verify it has been removed

Copy link
Collaborator Author

savil commented Sep 29, 2022

@savil savil force-pushed the savil/add-using-nix-profile branch 8 times, most recently from 8b711f3 to 513c9b4 Compare October 3, 2022 23:10
@savil savil changed the title [shell] use nix-profile [shell] use nix-profile to enable add within shell Oct 3, 2022
@savil savil changed the title [shell] use nix-profile to enable add within shell [shell] use nix-profile to enable adding packages within shell Oct 3, 2022
@savil savil changed the title [shell] use nix-profile to enable adding packages within shell [shell] add packages within shell, leveraging nix-profile Oct 3, 2022
@savil savil requested review from loreto and LucilleH October 3, 2022 23:14
@savil savil marked this pull request as ready for review October 3, 2022 23:17
boxcli/rm.go Outdated
fmt.Print(successMsg)

// Sadface. This doesn't seem to work within devbox shell for now.
fmt.Println(" You may need to restart `devbox shell` for this to take effect.")
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think that's reasonable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed this via doing nix-env --install development.nix again!

devbox.go Outdated
@@ -49,13 +49,26 @@ func Open(dir string) (*Devbox, error) {
return nil, errors.WithStack(err)
}

// if dir is current directory, then get the full path
if dir == "." {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

solved via #200. Removed this code.

nix/shell.go Outdated
@@ -250,11 +258,13 @@ func (s *Shell) writeDevboxShellrc() (path string, err error) {
OriginalInitPath string
UserHook string
PlanInitHook string
NixProfileDir string
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: ProfileBinDir

nix/shellrc.tmpl Outdated
@@ -57,7 +61,7 @@ PATH="$(
*) non_nix_path="${non_nix_path:+$non_nix_path:}${path_dir}" ;;
esac
done
echo "${nix_path:+$nix_path:}${non_nix_path}"
echo "{{ .NixProfileDir }}:${non_nix_path}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice!

devbox.go Outdated
@@ -49,13 +49,26 @@ func Open(dir string) (*Devbox, error) {
return nil, errors.WithStack(err)
}

// if dir is current directory, then get the full path
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of only resolving ., why not call filpath.Abs on any directory, to ensure everything is an absolute 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.

solved via #200

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

deleted this code

boxcli/add.go Outdated
@@ -27,6 +30,31 @@ func addCmdFunc() runFunc {
if err != nil {
return errors.WithStack(err)
}
return box.Add(args...)

if err = box.Add(args...); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you have too much logic in here for add, rm and shell.

The goal of the devbox library is that it can be used as an SDK that is equivalent to using the CLI. In other words, box.Add("package") should be roughly the same as calling devbox add package; similarly box.Remove("package") should be roughly the same as calling devbox rm package. But since you've added all this logic inside of the CLI, instead of inside of the library, that is no longer true.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oh gotcha - sure I can move more logic inside the devbox library.

Do we want user-messaging to happen inside the library as well i.e. the print statements? Should we consider passing in an io.Writer in the constructor for box in devbox.Open?

nix/shell.go Outdated
@@ -107,7 +110,7 @@ func rcfilePath(basename string) string {
return filepath.Join(home, basename)
}

func (s *Shell) Run(nixPath string) error {
func (s *Shell) Run(nixShellFilePath string, srcDir string) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

I find this new interface for Run slightly confusing. Can you elaborate on why we now need both of these to be passed in?

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 use it to construct the directory-for-profile-binaries i.e. srcDir + .devbox/profile/bin

It seems that the intention is for nix package to simulate a go-sdk. In that case, this is maybe not the best API.

devbox.go Outdated
box := &Devbox{
cfg: cfg,
srcDir: dir,
}
return box, nil
}

func (d *Devbox) SourceDir() string {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure this needs to be public – I think you have to make it public right now because you implemented a lot of the logic outside of the Devbox SDK – if you move that logic to be internal, then you don't need to expose this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

removed this function

@savil savil changed the base branch from main to savil/subfolder-search-config October 4, 2022 23:13
@savil savil force-pushed the savil/add-using-nix-profile branch from 3fa3da9 to 7e8a62e Compare October 4, 2022 23:13
@savil savil force-pushed the savil/subfolder-search-config branch from a4901dc to 8b8e7ab Compare October 4, 2022 23:16
@savil savil force-pushed the savil/add-using-nix-profile branch 2 times, most recently from beffd38 to ac4992b Compare October 5, 2022 00:17
@savil savil requested review from loreto and LucilleH October 5, 2022 00:24
@savil
Copy link
Collaborator Author

savil commented Oct 5, 2022

@loreto @LucilleH PTAL. Made a number of changes:

  1. Pushed logic from CLI into devbox package.
  2. Fixed shell.Exec() functionality
  3. added nix.WithProfile api to handle the profileBinDir that is used in the shellrc file.

@savil savil force-pushed the savil/add-using-nix-profile branch from ac4992b to 8dfb76f Compare October 5, 2022 00:29
Copy link
Collaborator

@LucilleH LucilleH left a comment

Choose a reason for hiding this comment

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

👍

if mode == uninstall {
installingVerb = "Uninstalling"
}
fmt.Fprintf(d.writer, "%s nix packages. This may take a while...", installingVerb)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we just say, "updating nix packages"?

installedVerb = "removed"
}

successMsg := fmt.Sprintf("%s is now %s.", pkgs[0], installedVerb)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just say "packages updated?"

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 had considered that, but "updated package" has another meaning i.e. has been updated from version 1.0.1 to 1.0.2 so felt it better to be super explicit

Copy link
Collaborator

Choose a reason for hiding this comment

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

oh right, haven't thought about that hehe

devbox.go Outdated

cmdStr := fmt.Sprintf(
"--profile %s --install -f %s/.devbox/gen/development.nix",
d.srcDir+"/"+profileDir,
Copy link
Collaborator

Choose a reason for hiding this comment

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

filepath.join?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oh yes of course :)

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'll audit other places in this PR too!

@savil savil mentioned this pull request Oct 5, 2022
Base automatically changed from savil/subfolder-search-config to main October 5, 2022 16:18
@savil savil force-pushed the savil/add-using-nix-profile branch from 8dfb76f to 40a2e33 Compare October 5, 2022 16:21
@savil savil force-pushed the savil/add-using-nix-profile branch from 40a2e33 to 8305051 Compare October 5, 2022 16:27
@savil savil added the feature New feature or request label Oct 5, 2022
@savil savil merged commit b5d56f6 into main Oct 5, 2022
@savil savil deleted the savil/add-using-nix-profile branch October 5, 2022 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Development

Successfully merging this pull request may close these issues.

3 participants