Source Control /etc dot Config Files in *nix
How do you manage your configuration files in a /etc folder? In this post, we go over a few ways to source control config files from /etc.
Join the DZone community and get the full member experience.Join For Free
For the past few years, I have been using NixOs as my primary OS. All NixOs configuration files are located in the
/etc/nixos folder and I needed to find a way to manage configuration files in the
/etc folder. But, there are few open source options available to manage and source control config files.
One option is to create a separate git repository and, on every change, copy/paste the modified files into the configuration repository in your home folder. This solution is troublesome at best, as you need to perform several steps to source control the system files.
Another option is to use git as the root user and create a repository in the folder where the configuration files are located. This solution is not optimal, as every time the configuration files change, you need to login as root, commit and push the changes, and there are security concerns, as we do not want root access to git. NixOs allows for installing application binaries specific for the users who need the app to use git only for the main user, and root does not have git installed.
Last year I started using git bare repositories that allow me to manage
/etcconfiguration files that are owned by root with the main user.
What's the difference between a standard repository created using
git init and
git init --bare?
git initcommand are called working directories. In the top-level folder of the repository, we can see:
.gitsubfolder with all the repository's git related revision history.
- a working tree, or checked out copies of your source controlled files.
Repositories created with
git init --bare contain no working or checked out copy of your source files. Bare repositories store the git revision history of your repo in the root folder of your repository instead of in a
Where Do I Start?
First, let’s create a bare repository in the user’s $HOME directory:
git init --bare $HOME/bare_repo/
The second step is to create an alias for git to operate on the bare repo.
git-bare = "git --git-dir=$HOME/bare_repo/ --work-tree=/etc/nixos"
Hint: In the above case, I only added a
/etc/nixosfolder. You can add all config files in
/etcif you are not using NixOs.
Now we can add all files in the folder to be tracked
git-bare add .
Hint: You can only add individual files to be tracked by the git bare repo instead of all files For example,
git-bare add file_name
The last step is to configure git to not show, or alert us about, untracked files.
git-bare --local status.showUntrackedFiles no
Now we can add the repository to GitHub.
git-bare remote add origin email@example.com:user/repo.git
Hint: Make sure you already have a GitHub repository before adding remote.
You can use an alias as a git command for bare repositories.
git-bare commit -am "commit message"
Note: The only limitation of the above solution is that you can’t pull the configuration files from the remote, as the command runs as a user, but the root owns the files.
Opinions expressed by DZone contributors are their own.