We often demands flexibility and the ability to work seamlessly across multiple machines. Managing our development environment consistently on different devices can be a challenging task. In this blog post, we’ll explore how nix and home-manager can be powerful allies in ensuring a consistent and efficient development experience, regardless of the machine you’re working on.
Working on various machines introduces complexities such as differing operating systems, hardware configurations, and software versions. Here’s how nix and home-manager come to the rescue:
$ curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
We need at least two files minimal flake.nix
and home.nix
in a git directory.
$ mkdir homie
$ cd homie
$ git init
flake.nix
sample for mac arm machine (aarch64-darwin)
{
description = "my home";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
home-manager = {
url = "github:nix-community/home-manager";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { nixpkgs, home-manager, ... }: {
homeConfigurations = {
"XXX" = home-manager.lib.homeManagerConfiguration {
pkgs = nixpkgs.legacyPackages.${"aarch64-darwin"};
modules = [ ./home.nix ];
};
};
};
}
Replace XXX with machine username.
home.nix
sample for minimal configuration
{ pkgs, config, ... }: {
home = {
stateVersion = "23.11";
username = "XXX";
homeDirectory = "/Users/XXX";
# List of pkgs you want to install in to your home
packages = with pkgs; [
git
go
autojump
starship
bat
];
# List of symlink files
file.".gitconfig" = { source = ./.gitconfig; };
};
# Enable home-manager so we can use home-manager bin
programs = {
home-manager = {
enable = true;
};
};
}
$ # Always keep track all the changes
$ git add .
$ # It will download and run home-manager
$ nix run github:nix-community/home-manager -- switch --flake .
We can search more pkgs on the nixOS website https://search.nixos.org/packages or in the terminal with
$ nix search nixpkgs <pkgs>
Let say we want to install eza
{ pkgs, config, ... }: {
home = {
stateVersion = "23.11";
username = "XXX";
homeDirectory = "/Users/XXX";
# List of pkgs you want to install in to your home
packages = with pkgs; [
git
go
autojump
starship
bat
eza # New pkg
];
# List of symlink files
file.".gitconfig" = { source = ./.gitconfig; };
};
# Enable home-manager so we can use home-manager bin
programs = {
home-manager = {
enable = true;
};
};
}
$ git add .
$ home-manager switch --flake .
Navigating the challenges of working across multiple machines is made significantly easier with nix and home-manager. These tools empower us to create consistent, reproducible, and personalized development environments, fostering collaboration and enhancing productivity.