Understanding Elixir Mix

This and the other “Deck” posts are a repurposing of flashcard study decks to Q&A blog posts.

What do Elixir developers use Mix for?

Many different things. Mix is used for creating Elixir apps, for compiling Elixir apps after code changes, to run an apps test suite, to resolve third party dependency compatibility, and for executing custom tasks.

What does it mean to say Mix is an Elixir executable?

Executable files are files that can be run by a computer's operating system, they are not data files like your projects source code. Elixir has its own executable and Mix is another executable built on top of Elixir's.

How does Mix help with dependency management?

Through an integration with Hex package mananger Mix applications can use third party dependencies. Hex is the tool doing the package compatibility resolution. Mix provides a way to declare dependencies and command line tasks for installing, removing, or updating dependencies, these tasks use Hex to accomplish this.

How can you tell Mix that you are using third party dependencies in your project?

In your applications `mix.exs` file you can declare a private `deps` function which includes a list of your dependencies and then execute `mix deps.get` to install them.
defmodule MyApp.MixProject do 
  use Mix.Project 

  def project do 
    [ deps: deps() ] 
  defp deps do 
    [ {:ecto, "~> 2.0"} ] 

How to you find the PATH that the Mix executable exists on macOS?

You can do this through the command line by executing `which mix`.

Below is an example of a custom Mix task, how would you invoke this task?

defmodule Mix.Tasks.Hello do 
  use Mix.Task 
  def run(_) do 
    Mix.shell().info("Hello world") 

On the command line execute mix hello.

If using Ecto for database interactions what are some Mix tasks you might use?

- `mix ecto.create` - creates the Ecto db. - `mix ecto.dump` - creates a file showing the db structure. - `mix ecto.gen.migration create_accounts` - creates a migration file that will be used to alter the db structure, in this case create the accounts table. - `mix ecto.migrate` - runs migrations that have not been previously run.

What tasks does Mix provide that are specific to Phoenix applications?

- `mix phx.new app_name` - creates a new Phoenix project - `mix phx.routes` - shows a list of routes - `mix phx.server` - starts the application and all servers