Caffeinated Simpleton

Hackon — A tool for managing dev environment variables

While flying across the country to visit family this week, I found myself without internet or patience. So I found a small problem and opened up some manpages; the result is hackon. It’s heavily inspired by virtualenvwrapper and virtualenv, but it’s not nearly as awesome. It is a lot more general though.

In brief, hackon is a tool that lets you turn on and off named sets of environment variables. It also lets you temporarily override environment variables.

The problem hackon solves is relatively simple. The web app development world is moving from monolithic applications that produce HTML to polyglot services that produce and consume JSON (or other machine-readable output). Tying all those services together can be challenging. One tried and true method for doing this is through environment variables. Environment variables are the great unifier in this respect: everything knows how to read them and there are many ways of managing them.

For instance, if you’re deploying your application on Heroku, all configuration is done through environment variables. A lot of these variables can have sensitive information contained in them and therefore are set either through the console or the Heroku CLI. They are not checked into source control. Your code can assume that those variables will be present wherever it’s running and fail quickly if they are not.

Docker is another tool that’s all in on environment variables. When you link two containers together, information about the linked containers are injected into the environment in a predictable way. This allows you to assume in your code that you’re in a docker-like environment, which allows you to quickly and easily combine a bunch of services together without worrying about how they’ll talk to each other. This even works in highly dynamic environments where services are coming and going.

Even the frontend is getting in on the action with tools like envify. Envify is great, it allows you to write your code like it’s going to run server side (in node.js), but if it’s actually being bundled up to run in the browser, all the references to process.env are represented by their values.

All these tools mean that no matter where we’re deploying and no matter what language we’re deploying in, we can configure these things using environment variables. However, I found that as I created more services with more clients connecting to them I needed some help managing the constantly in-flux variables as I added services and dependencies.

Enter hackon

hackon doesn’t do much, but it helps with the problem I described. I use it in conjunction with fig to manage docker-based projects. I’ll walk through an example to demonstrate how I use this.

Starting a project

Let’s say you want to start a new project that keeps track of Christmas lists. It uses Facebook login for authentication. You have 3 different concerns already.

There are a few things that we’re going to need to configure through the environment:

To manage all this while we’re developing, we just run hackon santas-list. This will create a new environment for this project. Next we need to add our environment variables.

hackon sethackenv DATABASE_URL="postgres://postgres@postgres/?sslmode=disable" FB_APP_ID=1234567890

There are some assumptions there that you can modify to match your environment.

Now you’re pretty much set. You can use your FB_APP_ID in both the frontend (assuming you’re using envify) and in the REST API. It’ll always be available. If you’re done working on this project, you can just run stophacking and the environment will go back to the way it was. Run hackon santas-list and your DATABASE_URL and FB_APP_ID are back in business.

More complicated stuff

There’s a couple more commands for making hackon more useful for more complicated stacks. If you have multiple services running and you want to point to a new service temporarily (for instance, because you’re making changes to multiple services at once), then you can temporarily override an environment variable by running hackon override. You can restore a specific variable by running hackon restore. If you run stophacking, all overridden variables are restored to their original values.

That’s all!

Currently the only other feature is tab-completions that are pretty helpful. They only work in ZSH (in fact, I’m not sure this works in bash at all), but pull requests from bash users are welcome!

If you think you might find hackon useful, head on over to the Github repo. It’s Apache 2.0 licensed. Comments are welcome, as are issues.

comments powered by Disqus