What Is Mix?
Mix is Elixir's build tool, bundled with every Elixir installation. It handles project scaffolding (mix new), compilation, dependency management, running tests, and generating production releases — the same role that tools like npm/webpack or Maven/Gradle play in other ecosystems, but built directly into the language toolchain.
Cricket analogy: Mix is like the team manager who handles everything off the field — selection, logistics, scheduling — so players can focus purely on the game.
Mix Tasks and Project Structure
A Mix project is defined by mix.exs, which specifies the project's name, version, Elixir version requirement, and a deps/0 function listing dependencies. Custom tasks can be defined by creating a module under lib/mix/tasks/ that uses Mix.Task and implements run/1, letting teams add project-specific commands like mix seed.demo_data that behave just like built-in tasks such as mix test or mix compile.
Cricket analogy: Running mix test is like calling for a net session on demand — a single command that sets up the pitch and runs the drill.
defmodule MyApp.MixProject do
use Mix.Project
def project do
[
app: :my_app,
version: "0.1.0",
elixir: "~> 1.16",
deps: deps()
]
end
defp deps do
[
{:phoenix, "~> 1.7.0"},
{:ecto_sql, "~> 3.11"},
{:jason, "~> 1.4"},
{:credo, "~> 1.7", only: [:dev, :test], runtime: false}
]
end
endManaging Dependencies with Hex
Hex is Elixir and Erlang's package manager and public repository at hex.pm. Adding {:jason, "~> 1.4"} to deps/0 and running mix deps.get downloads the package and locks its resolved version (along with transitive dependencies) into mix.lock, ensuring every developer and every deployment installs the exact same dependency tree. The ~> operator specifies a version requirement — ~> 1.4 allows any 1.x release >= 1.4.0, following semantic versioning conventions.
Cricket analogy: Adding a Hex dependency is like signing an overseas player through the auction — you pull in proven talent (a package) rather than building that skill in-house.
Environments and Releases
Mix supports distinct environments — :dev, :test, and :prod by default — configured via config/dev.exs, config/test.exs, and config/prod.exs, each imported conditionally based on Mix.env(). For deployment, mix release (backed by Elixir's built-in :mix_release / Distillery's successor) packages the compiled application, the Erlang runtime, and all dependencies into a self-contained artifact that doesn't require Elixir or Mix installed on the target server.
Cricket analogy: Building a release with mix release is like preparing the final XI for a World Cup final — a locked, production-ready squad, distinct from the wider training pool.
Dependencies marked runtime: false (like Credo or Dialyxir) are compiled but never started as part of your application's supervision tree — forgetting this flag on a dev/test-only tool can bloat your production release or, worse, cause it to try starting a GenServer that was never meant to run in production.
- Mix is Elixir's built-in build tool: scaffolding, compiling, testing, dependency management, and releases.
- mix.exs declares the project metadata and dependency list via the deps/0 function.
- Custom Mix tasks live under lib/mix/tasks/ as modules implementing Mix.Task's run/1.
- Hex (hex.pm) is the package registry; mix deps.get resolves and downloads packages into mix.lock.
- Version requirements like ~> 1.4 follow semantic versioning, allowing compatible minor/patch updates.
- Mix environments (:dev, :test, :prod) each load their own config file based on Mix.env().
- mix release produces a self-contained deployable artifact bundling the app, deps, and the Erlang runtime.
Practice what you learned
1. What file declares a Mix project's dependencies?
2. What does the version requirement ~> 1.4 allow?
3. What does mix deps.get do?
4. What is the purpose of runtime: false on a dependency?
5. What does mix release produce?
Was this page helpful?
You May Also Like
Phoenix Framework Basics
An introduction to Phoenix's router, controllers, contexts, and plug pipeline — the core building blocks of a Phoenix web application.
Testing with ExUnit
A practical guide to writing, organizing, and running tests in Elixir using ExUnit's assertions, fixtures, tags, and doctests.
Ecto and Databases
How Ecto's Repo, Schema, Changeset, and Query modules work together to safely model, validate, and query data in Elixir.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics