Skip to content

Commit 4dbebb6

Browse files
committed
add more installable workarounds
1 parent 1385a38 commit 4dbebb6

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

internal/nix/build.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ type BuildArgs struct {
2020

2121
func Build(ctx context.Context, args *BuildArgs, installables ...string) error {
2222
defer debug.FunctionTimer().End()
23+
24+
fixInstallableArgs(installables)
25+
2326
// --impure is required for allowUnfreeEnv/allowInsecureEnv to work.
2427
cmd := command("build", "--impure")
2528
cmd.Args = appendArgs(cmd.Args, args.Flags)

internal/nix/nix.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,31 @@ func restartDaemon(ctx context.Context) error {
259259
time.Sleep(2 * time.Second)
260260
return nil
261261
}
262+
263+
// fixInstallableArgs removes the narHash and lastModifiedDate query parameters
264+
// from any args that are valid installables and the Nix version is <2.25.
265+
// Otherwise it returns them unchanged.
266+
//
267+
// This fixes an issues with some older versions of Nix where specifying a
268+
// narHash without a lastModifiedDate results in an error.
269+
func fixInstallableArgs(args []string) {
270+
if AtLeast(Version2_25) {
271+
return
272+
}
273+
274+
for i := range args {
275+
parsed, err := flake.ParseInstallable(args[i])
276+
if err == nil {
277+
parsed.Ref.NARHash = ""
278+
parsed.Ref.LastModified = 0
279+
args[i] = parsed.String()
280+
}
281+
}
282+
}
283+
284+
// fixInstallableArg calls fixInstallableArgs with a single argument.
285+
func fixInstallableArg(arg string) string {
286+
args := []string{arg}
287+
fixInstallableArgs(args)
288+
return args[0]
289+
}

internal/nix/profiles.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func ProfileInstall(ctx context.Context, args *ProfileInstallArgs) error {
5252
"--priority", nextPriority(args.ProfilePath),
5353
)
5454

55+
fixInstallableArgs(args.Installables)
5556
cmd.Args = appendArgs(cmd.Args, args.Installables)
5657
cmd.Env = allowUnfreeEnv(os.Environ())
5758

@@ -75,6 +76,8 @@ func ProfileRemove(profilePath string, packageNames ...string) error {
7576
"--profile", profilePath,
7677
"--impure", // for NIXPKGS_ALLOW_UNFREE
7778
)
79+
80+
fixInstallableArgs(packageNames)
7881
cmd.Args = appendArgs(cmd.Args, packageNames)
7982
cmd.Env = allowUnfreeEnv(allowInsecureEnv(os.Environ()))
8083
return cmd.Run(context.TODO())

internal/nix/store.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"go.jetpack.io/devbox/internal/debug"
1414
"go.jetpack.io/devbox/internal/redact"
1515
"go.jetpack.io/devbox/nix"
16-
"go.jetpack.io/devbox/nix/flake"
1716
"golang.org/x/exp/maps"
1817
)
1918

@@ -29,20 +28,8 @@ func StorePathFromHashPart(ctx context.Context, hash, storeAddr string) (string,
2928
func StorePathsFromInstallable(ctx context.Context, installable string, allowInsecure bool) ([]string, error) {
3029
defer debug.FunctionTimer().End()
3130

32-
// Some older versions of Nix have a bug where specifying a narHash
33-
// without a lastModifiedDate query parameter results in an error. I'm
34-
// not sure when it was fixed, but I know it works in 2.25+.
35-
if !nix.AtLeast(nix.Version2_25) {
36-
parsed, err := flake.ParseInstallable(installable)
37-
if err == nil {
38-
parsed.Ref.NARHash = ""
39-
parsed.Ref.LastModified = 0
40-
installable = parsed.String()
41-
}
42-
}
43-
4431
// --impure for NIXPKGS_ALLOW_UNFREE
45-
cmd := command("path-info", installable, "--json", "--impure")
32+
cmd := command("path-info", fixInstallableArg(installable), "--json", "--impure")
4633
cmd.Env = allowUnfreeEnv(os.Environ())
4734

4835
if allowInsecure {

0 commit comments

Comments
 (0)