Error while using a stand alone flake of home-manager

I am trying to write a stand-alone flake for home-manager, so that I can use it’s output in my main NixOS flake.
Here is what I have:

{
  description = "My Home Manager configuration";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    home-manager = {
      url = "github:nix-community/home-manager";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = { self, nixpkgs, home-manager }: {
    homeConfigurations."refaelsh" = home-manager.lib.homeManagerConfiguration {
      pkgs = nixpkgs.legacyPackages.x86_64-linux;

      modules = [
        ./nix/home.nix
        {
          # You might want to set some custom options here
          home = {
            username = "refaelsh";
            homeDirectory = "/home/refaelsh";
            stateVersion = "24.05";
          };
        }
      ];
    };
  };
}

As I understand, the output of the above flake is a NixOS module, right?
How can I use it in my main NixOS flake?
I tried the following:

{
  description = "NixOS flake";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
    sdfsd.url = "github:refaelsh/dotfiles?dir=bla";
    nixvim.url = "github:refaelsh/dotfiles?dir=nixvim";
  };
  outputs =
    { nixpkgs, ... }@inputs:
    let
      system = "x86_64-linux";
    in
    {
      nixosConfigurations = {
        myNixos = nixpkgs.lib.nixosSystem {
          specialArgs = {
            inherit inputs system;
          };

          modules = [
            ./nixos/configuration.nix
            inputs.home-manager.homeConfigurations.refaelsh
          ];
        };
      };
    };
}

But when I run sudo nixos-rebuild switch --flake ~/repos/dotfiles/#myNixos, I get the following error:

error: attribute 'refaelsh' missing

What am I doing wrong? Please help :slight_smile:

Also, my full dotfiles are here: GitHub - refaelsh/dotfiles.

Please provide your entire flake, including inputs.

No, it’s a home-manager config output for standalone HM. It cannot be consumed by a NixOS config this way.

I’ve updated the original post with the info you rightly requested.

What can I do to consume it in NixOS flake?

I believe the piece of information you’re looking for is here

Since the entire point of home manager is being a manager for a single user’s environment, the nixos module provided by home-manager is made to support multiple users. This allows you to include multiple home-manager configurations on a single nixos machine. In your case, it probably means you want something along the following lines:

myNixos = nixpkgs.lib.nixosSystem {
  ...

  modules = [
    ./nixos/configuration.nix

    inputs.home-manager.nixosModules.home-manager

    # Assuming you're fetching your dotfiles flake as 'dotfiles'
    {
      home-manager.users.refaelsh = import "${inputs.dotfiles}/nix/home.nix";
    }
  ];
};

@h7x4 This is what I had working for me, But, now, I want to use a stand-alone alone flake of home-manager and consume it in my NixOS flake as an input.

AFAIK, the output of home-manager.lib.homeManagerConfiguration is meant for the standalone home-manager tool, not for reuse in a nixos module. You can either refer to your home-manager configuration using a path into the flake like shown above, or make your own flake output that is just the home.nix file, something like this:

outputs = { self, nixpkgs, home-manager }: {
  homeModule = ./nix/home.nix;
};

And then import it like this:

home-manager.users.refaelsh = inputs.dotfiles.homeModule;
1 Like

This feels like a hack.

It’s not a hack, that’s basically your only option.
There’s no official name as per the flake schema at the moment, but you could name the output homeManagerModules.<name> or homeModules.<name> as many other projects do.

Though, since no CLI knows about such an output nor is it part of the current schema, the name of the output is entirely up to you.

I understand. Thank you very much.

For googlers from the future, I found a better solution, IMHO. See my dotfiles: GitHub - refaelsh/dotfiles at e06cfcd42194577e53ee6edf45b30d3093eee812.

Main points from the dotfiles:

  1. Have this in home-manager flake output : packages.x86_64-linux.default = self.homeConfigurations.standalone.activationPackage;
  2. Have the following in main NixOS falke:
            {
              environment.systemPackages = [
                inputs.home-manager.packages.${system}.default
              ];
            }
  1. And execute nix run ~/repos/dotfiles/home-manager when you need to switch home-managere configuration.