Skip to content

Commit 14e5ea1

Browse files
authored
Adds code to call nix print-dev-env (#456)
## Summary This simply adds the ability to call `nix print-dev-env -f <file>`, parses the result and returns it. The idea is to then use the output of this to implement a better (read faster and more isolated) version of `devbox run` and eventually `devbox shell`. The output can also be used for a more correct implementation of `devbox shell --print-env` (albeit slower). ## How was it tested? Used it for experimental implementation of `devbox shell` that relies on it. Will send that on a separate PR.
1 parent 11fba11 commit 14e5ea1

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

internal/nix/nix.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,34 @@ func parseInfo(pkg string, data []byte) *Info {
9191
func DefaultEnv() []string {
9292
return append(os.Environ(), "NIXPKGS_ALLOW_UNFREE=1")
9393
}
94+
95+
type varsAndFuncs struct {
96+
Functions map[string]string // the key is the name, the value is the body.
97+
Variables map[string]variable // the key is the name.
98+
}
99+
type variable struct {
100+
Type string // valid types are var, exported, and array.
101+
Value any // can be a string or an array of strings (iff type is array).
102+
}
103+
104+
// PrintDevEnv calls `nix print-dev-env -f <path>` and returns its output. The output contains
105+
// all the environment variables and bash functions required to create a nix shell.
106+
func PrintDevEnv(nixFilePath string) (*varsAndFuncs, error) {
107+
cmd := exec.Command("nix", "print-dev-env",
108+
"-f", nixFilePath,
109+
"--extra-experimental-features", "nix-command",
110+
"--extra-experimental-features", "ca-derivations",
111+
"--option", "experimental-features", "nix-command flakes",
112+
"--json")
113+
out, err := cmd.Output()
114+
if err != nil {
115+
return nil, errors.WithStack(err)
116+
}
117+
118+
var vaf varsAndFuncs
119+
err = json.Unmarshal(out, &vaf)
120+
if err != nil {
121+
return nil, errors.WithStack(err)
122+
}
123+
return &vaf, nil
124+
}

0 commit comments

Comments
 (0)