NixOS, Flake and Home-Manager interaction trough NixVim

Hi,
I’m still pretty new to NixOS, flakes, home-manager, and all this stuff.

I’m trying to understand how flakes and home-manager interact, in the context of NixOS.

My case here is about NixVim. I’m trying to get it to work in my Home-Manager.

Before I touch anything, my flake looks like that (full repo here):

{
  inputs = {
    ...
    nixvim = {
      url = "github:nix-community/nixvim";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = { ... } {
    ...
    nixosConfigurations = {
      desktop = nixpkgs.lib.nixosSystem {
        modules = [
          ...
          ./hosts/desktop
        ];
      };
      ...
    };
  };
}

And my ./hosts/desktop import my home-manager config:

home-manager.users.bhasher = import ../../../home/bhasher.nix;

At the end, I would like to be able to use NixVim in my bhasher.nix (more precisely, nvim.nix imported by bhasher.nix) with:

{ ... }: {
  programs.nixvim.enable = true;
}

To accomplish this, I tried two ways:

1. Module in flake.nix

In my output, in added inputs.nixvim.nixosModules.nixvim:

nixosConfigurations = {
  desktop = nixpkgs.lib.nixosSystem {
    modules = [
      ./hosts/desktop
      inputs.nixvim.nixosModules.nixvim
    ];
  };
};

But when I compile, I get:

error: The option `home-manager.users.bhasher.programs.nixvim' does not exist.

2. Import in nvim.nix

I also tried to import the Home-Manager module directly in nvim.nix:

{ ... }: {
  imports [ nixvim.homeManagerModules.nixvim ];

  programs.nixvim.enable = true;
}

Here too, I receive an error:

error: infinite recursion encountered

I probably don’t understand correctly how NixOS, flake & Home-Manager interact. I saw a ton of examples based on homeManagerConfiguration, but it seems to be only valid for non-nixos usages.

I’m probably confused by the 1000 ways to do almost everything in nix, and the mix between all options (nixos or not, home manager or not, flake or not, etc)

Thank you!

In the case of (1), the NixOS module exposes a NixOS option, not a Home Manager option. So rather than home-manager.users.bhasher.programs.nixvim, you just want programs.nixvim.

For (2), how is nixvim getting into the arguments of your Home Manager modules? You probably want to make some changes here to pass inputs to home-manager or to import the nixvim module there, where you have inputs in scope.

Thnak you a lot!

That’s what I guessed for the (1). I will fallback to it if I can’t make (2) working, but I would prefer a Home-Manager solution (mainly to understand their interaction).

I don’t get how to add inputs, nor import NixVim directly. (I would prefer the first, as it’s more general, and can be reused later)

I tried to combine the imports

  home-manager.users.bhasher = import ../../../home/bhasher.nix && import inputs.nixvim.homeManagerModules.nixvim;

or to use some weird other ways to combine:

  home-manager.users.bhasher = rec {
    specialArgs = {
      inherit inputs;
    };
    inherit (import ../../../home/bhasher.nix specialArgs) home;
  };

But none of them was successful.

I think you’re trying to do too much with too little understanding of the Nix language at this stage. That use of && stands out. && in Nix is a Boolean operator. It seems like you’re trying to use it like the English word ‘and’, as in ‘import this and that’. But && isn’t ‘and’, and Nix isn’t English, of course.

Check out some starter resources on the Nix language.

Take some time with those, especially the Tour of Nix, and make sure you understand the basic syntax.

As for your config, consider writing an inline Home-Manager module there.

home-manager.users.bhasher = { ... }: {
  <your stuff here>
};

and remember the link I sent in the HM docs: you can set an attribute imports in your modules to import other files (or from inputs, if inputs is in scope there).

1 Like

home-manager.sharedModules might be just what you’re looking for, see: Installing and configuring nixvim entirely from home-manager - #18 by pmyjavec

I am not sure if this is the idiomatic way to merry home-manager and nixvim, certainly nixvim readme doesn’t say anything about home-manager.sharedModules… but I tried the linked method, and it worked

1 Like