GNU Stow

October 9, 2024 · 3 min read

How do we manage configuration files like .zshrc, .vimrc, .bashrc, and other dotfiles?

When experimenting with new tools or configurations, you might accidentally break a setup that was working fine before. Do your configurations differ between your work and personal computers? What if your computer crashes—will you have to reinstall and configure everything from scratch?

These issues can be solved using version control. However, since dotfiles are scattered throughout the system, using traditional git version control presents some challenges.

gnu_stow

I solved this using GNU Stow.

What is GNU Stow?

GNU Stow is a symlink manager that helps organize and manage dotfiles efficiently. Instead of scattering configuration files across your system, Stow allows you to store them in a single directory and create symbolic links in your HOME directory. This means any changes made to a file in your dotfiles folder will automatically reflect in your system.

Key Features:

  1. Symlink farm management: Stow helps manage configurations in your HOME directory by creating symlinks from your dotfiles folder.
  2. Modular organization: Allows you to organize dotfiles into separate modules, making deployment and maintenance easier.
  3. Non-destructive: Stow won't overwrite existing files, making it safe for your current configuration files.
  4. Version control friendly: Since files are centralized, it's easier to track changes using git version control.

Stow mirrors your configuration file structure in your HOME directory, ensuring everything stays organized and consistent.

The structure looks like this:

home/
    dotfiles/
        .config/
            zsh/
                [...some files]
        .local/
            share/
                uzbl/
                    [...some files]
        .vim/
            [...some files]
        .bashrc
        .bash_profile
        .bash_logout
        .vimrc

Let's explore how to set up and use GNU Stow:

  1. Install stow If you use MacOS and have homebrew installed, simply run:
   brew install stow
  1. Create a new dotfiles directory
   mkdir $HOME/dotfiles
   cd ~/dotfiles
  1. To prevent issues, let's copy the original dotfiles. We'll test with .zshrc file.
   mkdir ~/copy_dotfiles
   cp ~/.zshrc ~/copy_dotfiles/.zshrc
   cp -R ~/.config ~/copy_dotfiles/
  1. Move configuration files to the newly created ~/dotfiles/ directory.
   cd $HOME/dotfiles
   mv ~/copy_dotfiles/.zshrc ~/dotfiles/.zshrc
   mv ~/copy_dotfiles/.config ~/dotfiles/.config
  1. Create symlinks

Navigate to your dotfiles directory and use Stow to create symlinks:

cd ~/dotfiles
stow zsh
stow config
  1. Verify
   ll -a

If successful, it should look like this:

dotfiles directory

Our HOME directory will look like this:

old config directory

  1. To track your dotfiles with Git: Using Git ensures that you can track changes, revert mistakes, and sync dotfiles across different machines. To initialize Git and commit your dotfiles:
   cd ~/dotfiles
   git init
   git add .
   git commit -m "Initial commit of dotfiles"

Additional:

Ignoring Files in Stow

By default, Stow ignores version control files like .git. You don’t have to worry about .git being symlinked.

To customize ignored files, create a .stow-local-ignore file and add your rules.

An unmodified stow ignore file looks like this:

default_ignore

If you want to exclude additional files from being symlinked, create a .stow-local-ignore file in the dotfiles directory and list the filenames or patterns you want to ignore.