serpent

serpent

devenv_new – Mix task wrapper to create projects in a new devenv.sh Nix environment

A Mix task that wraps any Elixir Mix project generator in a devenv.sh environment.

Note that you can install Nix on top of macOS or most Linux distros without requiring a full NixOS setup.

Installation

Install the archive directly from Hex:

mix archive.install hex devenv_new

Usage

Use mix devenv.new to wrap any Mix project generator:

# Create a Phoenix project with devenv
mix devenv.new phx.new my_app --devenv postgres,redis

# Create an Igniter project with devenv
mix devenv.new igniter.new my_project --devenv elixir=1.17,postgres --install ash,ash_postgres

# Create a basic Elixir project with devenv
mix devenv.new new my_lib --devenv elixir=1.17,minio --sup

# To run Mix in a temporary Elixir Nix environment
nix-shell -p elixir --run 'mix devenv.new igniter.new demo_app '\
'--devenv postgres,bun '\
'--install ash,ash_postgres,ash_authentication_phoenix,ash_graphql '\
'--auth-strategy magic_link '\
'--with phx.new'

cd demo_app
devenv up
MIX_ENV=test mix ash.reset
mix test

See story.html for a full log of a generator run.

Devenv Features

Available devenv features:

  • elixir - Elixir runtime (supports version specification, e.g., elixir=1.17)
  • postgres - PostgreSQL database with project-specific databases
  • redis - Redis cache/session store
  • minio - MinIO object storage (S3-compatible)
  • npm - Node.js runtime with npm
  • bun - Bun runtime/package manager

The generator is built to be easily extendable, by creating an .eex template file and adding it to the look-up table in devenv.new.ex.

Most Liked

jozuas

jozuas

devenv provides community-driven abstractions for developer environments—much like NixOS does for system configuration, and home-manager does for user configuration of the home directory.

You could just take a Nix shell, and define your PostgreSQL configuration with various ENV vars (PGDATA, …) to make a PostgreSQL installation “local”, but with devenv as a minimum you just need to declare:

  services.postgres = {
    enable = true;
  }

In addition to the above, devenv also provides abstractions for:

  • background process management
  • composable tasks (kind of make replacement)
  • building OCI containers
  • … and more

In my company we have been using devenv for developer environments for around 2 years now. We have employees on Linux, Windows WSL and MacOS. Each time a new OS came up, there were some env specifics that had to be resolved / improved, but the problems were not devenv related per se. Overall the experience has been overwhelmingly positive, with new employees being able to go from 0 to running a monolith with a large number of binary dependencies and source modified PostgreSQL plugins in minutes.

This is how the setup instructions roughly look like for a new MacOS user with a brand-new laptop:

  • Install direnv and setup shell hooks
  • Clone repo
  • Run a few first time setup scripts to get Nix installed and setup
  • direnv allow to allow automatic env var loading / unloading in the directory of the repo
  • devenv up to start background processes
  • make ci to confirm the application passes various static analyses and tests
  • Install Zed (as Zed requires no plugin setup - Zed supports elixir-ls and direnv out of the box. We install elixir-ls via devenv so that all devs have identical version of the language server)
  • Write prod code
jozuas

jozuas

It might feel like a lot of work, but in general, the amount of time spent is dependent on:

  • prior knowledge.
  • environment complexity. For a greenfield project, it could be 2 mins of LLM generated devenv config.

Purely on devenv configuration itself, I’ve spent at most a few full-time work days in the last two years, as lot of the time gets spent on things you’d have to do anyway, regardless of devenv:

  • PostgreSQL config shows up in devenv, and it’s a big chunk by size, but you’d need that setup no matter how you manage your dev environments.
  • If I spend time packaging something with nix, that time would still have to be spent even if there was no nix - trying to get everyone on the same package.

Nix is one of those technologies that have a large learning curve. But that’s the same with many other technologies that are worth learning - you are not going to become proficient in Haskell, Clojure, Prolog, … in a weekend. What I can say, having learned a bunch of Nix/NixOS before the advent of LLMs, is that LLMs make it way faster to develop the fundamental understanding of the Nix ecosystem. But one has to be careful with prompting as the purpose of prompting should be building understanding as opposed to hoping that LLMs can generate some black-box that works.

sezaru

sezaru

Super cool! I will try it out later.

One question, did you also setup the variable envs so all created files are encapsuled inside the project directory? For example, in my devenv config, I setup it so all postgres data will live inside the project directory (I think inside .env/.devenv/state/postgres), I also made that all elixir config files like hex, mix, erlang-history, etc are all inside the .env/.devenv/state too.

In other words, running elixir, postgres, erlang, etc never leaks anything outside the scope of the project.

serpent

serpent

Thank you for the inspiration! I was considering isolating Hex archives, actually.

zachdaniel

zachdaniel

Creator of Ash

This is pretty neat! I’ve been planning on getting into nix & devenv at some point, especially considering how important it is getting to be able to run containerized local development. Its on my short list to checkout :person_bowing:

Where Next?

Popular in Libraries Top

wmnnd
Hi there, for my project DBLSQD, I needed a file storage solution that is a bit more flexible than Arc. Because I thought others might f...
New
Qqwy
While not as prevalent as in imperative languages, arrays (collections with efficient random element access) are still very useful in Eli...
New
Eiji
ExApi is a library that I’m developing now and hope release soon This library will allow to: list all apis list all api implementation...
New
dbern
I’m excited to announce that TaxJar has developed and open-sourced DateTimeParser. We developed it because we found a need to parse user ...
New
riverrun
I’ve just released version 3 of Comeonin, a password hashing library. The following small changes have been made: changes to the NIF c...
New
tfwright
After working on it for a couple of months and using it in production for most of that time, today I’ve released LiveAdmin, a LiveView ba...
New
nikokozak
Hello all, I’ve been working on Svonix - a library for quickly integrating Svelte components into Phoenix views. It’s a much-needed succ...
New
michalmuskala
Hello everybody. I have just released Jason - a new JSON library. You might be wondering, why do we need a new library? The primary foc...
New
aditya7iyengar
Rummage.Ecto and Rummage.Phoenix provide ways to perform Searching, Sorting and Pagination over Ecto queries and Phoenix collections. Fo...
New
maltoe
Hello! Came here to announce ChromicPDF, a pet project PDF generator I’ve been working on for the past few months. Why another PDF gener...
New

Other popular topics Top

josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Sub Categories:

We're in Beta

About us Mission Statement