Go: Difference between revisions

Klinger (talk | contribs)
m Category:Go added, category:applications removed
DHCP (talk | contribs)
m relace non-functional `` monospace notation with <code> tags
Line 4: Line 4:
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''']
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''']


`buildGoModule` uses the version of Go that's included in `nixpkgs` to build the software.
<code>buildGoModule</code> uses the version of Go that's included in <code>nixpkgs</code> to build the software.


==== Using a specific version of Go ====
==== 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.
To build for a specific version of Go, you may need to find the appropriate <code>pkgs.buildGoXXXModule</code> 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`.
This function may not be present in the version of nixpkgs that you're using, for example, <code>buildGo122Module</code> is not available in <code>github:NixOS/nixpkgs/nixos-23.05</code>, but is available in <code>github:NixOS/nixpkgs/nixos-unstable</code>.


==== Subpackages ====
==== Subpackages ====
By default, `buildGoModule` will attempt to build the `main` package that's in the root of the source code location.
By default, <code>buildGoModule</code> will attempt to build the <code>main</code> 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.
However, it's a common pattern in Go applications to have binaries within the <code>./cmd/binary-name</code> directory instead.


Setting the `subPackages` attribute to be a list of the packages to build supports this pattern.
Setting the <code>subPackages</code> attribute to be a list of the packages to build supports this pattern.


==== Example (downloading source code from Github) ====
==== 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.
The following <code>flake.nix</code> demonstrates how to build a Go module, where the source code is located in Github. To use it, copy this file as <code>flake.nix</code> into an empty directory on your computer, and run <code>nix build</code>. Nix will download the source code, including dependencies, and produce a <code>./result</code> folder containing a <code>ziti</code> binary.


Running `nix shell` will create a shell, where you can execute the `ziti` binary.<syntaxhighlight lang="nix">
Running <code>nix shell</code> will create a shell, where you can execute the <code>ziti</code> binary.<syntaxhighlight lang="nix">
{
{
   description = "OpenZiti";
   description = "OpenZiti";
Line 62: Line 62:


==== Example (local source) ====
==== 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">
If you want to build a local project with Nix, replace the <code>src</code> attribute to be the local directory, e.g.:<syntaxhighlight lang="nix">
   some-package = buildGoModule {
   some-package = buildGoModule {
     src = ./.
     src = ./.