Skip to content

Instantly share code, notes, and snippets.

@linw1995
Last active October 11, 2024 06:26
Show Gist options
  • Save linw1995/00f086de4e4a14475177b463946aa28e to your computer and use it in GitHub Desktop.
Save linw1995/00f086de4e4a14475177b463946aa28e to your computer and use it in GitHub Desktop.
Use Docker or nix to build vector.

Docker in Docker

docker build -t vector-dev:alpine --target=final .
docker run --rm \
  -v ~/Documents/opensource/vector:/workspace \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -it vector-dev:alpine sh

Run inside the container

# https://github.com/vectordotdev/vector/blob/c3c0ec0cf34f4d5defa19458a76e4e69678ee2a2/scripts/cross/x86_64-unknown-linux-musl.dockerfile
# pre-build cross-image
docker build --tag vector-cross-env:x86_64-unknown-linux-musl \
  --file scripts/cross/x86_64-unknown-linux-musl.dockerfile .

# cargo build
CROSS_CONTAINER_IN_CONTAINER=1 cross build \
  --release --target x86_64-unknown-linux-musl \
  --no-default-features --features target-x86_64-unknown-linux-musl

Nix

nix develop .#x86_64-unknown-linux-musl
cargo build \
  --release --target x86_64-unknown-linux-musl \
  --no-default-features --features target-x86_64-unknown-linux-musl
ARG RUST_VERSION=1.79.0
FROM rust:${RUST_VERSION}-alpine AS base
RUN apk add --no-cache musl-dev
WORKDIR /workspace
FROM base AS cargo-cross
RUN --mount=type=cache,target=/usr/local/cargo/git/db \
--mount=type=cache,target=/usr/local/cargo/registry/cache \
--mount=type=cache,target=/usr/local/cargo/registry/index \
cargo install cross --git https://github.com/cross-rs/cross
FROM rust:${RUST_VERSION}-alpine AS final
RUN apk add --no-cache git docker-cli openssh-client-default
COPY --from=cargo-cross /usr/local/cargo/bin/cross /usr/local/bin/cross
{
inputs = {
utils.url = "github:numtide/flake-utils";
stable.url = "github:NixOS/nixpkgs/nixos-24.05";
};
outputs = {
stable,
utils,
...
}:
utils.lib.eachDefaultSystem
(
system: let
pkgs = import stable {inherit system;};
cross = import stable {
inherit system;
crossSystem = {config = "x86_64-unknown-linux-musl";};
};
tools = pkgs:
with pkgs; [
# compile tools
protobuf
cmakeMinimal
];
libs = pkgs:
with pkgs; [
# lib
openssl
cyrus_sasl
zlib
];
in {
# Used by `nix develop`
devShells.default = pkgs.mkShell {
nativeBuildInputs = with pkgs; [
pkg-config
];
buildInputs = (tools pkgs) ++ (libs pkgs);
};
devShells.x86_64-unknown-linux-musl = pkgs.mkShell (with cross.pkgsMusl; {
nativeBuildInputs = [
pkg-config
];
buildInputs = (tools pkgs) ++ (libs cross.pkgsMusl);
CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER = "${stdenv.cc.targetPrefix}cc";
});
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment