Using direnv with ruby and 12factor apps

Intro

Jonas Pfenniger Chevalier IRL

zimbatm everywhere else

@zimbatm on twitter, github, skillmatter, …​

Works at Pusher on the pubsub messaging product.

Overview

25 minutes

  • presenting direnv
  • how to use with ruby
  • …​ and 12factor apps

Context

  • shell
  • prompt
  • environment variables

Enter direnv

brew install direnv
echo 'eval "$(direnv hook bash)"' >> ~/.bashrc
exec bash

supports bash, zsh, fish, tcsh and more

packages for homebrew, debian, archlinux and nixos. or just grab the binary.

how it works

hook into shell

direnv export executed before every prompt * looks for .envrc in the current directory and up * if found, loads the .envrc in bash and get the ENV diff * then injects the diff into the current shell (with state serialization) * otherwise unloads

TODO: mtime, allowed

TODO: decision graph

tldr; cd into director, loads env, cd out, unloads env

security model

~/.config/direnv/allowed/$(hash(path + .envrc content))

woot

basic usage

demo

one more thing - the stdlib

use

PATH_add load_prefix dotenv source_env / source_up layout / _ruby use

build your own ruby version switcher

ruby-install ruby 1.9.3

~/.direnvrc

use_ruby() {
	load_prefix "$HOME/.rubies/ruby-$1"
}

.envrc

use ruby 1.9.3-p551

small improvements

~/.direnvrc

use_ruby-version() {
  use ruby "$(< .ruby-version)"
  layout ruby
  PATH_add bin
}

.envrc

dotenv
use ruby-version

pulling external dependencies

load_brew() {
	...
}

or ./configure --prefix=$HOME/.custom/<my-lib>

or use_nix

pros and cons

+ simple, can reason about + language-agnostic + flexible + easy to debug (if you know unix)

  • need bash on the machine
  • needs integration with IDE

Questions ?

/