I couldn't find a nix
community, so I'm hoping it is ok that I'm posting on the nixos
one instead. I'll switch to mailing list/discord if necessary, but I have a lemmy app on my phone, and it is much easier to have an ongoing conversation from here, so I decided to give it a shot. Here goes!
While I have a NixOS laptop, I primarily use other systems (e.g. OpenSuse Tumbleweed) as of now.
I love the ability to define the packages I want installed, with home-manager
managing my command line utilities (e.g mtr, dig, protobuf etc).
I've been playing around a bit with protobuf recently, and after generating some c++ code using protoc
, I loaded up the generated code in vscode, which understandably wasn't able to find the development headers for protobuf (since they are in the nix store - /nix/store/h2h5fs8iv2a8rmlkfhr6id6y4jxwd5i1-protobuf-3.21.12/include/google/protobuf/io
)
I tried to compile the code anyways on the command line, and got some errors.
I might need an OS specific protobuf install just for the development headers, but I'm pretty sure I should be able to, and just don't know how. Here's what I get when I try to compile:
$ g++ searchReq.pb.cc
In file included from searchReq.pb.cc:4:
searchReq.pb.h:10:10: fatal error: google/protobuf/port_def.inc: No such file or directory
10 | #include
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Any tips/pointers would be appreciated. Thanks in advance!
===========================================
Edit: Thank you all, particularly @[email protected] and @[email protected] for the help. After setting the include path (compile) and LD_LIBRARY_PATH (link), things work great.
I ended up writing a small Makefile for convenience in the short run:
INC_FLAGS:=-I$(HOME)/.nix-profile/include -Icpp
LD_FLAGS:=-L$(HOME)/.nix-profile/lib -l protobuf
run: task
LD_LIBRARY_PATH=$(HOME)/.nix-profile/lib ./task
task:
g++ $(LD_FLAGS) $(INC_FLAGS) main.cpp cpp/searchReq.pb.cc -o task
regen:
protoc --python_out=python --cpp_out=cpp searchReq.proto
That's enough to get me going for now.
TODO - read up the NIXOs Wiki C page in more detail
Sounds like you have installed protobuf via Home Manager or nix-profile and/or are using the system's compiler instead of one installed through nix?
All compilers in the nix package store are wrapped in custom shell scripts that injects the needed paths the compilers are looking for in FHS directories. In case of gcc glibc and libcxx, as well as $NIX_COMPILER_FLAGS (name from the top if my head) which is populated by nix depending on what is installed. For example, if you would use a nix development shell (classic or flake based) and install protobuf there, the correct include paths the wrapped compiler needs would be contained in this variable.
Have a look at https://nixos.wiki/wiki/C which answers a few questions and look at the wrapper code of gcc (in the gcc package) which points to gcc-unwrapped.
I had protobuf installed via home-manager and gcc/g++ installed via the same.
Before posting, I had tried with both the globally installed g++ (full path specified manually to override nix one), as well as the nix store installed one. Both gave errors: one with more (the non-nix one) - I got the error I posted originally in my nix install.
I have the native distro packages installed (removed from home-manager for now to keep things sane), but I'd like to get that working too, and understand what's going on.
To add more information, after posting originally, I spun up my Nixos laptop and tried using protobuf there after I got a python native setup for test code.
The same code errored out on Nixos, even after regenerating the protobuf code locally using protoc. Python was unable to find the Google modules.
I checked nixpkgs to see if there were additional packages besides protobuf that needed to be installed. I installed both python310Packages.protobuf and python311Packages.protobuf, but kept getting the same error (I can see the modules in the nix store).
I don't have access to my laptop right now, but I'm not sure if it's related to environment variables in my dot config (I don't override variables.. I only append to them for variables such as PATH).
AFAIK installing packages via HM does not automatically give you the right includes. Either set
$NIX_CFLAGS_COMPILE
(and potentially$NIX_LDFLAGS
) manually (or pass-I
argument to the compiler or use a development shell while declaring your inputs (protobuf) there. This will then automatically populate the aforementioned env variables automagically.Thank you, will try by the weekend and update!
- works now, thank you!