Cannot get nixvim to install as a module in home-manager

last part of the error message:

       … while evaluating the module argument `nixvim' in "/nix/store/0cwwk05gvsrdy1i2b57amrcdm3shg082-source/home.nix":

       error: infinite recursion encountered

       at /nix/store/svas0rbjm1vfiqhls8rmzcygwiwvcj32-source/lib/modules.nix:508:28:

          507|         builtins.addErrorContext (context name)
          508|           (args.${name} or config._module.args.${name})
             |                            ^
          509|       ) (lib.functionArgs f);

Found an upstream issue for this. Apparently it’s a home-manager bug and you shouldn’t import the module from inside another module.

Perhaps try to move the import here, as the comment suggests?

It’s not a bug, you just need to pass nixvim into the module system via extraSpecialArgs.

1 Like

It’s a feature :wink:

Joking aside, if using extraSpecialArgs also works, then sure. Another option is to just use the NixOS module, which I don’t think is any different from the HM one.

Edit: Looking back on this, since you’re already passing inputs to extraSpecialArgs, you can just use that:

# home.nix
{ config, lib, pkgs, inputs, ... }:

{

  imports = [
    inputs.nixvim.homeManagerModules.nixvim
  ];


  home.username = "user";
  home.homeDirectory = "/home/user";
  # You do not need to change this if you're reading this in the future.
  # Don't ever change this after the first build.  Don't ask questions.
  home.stateVersion = "24.05";

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

PS: You will probably also need to disable neovim, since it will collide with nixvim:

After doing this, you’d also need to add defaultEditor to nixvim to keep it as the default editor:

  programs.nixvim = {
    enable = true;
    defaultEditor = true;
  };
# flake.nix
{
  
  inputs = {
    nixpkgs = {
      url = "github:NixOS/nixpkgs?ref=nixos-24.05";
    };
    home-manager = {
      url = "github:nix-community/home-manager?ref=release-24.05";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    nixvim = {
      url = "github:nix-community/nixvim?ref=nixos-24.05";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = inputs @ { self, nixpkgs, home-manager, ... }: {
    nixosConfigurations = {
      hostname = nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        modules = [
          ./configuration.nix
   	      home-manager.nixosModules.home-manager {
	      home-manager.extraSpecialArgs = { inherit inputs; };
              home-manager.useGlobalPkgs = true;
	      home-manager.useUserPackages = true;
	      home-manager.users.user = {
	        imports = [
                  ./home.nix
	          ./modules/firefox/firefox.nix
	          ./modules/nvim/nvim.nix
	         ./modules/bash/bash.nix
	        ];
	      };
	    }
        ];
      };
    };
  };
}
# home.nix
{ config, lib, pkgs, inputs, ... }:

{

  imports = [
    inputs.nixvim.homeManagerModules.nixvim
  ];


  home.username = "user";
  home.homeDirectory = "/home/user";
  # You do not need to change this if you're reading this in the future.
  # Don't ever change this after the first build.  Don't ask questions.
  home.stateVersion = "24.05";


  programs.nixvim = {
    enable = true;
    defaultEditor = true;
    colorschemes.gruvbox.enable = true;
    plugins.lightline.enable = true;
  };  
}

This is the stuff made it run for me:

       error: The option `home-manager.users.user.programs.nixvim' in `/nix/store/svas0rbjm1vfiqhls8rmzcygwiwvcj32-source/flake.nix' is already declared in `/nix/store/0vd8bfjgaiwvxvqp51q9vdk1snjrxkfx-source/home.nix'.

This informed me that I was importing both in flake.nix and in home.nix
You really should not import modules in flake.nix

Make sure not to miss some stuff in this config, I’ve removed diff so it’s actually copy-pastable.

I’ve used this repo when debugging:

If the link expires:

# home.nix
{
  inputs,
  lib,
  config,
  pkgs,
  theme,
  gtkThemeFromScheme,
  ...
}:

{

  colorScheme = inputs.nix-colors.colorSchemes."${theme}";

  # You can import other home-manager modules here
  imports = [
    # If you want to use home-manager modules from other flakes (such as nix-colors):
    inputs.nix-colors.homeManagerModules.default
    inputs.nixvim.homeManagerModules.nixvim
    #inputs.hyprland.homeManagerModules.default

    # You can also split up your configuration and import pieces of it here:
    ./home-manager/imports.nix
  ];

  home = {
    username = "khoa";
    homeDirectory = "/home/khoa";
  };

  # Add stuff for your user as you see fit:
  # programs.neovim.enable = true;
  # home.packages = with pkgs; [ steam ];

  # Enable home-manager and git
  programs = {
    home-manager.enable = true;
    git = {
      enable = true;
      userName = "dxcently";
      userEmail = "dxcently@gmail.com";
    };
  };

  # Nicely reload system units when changing configs
  systemd.user.startServices = "sd-switch";

  dconf.settings = {
    "org/virt-manager/virt-manager/connections" = {
      autoconnect = [ "qemu:///system" ];
      uris = [ "qemu:///system" ];
    };
  };

  # https://nixos.wiki/wiki/FAQ/When_do_I_update_stateVersion
  home.stateVersion = "23.11";
}
flake.nix
{
  description = "fart";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    home-manager.url = "github:nix-community/home-manager/master";
    home-manager.inputs.nixpkgs.follows = "nixpkgs";
    nixpkgs_patched.url = "github:nixos/nixpkgs/468a37e6ba01c45c91460580f345d48ecdb5a4db";
    nix-colors.url = "github:misterio77/nix-colors";
    spicetify-nix.url = "github:the-argus/spicetify-nix";
    nixvim = {
      url = "github:nix-community/nixvim";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    /*
      hyprland.url = "github:hyprwm/Hyprland";
      hyprland-plugins = {
        url = "github:hyprwm/hyprland-plugins";
        inputs.hyprland.follows = "hyprland";
      };
    */
  };
  outputs =
    inputs@{
      nixpkgs,
      home-manager,
      spicetify-nix,
      ...
    }:
    let
      system = "x86_64-linux";

      # User Variables
      hostname = "dxflake";
      username = "khoa";
      gitUsername = "dxcently";
      gitEmail = "dxcently@gmail.com";
      theme = "sakura";

      pkgs = import nixpkgs {
        inherit system;
        config = {
          allowUnfree = true;
        };
      };
    in
    {
      nixosConfigurations = {
        "${hostname}" = nixpkgs.lib.nixosSystem {
          specialArgs = {
            inherit system;
            inherit inputs;
            inherit username;
            inherit hostname;
            inherit gitUsername;
            inherit gitEmail;
          };
          modules = [
            ./configuration.nix
            home-manager.nixosModules.home-manager
            {
              home-manager.extraSpecialArgs = {
                inherit username;
                inherit gitEmail;
                inherit inputs;
                inherit gitUsername;
                inherit theme;
                inherit (inputs.nix-colors.lib-contrib { inherit pkgs; }) gtkThemeFromScheme;
                inherit spicetify-nix;
              };
              home-manager.useGlobalPkgs = true;
              home-manager.useUserPackages = true;
              home-manager.backupFileExtension = "backup";
              home-manager.users.${username} = import ./home.nix;
            }
          ];
        };
      };
    };
}

@dxcently thank you for the config
This is now solved.

some words to make the search easier: minimal nixvim installation, home manager, home-manager, flake, infinite recursion encountered, simple nixvim, flake.nix, home.nix, neovim

1 Like

Well, yeah. You should only import it once, so choose one or the other.

Don’t think that’s an issue, really. You can try to remove the import from home.nix and use the one from flake.nix and it will still work (don’t forget to use inputs.nixvim or pass nixvim to the ourputs, though).

1 Like