Skip to content

Golang: add support for the gcflags attribute - #277609

Closed
lopter wants to merge 1 commit into
NixOS:stagingfrom
lopter:lo-golang-add-gcflags
Closed

lopter wants to merge 1 commit into
NixOS:stagingfrom
lopter:lo-golang-add-gcflags

Conversation

@lopter

@lopter lopter commented Dec 29, 2023

Copy link
Copy Markdown
Contributor

This is required to disable optimizations and produce binaries that can be debugged with delve, for example:

pkgs.caddy.overrideAttrs (final: prev: {
  # upstream ldflags have "-s" "-w" and we don't want that:
  ldflags = [ "-X "github.com/caddyserver/caddy/v2.CustomVersion=${prev.version}" ];
  gcflags = [ "all=-N -l" ];
  dontStrip = true;
  dontFixup = true;
});

"-all=-N -l" disables optimization and inlining see go tool compile -help.

This is a follow up to #274503.

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 24.05 Release Notes (or backporting 23.05 and 23.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions Bot added the 6.topic: golang Go is a high-level general purpose programming language that is statically typed and compiled. label Dec 29, 2023
@ofborg ofborg Bot added 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 1001-2500 This PR causes many rebuilds on Darwin and should most likely target the staging branches. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 2501-5000 This PR causes many rebuilds on Linux and should target the staging branches. labels Dec 29, 2023
@zowoq
zowoq marked this pull request as draft December 29, 2023 23:31
@zowoq
zowoq changed the base branch from master to staging December 29, 2023 23:31
This is required to disable optimizations and produce binaries that can
be debugged with delve, e.g.:

    pkgs.caddy.overrideAttrs (final: prev: {
      # upstream ldflags have "-s" "-w" and we don't want that:
      ldflags = [ "-X "github.com/caddyserver/caddy/v2.CustomVersion=${prev.version}" ];
      gcflags = [ "all=-N -l" ];
      dontStrip = true;
      dontFixup = true;
    });
@zowoq
zowoq force-pushed the lo-golang-add-gcflags branch from 7debc63 to 8d22588 Compare December 29, 2023 23:32
@zowoq

zowoq commented Dec 29, 2023

Copy link
Copy Markdown
Contributor

I've already rebased the PR but for future reference: CONTRIBUTING.md#rebasing-between-branches-ie-from-master-to-staging

@zowoq
zowoq marked this pull request as ready for review December 29, 2023 23:41
@lopter

lopter commented Dec 30, 2023 •

Copy link
Copy Markdown
Contributor Author

Thank you for doing that @zowoq, sorry I missed the part about mass rebuilds, would love to have your input on #274503#issuecomment-1872322299.

@lopter

lopter commented Jan 5, 2024

Copy link
Copy Markdown
Contributor Author

I am not sure to understand why the checks are failing.

Is there value for me to run https://github.com/Mic92/nixpkgs-review ?

@katexochen

katexochen commented Jan 17, 2024 •

Copy link
Copy Markdown
Member

Could GOFLAGS be used instead here?

GOFLAGS = [
    "-gcflags='all=-N -l'"
];

should have the same effect, shouldn't it?


On the general matter: I'm a fan of using the Go "-s -w" flags for stripping, but I agree that this shouldn't be done on the package level. Maybe we could move it into the builder, set it by default and remove it when dontStrip = true is set? I've recently added some filtering to ldflags (#276685), I think handling of stripping could be done in a similar way. I can also help with this change if wanted. :)

@lopter

lopter commented Jan 29, 2024

Copy link
Copy Markdown
Contributor Author

Hello @katexochen, thanks for the idea, it works though you have to use "'-gcflags=all=-N -l'" (i.e. put the whole thing inside 's), to work around escaping issues.


caddy = pkgs.caddy.overrideAttrs (final: prev: {
    # upstream ldflags have "-s" "-w" and we don't want that:
    ldflags = [ "-X ${importPath}.CustomVersion=${prev.version}" ];
    GOFLAGS = prev.GOFLAGS ++ [ "'-gcflags=all=-N -l'" ];
    dontStrip = true;
    dontFixup = true;
});

@lopter

lopter commented Jan 29, 2024 •

Copy link
Copy Markdown
Contributor Author

I would be happy if dontStrip was honored, and toggled the (builder controlled) use of -s -w.

I saved some (horrible) code to remove the "-s" and "-w" ldflags from almost all Golang packages in Nixpkgs, so if you wish I could take on that part.

@wegank wegank added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jul 4, 2024
@katexochen

Copy link
Copy Markdown
Member

@lopter can this PR be closed, as it is already covered by GOFLAGS?

@stale stale Bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Sep 29, 2024
@lopter

lopter commented Sep 30, 2024

Copy link
Copy Markdown
Contributor Author

Sure, otoh I am still interested in not setting "-s" and "-w" at the package level to make debugging easier. You were saying that we could add them from pkgs/build-support/go/package.nix depending on dontStrip instead?

@katexochen

Copy link
Copy Markdown
Member

I've opened #346380 to discuss the stripping refactoring.

@katexochen katexochen closed this Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: golang Go is a high-level general purpose programming language that is statically typed and compiled. 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 1001-2500 This PR causes many rebuilds on Darwin and should most likely target the staging branches. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 2501-5000 This PR causes many rebuilds on Linux and should target the staging branches.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants