Dependencies
A Mix project in Elixir ships with the standard library and nothing else. A package outside it is a dependency: a line in the deps/0 function of mix.exs, a download from Hex, and a folder under deps/. This lesson shows the block and the command. The packages themselves are described and not run here, because the environment where this course is checked has no Hex and no network.
The deps block
A dependency is a tuple of the package name and a version requirement. The function returns a list, and the requirement follows the version syntax of Version:
# in mix.exs defp deps do [ {:jason, "~> 1.4"}, {:plug, "~> 1.15"} ] end
~> 1.4 accepts 1.4 and later 1.x, but not 2.0. A dependency can also come from a Git repository with git: or from a folder with path:, which is how two projects of one team share code before a release. The full file is the module of the Mix projects lesson, with this list in place of the empty one.
mix deps.get
mix deps.get reads the list, resolves the versions and downloads what is missing. It needs the Hex package manager, which is a one-time install with mix local.hex:
mix local.hex
mix deps.get
The two commands are not run on this page: there is no network here and no Hex, so their output cannot be shown. A reader with a connection runs mix local.hex once, then mix deps.get in the project; the command writes the lockfile mix.lock, which pins the exact versions a team builds with.
mix deps lists the state of every dependency, and mix deps.update NAME moves one to a newer version that still fits the requirement. Both read the lockfile, and both are also marked as not run here:
mix deps
mix deps.update jason
Hex and the ecosystem
Hex is the package registry of the Elixir community, and every package is a normal Mix project with a version and a list of its own dependencies. The packages a reader meets first are described here and not run:
- jason reads and writes JSON, with Jason.encode/1 and Jason.decode/1.
- plug is the layer a web server uses for requests and responses; Phoenix builds on it.
- ecto maps rows to structs and builds queries; it needs a database driver of its own.
mix hex.info jason prints the metadata of a package from the registry, which is another command that needs the network and is not run here:
mix hex.info jason
The applications that ship with OTP
Not every application is a download. Erlang/OTP ships a set of applications, and a project names the ones it needs in extra_applications. The logger is one of them, and the program can ask whether it is already started:
apps.exs
IO.inspect(Application.ensure_all_started(:logger)) IO.inspect(Application.spec(:logger, :vsn) |> to_string() |> String.starts_with?("1."))
elixir apps.exs
{:ok, []}
true
The empty list means the application was already running, so nothing had to start. The second line reads the version from the specification and checks the major number; the exact version moves with OTP, so the program prints a boolean rather than the string.
Starting an application
ensure_all_started/1 starts an application and its dependencies, and answers the list of the applications it actually started. Stopping the logger first makes the list non-empty:
start.exs
IO.inspect(Application.stop(:logger)) IO.inspect(Application.ensure_all_started(:logger))
elixir start.exs
:ok
{:ok, [:logger]}
The {:ok, [:logger]} is the answer a project gets at boot for the applications of its extra_applications list. A dependency outside OTP is the same kind of application, fetched by mix deps.get and started by Mix before the project code runs; the OTP applications lesson shows the mod: key that starts a tree of its own.
The programs on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container. The commands of Hex are marked as not run, as the environment has no network.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Where does a Mix project list its dependencies?
Answer
In the deps/0 function of mix.exs
The deps/0 function returns the list of tuples that mix deps.get resolves.
-
What does
~> 1.4accept?Answer
1.4 and later 1.x, but not 2.0
The pessimistic requirement allows the last part to grow, not the major version.
-
What does
mix deps.getneed that a plainmix compiledoes not?Answer
The Hex package manager and a network
Hex resolves and downloads the packages; the command is not run in this offline environment.
-
What does
mix.lockrecord?Answer
The exact versions the project was resolved with
The lockfile pins the resolved versions so every machine builds the same code.
-
What does this program print?
IO.inspect(Application.ensure_all_started(:logger))Answer
{:ok, []}
The logger is already started by the runtime, so the list of newly started applications is empty.
-
What does this program print?
IO.inspect(Application.stop(:logger)) IO.inspect(Application.ensure_all_started(:logger))Answer
:ok then {:ok, [:logger]}
After the stop, ensure_all_started starts the logger and lists it.
-
Which key of a dependency tuple points at a local folder?
Answer
path:
path: names a folder of another project, which needs no registry.
-
Which of these is an application that ships with Erlang/OTP?
Answer
logger
The logger is part of OTP; the other three are Hex packages.
-
What does
Application.ensure_all_started/1answer?Answer
{:ok, list of the applications it started}
The list names the applications that were not running yet and were started.
-
Why does this page mark the Hex commands as not run?
Answer
Because the environment has no network and no Hex
Without a network the download cannot happen, so the page describes the commands and marks them.
Article author: Arthur Isaev