Developing Software on NixOS

23 May 2017 16:34 GMT

I have been personally developing my own software on NixOS for a while now and there isn't much information out on the web on how to do it, so here's a quick guide.

Setting up a Build Environment on NixOS

I will demonstrate in this example how to install the boost library through the Nix package manager and set up a build environment using the nix-shell.

First we will need to create a file called default.nix in the current projects root directory:

with import  {};
stdenv.mkDerivation rec {
   name = "my-program";
   buildInputs = [
     gcc
     gnumake
     cmake
     boost
   ];
}

Chances are you have already worked with the default /etc/nixos/configuration.nix file before, this file is similar in that it sets up a build environment with the packages in the buildInputs variable. In the above example we require the following packages in our build environment: gcc, gnumake, cmake and boost. You can find a list of NixOS packages on the nifty Search NixOS packages page.

All we have to do now is open up our terminal, cd to our projects' root directory and execute the nix-shell command. After we are in the nix-shell, we can execute all commands available in our build environment.

$ nix-shell
$ cmake -G "Unix Makefiles"
$ make

That's basically it.

Debugging Your Program on NixOS

Unfortunately, due to hardened compiler flags on NixOS you must export the following environment variable to be able to properly debug your programs on NixOS:

$ export hardeningDisable=all

This is considered a bug and will hopefully be resolved soon.

Conclusion

As demonstrated, setting up a build environment on NixOS isn't much more complicated than setting up a build environment on Debian based distributions. It may take a little bit of getting used to, but it is definitely worth it.

Copyright © Richard Szibele. All rights reserved.