Linking a nixosConfiguration to a given homeConfiguration

I am trying to migrate my dotfiles to use flakes (and also perform some well needed refactor to certain parts) and I just run into a weird dead end. Hope any of you guys have some experience with it and can help me a bit.

In the flake, I want to define both my home-manager configurations and my NixOS configurations. The important thing is that I would like each of my NixOS hosts to have one of the home-manager configurations associated. The general outline of the flake is:

{
    inputs = {
        # ...
    };

    outputs = {
         nixosConfigurations = {
             soyuz = { 
                 # ... 
             };
             korolev = { 
                 # ... 
             };
         };
         homeConfigurations = {
             "dvicente@soyuz" = { 
                 # ... 
             };
             "dvicente@korolev" = { 
                 # ... 
             };
         };
    };
}

Here, you can see there are two different hosts with their respective configurations that I can build using nixos-rebuild switch --flake . with no issues. I also have their respective home-manager configurations I can build using home-manager switch --flake. What I would like to do is explicitly defining that the home configuration for korolev is dvicente@korolev and it should be built when I run nixos-rebuild. I have seen something similar in the home-manager manual, but I have not been able to plug a homeConfigurations set in it.

Does any of you have any pointers or examples that do something like this? Thank you in advance!

1 Like

Why not use home-manager module within nixosConfiguration? Something like below:

nixosConfigurations = {
        hostName = nixpkgs.lib.nixosSystem {
          system = "x86_64-linux";
          modules = [    
            ./configuration.nix
            
            home-manager.nixosModules.home-manager
            {
              home-manager.useGlobalPkgs = true;
              home-manager.useUserPackages = true;
              home-manager.users = {
                user = import ./home.nix;
                root = import ./root-home.nix;
              };
            }

          ];
        };

Why not use home-manager module within nixosConfiguration?

Because I would like to keep the homeConfigurations as a different part of the flake as well, so that I can use it in a different, non-NixOS machine as well if I ever need to. As far as I have tried, I cannot plug homeConfigurations in the declaration as a module

Why not? The home.nix mentioned above can be imparted in nixosConfiguration as well as independent home-manager flake or whatever have you. It is a plain nix expression utilizing home-manager config, its up to you where to source it from. Options remain the same.

Think of it like this. You’re trying to make homeConfiguration independent module, but it is now home.nix. Same file gets called from 2 places. Depending on which source calls it, either nixos or homeConfig gets built.

I also prefer to have the system and user configurations separate. However, I do not link between them, like you want to do. Did you try to use the specialArgs argument of nixpkgs? Maybe it can be used to this end.

Thank you @payas and @dschrempf, I was able to solve it using both your advices. As @payas said, it was much easier to keep using home.nix as a central point and importing it both for the nixConfigurations and the homeConfigurations, adapting the import to each of their needs. It was simple at first since both the standalone setup and the NixOS module configurations have documented examples using home.nix.

Using @dschrempf answer I was able to pass arguments to that home.nix file, which I needed since my configuration is abstracted from the details of each machine. To be able to do so, I had to use extraSpecialArgs, which is propagated to all the modules imported by home-manager.

In case anyone still needs details on how to do it, you can check my dotfiles (revision 6c47284 in case it changes in the future).

1 Like