OFFSET偏移
← All articlesOFFSET / FIELD NOTESNO. 033

Home Manager integration and modular configuration

Compare standalone and NixOS module modes, then split user configuration into modules.

English translation · Code and commands follow the Chinese original.

IN THESE READING PATHSHome Manager: managing user configuration ↗
About 2 min readStart reading

Trade-offs between the two installation modes#

Feature 1. Standalone mode 2. NixOS module mode
Command to apply changes home-manager switch nixos-rebuild switch
Independence High; no system configuration changes required Low; must be included in the system configuration
Rollback System and HM rollbacks are separate Rolls back with the system (closely synchronized)
Recommended for People who want to use Nix on Arch/macOS NixOS users (strongly recommended)

In practice: integrate as a module in a flake#

This approach ensures that your system and personal configuration are managed through the same flake.lock.

1. Edit flake.nix

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

  outputs = { nixpkgs, home-manager, ... }: {
    nixosConfigurations.my-host = 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.alice = import ./home.nix;
        }
      ];
    };
  };
}

2. Create home.nix

This is the core of your personal configuration.

{ config, pkgs, ... }: {
  home.username = "alice";
  home.homeDirectory = "/home/alice";

  # 只有在这里添加的包,才属于你个人
  home.packages = [ pkgs.ripgrep pkgs.fd ];

  programs.home-manager.enable = true;
  home.stateVersion = "24.11"; # 示例初装版本,升级输入时不自动修改
}

Modularize your home.nix#

As your configuration grows, home.nix can become very unwieldy. Use imports to split it up:

# home.nix
{
  imports = [
    ./modules/zsh.nix
    ./modules/git.nix
    ./modules/vscode.nix
  ];
}

Why choose module mode?#

When you select an older version in the boot menu to roll back, module mode includes the corresponding Home Manager activation entry in the system configuration. Still check the activation service to confirm whether the user configuration has actually switched. Mutable application data, running processes, and databases do not automatically revert to a historical snapshot.

References & further reading

  1. Home Manager manual ↗
  2. Home Manager command source code ↗
  3. Home Manager file option source code ↗

Adapted from technical notes. Examples use generic names.

Continue along a reading path

Home Manager: managing user configuration

  1. 01Home Manager integration and modular configuration
  2. 02Configuring a terminal environment with Home Manager
  3. 03Desktop and theme configuration with Home Manager
  4. 04Home Manager files and symbolic links
  5. 05Secrets management with Home Manager and NixOS
  6. 06A tutorial on user-level configuration with Home Manager
CONTINUE READING / NEXT ARTICLEConfiguring a terminal environment with Home Manager

Use modules to configure Zsh, Starship, and common command-line tools.