How to use the command `home-manager` (with examples)
home-manager
is a command-line tool that allows users to manage their environment using Nix, a purely functional package manager. It enables users to define and activate configurations for their user environment, making it easy to reproduce and share their environment settings across different machines. This article provides examples of two common use cases of the home-manager
command.
Use case 1: Activate the configuration defined in ~/.config/nixpkgs/home.nix
Code:
home-manager build
Motivation:
The motivation to use this example is when you have made changes to your home.nix
file and want to see the resulting configuration without actually switching to it. The home-manager build
command is useful for simply building the configuration and verifying that there are no errors before switching to the new configuration.
Explanation:
home-manager
: This is the command used to manage the user environment.build
: This subcommand instructshome-manager
to build the configuration defined in thehome.nix
file located at~/.config/nixpkgs/
.
Example output:
building the user environment...
created /nix/store/...-home
created /nix/store/...-home-link
created /nix/store/...-home-mock
created /nix/store/...-manifest.nix
Use case 2: Activate the configuration and switch to it
Code:
home-manager switch
Motivation:
The motivation to use this example is when you want to activate and switch to a different configuration defined in the home.nix
file. This is useful when you want to update your user environment with new settings defined in the file.
Explanation:
home-manager
: This is the command used to manage the user environment.switch
: This subcommand instructshome-manager
to activate and switch to the specified configuration defined in thehome.nix
file located at~/.config/nixpkgs/
.
Example output:
switching to the new user environment...
created /nix/store/...-home
created /nix/store/...-home-link
created /nix/store/...-home-mock
created /nix/store/...-manifest.nix
setting up user environment...
Conclusion:
home-manager
is a powerful tool for managing user environments using Nix and the home.nix
configuration file. The build
subcommand allows users to build the configuration and verify its correctness, while the switch
subcommand activates and switches the user environment to the specified configuration. By utilizing home-manager
, users can easily manage and share their environment settings across different machines.