Stow for Dotfile Management

Posted on Jan 1, 2024

While most users on both Linux and MacOS familiar with customisation or development have experience with saving their dotfiles and pulling them down across new/multiple installs, many don’t seem to use a tool to symlink these at all. Instead what I mostly see is people pulling their dotfiles repo from Github and then manually moving the files to where they need to be e.g. ~/.config/tmux/tmux.conf as an example.

However, for a number of years I have been using the tool Stow to immediately symlink my files where they need to be, resulting in my setup time dramtically reducing when configuring a new machine how I want.

By simply cloning my dotfiles repo, changing into the directory and running stow tmux, Stow will automatically create the symlink to the correct location ~/.config/tmux.

Part 0 - Prerequisites

Obviously to start the Stow application needs to be installed, it’s in the official repos for all major Linux distributions and via brew for Mac:

$ sudo apt install stow
$ sudo dnf install stow
$ sudo pacman -S stow
$ brew install stow 

Part 1 - Setup Folder Structure

This is the most important step, the folder structure needs to be correctly setup in your dotfiles folder to mimic the locations the files would usually reside in.

As an example, usually I would have my Alacritty terminal config under ~/.config/alacritty/alacritty.yml. In my dotfiles directory managed via Stow this would be ~/Dotfiles/alacritty/.config/alacritty/alacritty.yml.

Similarly, for Tmux the same would apply. The usual location would be ~/.config/tmux/tmux.config. The Stow version in my dotfiles would be ~/Dotfiles/tmux/.config/tmux/tmux.conf.

Visualised this would look like the following:

Dotfiles
├── alacritty
├──── .config
├────── alacritty
├──────── alacritty.yml
├── tmux
├──── .config
├────── tmux
├──────── tmux.conf

This can be amended to however you want to place the files.

Just as a useless example this could be a test file usually placed under ~/.local/share/tmux/test. With the dotfiles directory this would be ~/Dotfiles/tmux/.local/share/tmux/test.

So you can see the directories within the dotfiles directory should always be the application name e.g. alacritty or tmux followed by the relevant location where this should be symlinked to underneath that.

Part 2 - Version Control

Now that the Dotfiles directory and subsequent application files have been added in using the correct directory structures, this should now be kept under version control. Whether that’s within Github/Gitlab, your own personal Gitea instance or just a folder copied from an external storage drive is up to you. But I do strongly recommend using some form of version control so that you can easily keep track of changes and also make the dotfiles easy to grab on a different machine.

My preferred method is on Github as this makes it extremely simple and safe from any local data wipes or drive failures. As such on a new machine I would simply use git clone to bring down the Dotfiles directory.

NOTE: Be sure not to include any sensitive data in your dotfiles if you intend on publishing them on a third party service such as Github or Gitlab - regardless of whether the repo is private or not.

Part 3 - Using Stow

Now it’s just a case of changing into the dotfiles directory and running stow on the applications you wish to create the symlinks for:

$ cd Dotfiles

$ stow alacritty
$ stow tmux
$ stow sway

Or if you simply want all the applications in your dotfiles directory you can make this even simpler and run against all applications:

$ cd Dotfiles

$ stow *

With that all your dotfiles should now be symlinked to the appropriate locations and it’s all achieved within a matter of seconds.