1<section xmlns="http://docbook.org/ns/docbook"
2 xmlns:xlink="http://www.w3.org/1999/xlink"
3 xml:id="sec-language-go">
4
5<title>Go</title>
6
7<para>The function <varname>buildGoPackage</varname> builds
8standard Go programs.
9</para>
10
11<example xml:id='ex-buildGoPackage'><title>buildGoPackage</title>
12<programlisting>
13deis = buildGoPackage rec {
14 name = "deis-${version}";
15 version = "1.13.0";
16
17 goPackagePath = "github.com/deis/deis"; <co xml:id='ex-buildGoPackage-1' />
18 subPackages = [ "client" ]; <co xml:id='ex-buildGoPackage-2' />
19
20 src = fetchFromGitHub {
21 owner = "deis";
22 repo = "deis";
23 rev = "v${version}";
24 sha256 = "1qv9lxqx7m18029lj8cw3k7jngvxs4iciwrypdy0gd2nnghc68sw";
25 };
26
27 goDeps = ./deps.json; <co xml:id='ex-buildGoPackage-3' />
28
29 buildFlags = "--tags release"; <co xml:id='ex-buildGoPackage-4' />
30}
31</programlisting>
32</example>
33
34<para><xref linkend='ex-buildGoPackage'/> is an example expression using buildGoPackage,
35the following arguments are of special significance to the function:
36
37<calloutlist>
38
39 <callout arearefs='ex-buildGoPackage-1'>
40 <para>
41 <varname>goPackagePath</varname> specifies the package's canonical Go import path.
42 </para>
43 </callout>
44
45 <callout arearefs='ex-buildGoPackage-2'>
46 <para>
47 <varname>subPackages</varname> limits the builder from building child packages that
48 have not been listed. If <varname>subPackages</varname> is not specified, all child
49 packages will be built.
50 </para>
51 <para>
52 In this example only <literal>github.com/deis/deis/client</literal> will be built.
53 </para>
54 </callout>
55
56 <callout arearefs='ex-buildGoPackage-3'>
57 <para>
58 <varname>goDeps</varname> is where the Go dependencies of a Go program are listed
59 in a JSON format described below.
60 </para>
61 </callout>
62
63 <callout arearefs='ex-buildGoPackage-4'>
64 <para>
65 <varname>buildFlags</varname> is a list of flags passed to the go build command.
66 </para>
67 </callout>
68
69</calloutlist>
70
71</para>
72
73<para>The <varname>goDeps</varname> attribute should point to a JSON file that defines which Go libraries
74 are needed and should be included in <varname>GOPATH</varname> for <varname>buildPhase</varname>.
75
76</para>
77
78<example xml:id='ex-goDeps'><title>deps.json</title>
79<programlisting>
80[ <co xml:id='ex-goDeps-1' />
81 {
82 "goPackagePath": "gopkg.in/yaml.v2", <co xml:id='ex-goDeps-2' />
83 "fetch": {
84 "type": "git", <co xml:id='ex-goDeps-3' />
85 "url": "https://gopkg.in/yaml.v2",
86 "rev": "a83829b6f1293c91addabc89d0571c246397bbf4",
87 "sha256": "1m4dsmk90sbi17571h6pld44zxz7jc4lrnl4f27dpd1l8g5xvjhh"
88 }
89 }
90]
91</programlisting>
92</example>
93
94<para>
95
96<calloutlist>
97
98 <callout arearefs='ex-goDeps-1'>
99 <para>
100 <varname>goDeps</varname> is a list of Go dependencies.
101 </para>
102 </callout>
103
104 <callout arearefs='ex-goDeps-2'>
105 <para>
106 <varname>goPackagePath</varname> specifies Go package import path.
107 </para>
108 </callout>
109
110 <callout arearefs='ex-goDeps-3'>
111 <para>
112 <varname>fetch type</varname> that needs to be used to get package source. If <varname>git</varname>
113 is used there should be <varname>url</varname>, <varname>rev</varname> and <varname>sha256</varname>
114 defined next to it.
115 </para>
116 </callout>
117
118</calloutlist>
119
120</para>
121
122<para>
123 <varname>buildGoPackage</varname> produces <xref linkend='chap-multiple-output' xrefstyle="select: title" />
124 where <varname>bin</varname> includes program binaries. You can test build a Go binary as follows:
125
126 <screen>
127 $ nix-build -A deis.bin
128 </screen>
129
130 or build all outputs with:
131
132 <screen>
133 $ nix-build -A deis.all
134 </screen>
135
136 <varname>bin</varname> output will be installed by default with <varname>nix-env -i</varname>
137 or <varname>systemPackages</varname>.
138
139</para>
140
141<para>
142You may use Go packages installed into the active Nix profiles by adding
143the following to your ~/.bashrc:
144
145<screen>
146for p in $NIX_PROFILES; do
147 GOPATH="$p/share/go:$GOPATH"
148done
149</screen>
150</para>
151
152<para>To extract dependency information from a Go package in automated way use <link xlink:href="https://github.com/kamilchm/go2nix">go2nix</link>.
153 It can produce complete derivation and <varname>goDeps</varname> file for Go programs.</para>
154</section>
155