marienz / nix-doom-emacs-unstraightened

Builds Doom Emacs using Nix
Apache License 2.0
27 stars 1 forks source link

how to use copilot? #11

Closed bbigras closed 3 weeks ago

bbigras commented 1 month ago

(use-package! copilot)

emacsPackages.copilot exits.

Is "No such file or directory" "git" the error?

❯ nix run .#doom-emacs
fetching git input 'git+file:///home/bbigras/src/nix-doom-emacs-unstraightened'
warning: Git tree '/home/bbigras/src/nix-doom-emacs-unstraightened' is dirty
building '/nix/store/sv0gip9a61gk1abvwrm8rr4zdz4kdiih-doom-intermediates.drv'...
error:
       … while calling the 'derivationStrict' builtin
         at /builtin/derivation.nix:1:208:
       … while evaluating derivation 'doom-emacs'
         whose name attribute is located at /nix/store/hsxq1bphjdbaimfj0jaa1f4vwp3xm408-source/pkgs/stdenv/generic/make-derivation.nix:331:7

       … while evaluating attribute 'buildCommand' of derivation 'doom-emacs'
         at /nix/store/hsxq1bphjdbaimfj0jaa1f4vwp3xm408-source/pkgs/build-support/trivial-builders/default.nix:68:17:
           67|         enableParallelBuilding = true;
           68|         inherit buildCommand name;
             |                 ^
           69|         passAsFile = [ "buildCommand" ]

       (stack trace truncated; use '--show-trace' to show the full trace)

       error: builder for '/nix/store/sv0gip9a61gk1abvwrm8rr4zdz4kdiih-doom-intermediates.drv' failed with exit code 255;
       last 25 log lines:
       >     (progn (if (not noeval) (load file noerror 'nomessage 'nosuffix) (if (fi...
       >     (unwind-protect (progn (if (not noeval) (load file noerror 'nomessage 'n...
       >     (save-current-buffer (set-buffer temp-buffer) (unwind-protect (progn (if...
       >     (let ((temp-buffer (generate-new-buffer " *temp*" t))) (save-current-buf...
       >     (condition-case e (let ((temp-buffer (generate-new-buffer " *temp*" t)))...
       >     (doom-packages--read "/nix/store/1vp1b43l2al4jvpgm1hx2jr8wisa7far-doomdi...
       >     (if file (doom-packages--read file nil 'noerror) nil)
       >     (let* ((file (and t (doom-module-locate-path (car key) (cdr key) doom-mo...
       > x There was an unexpected runtime error
       >   Message: Error with packages
       >   Details: ((:user) "/nix/store/1vp1b43l2al4jvpgm1hx2jr8wisa7far-doomdir/packages.el" (file-missing "Searching for program" "No such file or directory" "git"))
       >   Backtrace:
       >     (signal doom-package-error ((:user) "/nix/store/1vp1b43l2al4jvpgm1hx2jr8...
       >     (condition-case e (let ((temp-buffer (generate-new-buffer " *temp*" t)))...
       >     (doom-packages--read "/nix/store/1vp1b43l2al4jvpgm1hx2jr8wisa7far-doomdi...
       >     (if file (doom-packages--read file nil 'noerror) nil)
       >     (let* ((file (and t (doom-module-locate-path (car key) (cdr key) doom-mo...
       >     (let ((doom-module-context (doom-module-context key))) (if doom-inhibit-...
       >     (read-packages (:user))
       >     (let (doom-packages) (read-packages (cons :user nil)))
       >     (progn (let (doom-packages) (read-packages (cons :user nil))))
       >     (if user? (progn (let (doom-packages) (read-packages (cons :user nil)))))
       >     (let ((user? (assq :user module-list))) (if user? (progn (let (doom-pack...
       >     (let ((doom-context doom-context)) (let ((tail (ensure-list 'packages)))...
       > Searching for program: No such file or directory, git
       For full logs, run 'nix log /nix/store/sv0gip9a61gk1abvwrm8rr4zdz4kdiih-doom-intermediates.drv'.
simple-coderxx commented 1 month ago

I'd highly recommend that you use gptel instead, it's much better:

https://github.com/karthink/gptel

bbigras commented 1 month ago

I'd highly recommend that you use gptel instead, it's much better:

https://github.com/karthink/gptel

I'm already using it with vanilla emacs. I think it doesn't support the autocomplete thing that copilot does.

marienz commented 1 month ago

If you put (use-package! copilot) in your packages.el, replace that with (package! copilot).

(That's true for Doom, not just Unstraightened. Doing something unexpected in packages.el may sort of work in Doom, but it will not work in Unstraightened: Unstraightened evaluates packages.el in Nix's build sandbox to figure out what you want to install (so it can then use Nix to actually build and install it), which is a much more restrictive environment than doom sync runs in.)

The latter builds on my machine, but there is one issue: Unstraightened isn't picking up its autoload file (meaning things like copilot-complete are not defined). Adding (use-package! copilot) to your config.el (in addition to changing packages.el, not instead of it) should eagerly load copilot, which is not ideal but should make its commands available. Manually loading copilot-autoloads.el would also work (with less eager loading).

(copilot.el's install instructions look like they will work, but I'd suggest leaving out the :recipe ... bit. That's not currently causing a build failure if included but it probably should unless you add a pin, hmm...)

I'll have a look to see if I can pick up its autoload file without having to do something copilot-specific...

If this does not solve your problem, please share your doomdir (or a minimal doomdir demonstrating the problem).

bbigras commented 1 month ago

it seems to work with (package! copilot) in packages.el and this:

(use-package! copilot
  :hook (prog-mode . copilot-mode)
  :bind (:map copilot-completion-map
              ("<tab>" . 'copilot-accept-completion)
              ("TAB" . 'copilot-accept-completion)
              ("C-TAB" . 'copilot-accept-completion-by-word)
              ("C-<tab>" . 'copilot-accept-completion-by-word)))

in config.el.

Thank you very much!

bbigras commented 1 month ago

btw I got this error now copilot--start-agent: Could not find node executable, but it might be while using :tools direnv and it might be a doom-emacs issue.

Can I do something like this?

(use-package! copilot
  :hook (prog-mode . copilot-mode)
  :bind (:map copilot-completion-map
              ("<tab>" . 'copilot-accept-completion)
              ("TAB" . 'copilot-accept-completion)
              ("C-TAB" . 'copilot-accept-completion-by-word)
              ("C-<tab>" . 'copilot-accept-completion-by-word))
  :config ''
    (setq copilot-node-executable "${pkgs.nodejs}/bin/node")
  ''
)
marienz commented 1 month ago

If you're using nix run .#doom-emacs (not home-manager or overlay), running nix shell nixpkgs#nodejs (or something similar) first to get nodejs on your shell seems like the fastest workaround.

If you're using Unstraightened's home-manager or overlay setup, I should probably give you a way of adding additional binaries to the $PATH Doom runs with. (I'm currently not forcing things like rg and fd into Doom's PATH, been meaning to fix that...)

If I let you do something like extraBins = [ pkgs.nodejs ]; in Unstraightened's home-manager or overlay setup, would that be useful?

You could also make your suggestion work, but it gets a little fiddly (and exactly how you'd do it depends on how much ELisp you're comfortable having in Nix strings). Something like this should work:

doomDir = pkgs.symlinkJoin { 
  name = "doomdir";
  paths = [
    (pkgs.writeTextDir "config.el" ''
      ;; config.el, as in your comment
    '')
    ./path/to/the/rest/of/your/doomdir
   ];
 };

...but to avoid having an unwieldy amount of Lisp in Nix strings, you might prefer using concatTextFile or substitute so you can keep the Lisp in a separate file. I'd expect there to be examples of this sort of thing out there, but I'm not really sure where to point you for them, sorry!

simple-coderxx commented 1 month ago

I'd highly recommend that you use gptel instead, it's much better: https://github.com/karthink/gptel

I'm already using it with vanilla emacs. I think it doesn't support the autocomplete thing that copilot does.

The author of gptel is adding the copilot functionality to gptel, but I am not sure if he's going to add the autocomplete functionality, I'm pretty sure he will at some point. I am using the open source gpt4all, and I am very happy with it.

bbigras commented 4 weeks ago

I am not sure if he's going to add the autocomplete functionality, I'm pretty sure he will at some point.

The autocomplete thing is very useful to me, so far.

https://news.ycombinator.com/item?id=40617082 was posted today and there's also 3 similar tools in the comments.

bbigras commented 3 weeks ago

I wish there was something for emacs packages like https://github.com/NixOS/nixpkgs/blob/1729f1a7a900cd285b42db9c158da7a581ca407a/pkgs/build-support/rust/default-crate-overrides.nix is for rust.

The emacs-init home-manager module has a similar feature https://gitlab.com/rycee/nur-expressions/-/blob/7e3ce9990c382947c33f0def2a1c33c420d8c710/hm-modules/emacs-init-defaults.nix. Which is awesome and work great, but you need to define your emacs config in a nix file:

{
            all-the-icons-dired = {
              enable = pkgs.stdenv.hostPlatform.system == "x86_64-linux";
              after = [
                "dired"
                "all-the-icons"
              ];
              hook = [ "(dired-mode . all-the-icons-dired-mode)" ];
            };
}

Being able to do stuff like enable = pkgs.stdenv.hostPlatform.system == "x86_64-linux" is awesome, but the downside is that you can't just copy paste some config snippet you found online.

marienz commented 3 weeks ago

I've added an extraBinPackages setting in c61cd622b7abb2de838c41d99cc9a0b1e4506917. This can be used to add things like node to the binary search path (if they're not on your default $PATH already).

Although I'm considering exposing the init.el generator I'm already using in tests (which converts { lang.rust = true; lang.nix = [ "+lsp" ] } in Nix to (doom! :lang rust (nix +lsp)), more or less, and could be used for conditionally enabling Doom modules in a home-manager config), and I'm open to adding more ways of passing paths / packages / etc from Nix into Doom (suggestions on how to approach this appreciated), I'm not currently planning to add more extensive support for configuring Doom in Nix. In particular, I feel that generating a Doom config.el would likely either turn into an entire configuration framework on top of Doom, or be too limited to be of much use.

With that, I think Unstraightened now supports Copilot about as well as Doom itself does, so I'm closing this issue.

marienz commented 2 weeks ago

The latter builds on my machine, but there is one issue: Unstraightened isn't picking up its autoload file (meaning things like copilot-complete are not defined). ... I'll have a look to see if I can pick up its autoload file without having to do something copilot-specific...

I'd almost forgetten about this problem, but it is now fixed (by fa6fe4cf937e25dfcf1a44bd4d69ecbca280a85f).