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

nix-build: add initial support #1480

Merged
merged 2 commits into from
Jan 15, 2023
Merged

Conversation

thatsmydoing
Copy link
Contributor

@thatsmydoing thatsmydoing commented Jan 15, 2023

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-completions

This 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

nix-build '<nixpkgs>' -A he<TAB>

completers/nix-build_completer/cmd/root.go Outdated Show resolved Hide resolved
pkg/actions/tools/nix/attr.go Outdated Show resolved Hide resolved
pkg/actions/tools/nix/path.go Show resolved Hide resolved
pkg/actions/tools/nix/path.go Outdated Show resolved Hide resolved
Copy link
Member

@rsteube rsteube left a 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

pkg/actions/tools/nix/path.go Outdated Show resolved Hide resolved
pkg/actions/tools/nix/path.go Show resolved Hide resolved
pkg/actions/tools/nix/attr.go Outdated Show resolved Hide resolved
completers/nix-build_completer/cmd/root.go Outdated Show resolved Hide resolved
@thatsmydoing
Copy link
Contributor Author

I think I'm happy with this. I'll play with it a bit and maybe make a PR for nix-instantiate later as they share quite a lot of logic.

@thatsmydoing thatsmydoing marked this pull request as ready for review January 15, 2023 16:54
@rsteube rsteube merged commit 0e66bbb into carapace-sh:master Jan 15, 2023
@rsteube
Copy link
Member

rsteube commented Jan 15, 2023

thanks

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