Skip to main content

Nix Builder Reference

Complete reference for the buildNopherGoApp Nix function.

Basic Usage

buildNopherGoApp {
pname = "myapp";
version = "1.0.0";
src = ./.;
modules = ./nopher.lock.yaml;
}

Parameters

Required Parameters

ParameterTypeDescription
pnamestringPackage name
versionstringPackage version
srcpathSource directory
modulespathPath to nopher.lock.yaml

Build Options

ParameterTypeDefaultDescription
subPackageslist of strings["."]Go packages to build
ldflagslist of strings[]Linker flags
tagslist of strings[]Build tags

Go Environment

ParameterTypeDefaultDescription
goderivation(from context)Go compiler to use (override for newer versions)
CGO_ENABLEDstring"0"Enable/disable CGO
GOOSstringnullTarget operating system
GOARCHstringnullTarget architecture

Testing

ParameterTypeDefaultDescription
doCheckbooltrueRun go test ./... after building
checkFlagslist of strings[]Extra flags passed to go test

Hooks

ParameterTypeDefaultDescription
preBuildstring""Script to run before build
postBuildstring""Script to run after build
preCheckstring""Script to run before tests
postCheckstring""Script to run after tests
preInstallstring""Script to run before install
postInstallstring""Script to run after install

Examples

Basic Application

buildNopherGoApp {
pname = "myapp";
version = "1.0.0";
src = ./.;
modules = ./nopher.lock.yaml;
}

Multiple Binaries

buildNopherGoApp {
pname = "myproject";
version = "2.0.0";
src = ./.;
modules = ./nopher.lock.yaml;
subPackages = [
"./cmd/server"
"./cmd/client"
"./cmd/cli"
];
}

With Linker Flags

Embed version information at build time:

buildNopherGoApp {
pname = "myapp";
version = "1.0.0";
src = ./.;
modules = ./nopher.lock.yaml;
ldflags = [
"-s" "-w" # Strip debug info
"-X main.version=1.0.0"
"-X main.commit=${self.shortRev or "dirty"}"
];
}

With Build Tags

buildNopherGoApp {
pname = "myapp";
version = "1.0.0";
src = ./.;
modules = ./nopher.lock.yaml;
tags = [ "production" "postgres" ];
}

Disabling Tests

Tests run by default. To skip them:

buildNopherGoApp {
pname = "myapp";
version = "1.0.0";
src = ./.;
modules = ./nopher.lock.yaml;
doCheck = false;
}

With Test Flags

Pass extra flags to go test:

buildNopherGoApp {
pname = "myapp";
version = "1.0.0";
src = ./.;
modules = ./nopher.lock.yaml;
checkFlags = [ "-v" "-count=1" ];
}

Cross-Compilation

Build for Linux from macOS:

buildNopherGoApp {
pname = "myapp";
version = "1.0.0";
src = ./.;
modules = ./nopher.lock.yaml;
GOOS = "linux";
GOARCH = "amd64";
}

Build for multiple platforms:

let
buildFor = goos: goarch: buildNopherGoApp {
pname = "myapp-${goos}-${goarch}";
version = "1.0.0";
src = ./.;
modules = ./nopher.lock.yaml;
GOOS = goos;
GOARCH = goarch;
};
in {
linux-amd64 = buildFor "linux" "amd64";
linux-arm64 = buildFor "linux" "arm64";
darwin-amd64 = buildFor "darwin" "amd64";
darwin-arm64 = buildFor "darwin" "arm64";
windows-amd64 = buildFor "windows" "amd64";
}

With Custom Go Version

Use a specific or newer Go version:

{ buildNopherGoApp, go_1_25_latest }:

buildNopherGoApp {
pname = "myapp";
version = "1.0.0";
src = ./.;
modules = ./nopher.lock.yaml;
go = go_1_25_latest; # Override default Go version
}

With CGO

buildNopherGoApp {
pname = "myapp";
version = "1.0.0";
src = ./.;
modules = ./nopher.lock.yaml;
CGO_ENABLED = "1";
nativeBuildInputs = [ pkg-config ];
buildInputs = [ sqlite ];
}

Post-Install Hook

Generate shell completions:

buildNopherGoApp {
pname = "myapp";
version = "1.0.0";
src = ./.;
modules = ./nopher.lock.yaml;
postInstall = ''
mkdir -p $out/share/bash-completion/completions
$out/bin/myapp completion bash > $out/share/bash-completion/completions/myapp

mkdir -p $out/share/zsh/site-functions
$out/bin/myapp completion zsh > $out/share/zsh/site-functions/_myapp
'';
}

With Metadata

buildNopherGoApp {
pname = "myapp";
version = "1.0.0";
src = ./.;
modules = ./nopher.lock.yaml;
meta = {
description = "My awesome application";
homepage = "https://github.com/myorg/myapp";
license = lib.licenses.mit;
maintainers = with lib.maintainers; [ yourname ];
mainProgram = "myapp";
};
}

Integration with Flakes

Full Flake Example

{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
nopher.url = "github:anthr76/nopher";
};

outputs = { self, nixpkgs, flake-utils, nopher }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
nopherLib = nopher.lib.${system};
in {
packages = {
default = nopherLib.buildNopherGoApp {
pname = "myapp";
version = "1.0.0";
src = ./.;
modules = ./nopher.lock.yaml;
};
};

devShells.default = pkgs.mkShell {
packages = [
pkgs.go
nopher.packages.${system}.default
];
};
}
);
}

Using the Overlay

Nopher provides a nixpkgs overlay:

{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
nopher.url = "github:anthr76/nopher";
};

outputs = { self, nixpkgs, nopher }:
let
pkgs = import nixpkgs {
system = "x86_64-linux";
overlays = [ nopher.overlays.default ];
};
in {
packages.x86_64-linux.default = pkgs.buildNopherGoApp {
pname = "myapp";
version = "1.0.0";
src = ./.;
modules = ./nopher.lock.yaml;
};
};
}