From 21c8967f1c05993637ab5b1456a7ad614432e4d6 Mon Sep 17 00:00:00 2001 From: Paul Meyer Date: Mon, 21 Jul 2025 17:16:09 +0200 Subject: [PATCH 1/3] buildGoModule: add buildTestBinaries option Signed-off-by: Paul Meyer --- pkgs/build-support/go/module.nix | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/pkgs/build-support/go/module.nix b/pkgs/build-support/go/module.nix index 9b692b4f6dec..a00e70ed3298 100644 --- a/pkgs/build-support/go/module.nix +++ b/pkgs/build-support/go/module.nix @@ -64,6 +64,12 @@ lib.extendMkDerivation { # Go build flags. GOFLAGS ? [ ], + # Instead of building binary targets with 'go install', build test binaries with 'go test'. + # The binaries found in $out/bin can be executed as go tests outside of the sandbox. + # This is mostly useful outside of nixpkgs, for example to build integration/e2e tests + # that won't run within the sandbox. + buildTestBinaries ? false, + ... }@args: { @@ -338,8 +344,18 @@ lib.extendMkDerivation { export NIX_BUILD_CORES=1 fi for pkg in $(getGoDirs ""); do - echo "Building subPackage $pkg" - buildGoDir install "$pkg" + ${ + if buildTestBinaries then + '' + echo "Building test binary for $pkg" + buildGoDir "test -c -o $GOPATH/bin/" "$pkg" + '' + else + '' + echo "Building subPackage $pkg" + buildGoDir install "$pkg" + '' + } done '' + lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) '' @@ -359,7 +375,7 @@ lib.extendMkDerivation { '' ); - doCheck = args.doCheck or true; + doCheck = args.doCheck or (!buildTestBinaries); checkPhase = args.checkPhase or '' runHook preCheck From 1d2563835a20392a2b867de6cfe8c1a176c6dc53 Mon Sep 17 00:00:00 2001 From: Paul Meyer Date: Mon, 21 Jul 2025 19:22:52 +0200 Subject: [PATCH 2/3] buildGoModule: add test for buildTestBinaries Signed-off-by: Paul Meyer --- pkgs/build-support/go/tests.nix | 9 +++++ .../go/tests/build-test-binaries/go.mod | 3 ++ .../go/tests/build-test-binaries/main_test.go | 7 ++++ .../go/tests/build-test-binaries/package.nix | 34 +++++++++++++++++++ pkgs/test/default.nix | 2 ++ 5 files changed, 55 insertions(+) create mode 100644 pkgs/build-support/go/tests.nix create mode 100644 pkgs/build-support/go/tests/build-test-binaries/go.mod create mode 100644 pkgs/build-support/go/tests/build-test-binaries/main_test.go create mode 100644 pkgs/build-support/go/tests/build-test-binaries/package.nix diff --git a/pkgs/build-support/go/tests.nix b/pkgs/build-support/go/tests.nix new file mode 100644 index 000000000000..8966fe927aea --- /dev/null +++ b/pkgs/build-support/go/tests.nix @@ -0,0 +1,9 @@ +{ + lib, + callPackage, +}: + +lib.packagesFromDirectoryRecursive { + inherit callPackage; + directory = ./tests; +} diff --git a/pkgs/build-support/go/tests/build-test-binaries/go.mod b/pkgs/build-support/go/tests/build-test-binaries/go.mod new file mode 100644 index 000000000000..26cef8bdf8fc --- /dev/null +++ b/pkgs/build-support/go/tests/build-test-binaries/go.mod @@ -0,0 +1,3 @@ +module build-test-binaries + +go 1.24 diff --git a/pkgs/build-support/go/tests/build-test-binaries/main_test.go b/pkgs/build-support/go/tests/build-test-binaries/main_test.go new file mode 100644 index 000000000000..29378df53b02 --- /dev/null +++ b/pkgs/build-support/go/tests/build-test-binaries/main_test.go @@ -0,0 +1,7 @@ +package main + +import "testing" + +func TestHelloFromTest(t *testing.T) { + t.Log("Hello from test") +} diff --git a/pkgs/build-support/go/tests/build-test-binaries/package.nix b/pkgs/build-support/go/tests/build-test-binaries/package.nix new file mode 100644 index 000000000000..761d793233bd --- /dev/null +++ b/pkgs/build-support/go/tests/build-test-binaries/package.nix @@ -0,0 +1,34 @@ +{ + buildGoModule, + runCommandCC, +}: + +let + testPackage = buildGoModule { + name = "build-test-binaries"; + src = ./.; + vendorHash = null; + buildTestBinaries = true; + }; +in + +runCommandCC "build-test-binaries-check" + { + nativeBuildInputs = [ testPackage ]; + passthru = { inherit testPackage; }; + } + '' + fail() { + echo "Test failed: $1" >&2 + exit 1 + } + + command -v build-test-binaries.test || + fail "build-test-binaries.test not found in PATH" + + build-test-binaries.test -test.v | tee $out || + fail "build-test-binaries.test failed" + + grep -q "Hello from test" $out || + fail "Output does not contain expected string" + '' diff --git a/pkgs/test/default.nix b/pkgs/test/default.nix index fe6f9eb43852..7636498fff79 100644 --- a/pkgs/test/default.nix +++ b/pkgs/test/default.nix @@ -148,6 +148,8 @@ with pkgs; php = recurseIntoAttrs (callPackages ./php { }); + go = recurseIntoAttrs (callPackage ../build-support/go/tests.nix { }); + pkg-config = recurseIntoAttrs (callPackage ../top-level/pkg-config/tests.nix { }) // { __recurseIntoDerivationForReleaseJobs = true; }; From 969cc016b0c096ebc5b2c7beecee96b06d05a654 Mon Sep 17 00:00:00 2001 From: Paul Meyer Date: Mon, 21 Jul 2025 20:08:18 +0200 Subject: [PATCH 3/3] docs: document buildTestBinaries for buildGoModule Signed-off-by: Paul Meyer --- doc/languages-frameworks/go.section.md | 6 ++++++ doc/redirects.json | 3 +++ 2 files changed, 9 insertions(+) diff --git a/doc/languages-frameworks/go.section.md b/doc/languages-frameworks/go.section.md index fcc3c687d354..e45269a35e48 100644 --- a/doc/languages-frameworks/go.section.md +++ b/doc/languages-frameworks/go.section.md @@ -194,6 +194,12 @@ Specifies the contents of the `go.sum` file and triggers rebuilds when it change Defaults to `null` +### `buildTestBinaries` {#var-go-buildTestBinaries} + +This option allows to compile test binaries instead of the usual binaries produced by a package. +Go can [compile test into binaries](https://pkg.go.dev/cmd/go#hdr-Test_packages) using the `go test -c` command. +These binaries can then be executed at a later point (outside the Nix sandbox) to run the tests. +This is mostly useful for downstream consumers to run integration or end-to-end tests that won't work in the Nix sandbox, for example because they require network access. ## Versioned toolchains and builders {#ssec-go-toolchain-versions} diff --git a/doc/redirects.json b/doc/redirects.json index bf27cda98186..eda98b1ce418 100644 --- a/doc/redirects.json +++ b/doc/redirects.json @@ -532,6 +532,9 @@ "typst-package-scope-and-usage": [ "index.html#typst-package-scope-and-usage" ], + "var-go-buildTestBinaries": [ + "index.html#var-go-buildTestBinaries" + ], "var-meta-teams": [ "index.html#var-meta-teams" ],