Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .cargo/config → .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ target = "x86_64-unknown-linux-musl"

[target.x86_64-unknown-linux-musl]
rustflags = [ "-C", "target-feature=+crt-static",
"-C", "link-arg=-lgcc" ]
"-C", "link-arg=-lgcc" ]
8 changes: 8 additions & 0 deletions .github/workflows/r10e-rust-overlay-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ jobs:
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Free Disk Space (Ubuntu)
uses: jlumbroso/free-disk-space@main
with:
# this might remove tools that are actually needed,
# if set to "true" but frees about 6 GB
tool-cache: false


- name: Checkout repository
uses: actions/checkout@v4.1.1
- name: Cross build artifacts
Expand Down
121 changes: 1 addition & 120 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@ rand = "0.3.14"
# The following commit breaks cross builds
#zstd = { git = "https://github.com/gyscos/zstd-rs", rev ="f0d8a12f0f520095cf26a0dac35b8d0939277add" }
# The following commit results in a successful build, which reverts the above commit
zstd = { git = "https://github.com/syncom/zstd-rs", rev ="abbc3b6a21c9825243933cbb6778781608db56dc" }
[target.'cfg(unix)'.dependencies]
openssl = "0.10.72"
zstd = { git = "https://github.com/syncom/zstd-rs", rev ="abbc3b6a21c9825243933cbb6778781608db56dc" }
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ complex. Hence this approach is less preferred.
[nix-rust-overlay](./nix-rust-overlay/)

- This approach is "pure", in that it uses Nix to manage dependencies entirely.
- It demonstrates how to statically link to `openssl` for the `unix` target family.
- It demonstrates cross builds on a `windows` target (without `openssl` static
linking, for simplicity).
- ~~It demonstrates how to statically link to `openssl` for the `unix` target
family.~~ `openssl` static linking is a pain - we dropped the support.
- It demonstrates cross builds on a `windows` target

## How to Build Using Both Approaches

Expand Down
2 changes: 1 addition & 1 deletion nix-cross-rs/rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.85.0
1.88.0
40 changes: 16 additions & 24 deletions nix-rust-overlay/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# The following information is from https://hub.docker.com/r/nixos/nix/tags
FROM nixos/nix:2.9.0@sha256:13b257cd42db29dc851f9818ea1bc2f9c7128c51fdf000971fa6058c66fbe4b6 as rust-overlay_builder
FROM nixos/nix:2.33.2@sha256:c6ebd12d96b3374ee15e3986c15aa43f5e49310634f17afcaaf4dafe4f6732b2 AS rust-overlay_builder

###################################################################
# Step 1: Prepare nixpkgs and rust-overlay for deterministic builds
Expand All @@ -8,20 +8,16 @@ WORKDIR /build
# Custom project name
ENV RUST_PROJECT_NAME="rust-cross-build-nix"

# nixpkgs 22.05
ENV NIXPKGS_COMMIT_SHA="ce6aa13369b667ac2542593170993504932eb836"
# rust-overlay version 20240502 with latest rust-bin.stable 1.78.0
# Note that this is the previous commit of the problematic commit
# 0bf05d8534406776a0fbc9ed8d4ef5bd925b056a that seemed to have introduced a
# breaking change
ENV RUST_OVERLAY_COMMIT_SHA="2e7ccf572ce0f0547d4cf4426de4482936882d0e"
# nixpkgs 25.11
ENV NIXPKGS_COMMIT_SHA="871b9fd269ff6246794583ce4ee1031e1da71895"
# rust-overlay version 20260205 with latest rust-bin.stable 1.93.0
ENV RUST_OVERLAY_COMMIT_SHA="42ec85352e419e601775c57256a52f6d48a39906"

# Apple M1 workaround
COPY nix.conf /build/nix.conf
ENV NIX_USER_CONF_FILES=/build/nix.conf

RUN nix-env -i git && \
mkdir -p /build/nixpkgs && \
RUN mkdir -p /build/nixpkgs && \
cd nixpkgs && \
git init && \
git remote add origin https://github.com/NixOS/nixpkgs.git && \
Expand All @@ -45,8 +41,6 @@ COPY nix-rust-overlay/shell-i686.nix /build/${RUST_PROJECT_NAME}/shell-i686.nix
COPY nix-rust-overlay/shell-aarch64.nix /build/${RUST_PROJECT_NAME}/shell-aarch64.nix
COPY nix-rust-overlay/shell-armv7.nix /build/${RUST_PROJECT_NAME}/shell-armv7.nix
COPY nix-rust-overlay/shell-armv6.nix /build/${RUST_PROJECT_NAME}/shell-armv6.nix
# Copy patch for musl-1.1.24
COPY nix-rust-overlay/musl-1.1.24.patch /build/musl-1.1.24.patch

# Warm up the cache layers
RUN cd /build/${RUST_PROJECT_NAME} && \
Expand All @@ -55,10 +49,6 @@ RUN cd /build/${RUST_PROJECT_NAME} && \
nix-shell shell-x86_64.nix
RUN cd /build/${RUST_PROJECT_NAME} && \
nix-shell shell-aarch64.nix
# Use musl-1.1.24 for 32-bit platforms, to work around a cross building issue
# similar to the one discussed in
# https://stackoverflow.com/questions/61934997/undefined-reference-to-stat-time64-when-cross-compiling-rust-project-on-mu
RUN cd /build/nixpkgs && git apply /build/musl-1.1.24.patch
RUN cd /build/${RUST_PROJECT_NAME} && \
nix-shell shell-armv6.nix
RUN cd /build/${RUST_PROJECT_NAME} && \
Expand All @@ -74,28 +64,30 @@ COPY Cargo.toml /build/${RUST_PROJECT_NAME}/Cargo.toml
COPY Cargo.lock /build/${RUST_PROJECT_NAME}/Cargo.lock
COPY nix-rust-overlay/Makefile /build/${RUST_PROJECT_NAME}/Makefile

# Revert the musl-1.1.24 patch because we don't need it for 64-bit platforms
RUN cd /build/nixpkgs && git apply -R /build/musl-1.1.24.patch
# For 64-bit platforms
RUN cd /build/${RUST_PROJECT_NAME} && \
nix-shell shell-x86_64-win.nix --show-trace \
--run "TARGET=x86_64-pc-windows-gnu make && \
TARGET=x86_64-pc-windows-gnu make run" && \
TARGET=x86_64-pc-windows-gnu make run"
# echo to NOP qemu commands to get around issue on Apple M1
RUN cd /build/${RUST_PROJECT_NAME} && \
nix-shell shell-x86_64.nix \
--run "TARGET=x86_64-unknown-linux-musl make && \
echo TARGET=x86_64-unknown-linux-musl make run" && \
echo TARGET=x86_64-unknown-linux-musl make run"
RUN cd /build/${RUST_PROJECT_NAME} && \
nix-shell shell-aarch64.nix \
--run "TARGET=aarch64-unknown-linux-musl make && \
echo TARGET=aarch64-unknown-linux-musl make run"
# Re-apply the musl-1.1.24 patch for 32-bit platforms
RUN cd /build/nixpkgs && git apply /build/musl-1.1.24.patch
# For 32-bit platforms
RUN cd /build/${RUST_PROJECT_NAME} && \
nix-shell shell-armv6.nix \
--run "TARGET=arm-unknown-linux-musleabihf make && \
echo TARGET=arm-unknown-linux-musleabihf make run" && \
echo TARGET=arm-unknown-linux-musleabihf make run"
RUN cd /build/${RUST_PROJECT_NAME} && \
nix-shell shell-armv7.nix \
--run "TARGET=armv7-unknown-linux-musleabihf make && \
echo TARGET=armv7-unknown-linux-musleabihf make run" && \
echo TARGET=armv7-unknown-linux-musleabihf make run"
RUN cd /build/${RUST_PROJECT_NAME} && \
nix-shell shell-i686.nix \
--run "TARGET=i686-unknown-linux-musl make && \
echo TARGET=i686-unknown-linux-musl make run"
4 changes: 2 additions & 2 deletions nix-rust-overlay/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This Makefile is expected to be run inside nix-shell.
TARGET ?= x86_64-unknown-linux-musl
TARGET ?= x86_64-unknown-linux-gnu
CARGO_FLAGS := --release --target $(TARGET)

mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
Expand All @@ -12,7 +12,7 @@ ifeq ($(TARGET),x86_64-pc-windows-gnu)
else
SRC_BIN := $(mkfile_dir)/target/$(TARGET)/release/hello-random
DEST_BIN := $(mkfile_dir)/out/hello-random-$(TARGET)
OPENSSL_STATIC_FLAG := OPENSSL_STATIC=1
OPENSSL_STATIC_FLAG := OPENSSL_STATIC=0
endif

.PHONY: all
Expand Down
8 changes: 7 additions & 1 deletion nix-rust-overlay/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ self_test
echo "Creating builder container image..."
# Need to run docker build in script's parent directory
cd "${SCRIPT_DIR}/.."
docker build --platform linux/amd64 -f "${SCRIPT_DIR}/Dockerfile" -t "${BUILDER_TAG_NAME}" .
docker buildx build --platform linux/amd64 --load \
-f "${SCRIPT_DIR}/Dockerfile" \
-t "${BUILDER_TAG_NAME}" \
. \
--builder "$(docker buildx create --driver-opt env.BUILDKIT_STEP_LOG_MAX_SIZE=-1 --driver-opt env.BUILDKIT_STEP_LOG_MAX_SPEED=-1)"


docker images "${BUILDER_TAG_NAME}"
rm -rf "${OUT_DIR}"
mkdir -p "${OUT_DIR}"
Expand Down
2 changes: 1 addition & 1 deletion nix-rust-overlay/shell-aarch64.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{ mkShell, stdenv, rust-bin, pkg-config, openssl, qemu }:
mkShell {
nativeBuildInputs = [
rust-bin.stable."1.78.0".minimal
rust-bin.stable."1.92.0".minimal
pkg-config
];

Expand Down
6 changes: 2 additions & 4 deletions nix-rust-overlay/shell-armv6.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
crossSystem = "armv6l-linux";
overlays = [ (import <rust-overlay>) ];
}).pkgsMusl.pkgsStatic.callPackage (
{ mkShell, stdenv, rust-bin, pkg-config, openssl, qemu }:
{ mkShell, stdenv, rust-bin, pkg-config, qemu }:
mkShell {
nativeBuildInputs = [
rust-bin.stable."1.78.0".minimal
rust-bin.stable."1.92.0".minimal
pkg-config
];

depsBuildBuild = [ qemu ];

buildInputs = [ openssl ];

CARGO_TARGET_ARM_UNKNOWN_LINUX_MUSLEABIHF_LINKER = "${stdenv.cc.targetPrefix}cc";
CARGO_TARGET_ARM_UNKNOWN_LINUX_MUSLEABIHF_RUNNER = "qemu-arm";
}) {}
6 changes: 2 additions & 4 deletions nix-rust-overlay/shell-armv7.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
crossSystem = "armv7l-linux";
overlays = [ (import <rust-overlay>) ];
}).pkgsMusl.pkgsStatic.callPackage (
{ mkShell, stdenv, rust-bin, pkg-config, openssl, qemu }:
{ mkShell, stdenv, rust-bin, pkg-config, qemu }:
mkShell {
nativeBuildInputs = [
rust-bin.stable."1.78.0".minimal
rust-bin.stable."1.92.0".minimal
pkg-config
];

depsBuildBuild = [ qemu ];

buildInputs = [ openssl ];

CARGO_TARGET_ARMV7_UNKNOWN_LINUX_MUSLEABIHF_LINKER = "${stdenv.cc.targetPrefix}cc";
CARGO_TARGET_ARMV7_UNKNOWN_LINUX_MUSLEABIHF_RUNNER = "qemu-arm";
}) {}
6 changes: 2 additions & 4 deletions nix-rust-overlay/shell-i686.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
crossSystem = "i686-linux";
overlays = [ (import <rust-overlay>) ];
}).pkgsMusl.pkgsStatic.callPackage (
{ mkShell, stdenv, rust-bin, pkg-config, openssl, qemu }:
{ mkShell, stdenv, rust-bin, pkg-config, qemu }:
mkShell {
nativeBuildInputs = [
rust-bin.stable."1.78.0".minimal
rust-bin.stable."1.92.0".minimal
pkg-config
];

depsBuildBuild = [ qemu ];

buildInputs = [ openssl ];

CARGO_TARGET_I686_UNKNOWN_LINUX_MUSL_LINKER = "${stdenv.cc.targetPrefix}cc";
CARGO_TARGET_I686_UNKNOWN_LINUX_MUSL_RUNNER = "qemu-i386";
}) {}
Loading