Skip to content

Commit e5c4fbc

Browse files
bazel modules build works
1 parent 6db1255 commit e5c4fbc

File tree

11 files changed

+1204
-573
lines changed

11 files changed

+1204
-573
lines changed

.bazelrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
#
2+
# .bazelrc
3+
#
4+
common --enable_bzlmod=true
5+
6+
# Disable WORKSPACE file
7+
common --noenable_workspace
8+
19
# Configuration for using Buildbarn on hp4.home
210
# Use 'build:hp4' to apply these flags to the build command when --config=hp4 is used
311
build:hp4 --remote_executor=grpc://hp4.home:8980

BUILD.bazel

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
# go_nix_simple/BUILD.bazel
2-
3-
# gazelle:prefix github.com/randomizedcoder/go_nix_simple
4-
# gazelle:map_kind go_repository go_repository //:deps.bzl%go_repository
5-
# gazelle:repository_macro deps.bzl%go_dependencies
6-
7-
load("@bazel_gazelle//:def.bzl", "gazelle")
1+
load("@gazelle//:def.bzl", "gazelle")
82

93
# Gazelle configuration
104
gazelle(
115
name = "gazelle",
12-
prefix = "github.com/randomizedcoder/go_nix_simple", # Your Go module path
6+
args = [
7+
"-build_file_name=BUILD.bazel",
8+
"-mode=fix",
9+
],
10+
command = "update",
11+
prefix = "github.com/randomizedcoder/go_nix_simple",
1312
)
1413

15-
exports_files(["VERSION"])
14+
# Run gazelle with specific arguments for updating Go dependencies
15+
gazelle(
16+
name = "gazelle-update-repos",
17+
args = [
18+
"-from_file=go.mod",
19+
"-to_macro=deps.bzl%go_dependencies",
20+
"-prune",
21+
],
22+
command = "update-repos",
23+
)

MODULE.bazel

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
1-
###############################################################################
2-
# Bazel now uses Bzlmod by default to manage external dependencies.
3-
# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel.
4-
#
5-
# For more details, please check https://github.com/bazelbuild/bazel/issues/18958
6-
###############################################################################
1+
module(
2+
name = "go_nix_simple",
3+
version = "0.1.0",
4+
)
5+
6+
# Core Bazel dependencies
7+
bazel_dep(name = "rules_go", version = "0.54.0")
8+
bazel_dep(name = "gazelle", version = "0.43.0")
9+
bazel_dep(name = "bazel_skylib", version = "1.6.1")
10+
bazel_dep(name = "rules_oci", version = "1.7.4")
11+
12+
# Go toolchain setup
13+
go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
14+
go_sdk.download(
15+
name = "go_sdk",
16+
version = "1.24.1",
17+
)
18+
use_repo(go_sdk, "go_sdk")
19+
20+
# Go dependencies from go.mod
21+
go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
22+
go_deps.from_file(go_mod = "//:go.mod")
23+
use_repo(
24+
go_deps,
25+
"com_github_clickhouse_clickhouse_go_v2",
26+
"com_github_docker_docker",
27+
"com_github_grafana_pyroscope_go",
28+
"com_github_nats_io_nats_go",
29+
"com_github_pkg_profile",
30+
"com_github_prometheus_client_golang",
31+
"com_github_prometheus_common",
32+
"com_github_redis_go_redis_v9",
33+
)
34+
35+
# OCI setup
36+
oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")
37+
oci.pull(
38+
name = "distroless_base",
39+
digest = "sha256:3d0f463de06b7ddff27684ec3bfd0b54a425149d0f8685308b1fdf297b0265e9",
40+
image = "gcr.io/distroless/static-debian12",
41+
)
42+
use_repo(oci, "distroless_base")

MODULE.bazel.lock

Lines changed: 724 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,16 @@ bazel_go:
370370
bazel_build_remote:
371371
bazel build --config=hp4 //cmd/go_nix_simple:go_nix_simple_binary_noupx
372372

373+
bazel_build_distroless:
374+
bazel build //cmd/go_nix_simple:image_bazel_distroless_noupx_tarball \
375+
--define REPO_PREFIX=docker.io/randomizedcoder \
376+
--define VERSION=latest
377+
378+
bazel_build_scratch:
379+
bazel build //cmd/go_nix_simple:image_bazel_scratch_upx_tarball \
380+
--define REPO_PREFIX=docker.io/randomizedcoder \
381+
--define VERSION=latest
382+
373383
bazel_clean:
374384
bazel clean --expunge
375385

cmd/go_nix_simple/BUILD.bazel

Lines changed: 3 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
# cmd/go_nix_simple/BUILD.bazel
2-
3-
# cmd/go_nix_simple/BUILD.bazel
4-
51
# Load necessary rules
62
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
7-
load("@rules_oci//oci:defs.bzl", "oci_image")
8-
9-
# load("@rules_oci//oci:tarball.bzl", "oci_tarball") # Keep tarball load disabled for now
10-
load("@rules_pkg//:pkg.bzl", "pkg_tar")
113

124
# Define the Go library for the application code
135
go_library(
@@ -27,169 +19,11 @@ go_library(
2719
],
2820
)
2921

30-
# Define the standard Go binary target
22+
# Define the Go binary target
3123
go_binary(
32-
name = "go_nix_simple_binary_noupx",
24+
name = "go_nix_simple",
3325
embed = [":go_nix_simple_lib"],
34-
gc_linkopts = [
35-
"-linkmode=external",
36-
"-extldflags=-static",
37-
],
3826
pure = "on",
3927
static = "on",
4028
visibility = ["//visibility:public"],
41-
)
42-
43-
# Define a genrule to run UPX on the binary
44-
genrule(
45-
name = "go_nix_simple_binary_upx_packed",
46-
srcs = [":go_nix_simple_binary_noupx"],
47-
outs = ["go_nix_simple_upx"],
48-
cmd = "$(execpath @upx_linux_amd64//:upx_bin) --best --lzma -o $@ $(location :go_nix_simple_binary_noupx)",
49-
executable = True,
50-
toolchains = ["@upx_linux_amd64//:upx_bin"],
51-
visibility = ["//visibility:public"],
52-
)
53-
54-
# --- Define pkg_tar targets separately ---
55-
pkg_tar(
56-
name = "image_bazel_distroless_noupx_files_tar",
57-
srcs = [
58-
":go_nix_simple_binary_noupx",
59-
"//:VERSION",
60-
],
61-
mode = "0755",
62-
package_dir = "/",
63-
remap_paths = {
64-
"cmd/go_nix_simple/go_nix_simple_binary_noupx": "go_nix_simple",
65-
},
66-
)
67-
68-
pkg_tar(
69-
name = "image_bazel_scratch_noupx_files_tar",
70-
srcs = [
71-
":go_nix_simple_binary_noupx",
72-
"//:VERSION",
73-
"@distroless_base//image_layer:certs",
74-
],
75-
mode = "0755",
76-
modes = {
77-
"etc/ssl/certs/ca-certificates.crt": "0644",
78-
},
79-
package_dir = "/",
80-
remap_paths = {
81-
"cmd/go_nix_simple/go_nix_simple_binary_noupx": "go_nix_simple",
82-
"certs/etc/ssl/certs/ca-certificates.crt": "etc/ssl/certs/ca-certificates.crt",
83-
},
84-
)
85-
86-
pkg_tar(
87-
name = "image_bazel_distroless_upx_files_tar",
88-
srcs = [
89-
":go_nix_simple_binary_upx_packed",
90-
"//:VERSION",
91-
],
92-
mode = "0755",
93-
package_dir = "/",
94-
remap_paths = {
95-
"cmd/go_nix_simple/go_nix_simple_upx": "go_nix_simple",
96-
},
97-
)
98-
99-
pkg_tar(
100-
name = "image_bazel_scratch_upx_files_tar",
101-
srcs = [
102-
":go_nix_simple_binary_upx_packed",
103-
"//:VERSION",
104-
"@distroless_base//image_layer:certs",
105-
],
106-
mode = "0755",
107-
modes = {
108-
"etc/ssl/certs/ca-certificates.crt": "0644",
109-
},
110-
package_dir = "/",
111-
remap_paths = {
112-
"cmd/go_nix_simple/go_nix_simple_upx": "go_nix_simple",
113-
"certs/etc/ssl/certs/ca-certificates.crt": "etc/ssl/certs/ca-certificates.crt",
114-
},
115-
)
116-
117-
# --- OCI Image Definitions ---
118-
# https://github.com/bazel-contrib/rules_oci/blob/main/docs/image.md
119-
120-
# Common attributes defined as variables
121-
_ENTRYPOINT = ["/go_nix_simple"]
122-
123-
_USER = "65534"
124-
125-
_EXPOSED_PORTS = ["9108/tcp"]
126-
127-
# Image: Distroless, No UPX
128-
oci_image(
129-
name = "image_bazel_distroless_noupx",
130-
#base = "@distroless_base//:image",
131-
base = "@distroless_base",
132-
# Use direct attributes based on documentation
133-
entrypoint = _ENTRYPOINT,
134-
exposed_ports = _EXPOSED_PORTS,
135-
# Reference the pkg_tar target by its label (string)
136-
tars = [":image_bazel_distroless_noupx_files_tar"],
137-
user = _USER,
138-
)
139-
140-
# Image: Scratch, No UPX
141-
oci_image(
142-
name = "image_bazel_scratch_noupx",
143-
base = "@scratch",
144-
entrypoint = _ENTRYPOINT,
145-
exposed_ports = _EXPOSED_PORTS,
146-
tars = [":image_bazel_scratch_noupx_files_tar"],
147-
user = _USER,
148-
)
149-
150-
# Image: Distroless, UPX
151-
oci_image(
152-
name = "image_bazel_distroless_upx",
153-
#base = "@distroless_base//:image",
154-
base = "@distroless_base",
155-
entrypoint = _ENTRYPOINT,
156-
exposed_ports = _EXPOSED_PORTS,
157-
tars = [":image_bazel_distroless_upx_files_tar"],
158-
user = _USER,
159-
)
160-
161-
# Image: Scratch, UPX
162-
oci_image(
163-
name = "image_bazel_scratch_upx",
164-
base = "@scratch",
165-
entrypoint = _ENTRYPOINT,
166-
exposed_ports = _EXPOSED_PORTS,
167-
tars = [":image_bazel_scratch_upx_files_tar"],
168-
user = _USER,
169-
)
170-
171-
# # --- Tarball Targets (for loading into Docker) ---
172-
# # Create tarball targets for each image variant
173-
# oci_tarball(
174-
# name = "image_bazel_distroless_noupx_tarball",
175-
# image = ":image_bazel_distroless_noupx",
176-
# repo_tags = ["$(REPO_PREFIX)/bazel-go-nix-simple-distroless-noupx:$(VERSION)"], # Match expected tag format
177-
# )
178-
179-
# oci_tarball(
180-
# name = "image_bazel_scratch_noupx_tarball",
181-
# image = ":image_bazel_scratch_noupx",
182-
# repo_tags = ["$(REPO_PREFIX)/bazel-go-nix-simple-scratch-noupx:$(VERSION)"],
183-
# )
184-
185-
# oci_tarball(
186-
# name = "image_bazel_distroless_upx_tarball",
187-
# image = ":image_bazel_distroless_upx",
188-
# repo_tags = ["$(REPO_PREFIX)/bazel-go-nix-simple-distroless-upx:$(VERSION)"],
189-
# )
190-
191-
# oci_tarball(
192-
# name = "image_bazel_scratch_upx_tarball",
193-
# image = ":image_bazel_scratch_upx",
194-
# repo_tags = ["$(REPO_PREFIX)/bazel-go-nix-simple-scratch-upx:$(VERSION)"],
195-
# )
29+
)

cmd/go_nix_simple/go_nix_simple.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
)
2727

2828
const (
29-
sleepTimeCst = 5 * time.Second
29+
sleepTimeCst = 6 * time.Second
3030

3131
debugLevelCst = 11 // Default debug level
3232

deps.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
load("@bazel_gazelle//:deps.bzl", "go_repository")
2+
3+
def go_dependencies():
4+
"""A function that declares the external Go dependencies."""
5+
pass

WORKSPACE renamed to old/WORKSPACE

File renamed without changes.

0 commit comments

Comments
 (0)