Home-manager in flake.nix

I’m trying to use home-manager in flake.nix, but there are some mistakes in configuration.

Flakes are enabled in configuration.nix
nix.settings.experimental-features = [ "nix-command" "flakes" ];

Any idea what and how to fix it and maybe format it a little better? There are also no home.sessionVariables yet. Need to figure out how to glue all that stuff together.
`

{
  description = "Home manager configuration";

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

  outputs = { self, nixpkgs, home-manager, hyprland, ... }@inputs:
    let
      system = "x86_64-linux";
      pkgs = nixpkgs.legacyPackages.${system};
    in
    {
      nixosConfigurations = {
        nixos = nixpkgs.lib.nixosSystem {
          inherit system;
          modules = [ ./configuration.nix ];
        };
      };

      homeConfigurations = {
        "mee@nixos" = home-manager.lib.homeManagerConfiguration {
          pkgs = nixpkgs.legacyPackages.${system};
          system = "x86_64-linux";
          configuration = { config, lib, pkgs, ... }: {
            modules = [
              {
                home.username = "mee";
                home.stateVersion = "23.11";
                home.homeDirectory = "/home/mee";
                programs.home-manager.enable = true;
                wayland.windowManager.hyprland.enable = true;
	      }
            ];

            nixpgks.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [
              # Add additional package name here
              "steam"
            ];

#            imports = [
#              ./modules/browser.nix
#              ./modules/alacritty.nix
#            ];

            home.packages = with pkgs; [
              pipewire
              hyprland
              steam
              xdg-desktop-portal-gtk
              xdg-desktop-portal-hyprland
            ];
          };
        };
      };
    };
}

Looks to me like you don’t fully understand nix syntax yet. You’re also using what looks like a mix of the old and new set of arguments for homeManagerConfiguration. Try this:

# Inside flake.nix
home-manager.lib.homeManagerConfiguration {
  inherit pkgs;
  modules = [ ./home.nix ];
};
# home.nix
{ pkgs, lib, ... }: {
  # imports = [
  #   ./modules/browser.nix
  #   ./modules/alacritty.nix
  # ];

  home.username = "mee";
  home.homeDirectory = "/home/mee";

  programs.home-manager.enable = true;
  wayland.windowManager.hyprland.enable = true;

  nixpgks.config.allowUnfreePredicate = pkg:
    builtins.elem (lib.getName pkg) [
      # Add additional package name here
      "steam"
    ];

  home.packages = with pkgs; [
    # It's likely none of these packages will work if you only add them
    # to your home-manager packages, they all require NixOS
    # configuration
    pipewire
    hyprland
    xdg-desktop-portal-gtk
    xdg-desktop-portal-hyprland
  ];

  # This value determines the Home Manager release that your configuration is
  # compatible with. This helps avoid breakage when a new Home Manager release
  # introduces backwards incompatible changes.
  #
  # You should not change this value, even if you update Home Manager. If you do
  # want to update the value, then make sure to first check the Home Manager
  # release notes.
  home.stateVersion = "23.11"; # Please read the comment before changing.
}

If you still get errors, please share them, hard to guess at what’s wrong without them.

1 Like

Yes, I had 3 configs before, but decide to put everything in 1 flake and it wasn’t working after all. As a newbie I’m very far away from fully understanding nix syntax. Just glued from different sources that broken flake. Getting read of home-manager module and configuration.nix was tempting idea and it looks like it’s not how it should be. Tnx for those configs.

I removed ; from last line in flake.nix and now there is an error about undefined variable ‘home-manager’

Well, you can:

# Inside flake.nix
home-manager.lib.homeManagerConfiguration {
  inherit pkgs;
  modules = [({ pkgs, lib, ... }: {
    # imports = [
    #   ./modules/browser.nix
    #   ./modules/alacritty.nix
    # ];

    home.username = "mee";
    home.homeDirectory = "/home/mee";

    programs.home-manager.enable = true;
    wayland.windowManager.hyprland.enable = true;

    nixpgks.config.allowUnfreePredicate = pkg:
      builtins.elem (lib.getName pkg) [
        # Add additional package name here
        "steam"
      ];

    home.packages = with pkgs; [
      # It's likely none of these packages will work if you only add them
      # to your home-manager packages, they all require NixOS
      # configuration
      pipewire
      hyprland
      xdg-desktop-portal-gtk
      xdg-desktop-portal-hyprland
    ];

    # This value determines the Home Manager release that your configuration is
    # compatible with. This helps avoid breakage when a new Home Manager release
    # introduces backwards incompatible changes.
    #
    # You should not change this value, even if you update Home Manager. If you do
    # want to update the value, then make sure to first check the Home Manager
    # release notes.
    home.stateVersion = "23.11"; # Please read the comment before changing.
  })];
};

I don’t recommend it though. It gets messy to put all this varied stuff in flake.nix, and makes it hard to share modules between multiple configurations. All the levels of indentation make it hard to visually parse what you’re editing as well, you very quickly end up in bracket hell…

IMO flake.nix is best thought of as an “index” in which you can quickly see where which output is defined, and how each input is mapped to each output, but not where the real business logic should reside. It’s an entrypoint.

1 Like

So I will keep them separated. Should I install all system packages using configuration.nix and user packages by importing modules in home.nix?

Probably, yes. For things like hyprland you can still use the home-manager modules to configure them, since ultimately it uses a configuration file in your home directory.

Installation is trickier because you need various system services, graphics libraries, PAM rules, display manager entries and whatnot to actually be installed on the system for hyprland to work, and home-manager simply cannot do that for you since it’s limited to your home directory.

Also, rather than adding things to environment.systemPackages and home.packages, check if there is an option with a .enable first.

I’m not sure I follow what you mean with “importing modules”.

1 Like

Tnx for helping me. I’m slowly moving forward.