Recently, I started learning about Elixir and at first, I was somewhat unsure about how to setup my development environment. After reading the various recommendations from the Elixir community, my questions remained. This is when I asked on Twitter to gather more thoughts on the matter. It led me to research further how I could setup a reproducible development environment for Elixir. I will share with you how I achieved this with Nix.
What Is Nix? Link to heading
Did a colleague or a friend ever tell you “But it works on my computer!” ? No…? Well consider yourself lucky then! For the unlucky ones, you know how this sucks… you simply want to run some application on your computer and it doesn’t work. It’s perhaps a missing undocumented dependency or some dependency version mismatch.
You don’t have to go through this. Do not rely on system packages, this is asking for trouble. Leave your runtime versions manager like asdf behind. They aren’t much better. Those approaches aren’t reliable and reproducible. This is where Nix comes in. It’s a purely functional and cross-platform package manager. It can be installed in various ways, this will depend on your operating system.
Once you have Nix on your computer, you can create reproducible development environment with nix-shell.
The nix-shell Command Link to heading
One of the commands provided by Nix is nix-shell
.
Whenever executing nix-shell --pure
in your terminal, the command looks for
the file shell.nix in the current working directory. This file contains Nix
code to create a shell or if you prefer, your development environment. As for
the --pure
flag, it ensures your system configuration and its packages do not
interfere with the shell you created just now. Everything else stays the same in
this shell, you can freely navigate your filesystem, run a Phoenix application
or start IEx, the world is yours!
Show Me the Money Link to heading
Let’s have a look at a real-world shell.nix example. The comments will walk you through what happens. You can horizontally scroll in the code block below if it’s not fully visible on your device.
|
|
Give this nix-shell a try by copying it in one of your Elixir projects, then
running nix-shell --pure
. Adapt it to your needs, perhaps you want to change
the Elixir or Erlang version. There is a lot of information on the Nix
packages available for Elixir and Erlang on
GitHub.
Regarding other Nix packages, you can find them with the Nix Search.
Will You Reproduce This? Link to heading
Let me know how it went for you by sending me a message on the Contact Me page. Constructive feedback is always welcomed!