diff options
Diffstat (limited to 'nixpkgs/doc/languages-frameworks/go.section.md')
-rw-r--r-- | nixpkgs/doc/languages-frameworks/go.section.md | 82 |
1 files changed, 50 insertions, 32 deletions
diff --git a/nixpkgs/doc/languages-frameworks/go.section.md b/nixpkgs/doc/languages-frameworks/go.section.md index 369eb88d331f..6db0e73505d2 100644 --- a/nixpkgs/doc/languages-frameworks/go.section.md +++ b/nixpkgs/doc/languages-frameworks/go.section.md @@ -38,24 +38,26 @@ The `buildGoModule` function accepts the following parameters in addition to the The following is an example expression using `buildGoModule`: ```nix -pet = buildGoModule rec { - pname = "pet"; - version = "0.3.4"; - - src = fetchFromGitHub { - owner = "knqyf263"; - repo = "pet"; - rev = "v${version}"; - hash = "sha256-Gjw1dRrgM8D3G7v6WIM2+50r4HmTXvx0Xxme2fH9TlQ="; - }; +{ + pet = buildGoModule rec { + pname = "pet"; + version = "0.3.4"; + + src = fetchFromGitHub { + owner = "knqyf263"; + repo = "pet"; + rev = "v${version}"; + hash = "sha256-Gjw1dRrgM8D3G7v6WIM2+50r4HmTXvx0Xxme2fH9TlQ="; + }; - vendorHash = "sha256-ciBIR+a1oaYH+H1PcC8cD8ncfJczk1IiJ8iYNM+R6aA="; + vendorHash = "sha256-ciBIR+a1oaYH+H1PcC8cD8ncfJczk1IiJ8iYNM+R6aA="; - meta = { - description = "Simple command-line snippet manager, written in Go"; - homepage = "https://github.com/knqyf263/pet"; - license = lib.licenses.mit; - maintainers = with lib.maintainers; [ kalbasit ]; + meta = { + description = "Simple command-line snippet manager, written in Go"; + homepage = "https://github.com/knqyf263/pet"; + license = lib.licenses.mit; + maintainers = with lib.maintainers; [ kalbasit ]; + }; }; } ``` @@ -72,20 +74,22 @@ In the following is an example expression using `buildGoPackage`, the following - `goDeps` is where the Go dependencies of a Go program are listed as a list of package source identified by Go import path. It could be imported as a separate `deps.nix` file for readability. The dependency data structure is described below. ```nix -deis = buildGoPackage rec { - pname = "deis"; - version = "1.13.0"; - - goPackagePath = "github.com/deis/deis"; +{ + deis = buildGoPackage rec { + pname = "deis"; + version = "1.13.0"; + + goPackagePath = "github.com/deis/deis"; + + src = fetchFromGitHub { + owner = "deis"; + repo = "deis"; + rev = "v${version}"; + hash = "sha256-XCPD4LNWtAd8uz7zyCLRfT8rzxycIUmTACjU03GnaeM="; + }; - src = fetchFromGitHub { - owner = "deis"; - repo = "deis"; - rev = "v${version}"; - hash = "sha256-XCPD4LNWtAd8uz7zyCLRfT8rzxycIUmTACjU03GnaeM="; + goDeps = ./deps.nix; }; - - goDeps = ./deps.nix; } ``` @@ -153,10 +157,12 @@ A string list of flags to pass to the Go linker tool via the `-ldflags` argument The most common use case for this argument is to make the resulting executable aware of its own version by injecting the value of string variable using the `-X` flag. For example: ```nix +{ ldflags = [ "-X main.Version=${version}" "-X main.Commit=${version}" ]; +} ``` ### `tags` {#var-go-tags} @@ -164,16 +170,20 @@ The most common use case for this argument is to make the resulting executable a A string list of [Go build tags (also called build constraints)](https://pkg.go.dev/cmd/go#hdr-Build_constraints) that are passed via the `-tags` argument of `go build`. These constraints control whether Go files from the source should be included in the build. For example: ```nix +{ tags = [ "production" "sqlite" ]; +} ``` Tags can also be set conditionally: ```nix +{ tags = [ "production" ] ++ lib.optionals withSqlite [ "sqlite" ]; +} ``` ### `deleteVendor` {#var-go-deleteVendor} @@ -188,10 +198,12 @@ Many Go projects keep the main package in a `cmd` directory. Following example could be used to only build the example-cli and example-server binaries: ```nix -subPackages = [ - "cmd/example-cli" - "cmd/example-server" -]; +{ + subPackages = [ + "cmd/example-cli" + "cmd/example-server" + ]; +} ``` ### `excludedPackages` {#var-go-excludedPackages} @@ -213,10 +225,12 @@ on a per package level using build tags (`tags`). In case CGO is disabled, these When a Go program depends on C libraries, place those dependencies in `buildInputs`: ```nix +{ buildInputs = [ libvirt libxml2 ]; +} ``` `CGO_ENABLED` defaults to `1`. @@ -245,15 +259,18 @@ This is done with the [`-skip` or `-run`](https://pkg.go.dev/cmd/go#hdr-Testing_ For example, only a selection of tests could be run with: ```nix +{ # -run and -skip accept regular expressions checkFlags = [ "-run=^Test(Simple|Fast)$" ]; +} ``` If a larger amount of tests should be skipped, the following pattern can be used: ```nix +{ checkFlags = let # Skip tests that require network access @@ -264,6 +281,7 @@ If a larger amount of tests should be skipped, the following pattern can be used ]; in [ "-skip=^${builtins.concatStringsSep "$|^" skippedTests}$" ]; +} ``` To disable tests altogether, set `doCheck = false;`. |