This document describes the steps I took to manage my NixOS configurations using Git with a bare repository and symbolic links. The goal was to version control my system configuration files efficiently, ensure consistency, and simplify the management of these files.
Today, I faced the challenge of version controlling my NixOS configuration files, which reside in the `/etc/nixos` directory. The main issues were:
- Ensuring all configuration files are backed up and version controlled.
- Managing system configuration files in a way that they are easy to update and deploy across different systems.
- Deciding on the best method to handle these files without duplicating them or creating inconsistencies.
To manage my configuration files, I set up a bare Git repository in my home directory. This allows me to track changes to the files without creating a working directory.
# Create a bare repository in the home directory
git init --bare $HOME/.dotfilesI moved the configuration files from `/etc/nixos` to a new directory within my `nixos` folder in the home directory.
# Create the directory structure
mkdir -p $HOME/nixos/etc/nixos
# Move the files
sudo mv /etc/nixos/configuration-append.nix $HOME/nixos/etc/nixos/
sudo mv /etc/nixos/flake.lock $HOME/nixos/etc/nixos/
sudo mv /etc/nixos/hardware-configuration.nix $HOME/nixos/etc/nixos/
sudo mv /etc/nixos/secrets.nix $HOME/nixos/etc/nixos/
sudo mv /etc/nixos/configuration.nix $HOME/nixos/etc/nixos/
sudo mv /etc/nixos/flake.nix $HOME/nixos/etc/nixos/
sudo mv /etc/nixos/home.nix $HOME/nixos/etc/nixos/I created symbolic links from the original locations in `/etc/nixos` to the new locations within my `nixos` folder. This ensures that the system continues to work as expected while the files are managed in the home directory.
# Create symbolic links
sudo ln -s $HOME/nixos/etc/nixos/configuration-append.nix /etc/nixos/configuration-append.nix
sudo ln -s $HOME/nixos/etc/nixos/flake.lock /etc/nixos/flake.lock
sudo ln -s $HOME/nixos/etc/nixos/hardware-configuration.nix /etc/nixos/hardware-configuration.nix
sudo ln -s $HOME/nixos/etc/nixos/secrets.nix /etc/nixos/secrets.nix
sudo ln -s $HOME/nixos/etc/nixos/configuration.nix /etc/nixos/configuration.nix
sudo ln -s $HOME/nixos/etc/nixos/flake.nix /etc/nixos/flake.nix
sudo ln -s $HOME/nixos/etc/nixos/home.nix /etc/nixos/home.nixUsing my predefined alias `dotfiles`, which simplifies Git commands, I added the files to the version control system.
cd $HOME/nixos
dotfiles add etc/nixos/configuration-append.nix
dotfiles add etc/nixos/flake.lock
dotfiles add etc/nixos/hardware-configuration.nix
dotfiles add etc/nixos/secrets.nix
dotfiles add etc/nixos/configuration.nix
dotfiles add etc/nixos/flake.nix
dotfiles add etc/nixos/home.nix
# Commit and push the changes
dotfiles commit -m "Add NixOS configuration files"
dotfiles pushSymbolic links were chosen over other alternatives due to several advantages:
- **Simpler Repository Structure:** Keeps the repository structure simple and organized, primarily focusing on the home directory.
- **Seamless Integration:** Allows the system to use the files in their expected locations without needing to move them back and forth.
- **Immediate Reflect:** Changes in the symlinked file are immediately reflected in both locations, ensuring consistency.
- **Reduced Risk of Discrepancies:** Since there is only one physical file, there is no risk of the file being out of sync between the repository and the actual configuration.
Using Git with a bare repository and symbolic links provides an efficient and consistent way to manage and version control NixOS configuration files. This method ensures that all changes are tracked, easily updated, and deployed across different systems.
