Rakshan Shetty

Setting up Elixir with Nix

October 02, 2022☕️ 2 min read

Setting up Elixir dev environment with Nix


  • nix - Follow this guide to install nix in your system

  • direnv - helps to initialize nix on the shell when you cd into the project directory


Fork rakshans1/elixir-starter by clicking on Use this template and you are good to go.

To manually setup a project

Initialize Niv

Niv helps to manage your dependencies by pinning them to specific versions

Create an empty folder and run

nix-shell -p niv --run 'niv init'

This will create nix folder with some files

└── nix
    ├── sources.json
    └── sources.nix

Now create default.nix file inside nix folder and add the following

{ sources ? import ./sources.nix
, pkgs ? import sources.nixpkgs { }

with pkgs;

buildEnv {
  name = "builder";
  paths = [


Create shell.nix at the root of the project dir and add the following to it

{ sources ? import ./nix/sources.nix
, pkgs ? import sources.nixpkgs { }

with pkgs;
  inherit (lib) optional optionals;

  basePackages = [
    (import ./nix/default.nix { inherit pkgs; })

  inputs =  basePackages ++ lib.optionals stdenv.isLinux [ inotify-tools ]
    ++ lib.optionals stdenv.isDarwin
    (with darwin.apple_sdk.frameworks; [ CoreFoundation CoreServices ]);

  hooks = ''
    # this allows mix to work on the local directory
    mkdir -p .nix-mix .nix-hex
    export MIX_HOME=$PWD/.nix-mix
    export HEX_HOME=$PWD/.nix-hex
    # make hex from Nixpkgs available
    # `mix local.hex` will install hex into MIX_HOME and should take precedence
    export MIX_PATH="${beam.packages.erlangR25.hex}/lib/erlang/lib/hex/ebin"
    export PATH=$MIX_HOME/bin:$HEX_HOME/bin:$PATH
    export LANG=C.UTF-8
    # keep your shell history in iex
    export ERL_AFLAGS="-kernel shell_history enabled"
    export MIX_ENV=dev

mkShell {
  buildInputs = inputs;
  shellHook = hooks;

  LOCALE_ARCHIVE = if pkgs.stdenv.isLinux then "${pkgs.glibcLocales}/lib/locale/locale-archive" else "";


Finally, to use the above setup in your shell environment we ask direnv to use nix whenever we entire this project directory

Create .envrc on root of project directory and add


and run

direnv allow

This will install the specified version of erlang and elixir

elixir --version

Erlang/OTP 25 [erts-13.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit:ns]

Elixir 1.14.1 (compiled with Erlang/OTP 25)

Tags: Tutorials, Elixir
Rakshan Shetty

Software engineer, Learning Web development and sharing my experience