Does CGO comments work on nix?

Hi, I’m trying to use nix build for go project.
It has cgo flags configured in *.go file like this.

#cgo CFLAGS: -I${SRCDIR}/include -O2 ~~~
#cgo LDFLAGS: -L ${SRCDIR}/lib -lxxx -lyyy

When I enter nix develop and go build it works, but it doesn’t work with nix build.

       > /nix/store/7y75b2ac80chlh6knld3943y46n3v9kf-go-1.21.4/share/go/pkg/tool/linux_arm64/link: running gcc failed: exit status 1
       > /nix/store/zzxh4c6lg7f04sav05h4hca94d4hch1m-binutils-2.40/bin/ld: cannot find -lxxx: No such file or directory

This issue is already mentioned in Go - NixOS Wiki (not 100% sure but looks similar). But have no idea how to fix this. Especially when I have *.so files in the source file which is not packaged in nix.

And I have additional CFLAGS in *.go file like -02. Does all the cgo comments not work on nix?

Sorry for noob questions.

bin/ld: cannot find -lxxx: No such file or directory

Judging by the error message the gcc was not able to locate the library’s header file.
By the looks of it you need to add the relevant libraries to nativeBuildInputs.

Especially when I have *.so files in the source file which is not packaged in nix.

If something is not being managed by git, it will not be available during builds, so you will need to generate these .so files as a part of your derivation. Maybe by running make in the preBuild phase?

Oh thanks… Added all *.so files to git and it works fine. Wasted so much hours…