buildvars

package
v25.3.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package buildvars centralizes the environment variables that configure the dgraph build: binary name, Docker image tags, build-toolchain refs, and derived paths such as /gobin/<binary>. It replaces ad-hoc os.Getenv calls scattered across the codebase with a single enumerated, typed registry.

Each Var carries its env-var name and a defaulter: either a literal string or a function that computes the default at Get() time. The function form supports Vars whose default depends on other Vars, such as /gobin/<BinaryName>.

Usage:

bin := buildvars.BinaryName.Get()       // reads $BIN or default
path := buildvars.GoBinDgraphPath.Get() // "/gobin/<BinaryName value>"

Forks override literal defaults from their own package init():

func init() {
    buildvars.BinaryName.SetDefault("myfork-dgraph")
}

Derived Vars automatically pick up the override because they recompute at Get() time.

The package depends on no private-fork tree. A vendor or downstream that removes all fork-specific files sees only the upstream defaults declared here.

Index

Constants

This section is empty.

Variables

View Source
var (
	// BinaryName is the name of the dgraph binary at build time (what
	// `go build -o` writes, matching upstream's $(BIN) in
	// dgraph/Makefile) and at runtime (what compose files and test
	// harnesses invoke as /gobin/$BIN). Upstream default: "dgraph".
	// Forks rename via env or SetDefault, e.g. "myfork-dgraph".
	//
	// The env-var name remains BIN for backward compatibility with
	// shell scripts, Makefiles, and CI configs that reference $(BIN)
	// or ${BIN} directly. The Go identifier is BinaryName because Go
	// callers benefit from the more descriptive symbol.
	BinaryName = newVar("BIN", "dgraph")

	// DockerImage is the Docker image tag (without :tag suffix) used by
	// the Makefile local-image and docker-image targets and by the
	// compose generator's --image default. Upstream default:
	// "dgraph/dgraph".
	DockerImage = newVar("DOCKER_IMAGE", "dgraph/dgraph")

	// BuildImage is the toolchain image for the Dockerfile build stage.
	// Upstream uses an OS base image; forks may substitute a hardened
	// toolchain such as Chainguard go-fips. Paired with BuildTag.
	BuildImage = newVar("BUILD_IMAGE", "ubuntu")

	// BuildTag is the tag for BuildImage. Upstream default: "24.04".
	BuildTag = newVar("BUILD_TAG", "24.04")

	// RuntimeImage is the runtime base image for the Dockerfile runtime
	// stage. Paired with RuntimeTag.
	RuntimeImage = newVar("RUNTIME_IMAGE", "ubuntu")

	// RuntimeTag is the tag for RuntimeImage.
	RuntimeTag = newVar("RUNTIME_TAG", "24.04")

	// BuildTags is the space-separated list of Go build tags passed to
	// `go build` via -tags. The upstream default composes "jemalloc"
	// (the well-known libjemalloc tag) with any tags a fork adds via
	// [CustomBuildTags]. The Makefile and this Var share the same
	// composition, so `make` and direct `go run` agree on the final
	// tag set.
	//
	// "jemalloc" is meaningful only on Linux and macOS; the Makefile
	// omits it on other hosts. Go code reading BuildTags treats the
	// value as informational and makes no OS-aware decisions. The
	// Makefile handles OS gating for the actual `go build` invocation.
	BuildTags = newDerivedVar("BUILD_TAGS", func() string {
		base := "jemalloc"
		extra := CustomBuildTags.Get()
		if extra == "" {
			return base
		}
		return base + " " + extra
	})

	// CustomBuildTags is the list of additional Go build tags a fork or
	// downstream consumer appends to [BuildTags]. Upstream default: empty.
	// Consumers needing compile-time code paths set this to
	// space-separated tag names, e.g. "myfork fips". The Makefile
	// concatenates CustomBuildTags onto BuildTags, so callers read
	// BuildTags to see the final tag set.
	CustomBuildTags = newVar("CUSTOM_BUILD_TAGS", "")

	// GoRunTags is the build-tag list passed via -tags to `go run`
	// invocations from Makefile helper recipes. For example, `build-env`
	// runs `go run -tags=$(GO_RUN_TAGS) ./buildvars/cmd/buildvars` so
	// the buildvars CLI picks up a fork's init-time default overrides.
	//
	// Distinct from [BuildTags]: GoRunTags contains only the tags
	// needed to trigger fork-specific init()-time registration,
	// typically just a fork's primary tag. BuildTags adds "jemalloc"
	// and any other compile-time-only tags a long-running binary needs.
	//
	// Upstream default: empty. Forks override via an init()-time
	// SetDefault call in the package their registration tag guards.
	GoRunTags = newVar("GO_RUN_TAGS", "")

	// DgraphVersion is the version tag baked into the Docker image and
	// into the binary's version output. Upstream default: "local",
	// the development build tag. CI overrides via env to the release
	// tag on tagged builds, e.g. "v25.3.0".
	DgraphVersion = newVar("DGRAPH_VERSION", "local")

	// Build is the short git commit SHA of the build source, baked
	// into the binary's version output via -ldflags -X. The default
	// computes at Get() time from `git rev-parse --short HEAD` and is
	// empty when the build happens outside a git working copy.
	Build = newDerivedVar("BUILD", func() string {
		return shellOutput("git", "rev-parse", "--short", "HEAD")
	})

	// BuildDate is the commit timestamp of the build source, baked
	// into the binary via -ldflags -X. The default computes from
	// `git log -1 --format=%ci`.
	BuildDate = newDerivedVar("BUILD_DATE", func() string {
		return shellOutput("git", "log", "-1", "--format=%ci")
	})

	// BuildBranch is the git branch of the build source, baked in via
	// -ldflags -X. The default computes from
	// `git rev-parse --abbrev-ref HEAD`.
	BuildBranch = newDerivedVar("BUILD_BRANCH", func() string {
		return shellOutput("git", "rev-parse", "--abbrev-ref", "HEAD")
	})

	// BuildCodename is a human-readable name for the release family,
	// baked into the binary via -ldflags -X. Upstream default: "dgraph";
	// CI overrides per release.
	BuildCodename = newVar("BUILD_CODENAME", "dgraph")

	// BuildVersion is the full version string baked into the binary,
	// e.g. "v25.3.0-5-gabc1234". The default computes from
	// `git describe --always --tags` and is empty when git is
	// unavailable. CI release builds override via env to the exact
	// release tag.
	BuildVersion = newDerivedVar("BUILD_VERSION", func() string {
		return shellOutput("git", "describe", "--always", "--tags")
	})

	// LinuxGobin is the host directory holding the Linux-built dgraph
	// binary used for bind-mounting into containers. On Linux, usually
	// $GOPATH/bin (native); on macOS, a separate $GOPATH/linux_<arch>/
	// directory so the cross-compiled Linux binary does not collide
	// with the Mach-O host binary. The default computes at Get() time
	// from $GOPATH + runtime.GOARCH.
	LinuxGobin = newDerivedVar("LINUX_GOBIN", func() string {
		gopath := os.Getenv("GOPATH")
		if gopath == "" {
			return ""
		}
		return gopath + "/linux_" + runtime.GOARCH
	})

	// GoBinDgraphPath is the in-container path to the dgraph binary:
	// the bind-mounted /gobin directory joined with the binary name.
	// Used by jepsen's --local-binary default and by the compose
	// generator for services configured with LocalBin. Derived from
	// [BinaryName] at Get() time so the path tracks the binary name.
	GoBinDgraphPath = newDerivedVar("GOBIN_DGRAPH_PATH", func() string {
		return "/gobin/" + BinaryName.Get()
	})

	// HostGopathDgraphPath is the host-side absolute path to the built
	// dgraph binary, $GOPATH/bin/<BinaryName>. Used by testutil and
	// t/t.go to locate the binary on the runner host outside
	// containers. Returns empty when $GOPATH is unset so callers can
	// fall back to build.Default.GOPATH or their own resolution.
	// Derived from [BinaryName] at Get() time.
	HostGopathDgraphPath = newDerivedVar("HOST_GOPATH_DGRAPH_PATH", func() string {
		gopath := os.Getenv("GOPATH")
		if gopath == "" {
			return ""
		}
		return gopath + "/bin/" + BinaryName.Get()
	})
)

The canonical set of build-configuration vars. Each constant initializes with the upstream OSS default value. Forks override via the SetDefault method from their own init().

All lists every defined Var in declaration order. Exposed so tooling such as diagnostic dumps, documentation generators, and resolver overrides can iterate the canonical set.

View Source
var FIPS140CryptoRestricted = false

FIPS140CryptoRestricted reports whether this binary's cryptographic selections are restricted to the FIPS 140 approved-algorithms set. Default false. A downstream fork running FIPS-restricted builds sets this var to true from a tag-guarded init(), where the tag is whatever the fork uses to gate its FIPS-aware code paths. The init() runs before any caller's main() or test body.

Test code uses it to skip cases the FIPS-restricted binary cannot satisfy:

if buildvars.FIPS140CryptoRestricted {
    t.Skip("test exercises crypto outside the FIPS 140 restrictions")
}

This is a build-time fact about which cryptographic choices the binary accepts. It does NOT report whether the underlying cryptographic implementation is FIPS 140-validated at runtime; crypto/fips140.Enabled() answers that.

Read-only after package init.

Functions

func ExportAll

func ExportAll() error

ExportAll calls Var.Export on every Var in All and returns the first error encountered, or nil on success. Convenient for entry points that must materialize the entire buildvars registry into the process environment before spawning subprocesses.

Types

type Var

type Var struct {
	// Name is the literal environment variable name. Set at declaration
	// and treated as immutable; do not mutate after package init.
	Name string
	// contains filtered or unexported fields
}

Var is a single build-configuration environment variable. It bundles the env-var name (Name) with a defaulter: either a literal fall-through string or a function that computes the default at Get() time. The two forms are exclusive; one is always nil.

Instances are pointers. Call sites reference exported constants such as BinaryName and resolve values via Var.Get.

Literal defaults are mutable via Var.SetDefault so forks can override from their own init() without affecting call sites. SetDefault must run during package init() before any goroutines exist; the type performs no synchronization because no concurrent reader/writer scenario is reachable in practice.

func (*Var) Default

func (v *Var) Default() string

Default returns the currently registered default and ignores any env override. For derived Vars it triggers the computation. Exposed for tests that mutate via Var.SetDefault and must capture the prior value to restore later, and for diagnostic tooling that logs the default independent of any env override.

func (*Var) Export

func (v *Var) Export() error

Export sets the process-level environment variable named by v.Name to the Var's currently resolved value, as returned by Var.Get. Useful when spawning subprocesses that read their own os.Getenv: call v.Export() before exec to make the registered default visible to the child as an explicit env entry.

Idempotent: when the env var is already set, Get returns the existing value and Export writes the same value back. Safe to call repeatedly.

Returns any error from os.Setenv (rare).

func (*Var) Get

func (v *Var) Get() string

Get returns the value of the environment variable when set and non-empty; otherwise the currently registered or computed default. Call sites prefer Get to os.Getenv so defaults can be overridden centrally without changing the call site.

func (*Var) SetDefault

func (v *Var) SetDefault(value string)

SetDefault replaces the registered default value with a literal string. For derived Vars it also clears the defaulter function, freezing the default at the new literal. Run once during package init() before any goroutines exist; the type performs no synchronization.

Directories

Path Synopsis
cmd
buildvars command
Command buildvars emits the buildvars registry as lines of text in a selectable format for consumption by shell eval, GNU Make eval, or direct parsing by a validation script.
Command buildvars emits the buildvars registry as lines of text in a selectable format for consumption by shell eval, GNU Make eval, or direct parsing by a validation script.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL