Skip to content

Commit d1f8777

Browse files
committed
Convert toolchain examples to bzlmod
1 parent a21add7 commit d1f8777

File tree

11 files changed

+2395
-474
lines changed

11 files changed

+2395
-474
lines changed

examples/toolchains/rust/.bazelrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ import %workspace%/../../../.bazelrc.remote-cache
33
# merge env and action_env variables for action.run
44
# (note, this is the default in Bazel 7)
55
build --incompatible_merge_fixed_and_default_shell_env
6+
build --enable_bzlmod
67

7-
build:nix --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host --incompatible_enable_cc_toolchain_resolution
8+
build:nix --host_platform=@rules_nixpkgs_core//platforms:host --incompatible_enable_cc_toolchain_resolution
9+
build:nix --crosstool_top=@nixpkgs_rust_toolchain//:rust_nix

examples/toolchains/rust/BUILD

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
load("@rules_rust//rust:defs.bzl", "rust_binary")
2-
load("@crate_index//:defs.bzl", "all_crate_deps")
32

43
exports_files([
54
"nixpkgs.nix",
@@ -10,5 +9,7 @@ exports_files([
109
rust_binary(
1110
name = "hello",
1211
srcs = ["hello.rs"],
13-
deps = all_crate_deps(normal = True),
12+
deps = [
13+
"@crates//:openssl",
14+
],
1415
)

examples/toolchains/rust/Cargo.lock

Lines changed: 36 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
module(
2+
name = "hello_world",
3+
version = "0.0.0",
4+
)
5+
6+
bazel_dep(name = "rules_nixpkgs_core", version = "0.0.0")
7+
local_path_override(
8+
module_name = "rules_nixpkgs_core",
9+
path = "../../../core",
10+
)
11+
12+
bazel_dep(name = "rules_nixpkgs_rust", version = "0.0.0")
13+
local_path_override(
14+
module_name = "rules_nixpkgs_rust",
15+
path = "../../../toolchains/rust",
16+
)
17+
18+
bazel_dep(name = "rules_nixpkgs_cc", version = "0.0.0")
19+
local_path_override(
20+
module_name = "rules_nixpkgs_cc",
21+
path = "../../../toolchains/cc",
22+
)
23+
24+
bazel_dep(name = "platforms", version = "1.0.0")
25+
bazel_dep(name = "rules_rust", version = "0.63.0")
26+
27+
# rules_rust also uses the cc compiler
28+
bazel_dep(name = "rules_cc", version = "0.1.4")
29+
30+
#
31+
# Nixpkgs
32+
#
33+
34+
# It is recommended to keep nixpkgs of nix-shell (which provide Bazel),
35+
# and nixpkgs of Bazel Workspace in sync - otherwise one may
36+
# got hit with nasty glibc mismatch errors.
37+
nix_repo = use_extension("@rules_nixpkgs_core//extensions:repository.bzl", "nix_repo")
38+
nix_repo.file(
39+
name = "nixpkgs",
40+
file = "//:nixpkgs.nix",
41+
file_deps = [
42+
"//:nixpkgs.json",
43+
],
44+
)
45+
use_repo(nix_repo, "nixpkgs")
46+
47+
nix_pkg = use_extension("@rules_nixpkgs_core//extensions:package.bzl", "nix_pkg")
48+
nix_pkg.file(
49+
name = "openssl-static",
50+
attr = "default",
51+
file = "//:openssl-static.nix",
52+
file_deps = [
53+
"//:nixpkgs.json",
54+
],
55+
)
56+
use_repo(nix_pkg, "openssl-static")
57+
58+
#
59+
# Toolchains
60+
#
61+
62+
cc_configure = use_extension("//:extension.bzl", "cc_configure")
63+
use_repo(cc_configure, "nixpkgs_config_cc")
64+
use_repo(cc_configure, "nixpkgs_config_cc_toolchains")
65+
use_repo(cc_configure, "nixpkgs_config_cc_info")
66+
67+
register_toolchains("@nixpkgs_config_cc_toolchains//:all")
68+
69+
rust_configure = use_extension("//:extension.bzl", "rust_configure")
70+
use_repo(rust_configure, "nixpkgs_rust")
71+
use_repo(rust_configure, "nixpkgs_rust_toolchain")
72+
73+
register_toolchains("@nixpkgs_rust_toolchain//:rust_nix")
74+
75+
#
76+
# Crates
77+
#
78+
79+
crate = use_extension("@rules_rust//crate_universe:extension.bzl", "crate")
80+
crate.annotation(
81+
build_script_data = [
82+
"@openssl-static//:include",
83+
"@openssl-static//:lib",
84+
],
85+
build_script_env = {
86+
"OPENSSL_INCLUDE_DIR": "../rules_nixpkgs_core~~nix_pkg~openssl-static/include",
87+
"OPENSSL_LIB_DIR": "../rules_nixpkgs_core~~nix_pkg~openssl-static/lib",
88+
"OPENSSL_STATIC": "1",
89+
},
90+
crate = "openssl-sys",
91+
data = [
92+
"@openssl-static//:include",
93+
"@openssl-static//:lib",
94+
],
95+
rustc_flags = [
96+
"-Lexternal/rules_nixpkgs_core~~nix_pkg~openssl-static/lib",
97+
],
98+
)
99+
crate.spec(
100+
package = "openssl",
101+
version = "0.10.40",
102+
)
103+
crate.from_specs(
104+
cargo_lockfile = "//:Cargo.lock",
105+
lockfile = "//:cargo-bazel-lock.json",
106+
)
107+
108+
inject_repo(crate, "openssl-static")
109+
110+
use_repo(crate, "crates")

0 commit comments

Comments
 (0)