You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
buildGoModule: Binaries are not stripped by default. Proposal to add -s -w ldflags by default #177698
2.status: stalehttps://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md6.topic: golangGo is a high-level general purpose programming language that is statically typed and compiled.
I was wondering, is there a specific reason for -s and -w ldflags not being in the buildGoModule's buildFlagsArray=() and shouldn't we enable them by default or, for instance, when the ldflags attribute is not explicitly defined by package's derivation?
-s
Omit the symbol table and debug information.
-w
Omit the DWARF symbol table.
Right now, if these flags are not included, binaries are left unstripped and consequently bigger in size. Basically, to include these flags would mean to mimic what strip command does in stdenv. Or we could just add strip itself after the build if it's not already somehow inherited from stdenv.
#157207 (comment)
I've looked at doing it a couple of times previously but it wasn't a good default to set. Sometimes caused weird issues or outright breakage and was generally just a PITA for the amount of derivations that would need to opt out. Homebrew and a couple of other distros also looked at making it a default but decided against it.
I haven't tried rebuilding all of nixpkgs with it since 1.16 1.15, once we're on 1.18 (hopefully soon) I'll take another look at it.
Since #179622 is merged now, @zowoq any chance you can look into this again? I'm willing to help but I currently don't have enough horse power for mass rebuilds.
Alpine have recently removed -s -wldflags from all packages as it was already handled by the strip in their build tooling. Rather than setting these flags we should find out why ours doesn't cover this and fix it. This would also be the more complete fix as setting default ldflags in buildGo{Module,Package} would only have worked for packages that don't override the default buildPhase.
we should find out why ours doesn't cover this and fix it.
Just tested this -- our strip produces the same llvm-dwarf-dump --all output that a Go-stripped binary does.
Did our strip at one point not work fully, or is that speculation? Did we all (as in, Alpine and us) just add the flags for completeness, when they were actually extraneous since we strip at the end? Or were they needed at some point to fully strip debug info?
our strip produces the same llvm-dwarf-dump --all output that a Go-stripped binary does.
Assuming that this was on linux, it isn't the same on x86_64-darwin.
Did our strip at one point not work fully, or is that speculation? Did we all (as in, Alpine and us) just add the flags for completeness, when they were actually extraneous since we strip at the end? Or were they needed at some point to fully strip debug info?
IIRC these flags did make a difference on linux at one point. Not sure why these flags were sometimes problematic in the past if the stdenv strip will produce the same result anyway.
IIRC these flags did make a difference on linux at one point. Not sure why these flags were sometimes problematic in the past if the stdenv strip will produce the same result anyway.
Removing these flags still seems to cause bigger binaries on linux?
If I'm correctly understanding the point you're trying to make: yes, that's to be expected, as we only strip debug symbols by default (as opposed to stripping all symbols).
If you manually run strip on a copy of a binary made without those ldflags, the size should be closer (if not the same) to the binaries made with them.
We can set stripDebugFlags to -s (note the lowercase) by default in buildGo{Module,Package} to get parity with setting the flags.
stripAllFlags=" " # the Darwin "strip" command doesn't know "-s"
Doesn't seem like we have a way of doing this consistently with strip. Guess that bring us back to where we started, setting defaultLdflags even though it will only work for packages using the default buildPhase.
Doesn't seem like we have a way of doing this consistently with strip. Guess that bring us back to where we started, setting defaultLdflags even though it will only work for packages using the default buildPhase.
Maybe we can create an example buildPhase in the docs or something that will encourage the usage of defaultLdflags with other flags appended/prepended?
2.status: stalehttps://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md6.topic: golangGo is a high-level general purpose programming language that is statically typed and compiled.
Activity
zowoq commentedon Jun 15, 2022
azahi commentedon Jul 22, 2022
Since #179622 is merged now, @zowoq any chance you can look into this again? I'm willing to help but I currently don't have enough horse power for mass rebuilds.
/cc @SuperSandro2000
zowoq commentedon Jul 22, 2022
I'll follow up on this in the next couple of weeks.
SuperSandro2000 commentedon Jul 24, 2022
IMO we should enable this by default and disable it for the few packages that do not support it.
zowoq commentedon Jul 24, 2022
Here is the PR: #182743
I still need to recheck what other distros are doing in case there are reasons not to do this and check for breakage in packages.
zowoq commentedon Sep 1, 2022
Alpine have recently removed
-s -wldflagsfrom all packages as it was already handled by the strip in their build tooling. Rather than setting these flags we should find out why ours doesn't cover this and fix it. This would also be the more complete fix as setting defaultldflagsinbuildGo{Module,Package}would only have worked for packages that don't override the defaultbuildPhase.winterqt commentedon Sep 1, 2022
Just tested this -- our strip produces the same
llvm-dwarf-dump --alloutput that a Go-stripped binary does.Did our strip at one point not work fully, or is that speculation? Did we all (as in, Alpine and us) just add the flags for completeness, when they were actually extraneous since we strip at the end? Or were they needed at some point to fully strip debug info?
zowoq commentedon Sep 1, 2022
Assuming that this was on linux, it isn't the same on x86_64-darwin.
IIRC these flags did make a difference on linux at one point. Not sure why these flags were sometimes problematic in the past if the stdenv strip will produce the same result anyway.
winterqt commentedon Sep 1, 2022
I (naively) only tested Linux, correct.
Do you remember how they were problematic?
zowoq commentedon Sep 1, 2022
Removing these flags still seems to cause bigger binaries on linux?
winterqt commentedon Sep 1, 2022
If I'm correctly understanding the point you're trying to make: yes, that's to be expected, as we only strip debug symbols by default (as opposed to stripping all symbols).
If you manually run
stripon a copy of a binary made without those ldflags, the size should be closer (if not the same) to the binaries made with them.We can set
stripDebugFlagsto-s(note the lowercase) by default inbuildGo{Module,Package}to get parity with setting the flags.zowoq commentedon Sep 21, 2022
nixpkgs/pkgs/stdenv/darwin/default.nix
Line 65 in 0fdc722
Doesn't seem like we have a way of doing this consistently with strip. Guess that bring us back to where we started, setting
defaultLdflagseven though it will only work for packages using the defaultbuildPhase.qbit commentedon Jan 24, 2023
fwiw, that's what we do in OpenBSD land: https://github.com/openbsd/ports/blob/master/lang/go/go.port.mk#L111
Maybe we can create an example
buildPhasein the docs or something that will encourage the usage ofdefaultLdflagswith other flags appended/prepended?