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

mhanberg
I just released the first version of Temple: an HTML DSL for Elixir and Phoenix! You can read this blog post or the docs for more info...
New
sasajuric
I’d like to announce a small library called boundaries. This is an experimental project which explores the idea of enforcing boundaries ...
New
kelvinst
Hey everyone! Well, we made this lib a while ago and now we decided to finally go out and public with it! It’s a tool for creating and m...
New
arkgil
Hi all! I’m happy to announce that Telemetry v0.3.0 is out! This release marks the conversion from Elixir to Erlang so that all the libr...
New
mindok
What is ContEx? A pure Elixir server-side data plotting/charting library outputting SVG. It has nice barcharts in particular and works g...
New
achempion
Hi, I would like to tell about my initiative to further maintain and develop Waffle project which is the fork of Arc library. The progre...
New
danschultzer
In short Plug n’ play OAuth 2.0 provider library. Just set up a resource owner schema with Ecto (your user schema), install the dependen...
New
martinthenth
Hello everybody :wave: Recently, some of my colleagues talked about database ids and uuids and their problems, and I remembered the pain...
New
alisinabh
Hey everyone i’ve developed a library for Jalaali calendar for elixir which supports converting Gregorian dates to Jalaali and vice vers...
New
bryanjos
Hi, I wanted share a small library we at Revelry Labs made for rendering react components from the server side. There are instructions fo...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Sub Categories:

We're in Beta

About us Mission Statement