Skip to content

Instantly share code, notes, and snippets.

@siph
Created September 10, 2023 23:51
Show Gist options
  • Save siph/288b7c6b5f68a1902d28aebc95fde4c5 to your computer and use it in GitHub Desktop.
Save siph/288b7c6b5f68a1902d28aebc95fde4c5 to your computer and use it in GitHub Desktop.
Building a standalone nixvim configuration

Building a standalone nixvim configuration

A standalone configuration is one that exists in its own project/repository. This means that you can externalize your entire neovim configuration, including flake inputs, nixpkgs version, etc...

This decoupling of neovim and NixOS configurations has multiple advantages including mixing stable/unstable nixpkgs and the ability to run your neovim configuration outside of your NixOS configuration. You can even run it outside of NixOS all together.

Generate and configure

To get started you can generate a flake in a new project from the provided template:

nix flake init --template github:nix-community/nixvim

The template starts with a basic structure and the bufferline plugin enabled.

.
├──  config
│   ├──  bufferline.nix
│   └──  default.nix
├──  flake.nix
└──  README.md

The initial layout encourages you to configure each plugin in its own file but you're free to organize it however you wish. From here you can configure nixvim like you would with the other methods.

Running

Running this from the project folder is as simple as:

nix run

If you have pushed this to a public git repository you can run it remotely from any machine with nix installed. This is one of the stronger advantages in my opinion because your configuration is now portable anywhere you need it.

You can try running mine with:

nix run 'github:siph/nixvim-flake'

# You can also provide multiple configs as different package outputs.
nix run 'github:siph/nixvim-flake#lite'

Installing into NixOS configuration

Your nixvim flake will output a derivation that you can easily include in either home.packages for home-manager, or environment.systemPackages for NixOS. Or whatever happens with darwin?

You can add your nixvim configuration as an input to your NixOS configuration like:

{
 inputs = {
    nixvim-flake.url = "github:<USER>/<REPOSITORY>";
 };
}

Direct installation

With the input added you can reference it directly.

{ inputs, system, ... }:
{
  # NixOS
  environment.systemPackages = [ inputs.nixvim-flake.packages.${system}.default ];
  # home-manager
  home.packages = [ inputs.nixvim-flake.packages.${system}.default ];
}

The binary built by nixvim is already named as nvim so you can call it just like you normally would.

Installing as an overlay

Another method is to overlay your custom build over neovim from nixpkgs.

This method is less straight-forward but allows you to install neovim like you normally would. With this method you would just install neovim in your configuration (home.packges = with pkgs; [ neovim ]), but you replace neovim in pkgs with your derivation from nixvim-flake.

{
  pkgs = import inputs.nixpkgs {
    inherit system;
    overlays = [
      (final: prev: {
        neovim = inputs.nixvim-flake.packages.${system}.default;
      })
    ];
  }
}

Bonus lazy method

You can just straight up alias something like nix run 'github:siph/nixvim-flake' to nvim.

@refaelsh
Copy link

Hi. Thank for this gist. Found it on google.
I followed the gist, specifally this part: environment.systemPackages = [ inputs.nixvim-flake.packages.${system}.default ];.
But when I do sudo nixos-rebuild switch --flake ~/repos/dotfiles/#myNixos I get the following error:

error: attribute 'default' missing

@siph
Copy link
Author

siph commented Sep 19, 2024

Hi. Thank for this gist. Found it on google. I followed the gist, specifally this part: environment.systemPackages = [ inputs.nixvim-flake.packages.${system}.default ];. But when I do sudo nixos-rebuild switch --flake ~/repos/dotfiles/#myNixos I get the following error:

error: attribute 'default' missing

The nixvim standalone flake that you're using as an input (inputs.nixvim-flake) needs to have a default package output. For example if you look at mine (nix flake show github:siph/nixvim-flake), it has packages.x86_64-linux.full, packages.x86_64-linux.lite and packages.x86_64-linux.default. If your input nixvim-config flake doesn't have a default output you can either make one, or just change default to the name of your output.

@refaelsh
Copy link

refaelsh commented Sep 19, 2024

It does have a default pacakge. It came this way when I ran nix flake init --template github:nix-community/nixvim.
Here is a snippet:

perSystem =
        { pkgs, system, ... }:
        let
          nixvimLib = nixvim.lib.${system};
          nixvim' = nixvim.legacyPackages.${system};
          nixvimModule = {
            inherit pkgs;
            module = import ./config; # import the module directly
            # You can use `extraSpecialArgs` to pass additional arguments to your module files
            extraSpecialArgs = {
              # inherit (inputs) foo;
            };
          };
          nvim = nixvim'.makeNixvimWithModule nixvimModule;
        in
        {
          checks = {
            # Run `nix flake check .` to verify that your config is not broken
            default = nixvimLib.check.mkTestDerivationFromNixvimModule nixvimModule;
          };

          packages = {
            # Lets you run `nix run .` to start nixvim
            default = nvim;
          };
        };

@siph
Copy link
Author

siph commented Sep 19, 2024

hmm idk, it should work. Maybe your lockfile is out of sync?

@refaelsh
Copy link

hmm idk, it should work.

Yeap. It should.

Maybe your lockfile is out of sync?

No idea what this means. Googling now.

@siph
Copy link
Author

siph commented Sep 19, 2024

I mean your flake.lock for your system configurations. ie nix flake update.

@refaelsh
Copy link

Nope, nix flake update did not help. Thank you for trying :-)
I will keep googling.

@refaelsh
Copy link

For future googlers, I found the solution here: nix-community/nixvim#2277.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment