@@ -59,14 +59,11 @@ const (
59
59
type Devbox struct {
60
60
cfg * devconfig.Config
61
61
env map [string ]string
62
- omitNixEnv bool
63
62
environment string
64
63
lockfile * lock.File
65
64
nix nix.Nixer
66
65
projectDir string
67
66
pluginManager * plugin.Manager
68
- preservePathStack bool
69
- pure bool
70
67
customProcessComposeFile string
71
68
72
69
// This is needed because of the --quiet flag.
@@ -98,14 +95,11 @@ func Open(opts *devopt.Opts) (*Devbox, error) {
98
95
box := & Devbox {
99
96
cfg : cfg ,
100
97
env : opts .Env ,
101
- omitNixEnv : opts .OmitNixEnv ,
102
98
environment : environment ,
103
99
nix : & nix.Nix {},
104
100
projectDir : projectDir ,
105
101
pluginManager : plugin .NewManager (),
106
102
stderr : opts .Stderr ,
107
- preservePathStack : opts .PreservePathStack ,
108
- pure : opts .Pure ,
109
103
customProcessComposeFile : opts .CustomProcessComposeFile ,
110
104
}
111
105
@@ -200,11 +194,11 @@ func (d *Devbox) Generate(ctx context.Context) error {
200
194
return errors .WithStack (shellgen .GenerateForPrintEnv (ctx , d ))
201
195
}
202
196
203
- func (d * Devbox ) Shell (ctx context.Context ) error {
197
+ func (d * Devbox ) Shell (ctx context.Context , envOpts devopt. EnvOptions ) error {
204
198
ctx , task := trace .NewTask (ctx , "devboxShell" )
205
199
defer task .End ()
206
200
207
- envs , err := d .ensureStateIsUpToDateAndComputeEnv (ctx )
201
+ envs , err := d .ensureStateIsUpToDateAndComputeEnv (ctx , envOpts )
208
202
if err != nil {
209
203
return err
210
204
}
@@ -228,15 +222,22 @@ func (d *Devbox) Shell(ctx context.Context) error {
228
222
WithShellStartTime (telemetry .ShellStart ()),
229
223
}
230
224
231
- shell , err := NewDevboxShell (d , opts ... )
225
+ shell , err := NewDevboxShell (d , envOpts , opts ... )
232
226
if err != nil {
233
227
return err
234
228
}
235
229
236
230
return shell .Run ()
237
231
}
238
232
239
- func (d * Devbox ) RunScript (ctx context.Context , cmdName string , cmdArgs []string ) error {
233
+ // runDevboxServicesScript invokes RunScript with the envOptions set to the appropriate
234
+ // defaults for the `devbox services` scenario.
235
+ // TODO: move to services.go
236
+ func (d * Devbox ) runDevboxServicesScript (ctx context.Context , cmdArgs []string ) error {
237
+ return d .RunScript (ctx , devopt.EnvOptions {}, "devbox" , cmdArgs )
238
+ }
239
+
240
+ func (d * Devbox ) RunScript (ctx context.Context , envOpts devopt.EnvOptions , cmdName string , cmdArgs []string ) error {
240
241
ctx , task := trace .NewTask (ctx , "devboxRun" )
241
242
defer task .End ()
242
243
@@ -256,7 +257,7 @@ func (d *Devbox) RunScript(ctx context.Context, cmdName string, cmdArgs []string
256
257
env [d .SkipInitHookEnvName ()] = "true"
257
258
} else {
258
259
var err error
259
- env , err = d .ensureStateIsUpToDateAndComputeEnv (ctx )
260
+ env , err = d .ensureStateIsUpToDateAndComputeEnv (ctx , envOpts )
260
261
if err != nil {
261
262
return err
262
263
}
@@ -341,9 +342,9 @@ func (d *Devbox) EnvExports(ctx context.Context, opts devopt.EnvExportsOpts) (st
341
342
)
342
343
}
343
344
344
- envs , err = d .computeEnv (ctx , true /*usePrintDevEnvCache*/ )
345
+ envs , err = d .computeEnv (ctx , true /*usePrintDevEnvCache*/ , opts . EnvOptions )
345
346
} else {
346
- envs , err = d .ensureStateIsUpToDateAndComputeEnv (ctx )
347
+ envs , err = d .ensureStateIsUpToDateAndComputeEnv (ctx , opts . EnvOptions )
347
348
}
348
349
349
350
if err != nil {
@@ -368,7 +369,7 @@ func (d *Devbox) EnvVars(ctx context.Context) ([]string, error) {
368
369
ctx , task := trace .NewTask (ctx , "devboxEnvVars" )
369
370
defer task .End ()
370
371
// this only returns env variables for the shell environment excluding hooks
371
- envs , err := d .ensureStateIsUpToDateAndComputeEnv (ctx )
372
+ envs , err := d .ensureStateIsUpToDateAndComputeEnv (ctx , devopt. EnvOptions {} )
372
373
if err != nil {
373
374
return nil , err
374
375
}
@@ -578,11 +579,12 @@ func (d *Devbox) Services() (services.Services, error) {
578
579
return result , nil
579
580
}
580
581
582
+ // TODO: move to services.go
581
583
func (d * Devbox ) StartServices (
582
584
ctx context.Context , runInCurrentShell bool , serviceNames ... string ,
583
585
) error {
584
586
if ! runInCurrentShell {
585
- return d .RunScript (ctx , "devbox" ,
587
+ return d .runDevboxServicesScript (ctx ,
586
588
append (
587
589
[]string {"services" , "start" , "--run-in-current-shell" },
588
590
serviceNames ... ,
@@ -622,14 +624,15 @@ func (d *Devbox) StartServices(
622
624
return nil
623
625
}
624
626
627
+ // TODO: move to services.go
625
628
func (d * Devbox ) StopServices (ctx context.Context , runInCurrentShell , allProjects bool , serviceNames ... string ) error {
626
629
if ! runInCurrentShell {
627
630
args := []string {"services" , "stop" , "--run-in-current-shell" }
628
631
args = append (args , serviceNames ... )
629
632
if allProjects {
630
633
args = append (args , "--all-projects" )
631
634
}
632
- return d .RunScript (ctx , "devbox" , args )
635
+ return d .runDevboxServicesScript (ctx , args )
633
636
}
634
637
635
638
if allProjects {
@@ -661,10 +664,10 @@ func (d *Devbox) StopServices(ctx context.Context, runInCurrentShell, allProject
661
664
return nil
662
665
}
663
666
667
+ // TODO: move to services.go
664
668
func (d * Devbox ) ListServices (ctx context.Context , runInCurrentShell bool ) error {
665
669
if ! runInCurrentShell {
666
- return d .RunScript (ctx ,
667
- "devbox" , []string {"services" , "ls" , "--run-in-current-shell" })
670
+ return d .runDevboxServicesScript (ctx , []string {"services" , "ls" , "--run-in-current-shell" })
668
671
}
669
672
670
673
svcSet , err := d .Services ()
@@ -700,11 +703,12 @@ func (d *Devbox) ListServices(ctx context.Context, runInCurrentShell bool) error
700
703
return nil
701
704
}
702
705
706
+ // TODO: move to services.go
703
707
func (d * Devbox ) RestartServices (
704
708
ctx context.Context , runInCurrentShell bool , serviceNames ... string ,
705
709
) error {
706
710
if ! runInCurrentShell {
707
- return d .RunScript (ctx , "devbox" ,
711
+ return d .runDevboxServicesScript (ctx ,
708
712
append (
709
713
[]string {"services" , "restart" , "--run-in-current-shell" },
710
714
serviceNames ... ,
@@ -739,6 +743,7 @@ func (d *Devbox) RestartServices(
739
743
return nil
740
744
}
741
745
746
+ // TODO: move to services.go
742
747
func (d * Devbox ) StartProcessManager (
743
748
ctx context.Context ,
744
749
runInCurrentShell bool ,
@@ -762,7 +767,7 @@ func (d *Devbox) StartProcessManager(
762
767
args = append (args , "--pcflags" , flag )
763
768
}
764
769
765
- return d .RunScript (ctx , "devbox" , args )
770
+ return d .runDevboxServicesScript (ctx , args )
766
771
}
767
772
768
773
svcs , err := d .Services ()
@@ -886,13 +891,17 @@ func (d *Devbox) execPrintDevEnv(ctx context.Context, usePrintDevEnvCache bool)
886
891
// Note that the shellrc.tmpl template (which sources this environment) does
887
892
// some additional processing. The computeEnv environment won't necessarily
888
893
// represent the final "devbox run" or "devbox shell" environments.
889
- func (d * Devbox ) computeEnv (ctx context.Context , usePrintDevEnvCache bool ) (map [string ]string , error ) {
894
+ func (d * Devbox ) computeEnv (
895
+ ctx context.Context ,
896
+ usePrintDevEnvCache bool ,
897
+ envOpts devopt.EnvOptions ,
898
+ ) (map [string ]string , error ) {
890
899
defer debug .FunctionTimer ().End ()
891
900
defer trace .StartRegion (ctx , "devboxComputeEnv" ).End ()
892
901
893
902
// Append variables from current env if --pure is not passed
894
903
currentEnv := os .Environ ()
895
- env , err := d .parseEnvAndExcludeSpecialCases (currentEnv )
904
+ env , err := d .parseEnvAndExcludeSpecialCases (currentEnv , envOpts . Pure )
896
905
if err != nil {
897
906
return nil , err
898
907
}
@@ -910,7 +919,7 @@ func (d *Devbox) computeEnv(ctx context.Context, usePrintDevEnvCache bool) (map[
910
919
originalEnv := make (map [string ]string , len (env ))
911
920
maps .Copy (originalEnv , env )
912
921
913
- if ! d . omitNixEnv {
922
+ if ! envOpts . OmitNixEnv {
914
923
nixEnv , err := d .execPrintDevEnv (ctx , usePrintDevEnvCache )
915
924
if err != nil {
916
925
return nil , err
@@ -989,13 +998,13 @@ func (d *Devbox) computeEnv(ctx context.Context, usePrintDevEnvCache bool) (map[
989
998
devboxEnvPath = envpath .JoinPathLists (devboxEnvPath , runXPaths )
990
999
991
1000
pathStack := envpath .Stack (env , originalEnv )
992
- pathStack .Push (env , d .ProjectDirHash (), devboxEnvPath , d . preservePathStack )
1001
+ pathStack .Push (env , d .ProjectDirHash (), devboxEnvPath , envOpts . PreservePathStack )
993
1002
env ["PATH" ] = pathStack .Path (env )
994
1003
slog .Debug ("new path stack is" , "path_stack" , pathStack )
995
1004
996
1005
slog .Debug ("computed environment PATH" , "path" , env ["PATH" ])
997
1006
998
- if ! d . pure {
1007
+ if ! envOpts . Pure {
999
1008
// preserve the original XDG_DATA_DIRS by prepending to it
1000
1009
env ["XDG_DATA_DIRS" ] = envpath .JoinPathLists (env ["XDG_DATA_DIRS" ], os .Getenv ("XDG_DATA_DIRS" ))
1001
1010
}
@@ -1011,6 +1020,7 @@ func (d *Devbox) computeEnv(ctx context.Context, usePrintDevEnvCache bool) (map[
1011
1020
// while ensuring these reflect the current (up to date) state of the project.
1012
1021
func (d * Devbox ) ensureStateIsUpToDateAndComputeEnv (
1013
1022
ctx context.Context ,
1023
+ envOpts devopt.EnvOptions ,
1014
1024
) (map [string ]string , error ) {
1015
1025
defer debug .FunctionTimer ().End ()
1016
1026
@@ -1037,7 +1047,7 @@ func (d *Devbox) ensureStateIsUpToDateAndComputeEnv(
1037
1047
// it's ok to use usePrintDevEnvCache=true here always. This does end up
1038
1048
// doing some non-nix work twice if lockfile is not up to date.
1039
1049
// TODO: Improve this to avoid extra work.
1040
- return d .computeEnv (ctx , true /*usePrintDevEnvCache*/ )
1050
+ return d .computeEnv (ctx , true /*usePrintDevEnvCache*/ , envOpts )
1041
1051
}
1042
1052
1043
1053
func (d * Devbox ) nixPrintDevEnvCachePath () string {
@@ -1256,7 +1266,7 @@ func (d *Devbox) addHashToEnv(env map[string]string) error {
1256
1266
1257
1267
// parseEnvAndExcludeSpecialCases converts env as []string to map[string]string
1258
1268
// In case of pure shell, it leaks HOME and it leaks PATH with some modifications
1259
- func (d * Devbox ) parseEnvAndExcludeSpecialCases (currentEnv []string ) (map [string ]string , error ) {
1269
+ func (d * Devbox ) parseEnvAndExcludeSpecialCases (currentEnv []string , pure bool ) (map [string ]string , error ) {
1260
1270
env := make (map [string ]string , len (currentEnv ))
1261
1271
for _ , kv := range currentEnv {
1262
1272
key , val , found := strings .Cut (kv , "=" )
@@ -1270,13 +1280,13 @@ func (d *Devbox) parseEnvAndExcludeSpecialCases(currentEnv []string) (map[string
1270
1280
// - HOME required for devbox binary to work
1271
1281
// - PATH to find the nix installation. It is cleaned for pure mode below.
1272
1282
// - TERM to enable colored text in the pure shell
1273
- if ! d . pure || key == "HOME" || key == "PATH" || key == "TERM" {
1283
+ if ! pure || key == "HOME" || key == "PATH" || key == "TERM" {
1274
1284
env [key ] = val
1275
1285
}
1276
1286
}
1277
1287
1278
1288
// handling special case for PATH
1279
- if d . pure {
1289
+ if pure {
1280
1290
// Setting a custom env variable to differentiate pure and regular shell
1281
1291
env ["DEVBOX_PURE_SHELL" ] = "1"
1282
1292
// Finding nix executables in path and passing it through
0 commit comments