Nix: Elixir Phoenix project example

Dec 7, 2021

I’ve never used Nix before.

I wanted to see how hard it would be to use Nix to configure all dependencies to run Phoenix project. I also wanted to build a docker image from the same source of truth - nix configuration. Here is the result of this quick experiment: phx-nix-example.

I plan to keep it up to date and as a source of truth when I bootstrap a project, so any suggestions are welcome. There’s a lot of more in the README so please check it out. There are also a few things more to do, and they’re listed in README.

Some thoughts on Nix:

  • Documentation isn’t that useful. The best way I found to see how something works or is configured is to read code. My approach to this is to use sourcegraph.com to search nixpkg and try to find a pattern I’m trying to apply
  • Nix is okayish.. Nix is okayish… I think it makes sense for what’s it’s trying to do but there’s definitely a learning curve.
  • I’ve managed to achieve my goal, that’s a success. It also required a lot of time. I couldn’t figure out why there are 2 Erlang versions in the final docker image until I understood how and then what to set in an overlay, it took waaaay too much time and a lot of guessing
final: prev:
let
  beam = prev.beam_minimal;
in rec {
  inherit beam;
  erlang = beam.interpreters.erlangR24;
  elixir = packages.elixir_1_12;
  hex = packages.hex.override { inherit elixir; };
  rebar3 = packages.rebar3;
  # !!! I was missing this:
  buildMix =
    prev.beam.packages.erlang.buildMix'.override { inherit elixir erlang hex; };
}
  • There’s a bit too much boilerplate for my taste and I’m closely following this thread
  • I’m now subscribed to way too many issues in both nixpkg and nix repositories like this, or this, or that one, and this one too.
  • Comma-less lists are great!
  • I like Nix, its design and goals. I’ve already started using home-manager and I’ll play with NixOS too. I plan to contribute back at least to nixpkg. Probably less so to Nix due to C++.

Other resources used to create the example