I help my clients acquire new users and make more money with their web businesses. I have ten years of experience with SaaS projects. If that’s something you need help with, we should get in touch!
< Back to article list

nix first steps: compiling a C++ program with SDL dependency.

I’ve been meaning to give Nix a try for a while. I’m somewhat convinced that the declarative, reproducible-build approach it suggests is very promising. Getting started is not trivial tough. There’s a lot of vocabulary and things.

Julia Evans wrote about some of her first steps. Here are some of mine: using nix to compile a small C++ program, with an external library.

External dependencies are usually a PITA with C/C++ programs. Even though you can something more complex with cmake (here is an example with sdl+opengl), that’s for another day : before we compile a complex project we need to be able to build a simple one.

Here is how we want to lay the code out:

├── default.nix
├── result -> /nix/store/74gp7qalrqxh11pzqqrdhl2hi18q09ri-sdl-sample
└── src
    └── main.cpp

First, here is the code. A minimalistic, black window opens. It relies on SDL2 being present.

// src/main.cpp
#include <SDL2/SDL.h>

int main(int arc, char ** argv) {
    if (SDL_Init( SDL_INIT_VIDEO ) < 0) {
        printf("SDL could not initialize!\n");
    } else {
            "SDL2 Demo",
            800, 600,


    return 0;

Now for the build script. Here is what I understand:

with import <nixpkgs> {};

stdenv.mkDerivation {
  name = "sdl-sample";
  src = ./src;
  buildInputs = [ gcc SDL2 ];
  buildPhase = "c++ -o main main.cpp -lSDL2";

  installPhase = ''
    mkdir -p $out/bin
    cp main $out/bin/

Now we can :

There are a lot of other features we did not cover, but we got it to work \o/

You May Also Enjoy