1# Go {#sec-language-go}
2
3## Go modules {#ssec-language-go}
4
5The function `buildGoModule` builds Go programs managed with Go modules. It builds a [Go Modules](https://github.com/golang/go/wiki/Modules) through a two phase build:
6
7- An intermediate fetcher derivation. This derivation will be used to fetch all of 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### Example for `buildGoModule` {#ex-buildGoModule}
11
12In the following is an example expression using `buildGoModule`, the following arguments are of special significance to the function:
13
14- `vendorSha256`: is the hash of the output of the intermediate fetcher derivation. `vendorSha256` can also take `null` as an input. When `null` is used as a value, rather than fetching the dependencies and vendoring them, we use the vendoring included within the source repo. If you'd like to not have to update this field on dependency changes, run `go mod vendor` in your source repo and set `vendorSha256 = null;`
15- `runVend`: runs the vend command to generate the vendor directory. This is useful if your code depends on c code and go mod tidy does not include the needed sources to build.
16- `proxyVendor`: Fetches (go mod download) and proxies the vendor directory. This is useful if any dependency has case-insensitive conflicts which will produce platform dependant `vendorSha256` checksums.
17
18```nix
19pet = buildGoModule rec {
20 pname = "pet";
21 version = "0.3.4";
22
23 src = fetchFromGitHub {
24 owner = "knqyf263";
25 repo = "pet";
26 rev = "v${version}";
27 sha256 = "0m2fzpqxk7hrbxsgqplkg7h2p7gv6s1miymv3gvw0cz039skag0s";
28 };
29
30 vendorSha256 = "1879j77k96684wi554rkjxydrj8g3hpp0kvxz03sd8dmwr3lh83j";
31
32 runVend = true;
33
34 meta = with lib; {
35 description = "Simple command-line snippet manager, written in Go";
36 homepage = "https://github.com/knqyf263/pet";
37 license = licenses.mit;
38 maintainers = with maintainers; [ kalbasit ];
39 platforms = platforms.linux ++ platforms.darwin;
40 };
41}
42```
43
44## `buildGoPackage` (legacy) {#ssec-go-legacy}
45
46The function `buildGoPackage` builds legacy Go programs, not supporting Go modules.
47
48### Example for `buildGoPackage` {#example-for-buildgopackage}
49
50In the following is an example expression using buildGoPackage, the following arguments are of special significance to the function:
51
52- `goPackagePath` specifies the package's canonical Go import path.
53- `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.
54
55```nix
56deis = buildGoPackage rec {
57 pname = "deis";
58 version = "1.13.0";
59
60 goPackagePath = "github.com/deis/deis";
61
62 src = fetchFromGitHub {
63 owner = "deis";
64 repo = "deis";
65 rev = "v${version}";
66 sha256 = "1qv9lxqx7m18029lj8cw3k7jngvxs4iciwrypdy0gd2nnghc68sw";
67 };
68
69 goDeps = ./deps.nix;
70}
71```
72
73The `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`:
74
75```nix
76# deps.nix
77[ # goDeps is a list of Go dependencies.
78 {
79 # goPackagePath specifies Go package import path.
80 goPackagePath = "gopkg.in/yaml.v2";
81 fetch = {
82 # `fetch type` that needs to be used to get package source.
83 # If `git` is used there should be `url`, `rev` and `sha256` defined next to it.
84 type = "git";
85 url = "https://gopkg.in/yaml.v2";
86 rev = "a83829b6f1293c91addabc89d0571c246397bbf4";
87 sha256 = "1m4dsmk90sbi17571h6pld44zxz7jc4lrnl4f27dpd1l8g5xvjhh";
88 };
89 }
90 {
91 goPackagePath = "github.com/docopt/docopt-go";
92 fetch = {
93 type = "git";
94 url = "https://github.com/docopt/docopt-go";
95 rev = "784ddc588536785e7299f7272f39101f7faccc3f";
96 sha256 = "0wwz48jl9fvl1iknvn9dqr4gfy1qs03gxaikrxxp9gry6773v3sj";
97 };
98 }
99]
100```
101
102To extract dependency information from a Go package in automated way use [go2nix](https://github.com/kamilchm/go2nix). It can produce complete derivation and `goDeps` file for Go programs.
103
104You may use Go packages installed into the active Nix profiles by adding the following to your ~/.bashrc:
105
106```bash
107for p in $NIX_PROFILES; do
108 GOPATH="$p/share/go:$GOPATH"
109done
110```
111
112## Attributes used by the builders {#ssec-go-common-attributes}
113
114Both `buildGoModule` and `buildGoPackage` can be tweaked to behave slightly differently, if the following attributes are used:
115
116### `ldflags` {#var-go-ldflags}
117
118Arguments to pass to the Go linker tool via the `-ldflags` argument of `go build`. The most common use case for this argument is to make the resulting executable aware of its own version. For example:
119
120```nix
121 ldflags = [
122 "-s" "-w"
123 "-X main.Version=${version}"
124 "-X main.Commit=${version}"
125 ];
126```
127
128### `tags` {#var-go-tags}
129
130Arguments to pass to the Go via the `-tags` argument of `go build`. For example:
131
132```nix
133 tags = [
134 "production"
135 "sqlite"
136 ];
137```
138
139```nix
140 tags = [ "production" ] ++ lib.optionals withSqlite [ "sqlite" ];
141```
142
143### `deleteVendor` {#var-go-deleteVendor}
144
145Removes the pre-existing vendor directory. This should only be used if the dependencies included in the vendor folder are broken or incomplete.
146
147### `subPackages` {#var-go-subPackages}
148
149Limits the builder from building child packages that have not been listed. If `subPackages` is not specified, all child packages will be built.