Just recently updated to the latest nixos-unstable commit.
go build --ldflags '-extldflags "-static"' . was working before.
Now it results in:
time go build --ldflags '-extldflags "-static"' . 18:10:02
# k8s.io/kubernetes/cmd/hyperkube
/nix/store/wz2nx10xv5m6hn5j91w15abxnlz7sjsp-go-1.6.2/share/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/nix/store/s0kpgl8a101lkymj599f5a04s4wflf5s-binutils-2.26-dev/bin/ld: cannot find -lpthread
/nix/store/s0kpgl8a101lkymj599f5a04s4wflf5s-binutils-2.26-dev/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
go build --ldflags '-extldflags "-static"' . 289.63s user 19.02s system 525% cpu 58.706 total
What nixos-version?
@lethalman
@wkennington
@colemickens I can't reproduce this - is this still an issue? If so, would you mind adding a bit more detail how you're running the command? E.g. in nix-shell? Installed via nix-env?
I know this is not StackOverflow, but I think I'll still write this comment - just in case someone else would find this issue from a search engine of their liking (that's how I came here).
If go build -ldflags "-s -w -linkmode external -extldflags -static" fails on NiOS, telling cannot find -lpthread and cannot find -lc - it's because the linker can't find static glibc to link with. You need to have glibc.static in your environment (and have CFLAGS/LDFLAGS adjusted accordingly).
One way to achieve this is to have something like the following as shell.nix, run nix-shell and then run go build/make/whatever:
with import <nixpkgs> {}; {
devEnv = stdenv.mkDerivation {
name = "dev";
buildInputs = [ stdenv git go glibc.static ];
CFLAGS="-I${pkgs.glibc.dev}/include";
LDFLAGS="-L${pkgs.glibc}/lib";
};
}
This could be added to https://nixos.org/nixpkgs/manual/#sec-language-go by making a PR to modify doc/languages-frameworks/go.xml
Just in case it does not make it into the manual, I have just copied 'n pasted the snippet here: https://github.com/nixos-users/wiki/wiki/Go
Is there an example somewhere about using -ldflags "-s -w -linkmode external -extldflags -static" inside buildGoPackage ? I keep getting errors that looks like flag provided but not defined: -ldflags when I try to use the attribute buildFlags or buildFlagsArray.
Thanks
I gave this a try in https://github.com/flokli/nixpkgs/commit/60d774c92a481daacf7d3844c7546b6da2d76804.
A
```
go build -ldflags "-v -linkmode external -extldflags -static"
````
of a simple helloworld project now succeeds.
However the checkPhase of the go derivation fails (which is why I didn't open a PR yet).
If there are no C dependencies, the easiest is to export CGO_ENABLE=0 and bypass the glibc entirely.
Thank you for your contributions.
This has been automatically marked as stale because it has had no activity for 180 days.
If this is still important to you, we ask that you leave a comment below. Your comment can be as simple as "still important to me". This lets people see that at least one person still cares about this. Someone will have to do this at most twice a year if there is no other activity.
Here are suggestions that might help resolve this more quickly:
We have it in the wiki: https://nixos.wiki/wiki/Go anything else required here?
Well, this documents a way to still build it with the current setup (and maybe doc/languages-frameworks/go.xml would be a more suitable place, as initially suggested in https://github.com/NixOS/nixpkgs/issues/15399#issuecomment-316040592).
I tried teaching the go compiler to know where the static libc is, so all this wouldn't be needed at all (but can't pursue this any further - if anyone could pick this up, I'd appreciate it)
As @zimbatm mentioned, setting CGO_ENABLED=0 seems to be the better solution, at least as long as we don't need to link to any dynamic libraries. Wherever the docs about this would live, they should cover that solution aswell IMHO.
As @zimbatm mentioned, setting
CGO_ENABLED=0seems to be the better solution, at least as long as we don't need to link to any dynamic libraries. Wherever the docs about this would live, they should cover that solution aswell IMHO.
Which docs?
The docs about how to build static go binaries (both with and without CGO) with the nixpkgs-provided go compiler.
Currently, it just fails, so we should either make it work out of the box, or document how it can be used in its current state.
Most helpful comment
Just in case it does not make it into the manual, I have just copied 'n pasted the snippet here: https://github.com/nixos-users/wiki/wiki/Go