DEV Community

Cover image for Mix: A Beginner’s Guide to Elixir’s Build Tool and Dependency Manage
Rafael Andrade
Rafael Andrade

Posted on

Mix: A Beginner’s Guide to Elixir’s Build Tool and Dependency Manage

Introduction

Mix is Elixir’s default build tool, designed to automate common tasks like project creation, compilation, testing, and dependency management. While similar to tools like dotnet CLI, Mix offers deeper extensibility and is tightly integrated with Elixir’s ecosystem.

Core Features

  1. Creating a Project

Generate a new Elixir project with a single command:

mix new friends  
Enter fullscreen mode Exit fullscreen mode

This creates a structured project with a mix.exs file at its core.

  1. The mix.exs File

This file defines project settings, dependencies, and tasks. Here’s a minimal example:

defmodule Friends.MixProject do  
  use Mix.Project  

  def project do  
    [  
      app: :friends,  
      version: "0.1.0",  
      elixir: "~> 1.18",  
      start_permanent: Mix.env() == :prod,  
      deps: deps()  
    ]  
  end  

  def application do  
    [extra_applications: [:logger]]  
  end  

  defp deps do  
    [] # Dependencies go here  
  end
end 
Enter fullscreen mode Exit fullscreen mode

Dependency Management

Adding Dependencies

Edit the deps function in mix.exs:

defp deps do  
  [  
    {:ecto_sql, "~> 3.0"},  
    {:postgrex, ">= 0.0.0"}  
  ]  
end  
Enter fullscreen mode Exit fullscreen mode

Note: Unlike npm or cargo, Mix lacks a mix deps.add command—edit mix.exs manually.

Restoring Dependencies

mix deps.get      # Fetches dependencies
mix deps.compile  # Compiles them
Enter fullscreen mode Exit fullscreen mode

Updating Dependencies

Update a single package:

mix deps.update ecto_sql  
Enter fullscreen mode Exit fullscreen mode

Update all packages:

mix deps.update --all
Enter fullscreen mode Exit fullscreen mode

Mix uses a mix.lock file to ensure consistent versions across environments.

Common Workflows

Compiling the Project

mix compile  
Enter fullscreen mode Exit fullscreen mode

Running Tests

mix test
Enter fullscreen mode Exit fullscreen mode

Extending Mix

Aliases

Mix aliases are custom shortcuts defined in your mix.exs file to simplify or combine repetitive tasks. They allow you to:

  • Chain multiple Mix commands into one.
  • Create project-specific workflows.
  • Simplify complex build/test/deploy processes.
def project do  
  [  
    ...,  
    aliases: aliases()  
  ]  
end  

defp aliases do  
  [  
    build_test: ["compile", "test"],  
    "build:test": ["compile", "test"]  
  ]  
end  
Enter fullscreen mode Exit fullscreen mode

Run with:

mix build_test  
Enter fullscreen mode Exit fullscreen mode

Custom Tasks

Create reusable tasks (e.g., lib/mix/tasks/hello.ex):

defmodule Mix.Tasks.Hello do  
  @moduledoc "Prints a greeting: `mix help hello`"  
  use Mix.Task  

  @shortdoc "Prints 'hello' to the console"  
  def run(_args) do  
    IO.puts("hello")  
  end  
end  
Enter fullscreen mode Exit fullscreen mode

Run with:

mix hello
Enter fullscreen mode Exit fullscreen mode

Why Mix Shines

  • Extensible: Frameworks like Phoenix and Ecto add their own tasks (e.g., mix ecto.gen.repo).
  • Batteries Included: Built-in tasks for testing, compilation, and dependency management.
  • Community-Driven: Most Elixir libraries integrate seamlessly with Mix.

Conclusion

Mix is the backbone of Elixir development. Its simplicity for common tasks and flexibility for customization make it indispensable.

Next Steps:

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay