-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
nix-build: add initial support #1480
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
thatsmydoing
commented
Jan 15, 2023
thatsmydoing
force-pushed
the
nix-build
branch
from
January 15, 2023 09:56
29cf307
to
cdae3db
Compare
rsteube
reviewed
Jan 15, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some ideas. With the struct it can be reused as macro: carapace --macros 'tools.nix.Attributes({source: "<nixos>"})' <TAB>
diff --git a/completers/nix-build_completer/cmd/root.go b/completers/nix-build_completer/cmd/root.go
index 65381edd..45a438b4 100644
--- a/completers/nix-build_completer/cmd/root.go
+++ b/completers/nix-build_completer/cmd/root.go
@@ -26,14 +26,18 @@ func init() {
rootCmd.Flags().StringP("attr", "A", "", "Attribute to build")
// TODO support --arg and --argstr
- carapace.Gen(rootCmd).PositionalAnyCompletion(nix.ActionPaths())
carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{
- "attr": nix.ActionAttr(rootCmd, func(_ *cobra.Command, c carapace.Context) string {
+ "attr": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
+ opts := nix.AttributeOpts{Source: "default.nix", Include: rootCmd.Flag("include").Value.String()}
if len(c.Args) > 0 {
- return c.Args[0]
- } else {
- return "default.nix"
+ opts.Source = c.Args[0]
}
+ return nix.ActionAttributes(opts)
}),
})
+
+ carapace.Gen(rootCmd).PositionalAnyCompletion(
+ nix.ActionPaths(),
+ )
+
}
diff --git a/pkg/actions/tools/nix/attr.go b/pkg/actions/tools/nix/attribute.go
similarity index 61%
rename from pkg/actions/tools/nix/attr.go
rename to pkg/actions/tools/nix/attribute.go
index 921bf2b6..3dd15fd8 100644
--- a/pkg/actions/tools/nix/attr.go
+++ b/pkg/actions/tools/nix/attribute.go
@@ -8,41 +8,47 @@ import (
"strings"
"github.com/rsteube/carapace"
- "github.com/spf13/cobra"
)
-//go:embed attrComplete.nix
-var attrCompleteScript string
+//go:embed attributeComplete.nix
+var attributeCompleteScript string
-func ActionAttr(cmd *cobra.Command, getSource func(*cobra.Command, carapace.Context) string) carapace.Action {
+type AttributeOpts struct {
+ Source string
+ Include string
+}
+
+// ActionAttributes completes attributes
+//
+// firefox
+// git
+func ActionAttributes(opts AttributeOpts) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
- attrPath := c.CallbackValue
- nixPath := ""
- if cmd.Flag("include").Changed {
- nixPath = cmd.Flag("include").Value.String()
+ if opts.Source == "" {
+ opts.Source = "default.nix"
}
- source := getSource(cmd, c)
+ attrPath := c.CallbackValue
- if strings.HasPrefix(source, "<") {
+ if strings.HasPrefix(opts.Source, "<") {
// expression is a local channel, use as-is
- } else if strings.HasPrefix(source, "http:") || strings.HasPrefix(source, "https:") {
+ } else if strings.HasPrefix(opts.Source, "http:") || strings.HasPrefix(opts.Source, "https:") {
// expression is a url, wrap in fetchTarball
- source = fmt.Sprintf("(fetchTarball %s)", source)
- } else if strings.HasPrefix(source, "channel:") {
+ opts.Source = fmt.Sprintf("(fetchTarball %s)", opts.Source)
+ } else if strings.HasPrefix(opts.Source, "channel:") {
// expression is a channel alias, convert to url
- channelName := source[8:]
- source = fmt.Sprintf("(fetchTarball https://nixos.org/channels/%s/nixexprs.tar.xz)", channelName)
+ channelName := opts.Source[8:]
+ opts.Source = fmt.Sprintf("(fetchTarball https://nixos.org/channels/%s/nixexprs.tar.xz)", channelName)
} else {
// expression is a local file
- filePath := path.Clean(source)
+ filePath := path.Clean(opts.Source)
if path.Base(filePath) == filePath {
// paths must have at least one `/`
filePath = fmt.Sprintf("./%s", filePath)
}
- source = filePath
+ opts.Source = filePath
}
- expression := fmt.Sprintf("import %s", source)
+ expression := fmt.Sprintf("import %s", opts.Source)
// nix itself handles quotes weirdly so we just avoid them altogether
if strings.Contains(attrPath, "\"") {
@@ -59,12 +65,12 @@ func ActionAttr(cmd *cobra.Command, getSource func(*cobra.Command, carapace.Cont
args := []string{
"--eval", "--json",
- "--expr", attrCompleteScript,
+ "--expr", attributeCompleteScript,
"--arg", "__carapaceInput__", expression,
"--argstr", "__carapaceAttrPath__", attrPath,
}
- if nixPath != "" {
- args = append(args, "-I", nixPath)
+ if opts.Include != "" {
+ args = append(args, "-I", opts.Include)
}
// TODO handle passing through --arg and --argstr from original command
diff --git a/pkg/actions/tools/nix/attrComplete.nix b/pkg/actions/tools/nix/attributeComplete.nix
similarity index 100%
rename from pkg/actions/tools/nix/attrComplete.nix
rename to pkg/actions/tools/nix/attributeComplete.nix
thatsmydoing
force-pushed
the
nix-build
branch
from
January 15, 2023 16:48
129a7f2
to
49ac640
Compare
I think I'm happy with this. I'll play with it a bit and maybe make a PR for |
rsteube
approved these changes
Jan 15, 2023
thanks |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This adds support for
nix-build
. The primary completion here is for attrs with the-A
parameter and positional completion for a "source". This is based on the code from https://github.com/spwhitt/nix-zsh-completionsThis works with the notable exception of not supporting
--arg
and--argstr
. Those flags take two arguments, one for key and one for value and it seems carapace doesn't support this at all. I looked at other commands and it seems for similar commands they just treat them as string or boolean flags and leave the arguments to be positional but we actually need to get the values to do the completion.A simple way to test the attr completion is