nmasur / dotfiles

Nix configuration and settings
MIT License
152 stars 6 forks source link

neovim v_y (visual mode yank) not working due to kitty-scrollback behavior #68

Closed eblume closed 3 months ago

eblume commented 3 months ago

Hi, thank you very much for creating this repository! I've been studying it carefully while learning how to use nix, nix flakes+commands, home-manager, and nix-darwin in a multi-"host" configuration very similar to your own.

Today I fixed an issue whereby visual mode yanking in neovim was simply not working. I could yank as expected all I wanted in any other mode, but while in visual mode, it was as if y was unbound (despite showing the <Plug>(KsbVisualYank)<CR><Plug>(KsbQuitAll) binding with telescope's <Leader>k binding).

As you can see, simply removing the setup.kitty-scrollback attrset (is that the right term here?) from modules/common/neovim/config/misc.nix seems to have fixed the issue for me... but I wonder if you can speak to this a bit more, perhaps there's something important here I'm missing?

Thanks again! PS, I've tried to maintain attribution and licensing on my fork of your repo, but if you feel I've done anything wrong or impolite, please let me know.

nmasur commented 3 months ago

Hey @eblume,

Thanks for taking interest in my project! I appreciate the bug report, I was actually aware of this issue and I was struggling to find a fix for it.

The context was: I wanted to be able to use to the keyboard to select text in my terminal (not just previous inputs, but the actual output content). Unfortunately, kitty doesn't have its own selection mode, so you have to use its scrollback pager feature to send the output content to a text editor. However, sending it to Neovim is also a bit broken, so I tried to use this Neovim plugin called kitty-scrollback.nvim. It's a cool plugin but it has this annoying feature of taking multiple steps to actually copy text and quit the program. I tried to rebind some of the keys to shortcut this option, but that had the unintended side effect of rebinding them even outside of the kitty scrollback buffer.

Faced with this issue for a little while (during which you forked this repo), I decided to abandon kitty and switch to WezTerm, which has a native selection mode and quick selection mode and doesn't force to me to rely on switching to Neovim. If you take a look again, you'll notice that I've removed the kitty scrollback plugin and keybind from Neovim.

Feel free to continue to use the repo as you wish. Love to see it! Let me know if you have any other questions I can help with.

Cheers, Noah

eblume commented 3 months ago

That's really funny, I actually kind of pieced that process together just from reading your code at that one transitionary commit - I could tell that a lot of keybinds (hammerspoon's ctrl+space + enter for instance) were set up to interact with kitty but could also see that wezterm was showing up in a number of places without kitty. I think it's really interesting to see how much you can learn just from reading these sorts of dotfile repos!

Since you've offered, may I ask an unrelated question: how can I go about building containers and AMIs with this config and test them using colima/docker and/or qemu?

Context: I'm trying to put together a pitch for my devops team to look in to nix for building our containerized workloads as well as making distributable developer environments a thing, and to demo this I would love to be able to show a quick whiz-bang tutorial along the lines of:

# Install nix (using DS installer, which claims easy uninstall)
$ curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install

# Install colima (macos)
$ nix-env -iA nixpkgs.colima

# Build worker container
$ nix build github:eblume/dotfiles/DEADBEEF .#worker

# Run your container
$ some-nix-or-colima-command ./result/some-path/worker

# Or, do the same as an ISO via qemu:
$ some-nix-command | some-qemu-command

... and the result should be a familiar container or image we already build elsewhere, demonstrating nix's build process.

I'm struggling a bit at the conceptual level here as I read through the "staff", "tempest", and "arrow" configuration examples - I understand that at a technical level what I want is to use a nixos generator, but I don't know what the actual commands to use would be to build and some of the errors I get seem different between nixos and nix-darwin. (I suspect some of these differences are due to aarch64-darwin platform differences vs x86_64-linux, but I need this demo to be useful to people on either platform.)

Is there anything you can say that would help straighten out my mental model so that I even know what the right question to ask might be? Best of all would be a demo similar to the above that I can work through and pull apart to understand, but I'll take even just a link to something I aught to read!

Thanks very much a second time for your time.

~Erich

PS - It seems like we have a lot of overlapping interests, do you have any recommendations for chat/forum/bbs/rss/matrix/mastadon/whatever communities about devops/devex/nix ?

nmasur commented 3 months ago

Hey @eblume,

I don't have a direct answer for you, but I'll try to elaborate on what you're asking.

Building Images

The nixos-generators project is basically just a wrapper around the nixos-rebuild build-vm tooling. It adds pre-configured options to format various output images and allows them to be added to your flake as output packages.

If you just want to build your current nixosConfiguration in your flake into a VM, you could do the following:

nixos-rebuild build-vm .#my-config

I'm using nixos-generators because I wanted to create a specific AWS-formatted AMI, and they make it easy. (This is their code, which really just wraps these settings from nixpkgs). First, you have to use the packages section instead of nixosConfigurations and replace nixpkgs.lib.nixosSystem with nixos-generators.nixosGenerate. You must also provide the format for the generator to use, and add any custom modules or settings you want to include for that format. Then you can just reference its package with a Nix build command:

nix build .#my-package

For myself, I would run (see here):

nix build .#arrow

However, this will not build on macOS. As far as I know, there's no way to build a NixOS ISO from a Mac. Maybe with some kind of nested VM, I'm not sure. You could do it with a GitHub Action or something like that and push your image somewhere for others to grab.

Running Images

Running a NixOS VM on macOS is doable, but not super straightforward. Here's what I've seen before:

Check out this post for more details.

Building Docker

My dotfiles aren't a great example for building a Docker image, because I'm generating my entire system configuration. Take a look at this tutorial for declaring a Docker image with Nix. You would then run something like this:

nix build .#my-docker-image

And it would generate an image you can load and run with the normal Docker tooling (and therefore Colima, etc.).

docker load < result
docker run my-docker-image

Building Dev Shells

If everyone you work with a team that are all willing to use Nix, you could skip Docker and VMs (yuck) and just go straight to a Nix environment. You already linked the docs for devshell, but there's also devenv and a few others. Personally, I just like to pair a basic Nix flake with direnv.

Take a look at my basic flake template for Rust projects as an example. If you add a .envrc file for direnv that simply says "use flake", you can drop this in your project repo and instantly get access to all of the packages defined in the mkShell block. When you leave the directory, they're gone!

If you don't have direnv, you can use:

nix develop

# or nix develop .#default

exit # to leave the environment

Nix Communities

There are a lot of options for talking Nix and NixOS, I generally lurk on the subreddit and forum but I'm less interested in keeping up on chat communities (but there are official/unofficial IRC, Matrix, and Discord servers).

I would recommend going to your local meetup! You can find some of them being advertised here. I've been starting to go regularly to mine and it's been cool to meet enthusiastic people.

eblume commented 3 months ago

Thank you very kindly, @nmasur! I will check out every one of these links, this is going to be a careful study for me.

eblume commented 3 months ago

Hi again @nmasur , I am hoping you might be able to help me with another question. Absolutely no problem if you're unable, I'll figure it out eventually :) (I can also move this to another ticket if that's best.)

I'm trying to add a module to modules/common/applications called mole.nix which I intend to provide a python package that I've developed. In the context of this nix environment, I don't need to be able to edit this package - I just want it to install it as an application that can be run like mole --help.

In the past I've used pipx for this purpose, which can be as simple as just pipx install https://github.com/eblume/mole. I've combined this with mise so that I can quickly pipx reinstall-all to upgrade all of my isolated python packages to a new version of python when that becomes available.

It seems from my reading that the sorry state of python packaging makes a poor match for nix, and so I gather that pipx is fundamentally pretty broken with nix installation (or at least no one seems to use it?), so my best guess is that I need to use something like buildPythonPackage to point at my package, but I can't figure out where to plug it in to this configuration - it seems like maybe something with overlays or the packages attribute of flake.nix is needed, but I am really struggling.

Any chance you could help point me in the right direction again? Thanks!!

Edit to add: this example seems like it has potential. In that case, though, it's to package a python package for nixpkgs, whereas I just want it as a common app across my nix-managed hosts.

nmasur commented 3 months ago

I was able to build your package successfully with this code, although I'm not sure it actually works besides attempting to run the very basic CLI.

There are a few different ways to do this. I copied the example of another Python tool that I know exists in Nixpkgs. Basically, I took all of the packages from your pyproject.toml and looked them up in Nixpkgs. Unfortunately, I had to patch your project file because there were a few dependencies that weren't in scope for the package versions that Nix was using, even if I tried an older version of Nixpkgs.

For kdl-py, which isn't packaged in Nixpkgs, I basically also had to package that myself for it to work.

Screenshot 2024-06-26 at 4 25 16 PM

Another way you could do this is using poetry2nix, which can generate a Python derivation using your Poetry lock file. This allows you to more strictly follow your Python project, but I've found that getting poetry2nix to work can be kind of hit or miss depending on how many system packages your project depends on.

eblume commented 3 months ago

Absolutely fantastic, thank you! This is really lighting things up for me - I can see now how you're able to use let bindings to build packages and then refer to them in the same expression. I'm still not quite grasping where the boundaries of home-manager packages vs system packages are but this is definitely making it clearer now.

Also, another compliment on this config: I was able to quickly use <Leader>t from within vim, then use nsr nix-prefetch-github eblume mole to regenerate that fetchFromGitHub block, then yank it over and reformat. All very smooth and ergonomic.

I am going to look in to poetry2nix because I don't love having to specify all of those package requirements - that's what poetry is "for", in my head. However I suspect the REAL answer is to set up a poetry sdist build/release via github actions and then install that way. Once I'm done with that I intend to configure it as a template.

Apologies about the kdl and package issues, I left that project in a rough state. It's not even using kdl at the moment. You may also wish to clean up the pyobjc nix store if you care, that library can be very large if it has to pull in all of xcode, I think. (Although IIRC you have nix gc set?)

Once again, thank you! Please let me know if I can ever return the favor.

eblume commented 3 months ago

This landed in https://github.com/eblume/dotfiles/commit/bb95f22c00047d2c9508b9be725b57ef3b58ab19 -- working great! Getting mise to work wasn't hard at all, too, which is something I was worried about since obviously mise and nix overlap a fair deal.

nmasur commented 3 months ago

Glad this is helpful, @eblume!

I'm still not quite grasping where the boundaries of home-manager packages vs system packages are but this is definitely making it clearer now.

There isn't really a "hard-and-fast" rule for this. Home-manager packages will only be available for your user (by default) whereas the system packages are available everywhere. That's the primary difference, so I usually default to home-manager if it's a package I expect only a human to use, but it's a subjective choice.

I think poetry2nix is good for projects that are already using Poetry. But if I were starting a Python project from scratch I would probably make it Nix-native just because Nix flakes already do most of what Poetry does, unless you are referencing lots of Python packages that aren't in nixpkgs.

Cool to see that it's working for you! No favors required, just let me know when you have anything cool you want to share.

eblume commented 3 months ago

I just managed to add an llm cli tool that I prefer with two slightly tricky dependencies - I'm off to the races!