Appearance
Introduction to Mix
Read first: Modules and functions
Source: Elixir guide, Introduction to Mix.
This guide builds a complete ApplicationEvery Elixir project is an application. Elixir itself is an application (:elixir), and so are parts of it such as :ex_unit.Read the page · Glossary: a distributed key-value store. Key-value pairs live in buckets, and the buckets are spread across nodes. A client can connect to any node. The client sends CREATE shopping, PUT shopping milk 1, GET shopping milk, or DELETE shopping eggs. The guide needs Elixir 1.18.0+ and Erlang/OTP 27+.
Three tools do the work:
- OTPOpen Telecom Platform: the set of libraries in Erlang that you use to build fault-tolerant applications. It includes supervision trees and event managers.Read the page · Glossary is the set of libraries in Erlang for fault-tolerant applications, for example Supervision treeSupervisors that have supervisors as children. The processes of an application then form a tree.Read the page · Glossary and event managers.
- Mix is the build tool. It creates and compiles projects, runs tests, and manages dependencies.
- ExUnit is the test framework.
You do not need this guide to use Elixir. Frameworks such as Phoenix, Ecto, Nerves, Nx, Membrane, and Broadway hide these details. This guide shows the foundations that the frameworks use.
Our first project
console
$ mix new kv --module KV--module KV overrides the default module name Kv.
mix.exs holds the configuration of the project. It has two public functions and one private function:
project/0returns configuration such asapp: :kv,version, anddeps.application/0supplies data for the generated application file (extra_applications: [:logger]).deps/0lists the dependencies.
mix compile builds into _build. iex -S mix opens a shell with the project loaded. After you edit the code, recompile() in the shell rebuilds the project. It returns :ok if it compiled something and :noop if it did not.
Under the hood Our own text. It is not from the official docs.
In short: compiling turns your source into files on disk, and Mix remembers what it compiled.
What mix compile writes. When we compiled a new kv project, Mix created these files under _build/dev/lib/kv:
- the compiled module (
Elixir.KV.beam, 1,592 bytes) - a small description of the ApplicationEvery Elixir project is an application. Elixir itself is an application (
:elixir), and so are parts of it such as:ex_unit.Read the page · Glossary (kv.app, 164 bytes) - the seven protocols in their consolidated form (from the Protocols chapter)
- bookkeeping files in
.mix/
When we ran mix compile again, Mix wrote no .beam file. Mix keeps a record of what it compiled, so it saw that nothing changed. For this reason a second compile prints nothing, and recompile() can return :noop.
Sources: strace -f -y -e trace=openat on mix compile with Elixir 1.20 / OTPOpen Telecom Platform: the set of libraries in Erlang that you use to build fault-tolerant applications. It includes supervision trees and event managers.Read the page · Glossary 29, and ls of _build. A .beam file starts with the bytes FOR1, the tag of its container format. The file sizes depend on the code and the Elixir version.
Running tests
By convention, every file in lib/ has a <name>_test.exs file in test/. Test files are scripts (.exs), so Mix does not compile them first.
elixir
defmodule KVTest do
use ExUnit.Case
doctest KV
test "greets the world" do
assert KV.hello() == :world
end
endtest/test_helper.exs runs ExUnit.start() before every run. A failure prints the test name, its location, the failed code, and the left and right sides of ==. To run one test, copy the location: mix test test/kv_test.exs:5. Tests run in random order.
Formatting
mix format formats the code as .formatter.exs says. Editors can run it on save. On CI, run mix format --check-formatted.
Environments
An environment changes the compilation for one scenario. Mix has three environments by default:
MIX_ENV=prod mix compile switches the environment. Mix.env/0 returns the environment as an AtomA constant with a value that is its own name, such as :ok. Two atoms are equal if their names are equal.Read the page · Glossary. start_permanent: Mix.env() == :prod makes the VM crash if the supervision tree of the application shuts down. You want this in production. You do not want it in dev or test, because a live VM helps you find the cause of a problem.
Mix is a build tool and is not available in production. Call Mix.env/0 only in mix.exs and configuration files. Never call it in lib/. mix help and mix help compile list the TaskAn asynchronous computation. You start it now and read its result later.Read the page · Glossary.