Skip to content

[StorePathParts] move to nix package #1722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 2 additions & 36 deletions internal/devpkg/narinfo_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"context"
"io"
"net/http"
"strings"
"sync"
"time"
"unicode"

"github.com/pkg/errors"
"go.jetpack.io/devbox/internal/boxcli/featureflag"
Expand Down Expand Up @@ -110,8 +108,8 @@ func (p *Package) fetchNarInfoStatus() (bool, error) {
)
}

pathParts := newStorePathParts(sysInfo.StorePath)
reqURL := BinaryCache + "/" + pathParts.hash + ".narinfo"
pathParts := nix.NewStorePathParts(sysInfo.StorePath)
reqURL := BinaryCache + "/" + pathParts.Hash + ".narinfo"
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodHead, reqURL, nil)
Expand Down Expand Up @@ -181,35 +179,3 @@ func (p *Package) sysInfoIfExists() (*lock.SystemInfo, error) {
}
return sysInfo, nil
}

// storePath are the constituent parts of
// /nix/store/<hash>-<name>-<version>
//
// This is a helper struct for analyzing the string representation
type storePathParts struct {
hash string
name string
version string
}

// newStorePathParts splits a Nix store path into its hash, name and version
// components in the same way that Nix does.
//
// See https://nixos.org/manual/nix/stable/language/builtins.html#builtins-parseDrvName
func newStorePathParts(path string) storePathParts {
path = strings.TrimPrefix(path, "/nix/store/")
// path is now <hash>-<name>-<version

hash, name := path[:32], path[33:]
dashIndex := 0
for i, r := range name {
if dashIndex != 0 && !unicode.IsLetter(r) {
return storePathParts{hash: hash, name: name[:dashIndex], version: name[i:]}
}
dashIndex = 0
if r == '-' {
dashIndex = i
}
}
return storePathParts{hash: hash, name: name}
}
43 changes: 0 additions & 43 deletions internal/devpkg/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,49 +181,6 @@ func TestHashFromNixPkgsURL(t *testing.T) {
}
}

func TestStorePathParts(t *testing.T) {
testCases := []struct {
storePath string
expected storePathParts
}{
// simple case:
{
storePath: "/nix/store/cvrn84c1hshv2wcds7n1rhydi6lacqns-gnumake-4.4.1",
expected: storePathParts{
hash: "cvrn84c1hshv2wcds7n1rhydi6lacqns",
name: "gnumake",
version: "4.4.1",
},
},
// the package name can have dashes:
{
storePath: "/nix/store/q2xdxsswjqmqcbax81pmazm367s7jzyb-cctools-binutils-darwin-wrapper-973.0.1",
expected: storePathParts{
hash: "q2xdxsswjqmqcbax81pmazm367s7jzyb",
name: "cctools-binutils-darwin-wrapper",
version: "973.0.1",
},
},
// version is optional. This is an artificial example I constructed
{
storePath: "/nix/store/gfxwrd5nggc68pjj3g3jhlldim9rpg0p-coreutils",
expected: storePathParts{
hash: "gfxwrd5nggc68pjj3g3jhlldim9rpg0p",
name: "coreutils",
},
},
}

for _, testCase := range testCases {
t.Run(testCase.storePath, func(t *testing.T) {
parts := newStorePathParts(testCase.storePath)
if parts != testCase.expected {
t.Errorf("Expected %v, got %v", testCase.expected, parts)
}
})
}
}

func TestCanonicalName(t *testing.T) {
tests := []struct {
pkgName string
Expand Down
40 changes: 40 additions & 0 deletions internal/nix/storepath.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package nix

import (
"strings"
"unicode"
)

// storePath are the constituent parts of
// /nix/store/<hash>-<name>-<version>
//
// This is a helper struct for analyzing the string representation
type StorePathParts struct {
Hash string
Name string
Version string
}

// NewStorePathParts splits a Nix store path into its hash, name and version
// components in the same way that Nix does.
//
// See https://nixos.org/manual/nix/stable/language/builtins.html#builtins-parseDrvName
//
// TODO: store paths can also have `-{output}` suffixes, which need to be handled below.
func NewStorePathParts(path string) StorePathParts {
path = strings.TrimPrefix(path, "/nix/store/")
// path is now <hash>-<name>-<version

hash, name := path[:32], path[33:]
dashIndex := 0
for i, r := range name {
if dashIndex != 0 && !unicode.IsLetter(r) {
return StorePathParts{Hash: hash, Name: name[:dashIndex], Version: name[i:]}
}
dashIndex = 0
if r == '-' {
dashIndex = i
}
}
return StorePathParts{Hash: hash, Name: name}
}
48 changes: 48 additions & 0 deletions internal/nix/storepath_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package nix

import (
"testing"
)

func TestStorePathParts(t *testing.T) {
testCases := []struct {
storePath string
expected StorePathParts
}{
// simple case:
{
storePath: "/nix/store/cvrn84c1hshv2wcds7n1rhydi6lacqns-gnumake-4.4.1",
expected: StorePathParts{
Hash: "cvrn84c1hshv2wcds7n1rhydi6lacqns",
Name: "gnumake",
Version: "4.4.1",
},
},
// the package name can have dashes:
{
storePath: "/nix/store/q2xdxsswjqmqcbax81pmazm367s7jzyb-cctools-binutils-darwin-wrapper-973.0.1",
expected: StorePathParts{
Hash: "q2xdxsswjqmqcbax81pmazm367s7jzyb",
Name: "cctools-binutils-darwin-wrapper",
Version: "973.0.1",
},
},
// version is optional. This is an artificial example I constructed
{
storePath: "/nix/store/gfxwrd5nggc68pjj3g3jhlldim9rpg0p-coreutils",
expected: StorePathParts{
Hash: "gfxwrd5nggc68pjj3g3jhlldim9rpg0p",
Name: "coreutils",
},
},
}

for _, testCase := range testCases {
t.Run(testCase.storePath, func(t *testing.T) {
parts := NewStorePathParts(testCase.storePath)
if parts != testCase.expected {
t.Errorf("Expected %v, got %v", testCase.expected, parts)
}
})
}
}