Mix is a versatile tool for Elixir similar to npm from Javascript. See how easy it is to use it
All about mix
Mix is a versatile tool used in elixir for
- installing dependencies
- creating apps ( in elixir or phoenix)
- compiling code
- running tests
- formatting/linting code
- running custom tasks
In addition to creation apps, compiling and formatting code, Mix provides tasks for phoenix and ecto.
To see a list of all options available in Mix
$ mix help
To see phoenix or ecto specific tasks
$mix help | grep ecto
Create elixir projects
Use mix new
to create a project
$mix new my_project
add a --sup
flag to create a supervised application
$mix new my_project --sup
To create an umbrella app
$mix new my_project --sup --umbrella
The above statement creates a new supervised elixir project with an apps folder. Inside this apps folder, we can house additional self contained elixir applications that are “micro services”
To create these “micro services” do the following
$cd my_project
$cd apps
$mix new my_service
Create phoenix projects
To create a phoenix application use the mix tool.
$mix phx.new app_name
You can pass additional flags to phx.new when generating the phoenix app.
pick a database
$mix phx.new app_name --database msql
Postgres is the default database used by phoenix. The other options are
- mysql
- mssql
- sqlite3
Specify which webserver you want to use
The default web server that ships with Phoenix is cowboy. However there is a new option available for us - bandit. It is a web server written in pure elixir
$mix phx.new app_name --adapter bandit
Create api only project
Often you might want to create an API only project in elixir, in which we might not need a web interface, db support.
In this case pass these additional options to generate a phoenix project. i.e. no dashboard, no javasript, no gettext etc
mix phx.new api_project --no-dashboard --no-gettext --no-html --no-live --no-mailer --tailwind
the above creates a new phoenix project without a dashboard, without any browser views ( no html/css), no tailwind, no mailer to send emails and no support for liveviews.
If you Api does not need access to a database via code you can also skip ecto support by passing --no-ecto
to the list of options.
Run mix tasks
$ mix compile --force
Run tests with mix
Run the command mix test
$mix test
Use mix to format code
$mix format
to ensure code is formatted
mix format --check-formatted
The above line is usually added to a script in a CI/CD pipeline