Skip to content

Commit 3905f67

Browse files
committed
add more installable workarounds
1 parent 1385a38 commit 3905f67

File tree

5 files changed

+42
-15
lines changed

5 files changed

+42
-15
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,36 @@ 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, _ := flake.ParseInstallable(args[i])
276+
if parsed.Ref.NARHash == "" && parsed.Ref.LastModified == 0 {
277+
continue
278+
}
279+
if parsed.Ref.NARHash != "" && parsed.Ref.LastModified != 0 {
280+
continue
281+
}
282+
283+
parsed.Ref.NARHash = ""
284+
parsed.Ref.LastModified = 0
285+
args[i] = parsed.String()
286+
}
287+
}
288+
289+
// fixInstallableArg calls fixInstallableArgs with a single argument.
290+
func FixInstallableArg(arg string) string {
291+
args := []string{arg}
292+
FixInstallableArgs(args)
293+
return args[0]
294+
}

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 {

internal/shellgen/flake_input.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/samber/lo"
1212
"go.jetpack.io/devbox/internal/devpkg"
13+
"go.jetpack.io/devbox/internal/nix"
1314
"go.jetpack.io/devbox/nix/flake"
1415
)
1516

@@ -43,7 +44,7 @@ func (f *flakeInput) HashFromNixPkgsURL() string {
4344

4445
func (f *flakeInput) URLWithCaching() string {
4546
if !f.IsNixpkgs() {
46-
return f.Ref.String()
47+
return nix.FixInstallableArg(f.Ref.String())
4748
}
4849
return getNixpkgsInfo(f.Ref.Rev).URL
4950
}

0 commit comments

Comments
 (0)