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- `vendorHash`: is the hash of the output of the intermediate fetcher derivation. `vendorHash` 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 `vendorHash = null;` 15- `proxyVendor`: Fetches (go mod download) and proxies 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 or if any dependency has case-insensitive conflicts which will produce platform dependant `vendorHash` checksums. 16 17```nix 18pet = buildGoModule rec { 19 pname = "pet"; 20 version = "0.3.4"; 21 22 src = fetchFromGitHub { 23 owner = "knqyf263"; 24 repo = "pet"; 25 rev = "v${version}"; 26 sha256 = "0m2fzpqxk7hrbxsgqplkg7h2p7gv6s1miymv3gvw0cz039skag0s"; 27 }; 28 29 vendorHash = "sha256-ciBIR+a1oaYH+H1PcC8cD8ncfJczk1IiJ8iYNM+R6aA="; 30 31 meta = with lib; { 32 description = "Simple command-line snippet manager, written in Go"; 33 homepage = "https://github.com/knqyf263/pet"; 34 license = licenses.mit; 35 maintainers = with maintainers; [ kalbasit ]; 36 }; 37} 38``` 39 40## `buildGoPackage` (legacy) {#ssec-go-legacy} 41 42The function `buildGoPackage` builds legacy Go programs, not supporting Go modules. 43 44### Example for `buildGoPackage` {#example-for-buildgopackage} 45 46In the following is an example expression using buildGoPackage, the following arguments are of special significance to the function: 47 48- `goPackagePath` specifies the package's canonical Go import path. 49- `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. 50 51```nix 52deis = buildGoPackage rec { 53 pname = "deis"; 54 version = "1.13.0"; 55 56 goPackagePath = "github.com/deis/deis"; 57 58 src = fetchFromGitHub { 59 owner = "deis"; 60 repo = "deis"; 61 rev = "v${version}"; 62 sha256 = "1qv9lxqx7m18029lj8cw3k7jngvxs4iciwrypdy0gd2nnghc68sw"; 63 }; 64 65 goDeps = ./deps.nix; 66} 67``` 68 69The `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`: 70 71```nix 72# deps.nix 73[ # goDeps is a list of Go dependencies. 74 { 75 # goPackagePath specifies Go package import path. 76 goPackagePath = "gopkg.in/yaml.v2"; 77 fetch = { 78 # `fetch type` that needs to be used to get package source. 79 # If `git` is used there should be `url`, `rev` and `sha256` defined next to it. 80 type = "git"; 81 url = "https://gopkg.in/yaml.v2"; 82 rev = "a83829b6f1293c91addabc89d0571c246397bbf4"; 83 sha256 = "1m4dsmk90sbi17571h6pld44zxz7jc4lrnl4f27dpd1l8g5xvjhh"; 84 }; 85 } 86 { 87 goPackagePath = "github.com/docopt/docopt-go"; 88 fetch = { 89 type = "git"; 90 url = "https://github.com/docopt/docopt-go"; 91 rev = "784ddc588536785e7299f7272f39101f7faccc3f"; 92 sha256 = "0wwz48jl9fvl1iknvn9dqr4gfy1qs03gxaikrxxp9gry6773v3sj"; 93 }; 94 } 95] 96``` 97 98To 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. 99 100You may use Go packages installed into the active Nix profiles by adding the following to your ~/.bashrc: 101 102```bash 103for p in $NIX_PROFILES; do 104 GOPATH="$p/share/go:$GOPATH" 105done 106``` 107 108## Attributes used by the builders {#ssec-go-common-attributes} 109 110Both `buildGoModule` and `buildGoPackage` can be tweaked to behave slightly differently, if the following attributes are used: 111 112### `ldflags` {#var-go-ldflags} 113 114Arguments 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: 115 116```nix 117 ldflags = [ 118 "-s" "-w" 119 "-X main.Version=${version}" 120 "-X main.Commit=${version}" 121 ]; 122``` 123 124### `tags` {#var-go-tags} 125 126Arguments to pass to the Go via the `-tags` argument of `go build`. For example: 127 128```nix 129 tags = [ 130 "production" 131 "sqlite" 132 ]; 133``` 134 135```nix 136 tags = [ "production" ] ++ lib.optionals withSqlite [ "sqlite" ]; 137``` 138 139### `deleteVendor` {#var-go-deleteVendor} 140 141Removes the pre-existing vendor directory. This should only be used if the dependencies included in the vendor folder are broken or incomplete. 142 143### `subPackages` {#var-go-subPackages} 144 145Specified 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. 146 147### `excludedPackages` {#var-go-excludedPackages} 148 149Specified as a string or list of strings. Causes the builder to skip building child packages that match any of the provided values. If `excludedPackages` is not specified, all child packages will be built.