serpent

serpent

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.

https://github.com/serpent213/devenv_new/

Showing Posts 1 to 10

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 OP

No special env setup, so far. PostgreSQL lives under .devenv/state/postgres by default. All state is contained within the project folder, but not necessarily under .devenv.

If you have a good env setup to share, let me know. So far it only knows about languages and services, but other classes could be added.

sezaru

sezaru

For Elixir, I define these variables:

  state_dir = config.env.DEVENV_STATE;

  mix_dir = "${state_dir}/mix";
  hex_dir = "${state_dir}/hex";

And then I set them like this:

    env.ERL_AFLAGS = "-kernel shell_history enabled -kernel shell_history_path '\"${state_dir}/erlang-history\"'";

    tasks."elixir:setup" = {
      exec = ''
        mkdir -p ${mix_dir}
        mkdir -p ${hex_dir}

        export PATH=${mix_dir}/bin:$PATH
        export PATH=${mix_dir}/escripts:$PATH
        export PATH=${hex_dir}/bin:$PATH
      '';
    };

    env.MIX_HOME = mix_dir;
    env.HEX_HOME = hex_dir;

    env.ERL_LIBS = "${hex_dir}/lib/erlang/lib";

For postgres I set this:

    env.PSQL_HISTORY = "${state_dir}/psql_history";

For node, these ones:

    env.NPM_CONFIG_CACHE = "${state_dir}/npm";
    env.NODE_REPL_HISTORY = "${state_dir}/node_repl_history";

And, if you plan to add support for rust, I set this:

    env.CARGO_HOME = "${state_dir}/cargo";
serpent

serpent OP

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:

olivermt

olivermt

Why would you want nix to run containerized stuff?

(Genuine question, the benefits seem overlapping)

sezaru

sezaru

It allows you to have full development dependencies per project and scoped only for them. It gives you access to basically all packages from Nix so you can setup it the way you want and do that in a declarative and consistent way.

IMO it is the best way to setup you development infrastructure without having to handle that at the OS level, specially if you work with multiple projects with different dependencies.

The only downside, IMO is that the nix language is kinda hard to work with if you don’t want to spend a lot of time understanding it.

PS. i’m talking here about using devenv, NixOS and Home Manager are not required to work with devenv at all (but I do like them a lot too)

olivermt

olivermt

I know what nix does, what is confusing to me is how zach is going to get benefit from nix AND containers at the same time. Unless he meant «comtainerized» abstractly achieved via nix and not oci combined with nix :laughing:

zachdaniel

zachdaniel

Creator of Ash

Sorry, I kind of conflated a lot of things in one post, but my real point is that there are a whole host of tools, like nix and devenv, that I’ve been meaning to look into, specifically for agentic workflows (which may involve containerization, repeatable builds, sandboxing etc. TBD).

serpent

serpent OP

Container from “to contain”, Docker, LXC etc. offer containers, Devenv achieves “containerisation” in a different way, without strict file system boundaries.

That being said, Nix in general has good support for bundling a config in an OCI container, see here for Devenv.

Devenv is a good way to get a feeling for Nix: You can treat your devenv.nix as a config data file without knowing the Nix language, some of the more complicated stuff can be setup via devenv.yaml, like adding additional package sources.

Without Devenv you would typically create a Flake providing a dev shell – for me that typically meant looking for and starting with a template. Always felt a bit cumbersome when I just required an ad-hoc environment to play around with some piece of software written in obscure language xy…

Where Next? Top

Trending in Announcing Top

woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. https://github.com/woylie/doggo Features Unstyled Phoenix components....
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews