Skip to content

Commit 4886e1f

Browse files
authored
[direnv] Added env variables from plugins to direnv (#458)
## Summary Transformed `devbox shell --print-env` command to add env variables from plugins when cd-ing into a direnv directory. ## How was it tested? - compile - run `./devbox init` (requires direnv) - respond yes to prompt for direnv integration - Using nginx as an example for a package with plugin, run `./devbox add nginx` - confirm nginx plugin's env vars are set - `echo $NGINX_CONFDIR `, `echo $NGINX_PATH_PREFIX`, and `echo $NGINX_TMPDIR`
1 parent c682fc3 commit 4886e1f

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

devbox.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Devbox interface {
2929
GenerateEnvrc(force bool) error
3030
Info(pkg string, markdown bool) error
3131
ListScripts() []string
32-
PrintShellEnv() error
32+
PluginEnv() (string, error)
3333
// Remove removes Nix packages from the config so that it no longer exists in
3434
// the devbox environment.
3535
Remove(pkgs ...string) error

internal/boxcli/shell.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,14 @@ func runShellCmd(cmd *cobra.Command, args []string, flags shellCmdFlags) error {
5151
}
5252

5353
if flags.PrintEnv {
54+
script, err := box.PluginEnv()
55+
if err != nil {
56+
return err
57+
}
58+
// print to stdout instead of stderr so that direnv can read the output
59+
cmd.Print(script)
5460
// return here to prevent opening a devbox shell
55-
return box.PrintShellEnv()
61+
return nil
5662
}
5763

5864
if devbox.IsDevboxShellEnabled() {

internal/impl/devbox.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -348,15 +348,16 @@ func (d *Devbox) Exec(cmds ...string) error {
348348
return nix.Exec(nixDir, cmds, env)
349349
}
350350

351-
func (d *Devbox) PrintShellEnv() error {
352-
profileBinDir, err := d.profileBinDir()
351+
func (d *Devbox) PluginEnv() (string, error) {
352+
pluginEnvs, err := plugin.Env(d.cfg.Packages, d.projectDir)
353353
if err != nil {
354-
return errors.WithStack(err)
354+
return "", err
355355
}
356-
// TODO: For now we just updated the PATH but this may need to evolve
357-
// to essentially a parsed shellrc.tmpl
358-
fmt.Fprintf(d.writer, "export PATH=\"%s:$PATH\"", profileBinDir)
359-
return nil
356+
script := ""
357+
for _, pluginEnv := range pluginEnvs {
358+
script += fmt.Sprintf("export %s\n", pluginEnv)
359+
}
360+
return script, nil
360361
}
361362

362363
func (d *Devbox) Info(pkg string, markdown bool) error {

internal/impl/tmpl/envrc.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use_devbox() {
55
watch_file devbox.json
66
if [ -f .devbox/gen/shell.nix ]; then
77
use nix .devbox/gen/shell.nix
8+
eval $(devbox shell --print-env)
89
fi
910
}
1011
use devbox

0 commit comments

Comments
 (0)