Skip to content

Developing an Elixir Phoenix application

In this tutorial, you will learn how to add flake.nix to an Elixir Phoenix project.

  1. Make mix executable available:

    Terminal window
    nix shell nixpkgs#elixir
  2. Install the scaffolder for Phoenix using mix archive.install command. You need to do this only once per machine:

    Terminal window
    mix archive.install hex phx_new
  3. Use mix phx.new to scaffold a new project with your preferred options:

    Terminal window
    mix phx.new 'your_new_project' --no-ecto
  4. Enter the directory:

    Terminal window
    cd 'your_new_project'
  1. Initialize the template from the root directory of the project:

    Terminal window
    nix flake init -t github:akirak/flake-templates#elixir-app
  2. Open flake.nix and choose the BEAM package set and Elixir version:

    let
    # Select the Erlang/OTP package set from nixpkgs
    beamVersion = "beam27Packages";
    beamVersion = "beam28Packages";
    # Set the Elixir version
    elixirVersion = "elixir_1_19";
    elixirVersion = "elixir_1_20";
    in

    The template maintains Erlang and Elixir versions through nixpkgs BEAM packages:

    • beamVersion selects a BEAM package set such as beam28Packages. erlang is taken from that package set, so the exact Erlang/OTP release is maintained by the pinned nixpkgs revision in your flake.lock.
    • elixirVersion selects the Elixir package from the same BEAM package set, such as elixir_1_20.

    When you update nixpkgs, the concrete patch versions of Erlang and Elixir can change even if these attribute names stay the same. To move to a newer major Erlang line, update beamVersion. To move to a newer Elixir release, update elixirVersion to an attribute available in that BEAM package set.

  3. Add .envrc:

    Terminal window
    use flake
  4. Allow direnv:

    Terminal window
    direnv allow

Lexical is the default language server for Elixir, but you can change it to a different one:

[
erlang
elixir
# You are likely to need Node.js if you develop a Phoenix
# application.
nodejs
# Add the language server of your choice.
inputs.lexical.packages.${system}.default
elixir-ls
# I once added Hex via a Nix development shell, but now I install
# and upgrade it using Mix. Hex installed using Nix can cause an
# issue if you manage Elixir dependencies using Mix.
]