diff options
Diffstat (limited to 'tvix/nar-bridge')
-rw-r--r-- | tvix/nar-bridge/pkg/http/narinfo.go | 49 | ||||
-rw-r--r-- | tvix/nar-bridge/pkg/http/narinfo_get.go | 38 |
2 files changed, 52 insertions, 35 deletions
diff --git a/tvix/nar-bridge/pkg/http/narinfo.go b/tvix/nar-bridge/pkg/http/narinfo.go new file mode 100644 index 000000000000..5d963c34f91d --- /dev/null +++ b/tvix/nar-bridge/pkg/http/narinfo.go @@ -0,0 +1,49 @@ +package http + +import ( + "fmt" + + storev1pb "code.tvl.fyi/tvix/store/protos" + nixhash "github.com/nix-community/go-nix/pkg/hash" + "github.com/nix-community/go-nix/pkg/narinfo" + "github.com/nix-community/go-nix/pkg/narinfo/signature" + "github.com/nix-community/go-nix/pkg/nixbase32" +) + +// ToNixNarInfo converts the PathInfo to a narinfo.NarInfo. +func ToNixNarInfo(p *storev1pb.PathInfo) (*narinfo.NarInfo, error) { + // ensure the PathInfo is valid, and extract the StorePath from the node in + // there. + storePath, err := p.Validate() + if err != nil { + return nil, fmt.Errorf("failed to validate PathInfo: %w", err) + } + + // convert the signatures from storev1pb signatures to narinfo signatures + narinfoSignatures := make([]signature.Signature, len(p.GetNarinfo().GetSignatures())) + for i, pathInfoSignature := range p.GetNarinfo().GetSignatures() { + narinfoSignatures[i] = signature.Signature{ + Name: pathInfoSignature.GetName(), + Data: pathInfoSignature.GetData(), + } + } + + // produce nixhash for the narsha256. + narHash, err := nixhash.FromHashTypeAndDigest( + 0x12, // SHA2_256 + p.GetNarinfo().GetNarSha256(), + ) + if err != nil { + return nil, fmt.Errorf("invalid narsha256: %w", err) + } + + return &narinfo.NarInfo{ + StorePath: storePath.Absolute(), + URL: "nar/" + nixbase32.EncodeToString(narHash.Digest()) + ".nar", + Compression: "none", + NarHash: narHash, + NarSize: uint64(p.GetNarinfo().GetNarSize()), + References: p.GetNarinfo().GetReferenceNames(), + Signatures: narinfoSignatures, + }, nil +} diff --git a/tvix/nar-bridge/pkg/http/narinfo_get.go b/tvix/nar-bridge/pkg/http/narinfo_get.go index 93192c03e52e..8338d43676f5 100644 --- a/tvix/nar-bridge/pkg/http/narinfo_get.go +++ b/tvix/nar-bridge/pkg/http/narinfo_get.go @@ -8,18 +8,13 @@ import ( "io" "io/fs" "net/http" - "path" "strings" "sync" - castorev1pb "code.tvl.fyi/tvix/castore/protos" storev1pb "code.tvl.fyi/tvix/store/protos" "github.com/go-chi/chi/v5" nixhash "github.com/nix-community/go-nix/pkg/hash" - "github.com/nix-community/go-nix/pkg/narinfo" - "github.com/nix-community/go-nix/pkg/narinfo/signature" "github.com/nix-community/go-nix/pkg/nixbase32" - "github.com/nix-community/go-nix/pkg/storepath" log "github.com/sirupsen/logrus" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -71,37 +66,10 @@ func renderNarinfo( return nil } - // convert the signatures from storev1pb signatures to narinfo signatures - narinfoSignatures := make([]signature.Signature, 0) - for _, pathInfoSignature := range pathInfo.Narinfo.Signatures { - narinfoSignatures = append(narinfoSignatures, signature.Signature{ - Name: pathInfoSignature.GetName(), - Data: pathInfoSignature.GetData(), - }) - } - - // extract the name of the node in the pathInfo structure, which will become the output path - var nodeName []byte - switch v := (pathInfo.GetNode().GetNode()).(type) { - case *castorev1pb.Node_File: - nodeName = v.File.GetName() - case *castorev1pb.Node_Symlink: - nodeName = v.Symlink.GetName() - case *castorev1pb.Node_Directory: - nodeName = v.Directory.GetName() - } - - narInfo := narinfo.NarInfo{ - StorePath: path.Join(storepath.StoreDir, string(nodeName)), - URL: "nar/" + nixbase32.EncodeToString(narHash.Digest()) + ".nar", - Compression: "none", // TODO: implement zstd compression - NarHash: narHash, - NarSize: uint64(pathInfo.Narinfo.NarSize), - References: pathInfo.Narinfo.GetReferenceNames(), - Signatures: narinfoSignatures, - } + // convert the PathInfo to NARInfo. + narInfo, err := ToNixNarInfo(pathInfo) - // render .narinfo from pathInfo + // Write it out to the client. _, err = io.Copy(w, strings.NewReader(narInfo.String())) if err != nil { return fmt.Errorf("unable to write narinfo to client: %w", err) |