Go: Difference between revisions
imported>Mitar Add musl static building. |
m Category:Go added, category:applications removed |
||
(7 intermediate revisions by 6 users not shown) | |||
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. | ||
== | == buildGoModule == | ||
nixpkgs includes a library function called '''buildGoModule''' ([https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/go/module.nix implementation]) See [https://nixos.org/manual/nixpkgs/stable/#sec-language-go nixpkgs manual '''Language: Go'''] | |||
See [https://nixos.org/manual/nixpkgs/stable/#sec-language-go nixpkgs manual '''Language: Go'''] | |||
=== buildGoModule === | `buildGoModule` uses the version of Go that's included in `nixpkgs` to build the software. | ||
==== Using a specific version of Go ==== | |||
To build for a specific version of Go, you may need to find the appropriate `pkgs.buildGoXXXModule` function to use. | |||
This function may not be present in the version of nixpkgs that you're using, for example, `buildGo122Module` is not available in `github:NixOS/nixpkgs/nixos-23.05`, but is available in `github:NixOS/nixpkgs/nixos-unstable`. | |||
==== Subpackages ==== | |||
By default, `buildGoModule` will attempt to build the `main` package that's in the root of the source code location. | |||
However, it's a common pattern in Go applications to have binaries within the `./cmd/binary-name` directory instead. | |||
Setting the `subPackages` attribute to be a list of the packages to build supports this pattern. | |||
==== Example (downloading source code from Github) ==== | |||
The following `flake.nix` demonstrates how to build a Go module, where the source code is located in Github. To use it, copy this file as `flake.nix` into an empty directory on your computer, and run `nix build`. Nix will download the source code, including dependencies, and produce a `./result` folder containing a `ziti` binary. | |||
Running `nix shell` will create a shell, where you can execute the `ziti` binary.<syntaxhighlight lang="nix"> | |||
{ | |||
description = "OpenZiti"; | |||
inputs = { | |||
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; | |||
}; | |||
outputs = { self, nixpkgs }: | |||
let | |||
allSystems = [ | |||
"x86_64-linux" # 64-bit Intel/AMD Linux | |||
"aarch64-linux" # 64-bit ARM Linux | |||
"x86_64-darwin" # 64-bit Intel macOS | |||
"aarch64-darwin" # 64-bit ARM macOS | |||
]; | |||
forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f { | |||
pkgs = import nixpkgs { inherit system; }; | |||
}); | |||
in | |||
{ | |||
packages = forAllSystems ({ pkgs }: { | |||
default = pkgs.buildGo122Module rec { | |||
pname = "openziti"; | |||
version = "1.0.0"; | |||
subPackages = [ "ziti" ]; | |||
src = pkgs.fetchFromGitHub { | |||
owner = "openziti"; | |||
repo = "ziti"; | |||
rev = "v${version}"; | |||
sha256 = "sha256-2li/+XWKk+lybB1DE0unKvQrA0pKE9VIRFoEYMcbLS8="; | |||
}; | |||
vendorHash = "sha256-uyjQd5kB61UEKSl1Qf1Gu6Fr40l4KixHSnEtTMq58Vc="; | |||
}; | |||
}); | |||
}; | |||
} | |||
</syntaxhighlight> | |||
==== Example (local source) ==== | |||
If you want to build a local project with Nix, replace the `src` attribute to be the local directory, e.g.:<syntaxhighlight lang="nix"> | |||
some-package = buildGoModule { | |||
src = ./. | |||
}; | |||
</syntaxhighlight> | |||
==== Monorepo support ==== | |||
the <tt>go.mod</tt> file must be in the source root for <tt>buildGoModule</tt>. | the <tt>go.mod</tt> file must be in the source root for <tt>buildGoModule</tt>. | ||
to change the source root, use | to change the source root, use | ||
Line 28: | Line 91: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Using cgo on NixOS == | |||
==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. | 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. | ||
Line 92: | Line 149: | ||
vendorSha256 = "1879j77k96684wi554rkjxydrj8g3hpp0kvxz03sd8dmwr3lh83j"; | vendorSha256 = "1879j77k96684wi554rkjxydrj8g3hpp0kvxz03sd8dmwr3lh83j"; | ||
ldflags = [ | |||
"-s -w -X github.com/knqyf263/pet/cmd.version=${version}" | |||
]; | |||
nativeBuildInputs = [musl]; | nativeBuildInputs = [musl]; | ||
Line 112: | Line 173: | ||
[[Category:Languages]] | [[Category:Languages]] | ||
[[Category: | [[Category:Go]] |