Make files can make environment set-up simple.

DISCLAIMER: I’m not good at this yet.


I wrote a lengthy setup setup script in bash that I use to deploy my dotfiles whenever I move them to a new machine.

I keep the whole thing in a git repository so it’s as easy as

git clone git@repo:some-repo
./setup.sh

It’s been working for me for a few years but I was always dissatisfied with the way the script handled certain things or needed to be manually massaged when something wasn’t quite right.

So today, I decided to finally try my hand at replacing my script with a MAKEFILE. Make is available everywhere and is probably already installed on your system. It’s well documented and efficient. And it already does all the things my script was doing. Why re-invent the wheel?


An example: my vimrc

I created a github repo recently for my vimrc and related files.
It’s pretty simple - a .vimrc file, a .vim directory and the makefile itself. I just have to clone the repository, symlink a couple files and update the plugins.

Not really big enough to merit a setup script or makefile - but what the hell - I used it as my first experiment.

This is it:

vimrepo: softlinks 
    @echo "-----------------------------------------------------------"
    @echo "Vim and environment"
    @echo "-----------------------------------------------------------"
    @echo "Open vim and type :PlugInstall to install extension manager"

softlinks: apps 
    @test -L ${HOME}/.vim && unlink ${HOME}/.vim || true
    @(test -d ${HOME}/.vim && mv ${HOME}/.vim ${HOME}/.vim_old) || true
    @ln -vsf ${PWD}/.vim ${HOME}/.vim
    @(test -L ${HOME}/.vimrc && unlink ${HOME}/.vimrc) || true
    @(test -f ${HOME}/.vimrc && mv ${HOME}/.vimrc ${HOME}/.vimrc_old) || true
    @ln -vsf ${PWD}/.vimrc ${HOME}/.vimrc

apps:
    @sudo apt install vim

When I type make vimrepo the make utility starts at the vimrepo label which tells it that items under softlinks are dependencies. It then sees that items under appsare dependencies of that! It finally gets to the apps label where it installs vim (or just returns a message that vim is already installed).

In the softlinks label, make checks and removes or renames existing softlinks or files/directories and softlinks the .vimrc file and the .vim directory from the repository to my $HOME directory.

Finally, the vimrepo label just echoes some messages out to the screen telling what happened. In this case - what to do next.


As I said - it’s a simple example, but it works for me so far. In my short time googling for answers I’ve found a lot of options for decision making and portability in the Makefile syntax, none of which I’ve touched here.

Make is a powerful utility used by programmers to automate building of complex applications with multitudes of options. But it’s also a clean, easy-to-understand method of doing smaller tasks more efficiently.

Let me know if my makefile is blatantly wrong. I’m looking forward to replacing my old setup script with makefiles in other more complex projects!


References: