Portable configurations with dotfiles and GNU Stow 🐧
Tags: linux, git, systems, opensource
Published: 2024-05-22 19:09:36

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](https://theposintech.hashnode.dev/simplify-your-terminal-commands) 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

```zsh
cd folderName
```

Go back to the previous directory

```zsh
cd ..
```

Move to the home directory

```zsh
cd
```

Create a directory

```zsh
mkdir folderName
```

Delete a directory

```zsh
# If it's empty
rm folderName

# If it's not empty
rm -rf folderName
```

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

```zsh
# Affects the current directory
ls -la

# Affects the specified directory
ls -la /folderName
```

View the content of a file

```zsh
cat fileName
```

Create a file

```zsh
# Usando touch
touch fileName

# Usando vim
vim fileName
```

Delete a file

```zsh
rm fileName
```

# 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.

```zsh
mkdir $HOME/dotfiles
```

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

```zsh
# Move to the directory
cd dotfiles

# Initialize the repository
git init
```

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

```zsh
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vcw0e7mwt4gf31ddkoo9.jpg)

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.

```zsh
rm fileName
```

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

```zsh
mv fileName.txt fileName.back
```

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

```zsh
stow .
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p84n0146f84gzuhllfs5.png)

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](https://github.com/theposi/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](https://www.instagram.com/theposintech/) or [GitHub](https://github.com/theposi).

Blessings, folks!