computeEnv computes the set of environment variables that define a Devbox environment. The "devbox run" and "devbox shell" commands source these variables into a shell before executing a command or showing an interactive prompt. The process for building the environment involves layering sets of env
( ctx context.Context, usePrintDevEnvCache bool, envOpts devopt.EnvOptions, )
| 702 | // some additional processing. The computeEnv environment won't necessarily |
| 703 | // represent the final "devbox run" or "devbox shell" environments. |
| 704 | func (d *Devbox) computeEnv( |
| 705 | ctx context.Context, |
| 706 | usePrintDevEnvCache bool, |
| 707 | envOpts devopt.EnvOptions, |
| 708 | ) (map[string]string, error) { |
| 709 | defer debug.FunctionTimer().End() |
| 710 | defer trace.StartRegion(ctx, "devboxComputeEnv").End() |
| 711 | |
| 712 | // Append variables from current env if --pure is not passed |
| 713 | currentEnv := os.Environ() |
| 714 | env, err := d.parseEnvAndExcludeSpecialCases(currentEnv, envOpts.Pure) |
| 715 | if err != nil { |
| 716 | return nil, err |
| 717 | } |
| 718 | |
| 719 | // check if contents of .envrc is old and print warning |
| 720 | if !usePrintDevEnvCache { |
| 721 | err := d.checkOldEnvrc() |
| 722 | if err != nil { |
| 723 | return nil, err |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | slog.Debug("current environment PATH", "path", env["PATH"]) |
| 728 | |
| 729 | originalEnv := make(map[string]string, len(env)) |
| 730 | maps.Copy(originalEnv, env) |
| 731 | |
| 732 | if !envOpts.OmitNixEnv { |
| 733 | nixEnv, err := d.execPrintDevEnv(ctx, usePrintDevEnvCache) |
| 734 | if err != nil { |
| 735 | return nil, err |
| 736 | } |
| 737 | |
| 738 | for k, v := range nixEnv { |
| 739 | env[k] = v |
| 740 | } |
| 741 | } |
| 742 | slog.Debug("nix environment PATH", "path", env["PATH"]) |
| 743 | |
| 744 | env["PATH"] = envpath.JoinPathLists( |
| 745 | nix.ProfileBinPath(d.projectDir), |
| 746 | env["PATH"], |
| 747 | ) |
| 748 | |
| 749 | wd, err := os.Getwd() |
| 750 | if err != nil { |
| 751 | return nil, err |
| 752 | } |
| 753 | |
| 754 | // Add helpful env vars for a Devbox project |
| 755 | env["DEVBOX_PROJECT_ROOT"] = d.projectDir |
| 756 | env["DEVBOX_WD"] = wd |
| 757 | env["DEVBOX_CONFIG_DIR"] = d.projectDir + "/devbox.d" |
| 758 | env["DEVBOX_PACKAGES_DIR"] = d.projectDir + "/" + nix.ProfilePath |
| 759 | |
| 760 | // Include env variables in devbox.json |
| 761 | configEnv, err := d.configEnvs(ctx, env) |