Skip to content

buildGoModule: always set -buildid= as ldflag - #276685

Merged
Mic92 merged 2 commits into
NixOS:stagingfrom
katexochen:buildGoModule/buildid
Jan 14, 2024
Merged

Mic92 merged 2 commits into
NixOS:stagingfrom
katexochen:buildGoModule/buildid

Conversation

@katexochen

@katexochen katexochen commented Dec 25, 2023 •

Copy link
Copy Markdown
Member

Description of changes

The buildid in Go depends on the source path, even if trimpath is set. It is mostly used as cache key to detect whether a binary needs to be rebuild or not (a use case with no importance for nixpkgs). For improving build reproducibility, we should always set it to an empty value. This is especially interesting for content-addressed builds.

For more background on buildid, see

This PR

  • lets buildGoModule add -buildid= to ldflags when not explicitly set
  • adds a warning for when -buildid= is still explicitly set
  • still allows to set buildid to another value where required
  • removes all explicitly set -buildid= from pkgs
  • requires ldflags to be a list, where it previously could also be set as string. The only affected pkg was gofumpt.

Things done

  • 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 25, 2023
@katexochen katexochen mentioned this pull request Dec 25, 2023
3 of 13 tasks
@katexochen
katexochen force-pushed the buildGoModule/buildid branch from ef05acc to ea8a179 Compare December 25, 2023 11:30
@zowoq

zowoq commented Dec 25, 2023

Copy link
Copy Markdown
Contributor

Please also set buildid in buildGoPackage.

diff --git a/pkgs/build-support/go/package.nix b/pkgs/build-support/go/package.nix
index 7e099b76f0b7..55c8c1390b5b 100644
--- a/pkgs/build-support/go/package.nix
+++ b/pkgs/build-support/go/package.nix
@@ -37,6 +37,8 @@
 
 , CGO_ENABLED ? go.CGO_ENABLED
 
+, ldflags ? [ ]
+
 # needed for buildFlags{,Array} warning
 , buildFlags ? ""
 , buildFlagsArray ? ""
@@ -91,6 +93,9 @@ let
 
     GOARM = toString (lib.intersectLists [(stdenv.hostPlatform.parsed.cpu.version or "")] ["5" "6" "7"]);
 
+    # If not set to an explicit value, set the buildid empty for reproducibility.
+    ldflags = ldflags ++ lib.optionals (!lib.any (lib.hasPrefix "-buildid=") ldflags) [ "-buildid=" ];
+
     configurePhase = args.configurePhase or (''
       runHook preConfigure
 

@katexochen
katexochen force-pushed the buildGoModule/buildid branch from ea8a179 to 9487240 Compare December 26, 2023 08:23
from buildGoModules invocations

This is now always set by buildGoModule.
@katexochen
katexochen force-pushed the buildGoModule/buildid branch from 9487240 to 77d6ba8 Compare December 28, 2023 09:01
@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 28, 2023
@delroth delroth added the 12.approvals: 1 This PR was reviewed and approved by one person. label Dec 31, 2023
@katexochen

Copy link
Copy Markdown
Member Author

@zowoq do we need an additional review or can this be merged?

@Mic92
Mic92 merged commit a292058 into NixOS:staging Jan 14, 2024
@katexochen
katexochen deleted the buildGoModule/buildid branch January 14, 2024 13:15
@muscaln muscaln mentioned this pull request Mar 4, 2024
1 of 13 tasks
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. 12.approvals: 1 This PR was reviewed and approved by one person.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants