
Managing Dotfiles with Nix and Home Manager: A Complete Guide
In this post, I’ll share my personal configuration for managing dotfiles using Nix and Home Manager. This solution provides a declarative and reproducible way to maintain your Linux system configuration, enabling easy synchronization across different machines and efficient version control.
Why Nix and Home Manager?
Nix is a purely functional package manager that offers several advantages:
- Reproducibility: Configurations are identical on any machine
- Declarative: You describe the desired state, not the steps to get there
- Rollbacks: You can easily revert changes
- Isolation: Configurations don’t interfere with each other
Home Manager, built on top of Nix, allows us to manage user environment configuration declaratively.
Repository Structure
My configuration is organized as follows:
dotfiles/
├── install.sh # Installation script
├── home-manager/
│ ├── home.nix # Main configuration
│ └── modules/ # Configuration modules
│ ├── astro.nix # AstroVim
│ ├── aws.nix # AWS CLI
│ ├── docker.nix # Docker
│ ├── git.nix # Git
│ ├── tmux.nix # Tmux
│ └── zsh.nix # ZSH
Installation Script
The install.sh script automates the initial setup:
#!/bin/bash
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'
# Utility functions
log() {
echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
}
# ... [rest of the script]
The script performs the following tasks:
- Verifies it’s not running as root
- Installs Nix if not present
- Installs Home Manager
- Clones or updates the dotfiles repository
- Configures necessary symbolic links
- Applies the configuration
Main Configuration (home.nix)
The home.nix file is the main entry point:
{ config, pkgs, ... }:
{
imports = [
./modules/git.nix
./modules/zsh.nix
./modules/docker.nix
./modules/aws.nix
./modules/tmux.nix
./modules/astro.nix
];
home = {
username = "kobo";
homeDirectory = "/home/kobo";
stateVersion = "23.11";
packages = with pkgs; [
# Basic tools
curl wget btop ripgrep
neofetch fd tree ncdu
# Development
gcc gnumake cargo rustc
# Kubernetes
kubernetes-helm k9s kubectx
kubectl kustomize
# Others
duckdb inetutils
];
};
programs.home-manager.enable = true;
}
Main Modules
AstroVim (astro.nix)
- Neovim configuration with AstroVim
- Preloaded plugins and configurations
- LSP and autocompletion support
AWS (aws.nix)
- AWS CLI configuration
- Integration with aws-vault
- Useful aliases for common operations
Docker (docker.nix)
- Basic Docker configuration
- Aliases and development tools
- Container integration
Git (git.nix)
- Global configuration
- Common aliases
- Integration with LazyGit
- Git LFS support
Tmux (tmux.nix)
- Modern configuration
- Popular plugins
- Themes and customization
- Intuitive keybindings
ZSH (zsh.nix)
- Powerlevel10k
- Useful plugins
- Custom aliases
- Integration with development tools
Package Management
The configuration includes a wide selection of packages:
-
Basic tools:
curlandwgetfor data transferbtopfor system monitoringripgrepandfdfor efficient searchingtreeandncdufor directory visualization
-
Development:
- Rust (
cargo,rustc,rust-analyzer) - C/C++ (
gcc,gnumake) - Node.js (
pnpm)
- Rust (
-
Kubernetes:
kubectl,helm,k9skubectxandkustomize
-
Databases and utilities:
duckdbfor OLAP analysisinetutilsfor communications
Installation and Usage
- Clone the repository:
git clone https://github.com/kobogithub/dotfiles-home-manager.git
cd dotfiles-home-manager
- Run the installation script:
chmod +x install.sh
./install.sh
- Verify the installation:
home-manager --version
Updates
To keep your configuration up to date:
cd ~/dotfiles
git pull
home-manager switch
Conclusion
This configuration provides a robust and reproducible development environment. The combination of Nix and Home Manager makes managing dotfiles easy and ensures your environment is consistent across all your machines.
The complete code is available on GitHub. Feel free to fork it and adapt it to your needs!