DEV Community

Cover image for Portable configurations with dotfiles and GNU Stow 🐧
Cristian Zapata
Cristian Zapata

Posted on

Portable configurations with dotfiles and GNU Stow 🐧

Since I started using Linux, I realized the great capacity it gives us as developers to interact directly with the system through the terminal. It's fantastic, but for very curious people like me, it can be a bit dangerous. Playing around in the terminal, I deleted something I shouldn't have and had to reinstall the operating system, a funny anecdote in my opinion. However, the issue changed when I had to reinstall all the applications I use and, on top of that, configure them from scratch.

Like everything in life, you have to see the good side of things and view everything as a learning opportunity. I started thinking, "Is there a way to save my configurations and apply them more easily?" And yes, it can be done, and today I'll show you how. But first...

What are dotfiles?

Simply put, dotfiles is a repository where we store all our configuration files for the applications we use daily. These files are usually hidden because they start with a dot (.), hence the name dotfiles.

We can also set up specific scripts for certain tasks or our aliases to improve our terminal workflow. If you don't know what an alias is or how to implement one, here is my blog on the topic for you to check out.

What will we need?

First of all, we must consider that usage and implementation vary from system to system, so in this blog, we'll see how to do it on our favorite penguin system 🐧.

Git and GitHub

As mentioned earlier, we will be working with a repository, so we will need Git to manage all our changes and keep track of the updates we make. Additionally, we need a place in the cloud to store them and have them available from any computer where we want to implement them, so I recommend GitHub.

Stow

This application, which is part of the GNU project, is the magic behind dotfiles as it will allow us to create symbolic links for our configuration files from our dotfiles to the configuration files originally in our system, ensuring they apply correctly.

IDE and Terminal

Lastly, we will need a terminal to run various commands for both Git and Stow and a text editor to make the different configurations we want in our applications. I recommend using Vi, Vim, or Neovim since they can be used from the terminal and unify the development process, but this is a personal choice.

Useful Commands

Here are some commands that will greatly help us achieve our goal and interact more easily with our file system from the terminal. Something to consider is that these commands work for Unix-based systems like Mac and Linux.

Move to a directory

cd folderName
Enter fullscreen mode Exit fullscreen mode

Go back to the previous directory

cd ..
Enter fullscreen mode Exit fullscreen mode

Move to the home directory

cd
Enter fullscreen mode Exit fullscreen mode

Create a directory

mkdir folderName
Enter fullscreen mode Exit fullscreen mode

Delete a directory

# If it's empty
rm folderName

# If it's not empty
rm -rf folderName
Enter fullscreen mode Exit fullscreen mode

List the contents of a directory with a detailed format, including hidden files (only works with directories).

# Affects the current directory
ls -la

# Affects the specified directory
ls -la /folderName
Enter fullscreen mode Exit fullscreen mode

View the content of a file

cat fileName
Enter fullscreen mode Exit fullscreen mode

Create a file

# Usando touch
touch fileName

# Usando vim
vim fileName
Enter fullscreen mode Exit fullscreen mode

Delete a file

rm fileName
Enter fullscreen mode Exit fullscreen mode

Development

Now that we know everything we'll need, we can begin our mission. The first thing we need to do is create the folder for our dotfiles in our home directory.

mkdir $HOME/dotfiles
Enter fullscreen mode Exit fullscreen mode

Now we need to initialize a repository in our dotfiles directory.

# Move to the directory
cd dotfiles

# Initialize the repository
git init
Enter fullscreen mode Exit fullscreen mode

Now we run the git status command to check the status of our repository. It should look something like this:

On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)
Enter fullscreen mode Exit fullscreen mode

At this point, we can push our repository from local to remote, following the guide on GitHub.

Before using Stow, we need to ensure that the structure of our dotfiles matches the structure of the configuration files as they are by default in our system.

Image description

Also, the files we want to link cannot exist in the destination before running the stow command, as trying to create the link will result in an error. To fix this, we will delete the files in our destination, which is our home in this case.

rm fileName
Enter fullscreen mode Exit fullscreen mode

Or, as a personal recommendation, you can rename them instead.

mv fileName.txt fileName.back
Enter fullscreen mode Exit fullscreen mode

With this done, we run our command to create the links. It's very important to run the command from the main dotfiles folder.

stow .
Enter fullscreen mode Exit fullscreen mode

Now we can go to our home directory to verify if the links were created correctly. We can check this by listing our files and seeing something like this:

Image description

Here we can see how our init.lua file in our home is linked and taking the content from our init.lua in our dotfiles.

With this setup, we can start adding all the configuration files for our applications, scripts to run when needed, or anything we can imagine. You can make your dotfiles whatever you want.

When you switch systems and want to apply your configurations, you just need to clone the repository in the home, rename the default files, and run stow from the dotfiles.

Thank you!

I hope this helps you as much as it has helped me to have my configurations always at hand and gives me great flexibility to work on any system I want.

Here are my dotfiles so you can have an idea of how I'm working on them, and of course, any improvements are welcome :).

If you found this interesting or know someone who might benefit from this, don't hesitate to share it. It would help me a lot.

And finally, if you want to contact me, don't hesitate to say hi on Instagram or GitHub.

Blessings, folks!

Top comments (0)