Go: Difference between revisions
m Category:Go added, category:applications removed |
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'''] | ||
<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 | 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, | 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, | 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 | However, it's a common pattern in Go applications to have binaries within the <code>./cmd/binary-name</code> directory instead. | ||
Setting the | 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 | 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 | 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 | 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 = ./. | ||