Skip to content
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

pkg/nixpath: move to pkg/storepath, make FromString receive non-absolute paths #113

Merged
merged 2 commits into from
Oct 5, 2023

Conversation

flokli
Copy link
Collaborator

@flokli flokli commented Oct 5, 2023

The whole module layout was a bit messy - store paths had to be passed in primarily with the store dir prefix, even though it's not possible to have a different prefix, at least with the way it's currently structured.

There were Absolute() helper functions which helped with concatenation of strings, but no actual validation, which also were not really used outside of tests. The comments were also wrong, as no cleaning of store paths actually did happen.
Some library code (like pkg/derivation) even started implementing their own String() methods.

Clean this up a bit - have NixPath's FromString() receive a non-absolute path, and have an additional FromAbsolutePath() method that can be used if you have an absolute path.

Move from nixpath.Absolute() to a Absolute() function defined on a specific NixPath struct.

This moves things closer to how they look in the corresponding nix_compat::store_path::StorePath rust codebase.

The whole module layout was a bit messy - store paths had to be passed
in primarily with the store dir prefix, even though it's not possible to
have a different prefix, at least with the way it's currently structured.

There were Absolute() helper functions which helped with concatenation
of strings, but no actual validation, which also were not really used
outside of tests. The comments were also wrong, as no cleaning of store
paths actually did happen.
Some library code (like pkg/derivation) even started implementing their
own String() methods.

Clean this up a bit - have NixPath's FromString() receive a non-absolute
path, and have an additional FromAbsolutePath() method that can be used
if you have an absolute path.

Move from nixpath.Absolute() to a Absolute() function defined on a
specific NixPath struct.

This moves things closer to how they look in the corresponding
`nix_compat::store_path::StorePath` rust codebase.
Copy link
Member

@zimbatm zimbatm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eventually, we'll have to make StoreDir configurable

Comment on lines 43 to 48
// String returns a NixPath without StoreDir.
// It starts with a digest (20 bytes), nixbase32-encoded,
// followed by a `-`, and ends with the name.
func (n *NixPath) String() string {
return Absolute(nixbase32.EncodeToString(n.Digest) + "-" + n.Name)
return nixbase32.EncodeToString(n.Digest) + "-" + n.Name
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks back-compat; some users might assume that they will get an absolute path here.

Maybe the best is to rename or remove this method?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it does break back-compat, but it'd be quite quick to notice, and matches the behaviour in the nix-compat crate (where impl Display also shows the non-absolute string). Similarly, String() is what gets called if you %s things in a Printf statement, for example.

The repo is marked with a "STATUS: experimental", so while it is, breaking compat is fine.

@flokli
Copy link
Collaborator Author

flokli commented Oct 5, 2023

Eventually, we'll have to make StoreDir configurable

This is easier said than done - we'd need to carry a pointer to the store dir used for individual store paths, and verify compatibility on every consumer side, because the store path would need to be compatible with whatever that consumer is doing with it.

For now, it's a build-time constant.

This prevents silently changing behaviour of the NixPath.String()
function, which might cause some debugging for users of the library
otherwise, and aligns the package name with how it's called in the nix-
compat crate too.
@flokli
Copy link
Collaborator Author

flokli commented Oct 5, 2023

As @zimbatm mentioned, nixpath.String() suddenly changing behaviour without warning is annoying and a footgun.

I renamed the package to storepath entirely, which will force users to explicitly upgrade (and look at the docstrings for the new package), hopefully keeping the blast radius smaller.

It's not perfect, but better than silently returning different strings on a go mod update.

@flokli flokli changed the title pkg/nixpath: make FromString receive non-absolute paths pkg/nixpath: move to pkg/storepath, make FromString receive non-absolute paths Oct 5, 2023
@flokli flokli merged commit b5bfc1f into main Oct 5, 2023
8 checks passed
@flokli flokli deleted the nixpath-relative branch October 5, 2023 14:36
tvlbot pushed a commit to tvlfyi/tvix that referenced this pull request Oct 5, 2023
nixpath.FromString -> storepath.FromAbsolutePath.

See nix-community/go-nix#113 for details.

Closes: https://b.tvl.fyi/issues/314
Change-Id: I25277fb6006cbbb2a323ffb5809a1be500822a97
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9551
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Reviewed-by: Connor Brewster <cbrewster@hey.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants