Lazy NixOS service (stopped unless network traffic)

Hi. I’m wondering whether it would be possible to wake-up some heavy services when they get a request (through nginx). The rest of the time, they’d be sleeping or stopped.

I’m thinking about NixOS containers, because I’d use docker for that usually. If I can do it with a systemd service, that would work too. But I’d prefer to avoid docker.

For context, I’d like to host two Photoprism instances. One for me, that would always be active. And one for family, that would wake up on request and sleep afterwards. Photoprism requires a lot of RAM, so I do not want to waste it 99% of the time.

Thanks

You can do this with systemd’s socket-based activation feature: systemd.socket

Doesn’t work with everything, but it works pretty well for anything http based. With some trickery you can make more complex things work too, @Solene has a blog post on doing this with minecraft: Solene'% : Using systemd to make a Minecraft server to start on-demand and stop when it has no player

Then you just need a mechanism for periodically turning the service off again.

3 Likes

@Infinisil has a more “Cloud” approach starting a server on demand when you connect, still using Minecraft as a reference :smiley: GitHub - infinisil/on-demand-minecraft

2 Likes

Thank you both, I’m gonna have a look

There’s also a nixus module for on-demand-minecraft here, making use of this NixOS module to upload DigitalOcean images, and this nixus module to set DNS records, and a nixus module for handling secrets.

Quite the custom setup and very much not directly usable for anybody other than me, but the final configuration is almost fully automated with just definitions like these, which I think is really cool!

3 Likes