direnv and nix

The plan

  1. quick direnv intro
  2. talk about the nix integration
  3. workshop!

direnv: what is it?

direnv is a language-agnostic environment switcher

think RVM, nvm, phpenv, …​

depends only on Bash

direnv: install

nix-env -i direnv or brew install direnv or apt-get install direnv or fetch binaries on http://direnv.net

and then:
$ echo 'eval "$(direnv hook bash)"' >> ~/.bashrc

(or equivalent for zsh, fish, tcsh, …​)

how does it work?

direnv workflow

loading of the .envrc:

  1. create bash sub-shell
  2. inject the stdlib
  3. source ~/.config/direnv/direnvrc (or ~/.direnvrc)
  4. source .envrc file

quick demo

…​

the stdlib

helpers for common patterns

security framework

  • all .envrc files are checked against the authorization list
  • direnv allow or direnv edit + save to authorize execution

BREAK

nix and direnv

Adding software dependencies

the nix-shell approach

use_nix() {
  direnv_load nix-shell --show-trace "$@" --run 'direnv dump'
  if [[ $# = 0 ]]; then
    watch_file default.nix
    watch_file shell.nix
  fi
}

the profile approach

nix-profile() {
  local profile=$PWD/.direnv/nix-profile
  mkdir -p .direnv

  nix-env --show-trace -p "$profile" -iAr "$@"

  load_prefix "$profile"
}

DONE

Thanks for listening!

Time to get the laptops out and hack away

/