diff --git a/pkg/cache/upstream/cache.go b/pkg/cache/upstream/cache.go index f99d6c98..182c80aa 100644 --- a/pkg/cache/upstream/cache.go +++ b/pkg/cache/upstream/cache.go @@ -381,10 +381,6 @@ func (c *Cache) GetNarInfo(ctx context.Context, hash string) (*narinfo.NarInfo, ni.FileSize = ni.NarSize } - // nix-serve serves the NarURL with the narinfo hash included in the URL as - // prefix to the NAR Hash. Trim that out, we don't need it. - ni.URL = strings.ReplaceAll(ni.URL, hash+"-", "") - return ni, nil } diff --git a/testdata/nar7.go b/testdata/nar7.go index 6c1a5b13..de86ca4d 100644 --- a/testdata/nar7.go +++ b/testdata/nar7.go @@ -14,7 +14,7 @@ var Nar7 = Entry{ NarInfoHash: "c12lxpykv6sld7a0sakcnr3y0la70x8w", NarInfoPath: filepath.Join("c", "c1", "c12lxpykv6sld7a0sakcnr3y0la70x8w.narinfo"), NarInfoText: `StorePath: /nix/store/c12lxpykv6sld7a0sakcnr3y0la70x8w-hello-2.12.2 -URL: nar/c12lxpykv6sld7a0sakcnr3y0la70x8w-09xizkfyvigl5fqs0dhkn46nghfwwijbpdzzl4zg6kx90prjmsg0.nar +URL: nar/09xizkfyvigl5fqs0dhkn46nghfwwijbpdzzl4zg6kx90prjmsg0.nar Compression: none NarHash: sha256:1yf3p87fsqig07crd9sj9wh7i9jpsa0x86a22fqbls7c81lc7ws2 NarSize: 113256 diff --git a/testdata/server.go b/testdata/server.go index 8ff05bb9..6ef7a249 100644 --- a/testdata/server.go +++ b/testdata/server.go @@ -12,6 +12,7 @@ import ( "github.com/klauspost/compress/zstd" "github.com/kalbasit/ncps/pkg/helper" + "github.com/kalbasit/ncps/pkg/nar" ) type Server struct { @@ -122,14 +123,43 @@ func (s *Server) handler() http.Handler { bs = []byte(entry.NarInfoText) } + // Support fetching narinfo by NAR hash (used by cache when only NAR hash is available) + if r.URL.Path == "/"+entry.NarHash+".narinfo" { + bs = []byte(entry.NarInfoText) + } + if r.URL.Path == "/nar/"+entry.NarHash+".nar" { bs = []byte(entry.NarText) } - if r.URL.Path == "/nar/"+entry.NarHash+".nar."+entry.NarCompression.ToFileExtension() { + // Build path with compression extension, only adding dot if extension is not empty + narPath := "/nar/" + entry.NarHash + ".nar" + if ext := entry.NarCompression.ToFileExtension(); ext != "" { + narPath += "." + ext + } + + if r.URL.Path == narPath { bs = []byte(entry.NarText) } + // Support fetching by normalized hash (with prefix stripped) + normalizedHash := (&nar.URL{Hash: entry.NarHash}).Normalize().Hash + if normalizedHash != entry.NarHash { + if r.URL.Path == "/nar/"+normalizedHash+".nar" { + bs = []byte(entry.NarText) + } + + // Build normalized path with compression extension + normalizedNarPath := "/nar/" + normalizedHash + ".nar" + if ext := entry.NarCompression.ToFileExtension(); ext != "" { + normalizedNarPath += "." + ext + } + + if r.URL.Path == normalizedNarPath { + bs = []byte(entry.NarText) + } + } + if len(bs) > 0 { if s := r.URL.Query().Get("fakesize"); s != "" { size, err := strconv.Atoi(s)