Skip to content

Commit fc99d42

Browse files
authored
[shell] move nix-profile into a specific directory inside .devbox directory (#210)
## Summary To avoid the nix-profile symlinks crowding the `.devbox/` directory, I push them inside a `.devbox/nix/profile/` sub-directory. The symlink is renamed to `default` inside `.devbox/nix/profile` to be consistent with [nix naming](https://nixos.org/manual/nix/stable/package-management/profiles.html). I also want to rename `profileDir` to `profilePath`. That is more accurate since it is not a directory but a symlink. However, will do in a separate PR since it has some conflicts with concurrent work like #207. ## How was it tested? ``` > cd testdata/rust/rust-stable > devbox shell > which rustc # see path in nix store > devbox add openssl > hash -r > which openssl # see path in nix store # inspect directory and symlinks: > ls -al /Users/savil/code/jetpack/devbox/testdata/rust/rust-stable/.devbox/nix/profile/ total 0 drwxr-xr-x 5 savil staff 160 Oct 6 11:55 . drwxr-xr-x 3 savil staff 96 Oct 6 11:55 .. lrwxr-xr-x 1 savil staff 14 Oct 6 11:55 default -> default-2-link lrwxr-xr-x 1 savil staff 60 Oct 6 11:55 default-1-link -> /nix/store/qn9px7zzx500d0l78nhz0b39v8nm1siq-user-environment lrwxr-xr-x 1 savil staff 60 Oct 6 11:55 default-2-link -> /nix/store/6q654ka2lcdvamxxvnsa9vp9dcj9b6yg-user-environment ```
1 parent e965cfd commit fc99d42

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

devbox.go

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ const (
3030
configFilename = "devbox.json"
3131

3232
// profileDir contains the contents of the profile generated via `nix-env --profile profileDir <command>`
33-
profileDir = ".devbox/profile"
33+
// Instead of using directory, prefer using the devbox.profileDir() function that ensures the directory exists.
34+
// TODO savil. Rename to profilePath. This is the symlink of the profile, and not a directory.
35+
profileDir = ".devbox/nix/profile/default"
3436

3537
// shellHistoryFile keeps the history of commands invoked inside devbox shell
3638
shellHistoryFile = ".devbox/shell_history"
@@ -179,10 +181,16 @@ func (d *Devbox) Shell() error {
179181
if err != nil {
180182
return errors.WithStack(err)
181183
}
184+
185+
profileDir, err := d.profileDir()
186+
if err != nil {
187+
return err
188+
}
189+
182190
nixShellFilePath := filepath.Join(d.srcDir, ".devbox/gen/shell.nix")
183191
sh, err := nix.DetectShell(
184192
nix.WithPlanInitHook(plan.ShellInitHook),
185-
nix.WithProfile(d.profileDir()),
193+
nix.WithProfile(profileDir),
186194
nix.WithHistoryFile(filepath.Join(d.srcDir, shellHistoryFile)),
187195
)
188196
if err != nil {
@@ -198,7 +206,12 @@ func (d *Devbox) Exec(cmds ...string) error {
198206
return err
199207
}
200208

201-
pathWithProfileBin := fmt.Sprintf("PATH=%s:$PATH", d.profileBinDir())
209+
profileBinDir, err := d.profileBinDir()
210+
if err != nil {
211+
return err
212+
}
213+
214+
pathWithProfileBin := fmt.Sprintf("PATH=%s:$PATH", profileBinDir)
202215
cmds = append([]string{pathWithProfileBin}, cmds...)
203216

204217
nixDir := filepath.Join(d.srcDir, ".devbox/gen/shell.nix")
@@ -256,12 +269,21 @@ func (d *Devbox) generateBuildFiles() error {
256269
return generate(d.srcDir, buildPlan, buildFiles)
257270
}
258271

259-
func (d *Devbox) profileDir() string {
260-
return filepath.Join(d.srcDir, profileDir)
272+
func (d *Devbox) profileDir() (string, error) {
273+
absPath := filepath.Join(d.srcDir, profileDir)
274+
if err := os.MkdirAll(filepath.Dir(absPath), 0755); err != nil {
275+
return "", errors.WithStack(err)
276+
}
277+
278+
return absPath, nil
261279
}
262280

263-
func (d *Devbox) profileBinDir() string {
264-
return filepath.Join(d.profileDir(), "bin")
281+
func (d *Devbox) profileBinDir() (string, error) {
282+
profileDir, err := d.profileDir()
283+
if err != nil {
284+
return "", err
285+
}
286+
return filepath.Join(profileDir, "bin"), nil
265287
}
266288

267289
func missingDevboxJSONError(dir string) error {
@@ -358,17 +380,21 @@ func (d *Devbox) printPackageUpdateMessage(mode installMode, pkgs []string) erro
358380
//
359381
// Will move to a store interface/package
360382
func (d *Devbox) ApplyDevNixDerivation() error {
383+
profileDir, err := d.profileDir()
384+
if err != nil {
385+
return err
386+
}
361387

362388
cmdStr := fmt.Sprintf(
363389
"--profile %s --install -f %s/.devbox/gen/development.nix",
364-
filepath.Join(d.srcDir, profileDir),
390+
profileDir,
365391
d.srcDir,
366392
)
367393
cmdParts := strings.Split(cmdStr, " ")
368394
execCmd := exec.Command("nix-env", cmdParts...)
369395

370396
debug.Log("running command: %s\n", execCmd.Args)
371-
err := execCmd.Run()
397+
err = execCmd.Run()
372398
return errors.WithStack(err)
373399
}
374400

0 commit comments

Comments
 (0)