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?