Go: Difference between revisions

From NixOS Wiki
imported>Fadenb
m Syntax highlighting
imported>HLandau
No edit summary
Line 1: Line 1:
[https://golang.org/ Go] is a statically-typed language with syntax loosely derived from that of C, adding garbage collected memory management, type safety, some dynamic-typing capabilities, additional built-in types such as variable-length arrays and key-value maps, and a large standard library.
[https://golang.org/ Go] is a statically-typed language with syntax loosely derived from that of C, adding garbage collected memory management, type safety, some dynamic-typing capabilities, additional built-in types such as variable-length arrays and key-value maps, and a large standard library.


== Compile go program with static compile flags ==
==Using cgo on NixOS==
On NixOS, include files and libraries aren't kept in a system-wide search path. If a Go program uses cgo and attempts to include C header files, or link against libraries, compilation is likely to fail.


If <syntaxhighlight lang="bash" inline>go build -ldflags "-s -w -linkmode external -extldflags -static"</syntaxhighlight> fails on NixOS, with the error message <code>cannot find</code>-lpthread<code>and</code>cannot find -lc` - it is because the linker cannot find static glibc to link with. You need to have glibc.static in your environment (and have CFLAGS/LDFLAGS adjusted accordingly).
In order to expose header files and libraries in environment variable search paths, <tt>nix-shell</tt> can be used to enter an environment which provides the requested development dependencies.


One way to achieve this is to have something like the following as <code>shell.nix</code> and run the compilation in a nix-shell:
For example, suppose a Go program includes <tt>&lt;sys/capability.h&gt;</tt> (provided by <tt>libcap</tt>), and links against <tt>libcap</tt>. To obtain an environment in which the program can be compiled, run:


<syntaxhighlight lang="nix">with import <nixpkgs> {}; {
<syntaxhighlight lang="console">
  devEnv = stdenv.mkDerivation {
$ nix-shell -p libcap go gcc
    name = "dev";
</syntaxhighlight>
    buildInputs = [ stdenv git go glibc.static ];
 
    CFLAGS="-I${pkgs.glibc.dev}/include";
You can verify the presence of the necessary environment variables via the following command:
    LDFLAGS="-L${pkgs.glibc}/lib";
<syntaxhighlight lang="console">
  };
$ export | egrep 'NIX_.*(LDFLAGS|COMPILE|LINK)'
}</syntaxhighlight>
</syntaxhighlight>
 
If you intend to compile against glibc statically (such as via <tt>go build -ldflags "-s -w -linkmode external -extldflags -static"</tt>), add <tt>glibc.static</tt> to the list of packages passed to <tt>nix-shell</tt>.
 
[[Category:Languages]]

Revision as of 17:41, 24 October 2017

Go is a statically-typed language with syntax loosely derived from that of C, adding garbage collected memory management, type safety, some dynamic-typing capabilities, additional built-in types such as variable-length arrays and key-value maps, and a large standard library.

Using cgo on NixOS

On NixOS, include files and libraries aren't kept in a system-wide search path. If a Go program uses cgo and attempts to include C header files, or link against libraries, compilation is likely to fail.

In order to expose header files and libraries in environment variable search paths, nix-shell can be used to enter an environment which provides the requested development dependencies.

For example, suppose a Go program includes <sys/capability.h> (provided by libcap), and links against libcap. To obtain an environment in which the program can be compiled, run:

$ nix-shell -p libcap go gcc

You can verify the presence of the necessary environment variables via the following command:

$ export | egrep 'NIX_.*(LDFLAGS|COMPILE|LINK)'

If you intend to compile against glibc statically (such as via go build -ldflags "-s -w -linkmode external -extldflags -static"), add glibc.static to the list of packages passed to nix-shell.