Skip to content

Commit cbc7099

Browse files
authored
[direnv] Fixed .envrc file to emulate nix-shell env (#453)
## Summary Changed the generated `.envrc` file to this logic: if `.devbox/gen/shell.nix` exists -> use direnv's `use nix` function to set the environment. Otherwise just watch `devbox.json` and activate direnv. ## How was it tested? - compile - run `./devbox init` and answer "y" to the prompt (requires direnv to be installed) - confirm there is no error and a `.envrc` file is created - run `./devbox add go_1_18` - run `which go` and confirm it points to nix store without any reload required
1 parent 15dff68 commit cbc7099

File tree

3 files changed

+38
-32
lines changed

3 files changed

+38
-32
lines changed

internal/boxcli/init.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,13 @@ func runInitCmd(cmd *cobra.Command, args []string) error {
3030
if err != nil {
3131
return errors.WithStack(err)
3232
}
33+
box, err := devbox.Open(path, cmd.ErrOrStderr())
34+
if err != nil {
35+
return errors.WithStack(err)
36+
}
37+
err = box.GenerateEnvrc(false)
38+
if err != nil {
39+
return errors.WithStack(err)
40+
}
3341
return nil
3442
}

internal/impl/devbox.go

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -60,34 +60,6 @@ func InitConfig(dir string, writer io.Writer) (created bool, err error) {
6060
color.HiYellowString(s),
6161
)
6262
}
63-
// .envrc file creation
64-
if commandExists("direnv") {
65-
// prompt for direnv allow
66-
var result string
67-
prompt := &survey.Input{
68-
Message: "Do you want to enable direnv integration for this devbox project?[y/n]",
69-
}
70-
err = survey.AskOne(prompt, &result)
71-
if err != nil {
72-
return false, errors.WithStack(err)
73-
}
74-
75-
if strings.ToLower(result) == "y" {
76-
envrcfilePath := filepath.Join(dir, ".envrc")
77-
filesExist := fileutil.Exists(envrcfilePath)
78-
if !filesExist { // don't overwrite an existing .envrc
79-
err := generate.CreateEnvrc(tmplFS, dir)
80-
if err != nil {
81-
return false, errors.WithStack(err)
82-
}
83-
}
84-
cmd := exec.Command("direnv", "allow")
85-
err = cmd.Run()
86-
if err != nil {
87-
return false, errors.WithStack(err)
88-
}
89-
}
90-
}
9163

9264
return cuecfg.InitFile(cfgPath, config)
9365
}
@@ -489,9 +461,31 @@ func (d *Devbox) GenerateEnvrc(force bool) error {
489461
filesExist := fileutil.Exists(envrcfilePath)
490462
// confirm .envrc doesn't exist and don't overwrite an existing .envrc
491463
if force || !filesExist {
492-
err := generate.CreateEnvrc(tmplFS, d.projectDir)
493-
if err != nil {
494-
return errors.WithStack(err)
464+
// .envrc file creation
465+
if commandExists("direnv") {
466+
// prompt for direnv allow
467+
var result string
468+
prompt := &survey.Input{
469+
Message: "Do you want to enable direnv integration for this devbox project?[y/n]",
470+
}
471+
err := survey.AskOne(prompt, &result)
472+
if err != nil {
473+
return errors.WithStack(err)
474+
}
475+
476+
if strings.ToLower(result) == "y" {
477+
if !filesExist { // don't overwrite an existing .envrc
478+
err := generate.CreateEnvrc(tmplFS, d.projectDir)
479+
if err != nil {
480+
return errors.WithStack(err)
481+
}
482+
}
483+
cmd := exec.Command("direnv", "allow")
484+
err = cmd.Run()
485+
if err != nil {
486+
return errors.WithStack(err)
487+
}
488+
}
495489
}
496490
} else {
497491
return usererr.New(

internal/impl/tmpl/envrc.tmpl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33

44
use_devbox() {
55
watch_file devbox.json
6-
eval $(devbox shell --print-env)
6+
if [ -f .devbox/gen/shell.nix ]; then
7+
use nix .devbox/gen/shell.nix
8+
fi
79
}
810
use devbox
911

12+
13+
1014
# check out https://www.jetpack.io/devbox/docs/ide_configuration/direnv/
1115
# for more details

0 commit comments

Comments
 (0)