Skip to content

Commit

Permalink
feat: full nix refactor -- all targets!
Browse files Browse the repository at this point in the history
now this repo can, through Nix, cross-compile to all major targets. this
was only tested in a nix-darwin environment, but cross-compilation to
all other targets (except MacOS, because... Apple) should work on NixOS
and such.
  • Loading branch information
levydsa committed Dec 31, 2024
1 parent 9ab03a9 commit 201e48e
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 64 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/target
target/
.DS_Store
test*.db*
*.o
local.db
example
result
.direnv/
5 changes: 5 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

set -xe

cargo build --target x86_64-apple-darwin --features encryption --release
cargo build --target aarch64-apple-darwin --features encryption --release
cargo build --target aarch64-unknown-linux-gnu --features encryption --release
cargo build --target x86_64-unknown-linux-gnu --features encryption --release
cargo build --target x86_64-pc-windows-gnu --features encryption --release

RUSTFLAGS="-C target-feature=-crt-static" cargo build --target aarch64-unknown-linux-musl --features encryption --release
RUSTFLAGS="-C target-feature=-crt-static" cargo build --target x86_64-unknown-linux-musl --features encryption --release
51 changes: 44 additions & 7 deletions flake.lock

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

105 changes: 51 additions & 54 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,57 @@

inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
flake-utils = {
url = "github:numtide/flake-utils";
flake-utils.url = "github:numtide/flake-utils";
crane.url = "github:ipetkov/crane";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem
(system:
let
pkgs = import nixpkgs {
inherit system;
config.allowUnsupportedSystem = true;
};
in
{
formatter = pkgs.nixpkgs-fmt;
devShells.default =
with pkgs;
mkShell {
nativeBuildInputs = [
pkg-config
rust-bindgen
cmake
];

buildInputs = [
] ++ lib.optionals stdenv.isDarwin [
iconv
darwin.apple_sdk.frameworks.Security
darwin.apple_sdk.frameworks.CoreFoundation
darwin.apple_sdk.frameworks.SystemConfiguration
darwin.apple_sdk.frameworks.CoreServices
];

CARGO_TARGET_X86_64_APPLE_DARWIN_RUSTFLAGS= lib.lists.fold (a: b: "${a} ${b}") "" [
"-Ctarget-feature=-crt-static"
"-Clink-arg=-target"
"-Clink-arg=x86_64-apple-darwin"
];
CC_x86_64_apple_darwin =
"${pkgs.pkgsCross.x86_64-darwin.stdenv.cc}/bin/x86_64-apple-darwin-clang";
CARGO_TARGET_X86_64_APPLE_DARWIN_LINKER =
"${pkgs.pkgsCross.x86_64-darwin.stdenv.cc}/bin/x86_64-apple-darwin-clang";

CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_RUSTFLAGS="-Ctarget-feature=-crt-static";
CC_x86_64_unknown_linux_gnu =
"${pkgs.pkgsCross.gnu64.stdenv.cc}/bin/x86_64-unknown-linux-gnu-gcc";
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER =
"${pkgs.pkgsCross.gnu64.stdenv.cc}/bin/x86_64-unknown-linux-gnu-gcc";

CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUSTFLAGS="-Ctarget-feature=-crt-static";
CC_aarch64_unknown_linux_gnu =
"${pkgs.pkgsCross.aarch64-multiplatform.stdenv.cc}/bin/aarch64-unknown-linux-gnu-gcc";
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER =
"${pkgs.pkgsCross.aarch64-multiplatform.stdenv.cc}/bin/aarch64-unknown-linux-gnu-gcc";
};
});
outputs = { self, nixpkgs, flake-utils, crane, rust-overlay }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ (import rust-overlay) ];
};
toUpper = pkgs.lib.strings.toUpper;
rust = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
craneLib = pkgs.lib.debug.traveVal ((crane.mkLib pkgs).overrideToolchain rust);
cargofy = s: builtins.replaceStrings [ "-" ] [ "_" ] s;
env = config: cc: {
nativeBuildInputs = [ cc ];
"CC_${cargofy config}" = "${cc.targetPrefix}cc";
"CXX_${cargofy config}" = "${cc.targetPrefix}c++";
"CARGO_TARGET_${toUpper (cargofy config)}_LINKER" = "${cc.targetPrefix}cc";
};
in
{
formatter = pkgs.nixpkgs-fmt;
packages.default = pkgs.callPackage ./package.nix { inherit craneLib; };
packages.musl64 = pkgs.pkgsCross.musl64.callPackage ./package.nix { inherit craneLib; };
packages.gnu64 = pkgs.pkgsCross.gnu64.callPackage ./package.nix { inherit craneLib; };
packages.apple64 = pkgs.pkgsCross.x86_64-darwin.callPackage ./package.nix { inherit craneLib; };
devShells.default =
with pkgs; mkShell (lib.zipAttrsWith
(name: values: if builtins.isList (builtins.head values) then builtins.concatLists values else builtins.head values)
[
{
nativeBuildInputs = [
pkg-config
cmake
rust-bindgen
rust
rust-analyzer
];
}
(with pkgsCross.mingwW64; env "x86_64-pc-windows-gnu" stdenv.cc)
(with pkgsCross.x86_64-darwin; env targetPlatform.config stdenv.cc)
(with pkgsCross.aarch64-multiplatform; env targetPlatform.config stdenv.cc)
(with pkgsCross.aarch64-multiplatform-musl; env targetPlatform.config stdenv.cc)
(with pkgsCross.gnu64; env targetPlatform.config stdenv.cc)
(with pkgsCross.musl64; env targetPlatform.config stdenv.cc)
(with pkgsCross.aarch64-darwin; env targetPlatform.config stdenv.cc)
]);
});
}
43 changes: 43 additions & 0 deletions package.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{ lib
, stdenv
, craneLib
, pkg-config
, cmake
, apple-sdk
}:
let target = lib.strings.toUpper (builtins.replaceStrings [ "-" ] [ "_" ] stdenv.targetPlatform.config);
in
craneLib.buildPackage {
src = lib.fileset.toSource rec {
root = ./.;
fileset = lib.fileset.unions [
(craneLib.fileset.commonCargoSources root)
(lib.fileset.fileFilter (file: file.hasExt "h") root)
];
};

strictDeps = true;
doCheck = false;

nativeBuildInputs = [
pkg-config
cmake
stdenv.cc
];

nativeInput = lib.optionals stdenv.isDarwin [ apple-sdk ];

CARGO_BUILD_TARGET = stdenv.targetPlatform.config;

"CARGO_TARGET_${target}_LINKER" = "${stdenv.cc.targetPrefix}cc";

RUSTFLAGS = "-C target-feature=-crt-static";

HOST_CC = "${stdenv.cc.nativePrefix}cc";
TARGET_CC = "${stdenv.cc.targetPrefix}cc";

BINDGEN_EXTRA_CLANG_ARGS =
if stdenv.isDarwin
then "-isystem ${apple-sdk.sdkroot}/usr/include"
else "-isystem ${stdenv.cc.libc.dev}/include";
}
10 changes: 8 additions & 2 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
[toolchain]
channel = "stable"
targets = [
"aarch64-apple-darwin",
"x86_64-apple-darwin",
"aarch64-apple-darwin",

"x86_64-unknown-linux-gnu",
"aarch64-unknown-linux-gnu",

"x86_64-unknown-linux-musl",
"x86_64-pc-windows-gnullvm",
"aarch64-unknown-linux-musl",

"x86_64-pc-windows-gnu",
]

0 comments on commit 201e48e

Please sign in to comment.