In this tutorial, you will learn how to add flake.nix to an Elixir Phoenix project.
Initialize a new project
Section titled “Initialize a new project”Make
mixexecutable available:Terminal window nix shell nixpkgs#elixirInstall the scaffolder for Phoenix using
mix archive.installcommand. You need to do this only once per machine:Terminal window mix archive.install hex phx_newUse
mix phx.newto scaffold a new project with your preferred options:Terminal window mix phx.new 'your_new_project' --no-ectoEnter the directory:
Terminal window cd 'your_new_project'
Add flake.nix to the project
Section titled “Add flake.nix to the project”Initialize the template from the root directory of the project:
Terminal window nix flake init -t github:akirak/flake-templates#elixir-appOpen
flake.nixand choose the BEAM package set and Elixir version:let# Select the Erlang/OTP package set from nixpkgsbeamVersion = "beam27Packages";beamVersion = "beam28Packages";# Set the Elixir versionelixirVersion = "elixir_1_19";elixirVersion = "elixir_1_20";inThe template maintains Erlang and Elixir versions through
nixpkgsBEAM packages:beamVersionselects a BEAM package set such asbeam28Packages.erlangis taken from that package set, so the exact Erlang/OTP release is maintained by the pinnednixpkgsrevision in yourflake.lock.elixirVersionselects the Elixir package from the same BEAM package set, such aselixir_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, updatebeamVersion. To move to a newer Elixir release, updateelixirVersionto an attribute available in that BEAM package set.Add
.envrc:Terminal window use flakeAllow direnv:
Terminal window direnv allow
Switch the language server
Section titled “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.]