
Everyone has seen thosedotfiles
repositories on GitHub. There's lots of different ways to manage them, but the method I use now requires no extra tooling (other thangit
and a command line shell of your choice), no symbolic links to get files into the right locations, can be triggered from any directory on disk, and is easy to replicate on a new system.
Basically what we're going to do is set up a git repository at~/.cfg
or~/.dotfiles
or any directory within your home directory of your choosing (although you probablydon't want to use~/.config
since that is the default value of$XDG_CONFIG_HOME), and a shell alias to help manage and control it.
Creating the Repo
If you're setting this up the first time, there's a few steps you'll need to take to set up. First, create the repository:
git init--bare$HOME/.dotfiles
This creates a "bare" git repository at~/.dotfiles
. Now we'll set up an alias to interact with it from any directory on disk. Add the following alias to your~/.bashrc
or~/.zshrc
or~/.config/fish/config.fish
file, thensource
the file:
# make sure the --git-dir is the same as the# directory where you created the repo above.aliasconfig="git --git-dir=$HOME/.dotfiles --work-tree=$HOME"
The--work-tree=$HOME
option sets the directory that the repository tracks to your home directory. Now, since there's probably more files in your home directory that youdon't want in the repo than files youdo want in the repo, you should configure the repo to not show untracked files by default. We can do that by setting a repository-local configuration option.
config config--local status.showUntrackedFiles no
Tracking Files
To track files in our new~/.dotfiles
repo, we just need to add them. From any directory on disk, you can run the following command to add your~/.bashrc
or~/.zshrc
or~/.config/fish/config.fish
file to your new dotfiles repo:
config add ~/.bashrcconfig add ~/.zshrcconfig add ~/.config/fish/config.fishconfig commit-m"Add .bashrc/.zshrc/config.fish file"config push
Installing on a New System
Of course, the main point of doing this is to easily sync your config across new machines. We can easily do this with a small bash script to initialize the system's dotfiles from your git repository.
#!/usr/bin/env bashgit clone--bare git@github.com:mrjones2014/dotfiles.git$HOME/.dotfiles# define config alias locally since the dotfiles# aren't installed on the system yetfunctionconfig{ git--git-dir=$HOME/.dotfiles/--work-tree=$HOME$@}# create a directory to backup existing dotfiles tomkdir-p .dotfiles-backupconfig checkoutif[$?= 0];thenecho"Checked out dotfiles from git@github.com:mrjones2014/dotfiles.git";elseecho"Moving existing dotfiles to ~/.dotfiles-backup"; config checkout 2>&1 | egrep"\s+\." |awk{'print $1'} | xargs-I{}mv{} .dotfiles-backup/{}fi# checkout dotfiles from repoconfig checkoutconfig config status.showUntrackedFiles no
Now, installing your dotfiles on a new system is as simple as running:
curl https://raw.githubusercontent.com/mrjones2014/dotfiles/master/scripts/config-init | bash
That's Really It
That's really all there is to it. Now you can easily add and track changes to dotfiles via your newconfig
shell alias.
config add ~/.config/something/somefileconfig commit-m"add somefile"config push
Comment below with your dotfiles repo links! Feel free to browse mine for inspiration.
mrjones2014 / dotfiles
❄️ My dotfiles for NixOS and macOS as a Nix flake. Neovim, Fish shell, Wezterm, etc.
Dotfiles
These are my NixOS and macOS dotfiles, packaged as a Nix flake, usinghome-manager
.
For info on how to set up my Nix flake, see thesetup instructions.
Why a Nix Flake
Nix is an incredibly complex piece of software, but despite that, I believe that Nix is the only sensible way to manage software today. Using Nix, and particularly a Nixflake, offers a few unique benefits:
- Reproducibility: Nix environments are described by text files (
*.nix
files), and as long as you stay within the guard rails, the environment should be deterministic. - Isolation: Packages can have access to their dependencies without those dependencies cluttering up the global environment — this also means different packages can depend on different versions of the same dependency without conflicts.
- Rollbacks: Totally screw up your environment by accident? Just roll back to a previous generation.
- Immutability…
Top comments(3)
For further actions, you may consider blocking this person and/orreporting abuse