Skip to content

[doc] Add NixOS and home-manager entry to wiki #908

@misumisumi

Description

@misumisumi

Feature description

I have submitted a PR for NixOS support in #906.
I would like to add to the wiki about NixOS support.
module options will be replaced with appropriate ones when the PR is merged
Please review.

what's inside ?

  • Usage for NixOS
    • mason.nvim cannot build packages or run installed binaries
    • How to check for required dependencies.
    • What to do if you can't build with mason.nvim.
  • Usage for home-manager on non-NixOS
  • Module Options

Preview

raw markdown code # NixOS and home-manager

This repository contains the nixosModule for home-manager provided by flakes.
You can use it right away if you are using home-manager on NixOS as well as non-NixOS.

Usage for NixOS.

Add the following to your flake.nix, configuration.nix and nvimdots.nix.
Relogin to shell after nixos-rebuild switch --flake <flake-url>.

  • flake.nix

    {
      inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
      inputs.nvimdots.url = "github:ayamir/nvimdots";
      inputs.home-manager = {
        url = "github:nix-community/home-manager";
        inputs.nixpkgs.follows = "nixpkgs";
      };
      nixosConfigurations = {
          use-nvimdots = inputs.nixpkgs.lib.nixosSystem {
            system = "x86_64-linux";
            modules =
              [ # ...
                inputs.home-manager.nixosModules.home-manager {
                  home-manager.users."example-user" = {
                    imports =
                      [
                        inputs.nvimdots.nixosModules.nvimdots
                        (import ./nvimdots.nix)
                      ];
                  };
                  home-manager.extraSpecialArgs = { inherit inputs; };
                }
                ./configuration.nix
              ];
            specialArgs = { inherit inputs; };
          };
        };
      };
    }
  • configuration.nix

    {
      # ...
      programs.nix-ld.enable = true;
      # ...
    }
  • nvimdots.nix

    setBuildEnv provides CPATH, CPLUG_INCLUDE_PATH, LD_LIBLARY_PATH, LIBRARY_PATH, NIX_LD_LIBRARY_PATH, PKG_CONFIG_PATH.
    withBuildTools provides basic build tools such as gcc and pkg-config.
    These are required for mason.nvim and nvim-treesitter to work properly.
    Also, they are only provided to neovim and do not affect the entire session.

    {
      programs.neovim.nvimdots = {
        enable = true;
        setBuildEnv = true;
        withBuildTools = true;
      };
    }

mason.nvim cannot build packages or run installed binaries.

This is because the dependencies are not included.
NixOS does not conform to the Filesystem Hierarchy Standard (FHS), so some ingenuity is required.
This nixosModules provides options to simplify dependency resolution.
You can still use programs.neovim.extraPackages, programs.neovim.extraPython3Packages, programs.neovim.extraLuaPackages provided by home-manager.

  • nvimdots.nix

    {pkgs, ...}:
    {
      programs.neovim.nvimdots = {
        enable = true;
        setBuildEnv = true;
        withBuildTools = true;
        extraRPackages = rPkgs: []; # need packages for R include `nixpkgs.rPackages`.
        extraHaskellPackages = hsPkgs: []; # need packages for haskell include `nixpkgs.haskellPackages`.
        extraDependentPackages = with pkgs; [] # need packages `lib`, `include`, and `pkgconfig`.
      };
    }

How to check for required dependencies.

For binaries, patchelf --print-needed <binary> will list the required packages.
You can also check the library link status with ldd <binary>.
You can check the dependencies required at build time from the build log from the UI of mason.nvim opened with :Mason.
If it is included in nixpkgs, you may be able to find the package you need by looking at the *.nix sources.

What to do if you can't build with mason.nvim.

If it is provided by nixpkgs, you should be able to use it by adding the following settings.

-- Setup lsps that are not supported by `mason.nvim` but supported by `nvim-lspconfig` here.
if vim.fn.executable("dart") == 1 then
local _opts = require("completion.servers.dartls")
local final_opts = vim.tbl_deep_extend("keep", _opts, opts)
nvim_lsp.dartls.setup(final_opts)
end

Usage for home-manager on non-NixOS

Unlike on NixOS, build tools and dependencies should be provided by the package manager provided by the distribution.
Of course, you can let nix resolve dependencies as they do for NixOS, but that can be a problem if you run the build command from within neovim.
Add the following to your flake.nix and nvimdots.nix.
Relogin to shell after home-manager switch --flake <flake-url>.

  • flake.nix

    {
      inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
      inputs.nvimdots.url = "github:ayamir/nvimdots";
      inputs.home-manager = {
        url = "github:nix-community/home-manager";
        inputs.nixpkgs.follows = "nixpkgs";
      };
      homeConfigurations = {
        use-nvimdots = inputs.home-manager.lib.homeManagerConfiguration {
          inherit (inputs) nixpkgs;
          modules = [
            inputs.nvimdots.nixosModules.nvimdots
            (import ./nvimdots.nix)
          ];
          extraSpecialArgs = { inherit inputs; };
        };
      };
    }
  • nvimdots.nix

    {
      programs.neovim.nvimdots = {
        enable = true;
        # Comment out if resolving dependencies on `nix`.
        # setBuildEnv = true;
        # withBuildTools = true;
      };
    }

Module Options

Please see https://github.com/misumisumi/nvimdots/blob/36d0b3d920f6e4e5397646ddb522f8c8aa43af93/nixos/neovim/default.nix#L11C1-L135C5


NixOS and home-manager

This repository contains the nixosModule for home-manager provided by flakes.
You can use it right away if you are using home-manager on NixOS as well as non-NixOS.

Usage for NixOS.

Add the following to your flake.nix, configuration.nix and nvimdots.nix.
Relogin to shell after nixos-rebuild switch --flake <flake-url>.

  • flake.nix

    {
      inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
      inputs.nvimdots.url = "github:ayamir/nvimdots";
      inputs.home-manager = {
        url = "github:nix-community/home-manager";
        inputs.nixpkgs.follows = "nixpkgs";
      };
      nixosConfigurations = {
          use-nvimdots = inputs.nixpkgs.lib.nixosSystem {
            system = "x86_64-linux";
            modules =
              [ # ...
                inputs.home-manager.nixosModules.home-manager {
                  home-manager.users."example-user" = {
                    imports =
                      [
                        inputs.nvimdots.nixosModules.nvimdots
                        (import ./nvimdots.nix)
                      ];
                  };
                  home-manager.extraSpecialArgs = { inherit inputs; };
                }
                ./configuration.nix
              ];
            specialArgs = { inherit inputs; };
          };
        };
      };
    }
  • configuration.nix

    {
      # ...
      programs.nix-ld.enable = true;
      # ...
    }
  • nvimdots.nix

    setBuildEnv provides CPATH, CPLUG_INCLUDE_PATH, LD_LIBLARY_PATH, LIBRARY_PATH, NIX_LD_LIBRARY_PATH, PKG_CONFIG_PATH.
    withBuildTools provides basic build tools such as gcc and pkg-config.
    These are required for mason.nvim and nvim-treesitter to work properly.
    Also, they are only provided to neovim and do not affect the entire session.

    {
      programs.neovim.nvimdots = {
        enable = true;
        setBuildEnv = true;
        withBuildTools = true;
      };
    }

mason.nvim cannot build packages or run installed binaries.

This is because the dependencies are not included.
NixOS does not conform to the Filesystem Hierarchy Standard (FHS), so some ingenuity is required.
This nixosModules provides options to simplify dependency resolution.
You can still use programs.neovim.extraPackages, programs.neovim.extraPython3Packages, programs.neovim.extraLuaPackages provided by home-manager.

  • nvimdots.nix

    {pkgs, ...}:
    {
      programs.neovim.nvimdots = {
        enable = true;
        setBuildEnv = true;
        withBuildTools = true;
        extraRPackages = rPkgs: []; # need packages for R include `nixpkgs.rPackages`.
        extraHaskellPackages = hsPkgs: []; # need packages for haskell include `nixpkgs.haskellPackages`.
        extraDependentPackages = with pkgs; [] # need packages `lib`, `include`, and `pkgconfig`.
      };
    }

How to check for required dependencies.

For binaries, patchelf --print-needed <binary> will list the required packages.
You can also check the library link status with ldd <binary>.
You can check the dependencies required at build time from the build log from the UI of mason.nvim opened with :Mason.
If it is included in nixpkgs, you may be able to find the package you need by looking at the *.nix sources.

What to do if you can't build with mason.nvim.

If it is provided by nixpkgs, you should be able to use it by adding the following settings.

-- Setup lsps that are not supported by `mason.nvim` but supported by `nvim-lspconfig` here.
if vim.fn.executable("dart") == 1 then
local _opts = require("completion.servers.dartls")
local final_opts = vim.tbl_deep_extend("keep", _opts, opts)
nvim_lsp.dartls.setup(final_opts)
end

Usage for home-manager on non-NixOS

Unlike on NixOS, build tools and dependencies should be provided by the package manager provided by the distribution.
Of course, you can let nix resolve dependencies as they do for NixOS, but that can be a problem if you run the build command from within neovim.
Add the following to your flake.nix and nvimdots.nix.
Relogin to shell after home-manager switch --flake <flake-url>.

  • flake.nix

    {
      inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
      inputs.nvimdots.url = "github:ayamir/nvimdots";
      inputs.home-manager = {
        url = "github:nix-community/home-manager";
        inputs.nixpkgs.follows = "nixpkgs";
      };
      homeConfigurations = {
        use-nvimdots = inputs.home-manager.lib.homeManagerConfiguration {
          inherit (inputs) nixpkgs;
          modules = [
            inputs.nvimdots.nixosModules.nvimdots
            (import ./nvimdots.nix)
          ];
          extraSpecialArgs = { inherit inputs; };
        };
      };
    }
  • nvimdots.nix

    {
      programs.neovim.nvimdots = {
        enable = true;
        # Comment out if resolving dependencies on `nix`.
        # setBuildEnv = true;
        # withBuildTools = true;
      };
    }

Module Options

Please see https://github.com/misumisumi/nvimdots/blob/36d0b3d920f6e4e5397646ddb522f8c8aa43af93/nixos/neovim/default.nix#L11C1-L135C5

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions