build: add bazel rust workflow
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -17,6 +17,9 @@ Thumbs.db
|
||||
# logs
|
||||
*.log
|
||||
|
||||
# Bazel convenience symlinks
|
||||
/bazel-*
|
||||
|
||||
# language package-manager caches and outputs
|
||||
node_modules/
|
||||
dist/
|
||||
|
||||
13
MODULE.bazel
Normal file
13
MODULE.bazel
Normal file
@@ -0,0 +1,13 @@
|
||||
module(name = "alexion", version = "0.1.0")
|
||||
|
||||
bazel_dep(name = "rules_rust", version = "0.74.0")
|
||||
|
||||
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
|
||||
rust.toolchain(versions = [])
|
||||
use_repo(rust, "rust_toolchains")
|
||||
|
||||
local_rust = use_extension("//bazel:rust_toolchain.bzl", "local_rust")
|
||||
use_repo(local_rust, "local_rust_toolchain")
|
||||
|
||||
register_toolchains("@rust_toolchains//:all")
|
||||
register_toolchains("@local_rust_toolchain//:local_rust_toolchain")
|
||||
1777
MODULE.bazel.lock
generated
Normal file
1777
MODULE.bazel.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
1
bazel/BUILD
Normal file
1
bazel/BUILD
Normal file
@@ -0,0 +1 @@
|
||||
exports_files(["rust_toolchain.bzl"])
|
||||
191
bazel/rust_toolchain.bzl
Normal file
191
bazel/rust_toolchain.bzl
Normal file
@@ -0,0 +1,191 @@
|
||||
def _required_path(repository_ctx, command):
|
||||
path = repository_ctx.which(command)
|
||||
if path == None:
|
||||
fail("{} must be available in PATH. Enter the rust dev shell first.".format(command))
|
||||
return path
|
||||
|
||||
|
||||
def _execute(repository_ctx, arguments):
|
||||
result = repository_ctx.execute(arguments)
|
||||
if result.return_code != 0:
|
||||
fail("command failed: {}\n{}".format(" ".join(arguments), result.stderr))
|
||||
return result.stdout.strip()
|
||||
|
||||
|
||||
def _local_rust_repository_impl(repository_ctx):
|
||||
rustc = _required_path(repository_ctx, "rustc")
|
||||
rustdoc = _required_path(repository_ctx, "rustdoc")
|
||||
cargo = _required_path(repository_ctx, "cargo")
|
||||
cargo_clippy = _required_path(repository_ctx, "cargo-clippy")
|
||||
clippy_driver = _required_path(repository_ctx, "clippy-driver")
|
||||
rustfmt = _required_path(repository_ctx, "rustfmt")
|
||||
cc = _required_path(repository_ctx, "cc")
|
||||
|
||||
sysroot = _execute(repository_ctx, [str(rustc), "--print", "sysroot"])
|
||||
verbose_version = _execute(repository_ctx, [str(rustc), "-vV"])
|
||||
|
||||
host_triple = None
|
||||
release = None
|
||||
for line in verbose_version.splitlines():
|
||||
if line.startswith("host: "):
|
||||
host_triple = line.removeprefix("host: ")
|
||||
if line.startswith("release: "):
|
||||
release = line.removeprefix("release: ")
|
||||
|
||||
if host_triple == None:
|
||||
fail("rustc -vV did not report a host triple")
|
||||
if release == None:
|
||||
fail("rustc -vV did not report a release")
|
||||
|
||||
repository_ctx.symlink(rustc, "bin/rustc")
|
||||
repository_ctx.symlink(rustdoc, "bin/rustdoc")
|
||||
repository_ctx.symlink(cargo, "bin/cargo")
|
||||
repository_ctx.symlink(cargo_clippy, "bin/cargo-clippy")
|
||||
repository_ctx.symlink(clippy_driver, "bin/clippy-driver")
|
||||
repository_ctx.symlink(rustfmt, "bin/rustfmt")
|
||||
repository_ctx.symlink(cc, "bin/cc")
|
||||
repository_ctx.symlink(sysroot, "rust")
|
||||
|
||||
repository_ctx.file("WORKSPACE.bazel", "")
|
||||
repository_ctx.file("BUILD.bazel", _build_file(host_triple, release))
|
||||
|
||||
|
||||
def _build_file(host_triple, release):
|
||||
return """
|
||||
filegroup(
|
||||
name = "rustc",
|
||||
srcs = ["bin/rustc"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "rustdoc",
|
||||
srcs = ["bin/rustdoc"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "cargo",
|
||||
srcs = ["bin/cargo"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "cargo_clippy",
|
||||
srcs = ["bin/cargo-clippy"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "clippy_driver",
|
||||
srcs = ["bin/clippy-driver"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "rustfmt",
|
||||
srcs = ["bin/rustfmt"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "cc",
|
||||
srcs = ["bin/cc"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "rustc_lib",
|
||||
srcs = glob(
|
||||
[
|
||||
"rust/lib/*.so*",
|
||||
"rust/lib/rustlib/{host_triple}/codegen-backends/*.so",
|
||||
"rust/lib/rustlib/{host_triple}/lib/*.so*",
|
||||
"rust/lib/rustlib/{host_triple}/lib/*.rmeta",
|
||||
],
|
||||
allow_empty = True,
|
||||
),
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "rust_objcopy",
|
||||
srcs = glob(
|
||||
["rust/lib/rustlib/{host_triple}/bin/rust-objcopy"],
|
||||
allow_empty = True,
|
||||
),
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
load("@rules_rust//rust:toolchain.bzl", "rust_stdlib_filegroup")
|
||||
|
||||
rust_stdlib_filegroup(
|
||||
name = "rust_std",
|
||||
srcs = glob(
|
||||
[
|
||||
"rust/lib/rustlib/{host_triple}/lib/*.rlib",
|
||||
"rust/lib/rustlib/{host_triple}/lib/*.rmeta",
|
||||
"rust/lib/rustlib/{host_triple}/lib/*.so*",
|
||||
"rust/lib/rustlib/{host_triple}/lib/*.a",
|
||||
"rust/lib/rustlib/{host_triple}/lib/self-contained/**",
|
||||
],
|
||||
allow_empty = True,
|
||||
),
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
load("@rules_rust//rust:toolchain.bzl", "rust_toolchain")
|
||||
|
||||
rust_toolchain(
|
||||
name = "rust_toolchain_impl",
|
||||
rust_doc = ":rustdoc",
|
||||
rust_std = ":rust_std",
|
||||
rustc = ":rustc",
|
||||
linker = ":cc",
|
||||
linker_type = "indirect",
|
||||
rust_objcopy = ":rust_objcopy",
|
||||
rustfmt = ":rustfmt",
|
||||
cargo = ":cargo",
|
||||
clippy_driver = ":clippy_driver",
|
||||
cargo_clippy = ":cargo_clippy",
|
||||
rustc_lib = ":rustc_lib",
|
||||
allocator_library = None,
|
||||
global_allocator_library = None,
|
||||
binary_ext = "",
|
||||
staticlib_ext = ".a",
|
||||
dylib_ext = ".so",
|
||||
stdlib_linkflags = ["-ldl", "-lpthread"],
|
||||
default_edition = "2024",
|
||||
exec_triple = "{host_triple}",
|
||||
target_triple = "{host_triple}",
|
||||
version = "{release}",
|
||||
channel = "stable",
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
toolchain(
|
||||
name = "local_rust_toolchain",
|
||||
toolchain = ":rust_toolchain_impl",
|
||||
toolchain_type = "@rules_rust//rust:toolchain_type",
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
""".format(
|
||||
host_triple = host_triple,
|
||||
release = release,
|
||||
)
|
||||
|
||||
|
||||
local_rust_repository = repository_rule(
|
||||
implementation = _local_rust_repository_impl,
|
||||
local = True,
|
||||
environ = ["PATH"],
|
||||
)
|
||||
|
||||
|
||||
def _local_rust_impl(module_ctx):
|
||||
local_rust_repository(name = "local_rust_toolchain")
|
||||
|
||||
|
||||
local_rust = module_extension(
|
||||
implementation = _local_rust_impl,
|
||||
)
|
||||
25
flake.nix
25
flake.nix
@@ -55,12 +55,14 @@
|
||||
inputs.nixpkgs.follows = "nixpkgs";
|
||||
};
|
||||
|
||||
# TODO: deprecate / remove
|
||||
# Agent-ergonomic CLI for Gitea, with a home-manager module for the agent context.
|
||||
gitea-axi = {
|
||||
url = "git+https://git.alexion.dev/alexion/gitea-axi";
|
||||
inputs.nixpkgs.follows = "nixpkgs";
|
||||
};
|
||||
|
||||
# TODO: deprecate / remove
|
||||
# Personal agent skills, packaged as per-skill derivations with a home-manager module.
|
||||
skills = {
|
||||
url = "git+https://git.alexion.dev/alexion/skills";
|
||||
@@ -75,7 +77,30 @@
|
||||
|
||||
outputs =
|
||||
{ self, nixpkgs, ... }@inputs:
|
||||
let
|
||||
system = "x86_64-linux";
|
||||
pkgs = nixpkgs.legacyPackages.${system};
|
||||
|
||||
pkgsDefault = [ pkgs.bazel ];
|
||||
pkgsRust = with pkgs; [
|
||||
cargo
|
||||
clippy
|
||||
rust-analyzer
|
||||
rustc
|
||||
rustfmt
|
||||
];
|
||||
|
||||
mkShell = packages:
|
||||
pkgs.mkShell {
|
||||
packages = pkgsDefault ++ packages;
|
||||
};
|
||||
in
|
||||
{
|
||||
devShells.${system} = {
|
||||
default = mkShell [ ];
|
||||
rust = mkShell pkgsRust;
|
||||
};
|
||||
|
||||
nixosConfigurations = (import ./src/dotfiles {
|
||||
inherit inputs self;
|
||||
lib = nixpkgs.lib;
|
||||
|
||||
1
src/machoke/.envrc
Normal file
1
src/machoke/.envrc
Normal file
@@ -0,0 +1 @@
|
||||
use flake //#rust
|
||||
13
src/machoke/BUILD
Normal file
13
src/machoke/BUILD
Normal file
@@ -0,0 +1,13 @@
|
||||
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library")
|
||||
|
||||
rust_library(
|
||||
name = "machoke_lib",
|
||||
crate_name = "machoke",
|
||||
srcs = ["lib.rs"],
|
||||
)
|
||||
|
||||
rust_binary(
|
||||
name = "machoke",
|
||||
srcs = ["main.rs"],
|
||||
deps = [":machoke_lib"],
|
||||
)
|
||||
7
src/machoke/Cargo.lock
generated
Normal file
7
src/machoke/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "machoke"
|
||||
version = "0.1.0"
|
||||
13
src/machoke/Cargo.toml
Normal file
13
src/machoke/Cargo.toml
Normal file
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "machoke"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[lib]
|
||||
path = "lib.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "machoke"
|
||||
path = "main.rs"
|
||||
|
||||
[dependencies]
|
||||
3
src/machoke/lib.rs
Normal file
3
src/machoke/lib.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub fn greeting() -> &'static str {
|
||||
"Hello from machoke"
|
||||
}
|
||||
3
src/machoke/main.rs
Normal file
3
src/machoke/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("{}", machoke::greeting());
|
||||
}
|
||||
Reference in New Issue
Block a user