nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1# Go {#sec-language-go}
2
3## Building Go modules with `buildGoModule` {#ssec-language-go}
4
5The function `buildGoModule` builds Go programs managed with Go modules. It builds [Go Modules](https://go.dev/wiki/Modules) through a two phase build:
6
7- An intermediate fetcher derivation called `goModules`. This derivation will be used to fetch all the dependencies of the Go module.
8- A final derivation will use the output of the intermediate derivation to build the binaries and produce the final output.
9
10### Attributes of `buildGoModule` {#buildgomodule-parameters}
11
12The `buildGoModule` function accepts the following parameters in addition to the [attributes accepted by both Go builders](#ssec-go-common-attributes):
13
14- `vendorHash`: is the hash of the output of the intermediate fetcher derivation (the dependencies of the Go modules).
15
16 `vendorHash` can be set to `null`.
17 In that case, rather than fetching the dependencies, the dependencies already vendored in the `vendor` directory of the source repo will be used.
18
19 To avoid updating this field when dependencies change, run `go mod vendor` in your source repo and set `vendorHash = null;`.
20 You can read more about [vendoring in the Go documentation](https://go.dev/ref/mod#vendoring).
21
22 To obtain the actual hash, set `vendorHash = lib.fakeHash;` and run the build ([more details here](#sec-source-hashes)).
23- `proxyVendor`: If `true`, the intermediate fetcher downloads dependencies from the
24 [Go module proxy](https://go.dev/ref/mod#module-proxy) (using `go mod download`) instead of vendoring them. The resulting
25 [module cache](https://go.dev/ref/mod#module-cache) is then passed to the final derivation.
26
27 This is useful if your code depends on C code and `go mod tidy` does not include the needed sources to build or
28 if any dependency has case-insensitive conflicts which will produce platform-dependent `vendorHash` checksums.
29
30 Defaults to `false`.
31- `modPostBuild`: Shell commands to run after the build of the goModules executes `go mod vendor`, and before calculating fixed output derivation's `vendorHash`.
32 Note that if you change this attribute, you need to update `vendorHash` attribute.
33- `modRoot`: The root directory of the Go module that contains the `go.mod` file.
34 Defaults to `./`, which is the root of `src`.
35
36### Example for `buildGoModule` {#ex-buildGoModule}
37
38The following is an example expression using `buildGoModule`:
39
40```nix
41{
42 pet = buildGoModule rec {
43 pname = "pet";
44 version = "0.3.4";
45
46 src = fetchFromGitHub {
47 owner = "knqyf263";
48 repo = "pet";
49 rev = "v${version}";
50 hash = "sha256-Gjw1dRrgM8D3G7v6WIM2+50r4HmTXvx0Xxme2fH9TlQ=";
51 };
52
53 vendorHash = "sha256-ciBIR+a1oaYH+H1PcC8cD8ncfJczk1IiJ8iYNM+R6aA=";
54
55 meta = {
56 description = "Simple command-line snippet manager, written in Go";
57 homepage = "https://github.com/knqyf263/pet";
58 license = lib.licenses.mit;
59 maintainers = with lib.maintainers; [ kalbasit ];
60 };
61 };
62}
63```
64
65## `buildGoPackage` (legacy) {#ssec-go-legacy}
66
67The function `buildGoPackage` builds legacy Go programs, not supporting Go modules.
68
69::: {.warning}
70`buildGoPackage` is deprecated and will be removed for the 25.05 release.
71:::
72
73### Migrating from `buildGoPackage` to `buildGoModule` {#buildGoPackage-migration}
74
75Go modules, released 6y ago, are now widely adopted in the ecosystem.
76Most upstream projects are using Go modules, and the tooling previously used for dependency management in Go is mostly deprecated, archived or at least unmaintained at this point.
77
78In case a project doesn't have external dependencies or dependencies are vendored in a way understood by `go mod init`, migration can be done with a few changes in the package.
79
80- Switch the builder from `buildGoPackage` to `buildGoModule`
81- Remove `goPackagePath` and other attributes specific to `buildGoPackage`
82- Set `vendorHash = null;`
83- Run `go mod init <module name>` in `postPatch`
84
85In case the package has external dependencies that aren't vendored or the build setup is more complex the upstream source might need to be patched.
86Examples for the migration can be found in the [issue tracking migration withing nixpkgs](https://github.com/NixOS/nixpkgs/issues/318069).
87
88### Example for `buildGoPackage` {#example-for-buildgopackage}
89
90In the following is an example expression using `buildGoPackage`, the following arguments are of special significance to the function:
91
92- `goPackagePath` specifies the package's canonical Go import path.
93- `goDeps` is where the Go dependencies of a Go program are listed as a list of package source identified by Go import path. It could be imported as a separate `deps.nix` file for readability. The dependency data structure is described below.
94
95```nix
96{
97 deis = buildGoPackage rec {
98 pname = "deis";
99 version = "1.13.0";
100
101 goPackagePath = "github.com/deis/deis";
102
103 src = fetchFromGitHub {
104 owner = "deis";
105 repo = "deis";
106 rev = "v${version}";
107 hash = "sha256-XCPD4LNWtAd8uz7zyCLRfT8rzxycIUmTACjU03GnaeM=";
108 };
109
110 goDeps = ./deps.nix;
111 };
112}
113```
114
115The `goDeps` attribute can be imported from a separate `nix` file that defines which Go libraries are needed and should be included in `GOPATH` for `buildPhase`:
116
117```nix
118# deps.nix
119[ # goDeps is a list of Go dependencies.
120 {
121 # goPackagePath specifies Go package import path.
122 goPackagePath = "gopkg.in/yaml.v2";
123 fetch = {
124 # `fetch type` that needs to be used to get package source.
125 # If `git` is used there should be `url`, `rev` and `hash` defined next to it.
126 type = "git";
127 url = "https://gopkg.in/yaml.v2";
128 rev = "a83829b6f1293c91addabc89d0571c246397bbf4";
129 hash = "sha256-EMrdy0M0tNuOcITaTAmT5/dPSKPXwHDKCXFpkGbVjdQ=";
130 };
131 }
132 {
133 goPackagePath = "github.com/docopt/docopt-go";
134 fetch = {
135 type = "git";
136 url = "https://github.com/docopt/docopt-go";
137 rev = "784ddc588536785e7299f7272f39101f7faccc3f";
138 hash = "sha256-Uo89zjE+v3R7zzOq/gbQOHj3SMYt2W1nDHS7RCUin3M=";
139 };
140 }
141]
142```
143
144To extract dependency information from a Go package in automated way use [go2nix (deprecated)](https://github.com/kamilchm/go2nix). It can produce complete derivation and `goDeps` file for Go programs.
145
146You may use Go packages installed into the active Nix profiles by adding the following to your ~/.bashrc:
147
148```bash
149for p in $NIX_PROFILES; do
150 GOPATH="$p/share/go:$GOPATH"
151done
152```
153
154## Attributes used by both builders {#ssec-go-common-attributes}
155
156Many attributes [controlling the build phase](#variables-controlling-the-build-phase) are respected by both `buildGoModule` and `buildGoPackage`. Note that `buildGoModule` reads the following attributes also when building the `vendor/` goModules fixed output derivation as well:
157
158- [`sourceRoot`](#var-stdenv-sourceRoot)
159- [`prePatch`](#var-stdenv-prePatch)
160- [`patches`](#var-stdenv-patches)
161- [`patchFlags`](#var-stdenv-patchFlags)
162- [`postPatch`](#var-stdenv-postPatch)
163- [`preBuild`](#var-stdenv-preBuild)
164- `env`: useful for passing down variables such as `GOWORK`.
165
166To control test execution of the build derivation, the following attributes are of interest:
167
168- [`checkInputs`](#var-stdenv-checkInputs)
169- [`preCheck`](#var-stdenv-preCheck)
170- [`checkFlags`](#var-stdenv-checkFlags)
171
172In addition to the above attributes, and the many more variables respected also by `stdenv.mkDerivation`, both `buildGoModule` and `buildGoPackage` respect Go-specific attributes that tweak them to behave slightly differently:
173
174### `ldflags` {#var-go-ldflags}
175
176A string list of flags to pass to the Go linker tool via the `-ldflags` argument of `go build`. Possible values can be retrieved by running `go tool link --help`.
177The most common use case for this argument is to make the resulting executable aware of its own version by injecting the value of string variable using the `-X` flag. For example:
178
179```nix
180{
181 ldflags = [
182 "-X main.Version=${version}"
183 "-X main.Commit=${version}"
184 ];
185}
186```
187
188### `tags` {#var-go-tags}
189
190A string list of [Go build tags (also called build constraints)](https://pkg.go.dev/cmd/go#hdr-Build_constraints) that are passed via the `-tags` argument of `go build`. These constraints control whether Go files from the source should be included in the build. For example:
191
192```nix
193{
194 tags = [
195 "production"
196 "sqlite"
197 ];
198}
199```
200
201Tags can also be set conditionally:
202
203```nix
204{
205 tags = [ "production" ] ++ lib.optionals withSqlite [ "sqlite" ];
206}
207```
208
209### `deleteVendor` {#var-go-deleteVendor}
210
211If set to `true`, removes the pre-existing vendor directory. This should only be used if the dependencies included in the vendor folder are broken or incomplete.
212
213### `subPackages` {#var-go-subPackages}
214
215Specified as a string or list of strings. Limits the builder from building child packages that have not been listed. If `subPackages` is not specified, all child packages will be built.
216
217Many Go projects keep the main package in a `cmd` directory.
218Following example could be used to only build the example-cli and example-server binaries:
219
220```nix
221{
222 subPackages = [
223 "cmd/example-cli"
224 "cmd/example-server"
225 ];
226}
227```
228
229### `excludedPackages` {#var-go-excludedPackages}
230
231Specified as a string or list of strings. Causes the builder to skip building child packages that match any of the provided values.
232
233### `CGO_ENABLED` {#var-go-CGO_ENABLED}
234
235When set to `0`, the [cgo](https://pkg.go.dev/cmd/cgo) command is disabled. As consequence, the build
236program can't link against C libraries anymore, and the resulting binary is statically linked.
237
238When building with CGO enabled, Go will likely link some packages from the Go standard library against C libraries,
239even when the target code does not explicitly call into C dependencies. With `CGO_ENABLED = 0;`, Go
240will always use the Go native implementation of these internal packages. For reference see
241[net](https://pkg.go.dev/net#hdr-Name_Resolution) and [os/user](https://pkg.go.dev/os/user#pkg-overview) packages.
242Notice that the decision whether these packages should use native Go implementation or not can also be controlled
243on a per package level using build tags (`tags`). In case CGO is disabled, these tags have no additional effect.
244
245When a Go program depends on C libraries, place those dependencies in `buildInputs`:
246
247```nix
248{
249 buildInputs = [
250 libvirt
251 libxml2
252 ];
253}
254```
255
256`CGO_ENABLED` defaults to `1`.
257
258### `enableParallelBuilding` {#var-go-enableParallelBuilding}
259
260Whether builds and tests should run in parallel.
261
262Defaults to `true`.
263
264### `allowGoReference` {#var-go-allowGoReference}
265
266Whether the build result should be allowed to contain references to the Go tool chain. This might be needed for programs that are coupled with the compiler, but shouldn't be set without a good reason.
267
268Defaults to `false`
269
270## Controlling the Go environment {#ssec-go-environment}
271
272The Go build can be further tweaked by setting environment variables. In most cases, this isn't needed. Possible values can be found in the [Go documentation of accepted environment variables](https://pkg.go.dev/cmd/go#hdr-Environment_variables). Notice that some of these flags are set by the builder itself and should not be set explicitly. If in doubt, grep the implementation of the builder.
273
274## Skipping tests {#ssec-skip-go-tests}
275
276`buildGoModule` runs tests by default. Failing tests can be disabled using the `checkFlags` parameter.
277This is done with the [`-skip` or `-run`](https://pkg.go.dev/cmd/go#hdr-Testing_flags) flags of the `go test` command.
278
279For example, only a selection of tests could be run with:
280
281```nix
282{
283 # -run and -skip accept regular expressions
284 checkFlags = [
285 "-run=^Test(Simple|Fast)$"
286 ];
287}
288```
289
290If a larger amount of tests should be skipped, the following pattern can be used:
291
292```nix
293{
294 checkFlags =
295 let
296 # Skip tests that require network access
297 skippedTests = [
298 "TestNetwork"
299 "TestDatabase/with_mysql" # exclude only the subtest
300 "TestIntegration"
301 ];
302 in
303 [ "-skip=^${builtins.concatStringsSep "$|^" skippedTests}$" ];
304}
305```
306
307To disable tests altogether, set `doCheck = false;`.
308`buildGoPackage` does not execute tests by default.