Home manager, flake and configuration. How to rebuild the whole environment with just nixos-rebuild switch?

Hello, it’s me with some silly questions again.

So i tried out flake, even though I don’t really understand how it works completely.

My goal is to have my environment (configuration.nix and home.nix) set up with only one command, and all my configs (configuration.nix and home.nix) is stored in /etc/nixos (though it would definitely be better if I can just store it in home directory, so I can edit with vscode)

The following file is my flake:

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

  outputs = { home-manager, nixpkgs, ... }@inputs:
  let
    username = "Peal";
    hostname = "Windows11";
    system = "x86_64-linux";
    pkgs = import nixpkgs {
      inherit system;
      config.allowUnfree = true;
    };
  in
  {
    nixosConfigurations."${hostname}" = nixpkgs.lib.nixosSystem {
      specialArgs = { inherit inputs username system; };
      modules = [ ./configuration.nix ];
    };
    homeConfigurations."${username}" = home-manager.lib.homeManagerConfiguration {
      inherit pkgs;
      extraSpecialArgs = { inherit inputs username; };
      modules = [ ./home.nix ];
    };
  };
}

But for some reason (skill issues), when i do nixos-rebuild switch --flake, only the confiuration.nix file got rebuild, and I don’t know how I can get my home manager to rebuild along with it too.

some unrelated info but hope it can be helpful:
in /etc/nixos:

.
├── configuration.nix
├── flake.lock
├── flake.nix
└── home.nix

content of few first line of home.nix:

  {config, pkgs, ...}: {
    home.stateVersion = "23.11";
    home.username = "kahlenden";
    home.homeDirectory = "/home/kahlenden/";
    programs.home-manager.enable = true;
    #...
  }

You need to put the homeManager module in your imports. See Getting Started with Home Manager | NixOS & Flakes Book

To be more specific, they need to import home-manager NixOS module (e.g. add it to modules argument of nixosSystem), and then add the following NixOS option either to their configuration.nix or directly to modules argument of nixosSystem (wrapped in {} to make it a module):

  home-manager.users.${username} = {
    imports = [
      ./home.nix
    ];
  };

Actually, what you came up with in Add an option to home manager as a nixos module using flake - #2 by Peal is almost good.

1 Like