Search code examples
c++ncursesnixos

Cannot include <ncurses.h> header using nixos derivation


I have this simple c++ code:

#include <ncurses.h>
int main ()
{
    return 0;
}

which I try to build with this shell.nix file

{stdenv , ncurses}:
with import <nixpkgs> {}; {
     testEnv = stdenv.mkDerivation {
       name = "helloTest";
       buildInputs = [stdenv ncurses];
     };
}

Running the command:

g++ main.cpp -lncurses -o main

I get the error output:

main.cpp:1:10: fatal error: ncurses.h: No such file or directory
#include <ncurses.h>
         ^~~~~~~~~~~

although I do have ncurses installed:

$ locate ncurses.h
/nix/store/ffjl7aw7f0gjwv4gb4mgb0w49v5dhrkg-ncurses-6.0-20171125-dev/include/ncurses.h

For the moment I was using a dedicated docker container for c++ builds. But I would like to avoid that, really awkward, and use the nix system properly.

Any idea what I'm obviously missing ?


Solution

  • It seems that it works fine after fixing the shell.nix file and entering the commands below. The problem with shell.nix was that it was unnecessarily wrapped in a lambda. Its parameters were shadowed by the with; expression anyway, so you can just remove the outer lambda.

    $ cat shell.nix
    with import <nixpkgs> {};
    {
         testEnv = stdenv.mkDerivation {
           name = "helloTest";
           buildInputs = [stdenv ncurses];
         };
    }
    $ nix-shell
    
    [nix-shell:~/tmp/so-54524324]$ g++ main.cpp -lncurses -o main
    
    [nix-shell:~/tmp/so-54524324]$ ./main 
    
    [nix-shell:~/tmp/so-54524324]$ echo $?
    0