Sunday, September 8, 2013

Managing your dotfiles with Puppet

As developers, we spend a lot of time tweaking our workstations to make sure it adjusts perfectly to our workflow. Most of our configuration is stored in dotfiles, ie, hidden files that start with a dot (.bashrc, .bash_profile, .vimrc, .gitconfig, etc). Lately, many developers have decided to store their dotfiles using version control. That way they have a safe copy in case anything might happen to their local computer, and it makes it easier to set up a new computer if they need to.

While everyone agrees this is a good idea, there's not consensus on what's the best way to do it. Github even published a website where it keeps track of different plugins to use and paths to follow. After trying a few of them, I decided Puppet was the best tool for this task.

To manage your dotfiles with puppet, start by creating a directory for your dotfiles (I use ~/.puppet/, but you can use any name you prefer).

Make sure you have the following structure setup:
                # copy your dotfiles here
As you can see, we're creating a "dotfiles" module, where we'll keep all the dotfiles we want to save. On the module's init.pp, we're going to tell Puppet where each of this files belong:
# dotfiles/manifests/init.pp

file { "/home/$id/.bash_profile":
    mode => 644,
    source => "puppet:///modules/bash_config/.bash_profile",

file { "/home/$id/.vim":
    mode => 755,
    ensure => directory,
    recurse => true,
    source => "puppet:///modules/vim_config/.vim",
There's a couple of things to mention from this file:
  • The $id variable will be initialized by Puppet. When your script runs, it will contain the name of the user that's running the script.
  • We can set file permissions with the "mode" param, which uses the chmod numeric permissions notation.
  • The "source" param tells vagrant where to get the file from. It uses the special puppet:/// url scheme, which starts from the root of your puppet directory (~/.puppet on our case)
  • If you want to copy an entire directory (like your .vim dir), then make sure you add "ensure => directory" and "recurse => true"
We're almost done now, but we first need to include the "dotfiles" module on our "default.pp" file
# manifests/default.pp

include dotfiles
Now, anytime you want to restore your dotfiles you just need to run
puppet apply --modulepath=$HOME/.puppet/modules $HOME/.puppet/manifests/default.pp
As you can see, using Puppet to manage your dotfiles is pretty straight forward. However, Puppet can do a lot more than that. I let Puppet handle almost every aspect of my configuration, from which apps should be installed up to which icons should show up on Unity's sidebar. If you want to take a look at how I achieve that, visit my .puppet repo on Github.
Post a Comment