How to deploy a service on a non-NixOS?

We have a cobbled-together FreeSWITCH server running in production on Debian on Azure, and finally made up my mind to re-write the whole configuration and deployment parts properly using Nix. I thought of creating a shell.nix to run it on Ubuntu at first by creating the mutable parts (logs, configs in dev, etc.) in a shellHook and just make a wrapper for the FreeSWITCH executable.

Is this a good general direction or am I overlooking some obvious alternatives?


Unfortunately, learning the ins & outs of NixOS and figuring out how to deploy it on Azure would be the best (and what Iā€™m hoping to achieve at one point), but Nix/pkgs already have my head spinning and was hoping to use the shell.nix as a stepping stone to learn what I can before diving into NixOS. Nonetheless, if you say that NixOS is the proper way to go to deploy a service with Nix then Iā€™ll just bite the bullet and go with that right away.

You can take a look at GitHub - juspay/services-flake: NixOS-like services for Nix flakes.

The closest thing pending to be supported is [RFC 0163] Portable Service Layer by svanderburg Ā· Pull Request #163 Ā· NixOS/rfcs Ā· GitHub. But it may not be ready soon.

1 Like

What do you want and what you donā€™t want much from Nix here?

  • Packages ā€” I assume you want them
  • Configs ā€” do you want NixOS config generators or are you fine writing your own simple templating for this?
  • Command line arguments ā€” do you want NixOS logic there, or are you fine writing a fixed set / trivial templating for this?

Because you can use parts of NixOS service definitions, but depending on what you want and how service is structured, different amounts of black magic might be involved based on your needs.

Nothing wrong with having a service where your neeeds are stable and simple enough that it is easier to write a service file statically and install it than using NixOS code!

1 Like

While longer-term NixOS might definitely be interesting for you, if youā€™re already at the point youā€™re comfortable creating a shell.nix with the functionality you want, it sounds like you donā€™t really need to take that plunge yet.

A shell.nix is typically meant for ā€˜manualā€™ use. Do you already have a plan for how youā€™ll make sure your services get started on system startup? From a shell.nix it might not be a big leap to wrap it in a flake and generate a systemd service for it with GitHub - serokell/systemd-nix: Generate systemd units from NixOS-style descriptions - you can find an example in raboof/nix-dependency-track: nix packaging of dependency-track - Codeberg.org

3 Likes

Wow, thank you! This looks amazing! Unfortunately I donā€™t really get Nix/OS modules yet (the same with flakes), but this gives me extra motivation to go deeper.

Great question and great breakdown of this topic. Iā€™m trying to keep the bar low for now, which means the same amateurish deployment quality, but use Nix wherever it is possible.

Didnā€™t even know that NixOS config generators can be re-used for this - but then again, I donā€™t know much about them. Nonetheless, thank you for bringing them up and giving me options!

When I used ā€œamateurishā€ above, I meant it:) The service is currently started manually in a shell, only FreeSWITCH is running in a Debian package-provided systemd service file, but I do want to learn how to properly use services.

Oooh, Iā€™ve been looking for something like this for a while now! I have been searching for ā€œsystemd services on non-nixosā€ from time to time, but serokell/systemd-nix never popped up, unfortunately.


Thanks again for for all the tips and advice!

I use GitHub - numtide/system-manager: Manage system config using nix on any distro on Arch, does the job for /etc and systemd config.
It uses the same option structure as NixOS, no need to translate abstractions. (In fact you could probably use some NixOS modules wholesale, as long as they donā€™t do too much outside of its featureset.)

1 Like

Thank you! This is another tool that Iā€™ve never heard of, but was looking for. Just thinking about the fragmentation of the Nix eco-system gives me anxiety, but then I think about all the awesome projects that crop up all the time and I think it is worth it.

Probably too black magic and heavy for your needs but I use lang-os/use-from-nixos.nix at bd9bf5e1062da140e6257e2fa5b89f3bad83b503 Ā· 7c6f434c/lang-os Ā· GitHub to evaluate NixOS and take only the parts I want (I donā€™t like parts of NixOS) like in lang-os/local/test-system.nix at bd9bf5e1062da140e6257e2fa5b89f3bad83b503 Ā· 7c6f434c/lang-os Ā· GitHub .

7c6f434c-configurations/tools-for-vps at master Ā· 7c6f434c/7c6f434c-configurations Ā· GitHub is close to your use case ā€” 7c6f434c-configurations/tools-for-vps/vps-side.nix at 36342e21c477c80c2aef1636147b92f89d69e0b3 Ā· 7c6f434c/7c6f434c-configurations Ā· GitHub mostly just copies static configs (including service files) with occasional sed for templating, but they use a known path where nix-build puts dependencies. There I wanted something simple, using default OVH boot/network config in Debian, but use the Nixpkgs versions of stuff I care about.

1 Like

Thank you! You were right that these are way over my head for now, but I really appreciate the examples (and for pointing to specific code locations, especially), because even if I found your repos on my own, Iā€™m quite confident that I wouldnā€™t have been able to tell whether they are germane to my use caseā€¦ I definitely want to up my Nix game though, and now I can dive in with zero understanding, but armed with the knowledge that the code is indeed relevant, which is super helpful:)

Do you mean here to take parts of the Nix configuration for NixOS and apply them in non-NixOS contexts? Iā€™m still having a hard time telling Nix expressions apart on the basis where they supposed to be evaluated (i.e,. NixOS or not).

This is a level Iā€™m striving to achieve at one point, to be able to cherry-pick parts because I have deep understanding of Nix(OS/pkgs)?.

aside: Looked at your post right after waking up and lang-os immediately conjured up this food in my mind:)

Yes, my laptop is Nix-generated Nixpkgs-based non-NixOS-core system, with a bit of NixOS pieces reused for things like Xorg config. use-from-nixos.nix evaluates a part of the NixOS code and provides me with store paths to use in my not-exactly-NixOS system.

The almost-static-with-minimal-sed thing is on a Debian VPS with Nix installed. I find it fine to have handwritten configs as long as they are under a VCS.

2 Likes

Another option is to build a systemd portable service image using pkgs.portableService. You use the systemd facilities in the .service files to bind configuration, data, log, etc. directories from the host into the service image. This is my preferred way to define a service with nix but run it on non nixOS. There are also good reasons to do it even on NixOS, e.g., to decouple the service and the base OS update cadence.

3 Likes

Thank you! I definitely need to dive into systemd deeper as well.