Build NixOS modules as regular derivation


I have a set of NixOS definition (a LGTM stack). I want to deploy them through an internal tool which only takes files as input (more or less). I want to know how can I take these definition, build them as regular derivation (to get a result/{files}), so I can copy and paste them in my tool?

Thanks in advance,

It’s relatively easy. If you were to run nixos-rebuild build, this line would get executed:

nix-build '<nixpkgs/nixos>' -A system

However, that only works on nixos, on other systems you get an error:

error: file 'nixos-config' was not found in the Nix search path (add it using $NIX_PATH or -I)

But it already explains that we can use the -I flag to solve this, and with the help of the section on NIX_PATH in the manual, we find that this is the final solution:

nix-build '<nixpkgs/nixos>' -A system -I nixos-config=configuration.nix

For me, this returns another error because I am on macOS:

error: assertion '(stdenv).isLinux' failed

But if you’re on Linux, this should work perfectly fine and will place the symlink result in your working directory.

I assume the gist you posted only shows part of the configuration.nix, as it’s missing the start and end of the function?

1 Like

Apologies for the necro, but OH MY GOD THANK YOU SO MUCH!

I don’t know WHAT kind of non-deterministic BLACK MAGIC CURSED SORCERY I had managed to perform, but I somehow managed to break my nixos system, latest generation was still bootable, but all the prev generations were bricked and any attempts to update would give the exact same error message of no nixos-config being found in the search path, even if the configuration was the default configuration or my normal one. I scoured the entire internet, forum posts, reddit threads, guides, wikis, everything, but this exact error message was nowhere else. But thanks to your message, I figured out that I could execute sudo nixos-rebuild switch -I nixos-config=configuration.nix and after that all my stuff worked just like normal again??? I don’t know how I caused this but all I know was that this helped me solve it. So thank you for that. My Nix-OS system LIVES ON, NO REINSTALLING NEEDED!!!

1 Like