Skip to content

Developing an Elixir Phoenix application

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

Initialize a new 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'

Add flake.nix to the project

  1. Initialize the template from the root directory of the project:

    Terminal window
    nix flake init github:akirak/flake-templates#elixir-app
  2. Open flake.nix and set the Erlang and Elixir versions:

    let
    # Set the Erlang version
    erlangVersion = "erlang_26";
    erlangVersion = "erlang_27";
    # Set the Elixir version
    elixirVersion = "elixir_1_15";
    elixirVersion = "elixir_1_17";
    in
  3. Add .envrc:

    Terminal window
    use flake
  4. Allow direnv:

    Terminal window
    direnv allow

Switch the language server

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.
]