Search code examples
c++eclipsecmakenix

How to use nix with Eclipse


I'm trying using package manager nix for my C++ project.

default.nix:

{ pkgs ? import <nixpkgs> {} }:
let
  stdenv = pkgs.stdenv;
in rec {
  myProject = stdenv.mkDerivation {
    name = "lynx";
    version = "dev-0.4.0";
    buildInputs = [
      pkgs.cmake
      pkgs.gtest
    ];
  }; 
}

I built the project in its directory by using cmake without any problems. Then I exported the project as an Eclipse project (I did it under nix-shell):

cd ..
mkdir lynx_eclipse
cd lynx_eclipse
cmake -G"Eclipse CDT4 - Unix Makefiles" -D PLATFORM:STRING="posix"  -DCMAKE_BUILD_TYPE=Debug ../lynx/

Having opened the exported project in Eclipse I found that include paths are fine and point to /nix/store/*. But when I try to build the project I have an error:

gtest/gtest.h: No such file or directory

I see that Eclipse doesn't add nix'es paths to the compiler flags:

 /nix/store/ix03iknfgyrx7421fppjdczd9r4sw7pz-gcc-wrapper-5.3.0/bin/g++    -I/home/ubuntu-pc/dcs/lynx/inc -I/home/ubuntu-pc/dcs/lynx_eclipse/inc -I/home/ubuntu-pc/dcs/lynx/test/./inc  -std=c++11 -static-libstdc++ -g   -o CMakeFiles/test_utils.dir/utils_test.cpp.o -c /home/ubuntu-pc/dcs/lynx/test/utils_test.cpp

That could be reason of the problem but I don't know how to change Eclipse's behaviour.


Solution

  • Judging by the number of the views the problem is very specific. However, I found the solution somehow.

    After the export to Eclipse project I made builder.sh file:

    export NIX_PATH=nixpkgs=/home/xxxx/.nix-defexpr/channels/nixpkgs
    /home/xxxx/.nix-profile/bin/nix-shell default.nix --run "make $1"
    

    Then I set the file as a build command in C\C++ General -> C\C++ Make Project

    bash ${project_loc}/build.sh
    

    That's all.