Skip to content

devpkg: better flake references and installable parsing #1581

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 5 commits into from
Oct 30, 2023
Merged

Conversation

gcurtis
Copy link
Collaborator

@gcurtis gcurtis commented Oct 23, 2023

In anticipation of adding support for non-default package outputs, I wanted to clean up some of the flakeref parsing in devpkg. This will make it easier to handle Nix's flakeref#attrpath^outputs installable syntax. This PR is broken up into two commits: one that handles flakeref parsing and one that handles installable parsing.

The ParseFlakeRef function is able to parse the more common flakerefs that devbox currently supports ("path", "flake", "indirect", etc.). It doesn't support mercurial, gitlab, and sourcehuthut for now. The ParseFlakeInstallable function parses Nix flake installables, which are a superset of the flake reference syntax. It allows an attribute path and/or outputs to be specified using the flake#attrpath^outputs syntax. The tests were validated using the nix expression builtins.parseFlakeRef "ref" and the nix flake metadata <ref> CLI.

The goal isn't to perfectly replicate Nix's parsing of flake references (which itself is sometimes inconsistent), but to make it good enough to handle flakerefs in devbox.json.

In anticipation of adding support for non-default package outputs, I
wanted to clean up some of the flakeref parsing in `devpkg`. This will
make it easier to handle Nix's `flakeref#attrpath^outputs` installable
syntax.

The `ParseFlakeRef` function is able to parse the more common flakerefs
that devbox currently supports ("path", "flake", "indirect", etc.). It
doesn't support mercurial, gitlab, and sourcehuthut for now. The tests
were validated using the nix expression `builtins.parseFlakeRef "ref"`
and the `nix flake metadata <ref>` CLI.

The goal isn't to perfectly replicate Nix's parsing of flake references
(which itself is sometimes inconsistent), but to make it good enough to
handle flakerefs in devbox.json.
Add a `ParseFlakeInstallable` to parse Nix flake installables. An
installable is a superset of a flake reference. It allows an attribute
path and/or outputs to be specified using the `flake#attrpath^outputs`
syntax.
Signed-off-by: Greg Curtis <[email protected]>
Copy link
Collaborator

@savil savil left a comment

Choose a reason for hiding this comment

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

This is great! What is the plan to integrate it with the rest of the code?

I wanted to clean up some of the flakeref parsing in devpkg. This will make it easier to handle Nix's flakeref#attrpath^outputs installable syntax. This PR is broken up into two commits: one that handles flakeref parsing and one that handles installable parsing.

Is the expectation that users would directly run devbox add flakeref#attrpath^outputs? If so, I worry that is a complicated string for our users. We can find a simpler way.

On the other hand, if we're saying the user's commands will get translated by Devbox CLI code into being of the form flakeref#attrpath^outputs and then this makes sense to me.

This product discussion is probably better in a different forum than this PR. Happy to chat elsewhere more :)

"go.jetpack.io/devbox/internal/redact"
)

// FlakeRef is a parsed Nix flake reference. A flake reference is as subset of
Copy link
Collaborator

Choose a reason for hiding this comment

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

...is a subset of...

Rev string `json:"rev,omitempty"`
Ref string `json:"ref,omitempty"`

// Dir is non-empty when the directory containinig the flake.nix file is
Copy link
Collaborator

Choose a reason for hiding this comment

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

typo: containing

func parseFlakeURLRef(ref string) (parsed FlakeRef, fragment string, err error) {
// A good way to test how Nix parses a flake reference is to run:
//
// nix eval --json --expr 'builtins.parseFlakeRef "ref"' | jq
Copy link
Collaborator

Choose a reason for hiding this comment

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

good pro-tip 👍🏾

@gcurtis
Copy link
Collaborator Author

gcurtis commented Oct 25, 2023

It's the second one. For handling the flake-like syntax supported in devbox.json (which users have to add manually).

@gcurtis gcurtis merged commit 5a94b6e into main Oct 30, 2023
@gcurtis gcurtis deleted the gcurtis/flakeref branch October 30, 2023 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants