2015-12-19 16:04:36 +01:00
<section xmlns= "http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink"
xml:id="sec-language-go">
2018-05-02 01:54:21 +02:00
<title > Go</title>
2015-12-19 16:04:36 +01:00
2019-03-08 20:17:05 +01:00
<section xml:id= "ssec-go-modules" >
<title > Go modules</title>
2015-12-19 16:04:36 +01:00
2019-03-08 20:17:05 +01:00
<para >
2019-09-18 22:12:54 +02:00
The function <varname > buildGoModule </varname> builds Go programs managed with Go modules. It builds a <link xlink:href= "https://github.com/golang/go/wiki/Modules" > Go modules</link> through a two phase build:
2019-03-08 20:17:05 +01:00
<itemizedlist >
<listitem >
<para >
2019-09-18 22:12:54 +02:00
An intermediate fetcher derivation. This derivation will be used to fetch all of the dependencies of the Go module.
2019-03-08 20:17:05 +01:00
</para>
</listitem>
<listitem >
<para >
2019-09-18 22:12:54 +02:00
A final derivation will use the output of the intermediate derivation to build the binaries and produce the final output.
2019-03-08 20:17:05 +01:00
</para>
</listitem>
</itemizedlist>
</para>
<example xml:id= 'ex-buildGoModule' >
<title > buildGoModule</title>
<programlisting >
pet = buildGoModule rec {
2019-12-15 23:58:16 +01:00
pname = "pet";
2019-03-08 20:17:05 +01:00
version = "0.3.4";
src = fetchFromGitHub {
owner = "knqyf263";
repo = "pet";
rev = "v${version}";
sha256 = "0m2fzpqxk7hrbxsgqplkg7h2p7gv6s1miymv3gvw0cz039skag0s";
};
2020-05-08 05:13:49 +02:00
vendorSha256 = "1879j77k96684wi554rkjxydrj8g3hpp0kvxz03sd8dmwr3lh83j"; <co xml:id= 'ex-buildGoModule-1' />
2019-03-08 20:17:05 +01:00
subPackages = [ "." ]; <co xml:id= 'ex-buildGoModule-2' />
2020-08-12 11:33:03 +02:00
deleteVendor = true; <co xml:id= 'ex-buildGoModule-3' />
runVend = true; <co xml:id= 'ex-buildGoModule-4' />
2020-08-01 19:56:47 +02:00
2019-03-08 20:17:05 +01:00
meta = with lib; {
description = "Simple command-line snippet manager, written in Go";
2020-02-16 17:31:03 +01:00
homepage = "https://github.com/knqyf263/pet";
2019-03-08 20:17:05 +01:00
license = licenses.mit;
maintainers = with maintainers; [ kalbasit ];
platforms = platforms.linux ++ platforms.darwin;
};
}
</programlisting>
</example>
<para >
2019-09-18 22:12:54 +02:00
<xref linkend= 'ex-buildGoModule' /> is an example expression using buildGoModule, the following arguments are of special significance to the function:
2019-03-08 20:17:05 +01:00
<calloutlist >
<callout arearefs= 'ex-buildGoModule-1' >
<para >
2020-05-08 05:13:49 +02:00
<varname > vendorSha256</varname> is the hash of the output of the intermediate fetcher derivation.
2019-03-08 20:17:05 +01:00
</para>
</callout>
<callout arearefs= 'ex-buildGoModule-2' >
<para >
2019-09-18 22:12:54 +02:00
<varname > subPackages</varname> limits the builder from building child packages that have not been listed. If <varname > subPackages</varname> is not specified, all child packages will be built.
2019-03-08 20:17:05 +01:00
</para>
</callout>
2020-06-04 04:54:39 +02:00
<callout arearefs= 'ex-buildGoModule-3' >
2020-08-12 11:33:03 +02:00
<para >
<varname > deleteVendor</varname> removes the pre-existing vendor directory and fetches the dependencies. This should only be used if the dependencies included in the vendor folder are broken or incomplete.
</para>
</callout>
<callout arearefs= 'ex-buildGoModule-4' >
2020-06-04 04:54:39 +02:00
<para >
2020-08-01 19:56:47 +02:00
<varname > runVend</varname> 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.
2020-06-04 04:54:39 +02:00
</para>
</callout>
2019-03-08 20:17:05 +01:00
</calloutlist>
</para>
2019-12-28 21:36:42 +01:00
<para >
2020-05-08 05:13:49 +02:00
<varname > vendorSha256</varname> can also take <varname > null</varname> as an input.
2019-12-28 21:36:42 +01:00
2020-05-08 05:13:49 +02:00
When `null` is used as a value, rather than fetching the dependencies
and vendoring them, we use the vendoring included within the source repo.
2020-07-31 07:06:53 +02:00
If you'd like to not have to update this field on dependency changes,
2020-05-08 05:13:49 +02:00
run `go mod vendor` in your source repo and set 'vendorSha256 = null;'
2019-12-28 21:36:42 +01:00
</para>
2019-03-08 20:17:05 +01:00
</section>
<section xml:id= "ssec-go-legacy" >
<title > Go legacy</title>
<para >
2019-09-18 22:12:54 +02:00
The function <varname > buildGoPackage </varname> builds legacy Go programs, not supporting Go modules.
2019-03-08 20:17:05 +01:00
</para>
<example xml:id= 'ex-buildGoPackage' >
<title > buildGoPackage</title>
2015-12-19 16:04:36 +01:00
<programlisting >
2016-06-10 20:26:13 +02:00
deis = buildGoPackage rec {
2019-12-15 23:58:16 +01:00
pname = "deis";
2016-06-10 20:26:13 +02:00
version = "1.13.0";
2017-06-27 16:34:03 +02:00
2016-06-10 20:26:13 +02:00
goPackagePath = "github.com/deis/deis"; <co xml:id= 'ex-buildGoPackage-1' />
subPackages = [ "client" ]; <co xml:id= 'ex-buildGoPackage-2' />
2015-12-19 16:04:36 +01:00
src = fetchFromGitHub {
2016-06-10 20:26:13 +02:00
owner = "deis";
repo = "deis";
rev = "v${version}";
sha256 = "1qv9lxqx7m18029lj8cw3k7jngvxs4iciwrypdy0gd2nnghc68sw";
2015-12-19 16:04:36 +01:00
};
2016-06-10 20:26:13 +02:00
2016-09-10 12:04:13 +02:00
goDeps = ./deps.nix; <co xml:id= 'ex-buildGoPackage-3' />
2016-06-10 20:26:13 +02:00
2020-08-31 07:54:19 +02:00
deleteVendor = true; <co xml:id= 'ex-buildGoPackage-4' />
buildFlags = [ "--tags" "release" ]; <co xml:id= 'ex-buildGoPackage-5' />
2016-06-10 20:26:13 +02:00
}
2015-12-19 16:04:36 +01:00
</programlisting>
2019-03-08 20:17:05 +01:00
</example>
<para >
2019-09-18 22:12:54 +02:00
<xref linkend= 'ex-buildGoPackage' /> is an example expression using buildGoPackage, the following arguments are of special significance to the function:
2019-03-08 20:17:05 +01:00
<calloutlist >
<callout arearefs= 'ex-buildGoPackage-1' >
<para >
2019-09-18 22:12:54 +02:00
<varname > goPackagePath</varname> specifies the package's canonical Go import path.
2019-03-08 20:17:05 +01:00
</para>
</callout>
<callout arearefs= 'ex-buildGoPackage-2' >
<para >
2019-09-18 22:12:54 +02:00
<varname > subPackages</varname> limits the builder from building child packages that have not been listed. If <varname > subPackages</varname> is not specified, all child packages will be built.
2019-03-08 20:17:05 +01:00
</para>
<para >
2019-09-18 22:12:54 +02:00
In this example only <literal > github.com/deis/deis/client</literal> will be built.
2019-03-08 20:17:05 +01:00
</para>
</callout>
<callout arearefs= 'ex-buildGoPackage-3' >
<para >
2019-09-18 22:12:54 +02:00
<varname > goDeps</varname> 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 <varname > deps.nix</varname> file for readability. The dependency data structure is described below.
2019-03-08 20:17:05 +01:00
</para>
</callout>
<callout arearefs= 'ex-buildGoPackage-4' >
2020-08-31 07:54:19 +02:00
<para >
<varname > deleteVendor</varname> removes the pre-existing vendor directory. This should only be used if the dependencies included in the vendor folder are broken or incomplete.
</para>
</callout>
<callout arearefs= 'ex-buildGoPackage-5' >
2019-03-08 20:17:05 +01:00
<para >
2019-09-18 22:12:54 +02:00
<varname > buildFlags</varname> is a list of flags passed to the go build command.
2019-03-08 20:17:05 +01:00
</para>
</callout>
</calloutlist>
</para>
<para >
2019-09-18 22:12:54 +02:00
The <varname > goDeps</varname> attribute can be imported from a separate <varname > nix</varname> file that defines which Go libraries are needed and should be included in <varname > GOPATH</varname> for <varname > buildPhase</varname> .
2019-03-08 20:17:05 +01:00
</para>
<example xml:id= 'ex-goDeps' >
<title > deps.nix</title>
2016-06-10 20:26:13 +02:00
<programlisting >
[ <co xml:id= 'ex-goDeps-1' />
2016-09-10 12:04:13 +02:00
{
goPackagePath = "gopkg.in/yaml.v2"; <co xml:id= 'ex-goDeps-2' />
fetch = {
type = "git"; <co xml:id= 'ex-goDeps-3' />
url = "https://gopkg.in/yaml.v2";
rev = "a83829b6f1293c91addabc89d0571c246397bbf4";
sha256 = "1m4dsmk90sbi17571h6pld44zxz7jc4lrnl4f27dpd1l8g5xvjhh";
};
}
{
goPackagePath = "github.com/docopt/docopt-go";
fetch = {
type = "git";
url = "https://github.com/docopt/docopt-go";
rev = "784ddc588536785e7299f7272f39101f7faccc3f";
sha256 = "0wwz48jl9fvl1iknvn9dqr4gfy1qs03gxaikrxxp9gry6773v3sj";
};
}
2016-06-10 20:26:13 +02:00
]
</programlisting>
2019-03-08 20:17:05 +01:00
</example>
<para >
<calloutlist >
<callout arearefs= 'ex-goDeps-1' >
<para >
<varname > goDeps</varname> is a list of Go dependencies.
</para>
</callout>
<callout arearefs= 'ex-goDeps-2' >
<para >
<varname > goPackagePath</varname> specifies Go package import path.
</para>
</callout>
<callout arearefs= 'ex-goDeps-3' >
<para >
2019-09-18 22:12:54 +02:00
<varname > fetch type</varname> that needs to be used to get package source. If <varname > git</varname> is used there should be <varname > url</varname> , <varname > rev</varname> and <varname > sha256</varname> defined next to it.
2019-03-08 20:17:05 +01:00
</para>
</callout>
</calloutlist>
</para>
<para >
2019-09-18 22:12:54 +02:00
To extract dependency information from a Go package in automated way use <link xlink:href= "https://github.com/kamilchm/go2nix" > go2nix</link> . It can produce complete derivation and <varname > goDeps</varname> file for Go programs.
2019-03-08 20:17:05 +01:00
</para>
<para >
2019-09-18 22:12:54 +02:00
You may use Go packages installed into the active Nix profiles by adding the following to your ~/.bashrc:
2015-12-19 16:04:36 +01:00
<screen >
for p in $NIX_PROFILES; do
GOPATH="$p/share/go:$GOPATH"
done
</screen>
2019-03-08 20:17:05 +01:00
</para>
</section>
2015-12-19 16:04:36 +01:00
</section>