Skip to content

Commit 7ba7e8d

Browse files
authored
[nix] create common ExperimentalFlags function (#678)
## Summary I noticed that print-dev-env and nix profile commands were using a different set of nix experimental features. I think we should unify these so that they're writing to the store in a common fashion. **Implementation Note** It appears that: `"--extra-experimental-features", "nix-command flakes"`, is different from `"--option", "experimental-features", "nix-command flakes"` for our `nix print-dev-env` testscripts. We must have the latter. The former is insufficient and not needed. This confuses me because the docs suggest that `--extra-experimental-features` should append these experimental features, whereas `--option` replaces the experimental features: > --option name value Set the Nix configuration setting name to value (overriding nix.conf). and > --extra-experimental-features value Append to the experimental-features setting. Concretely, this works: ``` "--extra-experimental-features", "ca-derivations", "--option", "experimental-features", "nix-command flakes", ``` but this does not work: ``` "--option", "experimental-features", "ca-derivations nix-command flakes", ``` and nor does this: ``` "--extra-experimental-features", "nix-command flakes", "--extra-experimental-features", "ca-derivations", ``` ## How was it tested? `go test ./...` in `examples/testdata/go/go-1.19` with flakes feature flag on for all commands: ``` > devbox shell (devbox)> devbox add vim emacs (devbox)> source activate shell... (devbox)> devbox rm emacs ```
1 parent 2b77105 commit 7ba7e8d

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

internal/impl/packages.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ func (d *Devbox) addPackagesToProfile(mode installMode) error {
8686
cmd := exec.Command(
8787
"nix", "profile", "install",
8888
"--profile", profileDir,
89-
"--extra-experimental-features", "nix-command flakes",
9089
"--impure", // Needed to allow flags from environment to be used.
9190
nix.FlakeNixpkgs(d.cfg.Nixpkgs.Commit)+"#"+pkg,
9291
)
92+
cmd.Args = append(cmd.Args, nix.ExperimentalFlags()...)
9393
cmd.Stdout = &nixPackageInstallWriter{d.writer}
9494

9595
cmd.Env = nix.DefaultEnv()
@@ -145,9 +145,9 @@ func (d *Devbox) removePackagesFromProfile(pkgs []string) error {
145145

146146
cmd := exec.Command("nix", "profile", "remove",
147147
"--profile", profileDir,
148-
"--extra-experimental-features", "nix-command flakes",
149148
attrPath,
150149
)
150+
cmd.Args = append(cmd.Args, nix.ExperimentalFlags()...)
151151
cmd.Stdout = d.writer
152152
cmd.Stderr = d.writer
153153
err = cmd.Run()
@@ -231,9 +231,9 @@ func (d *Devbox) ensureNixpkgsPrefetched() error {
231231
fmt.Fprintf(d.writer, "Ensuring nixpkgs registry is downloaded.\n")
232232
cmd := exec.Command(
233233
"nix", "flake", "prefetch",
234-
"--extra-experimental-features", "nix-command flakes",
235234
nix.FlakeNixpkgs(d.cfg.Nixpkgs.Commit),
236235
)
236+
cmd.Args = append(cmd.Args, nix.ExperimentalFlags()...)
237237
cmd.Env = nix.DefaultEnv()
238238
cmd.Stdout = d.writer
239239
cmd.Stderr = cmd.Stdout

internal/nix/nix.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,8 @@ func flakesPkgInfo(nixpkgsCommit, pkg string) (*Info, bool) {
8787
exactPackage = fmt.Sprintf("nixpkgs#%s", pkg)
8888
}
8989

90-
cmd := exec.Command("nix", "search",
91-
"--extra-experimental-features", "nix-command flakes",
92-
"--json", exactPackage)
90+
cmd := exec.Command("nix", "search", "--json", exactPackage)
91+
cmd.Args = append(cmd.Args, ExperimentalFlags()...)
9392
return pkgInfo(cmd, pkg)
9493
}
9594

@@ -150,12 +149,8 @@ func PrintDevEnv(nixShellFilePath, nixFlakesFilePath string) (*varsAndFuncs, err
150149
} else {
151150
cmd.Args = append(cmd.Args, "-f", nixShellFilePath)
152151
}
153-
cmd.Args = append(cmd.Args,
154-
"--extra-experimental-features", "nix-command",
155-
"--extra-experimental-features", "ca-derivations",
156-
"--option", "experimental-features", "nix-command flakes",
157-
"--impure",
158-
"--json")
152+
cmd.Args = append(cmd.Args, ExperimentalFlags()...)
153+
cmd.Args = append(cmd.Args, "--impure", "--json")
159154
debug.Log("Running print-dev-env cmd: %s\n", cmd)
160155
cmd.Env = DefaultEnv()
161156
out, err := cmd.Output()
@@ -178,3 +173,10 @@ func FlakeNixpkgs(commit string) string {
178173
// The nixpkgs entry in the flake registry, with its Git revision overridden to a specific value.
179174
return "nixpkgs/" + commit
180175
}
176+
177+
func ExperimentalFlags() []string {
178+
return []string{
179+
"--extra-experimental-features", "ca-derivations",
180+
"--option", "experimental-features", "nix-command flakes",
181+
}
182+
}

internal/nix/profile.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ func ProfileListItems(writer io.Writer, profileDir string) ([]*NixProfileListIte
2121

2222
cmd := exec.Command(
2323
"nix", "profile", "list",
24-
"--extra-experimental-features", "nix-command flakes",
2524
"--profile", profileDir,
2625
)
26+
cmd.Args = append(cmd.Args, ExperimentalFlags()...)
2727

2828
// We set stderr to a different output than stdout
2929
// to ensure error output is not mingled with the stdout output
@@ -188,8 +188,8 @@ func ProfileInstall(profilePath, nixpkgsCommit, pkg string) error {
188188
cmd := exec.Command("nix", "profile", "install",
189189
"--profile", profilePath,
190190
"nixpkgs/"+nixpkgsCommit+"#"+pkg,
191-
"--extra-experimental-features", "nix-command flakes",
192191
)
192+
cmd.Args = append(cmd.Args, ExperimentalFlags()...)
193193
cmd.Env = DefaultEnv()
194194
out, err := cmd.CombinedOutput()
195195
if bytes.Contains(out, []byte("does not provide attribute")) {
@@ -207,8 +207,8 @@ func ProfileRemove(profilePath, nixpkgsCommit, pkg string) error {
207207
cmd := exec.Command("nix", "profile", "remove",
208208
"--profile", profilePath,
209209
info.attributeKey,
210-
"--extra-experimental-features", "nix-command flakes",
211210
)
211+
cmd.Args = append(cmd.Args, ExperimentalFlags()...)
212212
cmd.Env = DefaultEnv()
213213
out, err := cmd.CombinedOutput()
214214
if bytes.Contains(out, []byte("does not match any packages")) {

0 commit comments

Comments
 (0)