Devenv vs services-flake vs?

Hi, all,

I’m starting to use nix flakes for development of mostly Drupal sites, sometimes others, generally always with a need for a daemonized language runtime, a database, and a web server.

Most Drupal developers these days use a Docker-based solution – ddev is currently the leading one, but there are otthers. This is a pretty decent experience, but I’m wondering if I can get close with a Nix-based solution.

This is purely for development right now – I was trying to create a flake that would start all of these services and then drop into a devShell, but could only get the first running, was trying to fork and trap so I could have a command to start and stop the servers, and didn’t get there.

I’ve found that both Services-flake and Devenv are trying to solve this problem – would love to hear comparisons between them, or if there’s a simpler way to do this?

My goals are:

  • Have an easy one-command nix run to download a project, start all the servers, and kick off an install script
  • Be able to start a new project with nix flake init --template github://org/my-project
  • Be able to run different language versions in different projects by changing a single line in the project’s flake
  • Be able to run several projects simultaneously (on different ports)
  • make this portable to run on Darwin/Windows

Would love to hear anybody’s experiences working with these!

5 Likes

I use devenv.sh for PHP development:

  • Have an easy one-command nix run to download a project, start all the servers, and kick off an install script

Here is a sample nix config that I use on Mac.
To start working I do:

nix develop --impure path:./nix -c $SHELL
devenv-custom up path:nix
  • Be able to start a new project with nix flake init --template github://org/my-project

You can package your typical project templates and put on GitHub

  • Be able to run different language versions in different projects by changing a single line in the project’s flake
  • Be able to run several projects simultaneously (on different ports)

Devenv.sh allows it.

  • make this portable to run on Darwin/Windows

It should be portable as long as there are nixpkg for both systems.

In my case I use Mac’s built-in apache as a server which won’t work on Windows, but it’s possible to setup a server via devenv.sh as well.

I haven’t tried services-flake though so can’t compare.

1 Like

We’re a Drupal shop and I made drupal-devenv with pretty-much the same goals as you. I tried to get as close to ddev in terms of making something that Just Works™.

It’s probably easiest if I answer each goal individually:

Have an easy one-command nix run to download a project, start all the servers, and kick off an install script
Be able to start a new project with nix flake init --template github://org/my-project

Unfortunately, when setting up an environment you have to copy some stuff to devenv.yaml, after that it’s just devenv up.

IIRC, I looked into it and devenv doesn’t support arbitrary project templates. If it did, that would turn setup into a one-liner; I should file a feature request.

Be able to run different language versions in different projects by changing a single line in the project’s flake

Devenv supports this, and by extension drupal-devenv does too.

  1. Be able to run several projects simultaneously (on different ports)

Hopefully it won’t disappoint you to hear that drupal-devenv doesn’t need to use multiple ports to run multiple projects simultaneously. Caddy seems to handle this and SSL for localhost reasonably well.

  1. make this portable to run on Darwin/Windows

Unfortunately, we’re a small shop and we all run Linux. And I’m pretty sure the code that does privilege escalation, so Caddy can use ports 80 and 443, is Linux specific. I’d love a PR that makes it work on Mac and WSL!

I was trying to create a flake that would start all of these services and then drop into a devShell

In devenv, this would be devenv up -d and devenv processes down. I do find the latter to be slightly less reliable than Docker Compose, meaning sometimes I have to go pid-hunting, which is a bummer but not a show-stopper.

I haven’t tried Services-flake, so I’m sorry I can’t speak to the comparison part of your question. I started out trying to use Flakes with Devenv, but it’s not the easiest way to use Devenv so I gave up and used the Nix module approach the project recommends. That turned out great, as it eased me into Nix gently. Then once I understood flakes, I was able to read the one devenv creates in each of my projects and steal ideas from it.

I hope this helps! Have you made any progress since creating this thread?

1 Like

Thanks for sharing! This looks like a great starting point. I am hitting some issues trying to get it to work on a NixOS desktop – the setcap call on Caddy is failing with “Read only filesystem” and a bunch of other little quirks and issues, but I did get it going by running a sysctl command to make it so Caddy could use port 443…

Had some other issues getting Devenv up and running, but did get it to the point where I’m running Drupal CMS using your config.

1 Like

That’s great news! If it helped one other person, it was worth open sourcing!

I am hitting some issues trying to get it to work on a NixOS desktop – the setcap call on Caddy is failing with “Read only filesystem”

I wish it worked perfectly, but I’m not surprised to hear this. drupal-devenv was the first thing I put together using Nix, so it’s pretty naively done. I’m also on Ubuntu + Nix, so with hindsight that setcap idea was never going to work generally, and especially not on NixOS.

Thank you for raising that issue, I’ll think about how we can address it.

but I did get it going by running a sysctl command to make it so Caddy could use port 443

Sweet! Presumably by making port 443 and up unprivileged? I had come across that solution myself, while it’s not something drupal-devenv could adopt I’m glad you’ve got something that works.

I’m sure you’re busy, but if you think of a general way to address the port issue, or something that just works for NixOS, please let me know. Also, if you have a minute, I’d like to hear about the other ‘quirks and issues’ you mentioned. Thanks!

Ok I wasn’t overall very happy with the feel of using devenv – overall it just feels a little clunky compared to a regular flake.

I spent the weekend trying out services-flake, and while I had to create a service for phpfpm, I’m very happy with where it is now – now anyone with nix (and nix-command/flake enabled) can spin up a fresh Drupal CMS install with a single line:

nix run github:freelock/drupal-flake#demo

… and I already have a template to load this into existing projects. It’s working really well!

For now I’ve skipped a few things…

  • Uses a high port to avoid needing root for ports 443, 80
  • Uses *.ddev.site as a domain, which is a DNS zone that the DDev project makes always resolve to localhost, so I don’t need to add a hosts entry
  • Does not generate a cert – am thinking about using Caddy instead of Nginx to get this (but services-flake doesn’t yet have a Caddy service definition)

I’m currently running this on NixOS. I’m curious whether it works for others on different platforms?

2 Likes

Ho cool! I’m highly interested to see how you did to package Drupal! Do you plan to put it online somewhere?

Think you can use from the flake uri GitHub - freelock/drupal-flake: A flake to jump-start Drupal development

This is a very good start! I wish Drupal could be built with php.buildComposerProject2 instead.

Also, I would replace nginx with caddy

Curious to hear more about this, I’d be happy to fix it :slight_smile:

1 Like

Devenv feels a little more clunky – takes more to get a particular project running, can’t do a one-liner to get a demo site up – at least I didn’t figure that out.

I still feel like a newbie to Nix! What benefit does php.buildComposerProject2 bring?

I did start with Caddy, but Services Flake already bundles Nginx so I ran with that to start – was thinking I would add a Caddy service to Services Flake and then switch over to that.

… I have been using this flake extensively for the past few days, and I think it has a really nice feel – I can go into an existing Drupal project, and:

  1. nix flake init -t github:freelock/drupal-flake
  2. cp env.example .env
  3. edit .env with project name, port, php version
  4. direnv allow
  5. nix run

… and in a few minutes it’s all up and running, ready to import a database backup!