2022-10-15 20:02:13 +00:00
|
|
|
{
|
2023-04-04 23:56:51 +00:00
|
|
|
inputs = {
|
2023-06-18 00:02:10 +00:00
|
|
|
nixpkgs.url = "github:NixOS/nixpkgs?ref=nixos-unstable";
|
2023-04-04 23:56:51 +00:00
|
|
|
flake-utils.url = "github:numtide/flake-utils";
|
2024-01-18 21:05:55 +00:00
|
|
|
nix-filter.url = "github:numtide/nix-filter";
|
2023-04-04 23:56:51 +00:00
|
|
|
|
|
|
|
fenix = {
|
|
|
|
url = "github:nix-community/fenix";
|
|
|
|
inputs.nixpkgs.follows = "nixpkgs";
|
|
|
|
};
|
2023-04-05 00:30:30 +00:00
|
|
|
crane = {
|
2024-01-24 05:29:03 +00:00
|
|
|
# TODO: Switch back to upstream after [this issue][0] is fixed
|
|
|
|
#
|
|
|
|
# [0]: https://github.com/ipetkov/crane/issues/497
|
|
|
|
url = "github:CobaltCause/crane?ref=crimes-for-cross";
|
2023-04-04 23:56:51 +00:00
|
|
|
inputs.nixpkgs.follows = "nixpkgs";
|
2023-01-27 21:10:21 +00:00
|
|
|
};
|
2024-01-25 19:34:46 +00:00
|
|
|
attic.url = "github:zhaofengli/attic?ref=main";
|
2023-04-04 23:56:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
outputs =
|
|
|
|
{ self
|
|
|
|
, nixpkgs
|
|
|
|
, flake-utils
|
2024-01-18 21:05:55 +00:00
|
|
|
, nix-filter
|
2023-04-04 23:56:51 +00:00
|
|
|
|
|
|
|
, fenix
|
2023-04-05 00:30:30 +00:00
|
|
|
, crane
|
2024-01-25 19:34:46 +00:00
|
|
|
, ...
|
2023-04-04 23:56:51 +00:00
|
|
|
}: flake-utils.lib.eachDefaultSystem (system:
|
|
|
|
let
|
2024-01-23 06:25:08 +00:00
|
|
|
pkgsHost = nixpkgs.legacyPackages.${system};
|
2023-04-04 23:56:51 +00:00
|
|
|
|
|
|
|
# Nix-accessible `Cargo.toml`
|
|
|
|
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
|
|
|
|
|
|
|
|
# The Rust toolchain to use
|
2024-01-23 06:48:58 +00:00
|
|
|
toolchain = fenix.packages.${system}.fromToolchainFile {
|
|
|
|
file = ./rust-toolchain.toml;
|
2023-04-04 23:56:51 +00:00
|
|
|
|
2024-01-23 06:48:58 +00:00
|
|
|
# See also `rust-toolchain.toml`
|
2023-06-18 00:04:11 +00:00
|
|
|
sha256 = "sha256-gdYqng0y9iHYzYPAdkC/ka3DRny3La/S5G8ASj0Ayyc=";
|
2023-04-04 23:56:51 +00:00
|
|
|
};
|
|
|
|
|
2024-01-23 06:26:16 +00:00
|
|
|
builder = pkgs:
|
2024-01-23 06:48:58 +00:00
|
|
|
((crane.mkLib pkgs).overrideToolchain toolchain).buildPackage;
|
2023-07-16 20:38:33 +00:00
|
|
|
|
2024-01-23 06:26:16 +00:00
|
|
|
nativeBuildInputs = pkgs: [
|
2024-01-23 19:22:18 +00:00
|
|
|
# bindgen needs the build platform's libclang. Apparently due to
|
|
|
|
# "splicing weirdness", pkgs.rustPlatform.bindgenHook on its own doesn't
|
|
|
|
# quite do the right thing here.
|
|
|
|
pkgs.buildPackages.rustPlatform.bindgenHook
|
2024-01-23 18:45:02 +00:00
|
|
|
];
|
2023-04-05 00:52:15 +00:00
|
|
|
|
2024-01-23 06:26:16 +00:00
|
|
|
env = pkgs: {
|
|
|
|
ROCKSDB_INCLUDE_DIR = "${pkgs.rocksdb}/include";
|
|
|
|
ROCKSDB_LIB_DIR = "${pkgs.rocksdb}/lib";
|
2024-01-23 19:22:18 +00:00
|
|
|
}
|
|
|
|
// pkgs.lib.optionalAttrs pkgs.stdenv.hostPlatform.isStatic {
|
|
|
|
ROCKSDB_STATIC = "";
|
|
|
|
}
|
|
|
|
// {
|
|
|
|
CARGO_BUILD_RUSTFLAGS = let inherit (pkgs) lib stdenv; in
|
|
|
|
lib.concatStringsSep " " ([]
|
|
|
|
++ lib.optionals
|
|
|
|
# This disables PIE for static builds, which isn't great in terms
|
|
|
|
# of security. Unfortunately, my hand is forced because nixpkgs'
|
|
|
|
# `libstdc++.a` is built without `-fPIE`, which precludes us from
|
|
|
|
# leaving PIE enabled.
|
|
|
|
stdenv.hostPlatform.isStatic
|
|
|
|
["-C" "relocation-model=static"]
|
|
|
|
++ lib.optionals
|
|
|
|
(stdenv.buildPlatform.config != pkgs.stdenv.hostPlatform.config)
|
|
|
|
["-l" "c"]
|
2024-01-23 19:49:52 +00:00
|
|
|
++ lib.optionals
|
|
|
|
# This check has to match the one [here][0]. We only need to set
|
|
|
|
# these flags when using a different linker. Don't ask me why,
|
|
|
|
# though, because I don't know. All I know is it breaks otherwise.
|
|
|
|
#
|
|
|
|
# [0]: https://github.com/NixOS/nixpkgs/blob/612f97239e2cc474c13c9dafa0df378058c5ad8d/pkgs/build-support/rust/lib/default.nix#L36-L39
|
|
|
|
(
|
|
|
|
pkgs.stdenv.hostPlatform.isAarch64
|
|
|
|
&& pkgs.stdenv.hostPlatform.isStatic
|
|
|
|
&& !pkgs.stdenv.isDarwin
|
|
|
|
&& !pkgs.stdenv.cc.bintools.isLLVM
|
|
|
|
)
|
|
|
|
[
|
|
|
|
"-l"
|
|
|
|
"stdc++"
|
|
|
|
"-L"
|
|
|
|
"${stdenv.cc.cc.lib}/${stdenv.hostPlatform.config}/lib"
|
|
|
|
]
|
2024-01-23 19:22:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
# What follows is stolen from [here][0]. Its purpose is to properly
|
|
|
|
# configure compilers and linkers for various stages of the build, and
|
|
|
|
# even covers the case of build scripts that need native code compiled and
|
|
|
|
# run on the build platform (I think).
|
|
|
|
#
|
|
|
|
# [0]: https://github.com/NixOS/nixpkgs/blob/612f97239e2cc474c13c9dafa0df378058c5ad8d/pkgs/build-support/rust/lib/default.nix#L64-L78
|
|
|
|
// (
|
|
|
|
let
|
|
|
|
inherit (pkgs.rust.lib) envVars;
|
|
|
|
in
|
|
|
|
pkgs.lib.optionalAttrs
|
|
|
|
(pkgs.stdenv.targetPlatform.rust.rustcTarget
|
|
|
|
!= pkgs.stdenv.hostPlatform.rust.rustcTarget)
|
|
|
|
(
|
|
|
|
let
|
|
|
|
inherit (pkgs.stdenv.targetPlatform.rust) cargoEnvVarTarget;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
"CC_${cargoEnvVarTarget}" = envVars.ccForTarget;
|
|
|
|
"CXX_${cargoEnvVarTarget}" = envVars.cxxForTarget;
|
|
|
|
"CARGO_TARGET_${cargoEnvVarTarget}_LINKER" =
|
|
|
|
envVars.linkerForTarget;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
// (
|
|
|
|
let
|
|
|
|
inherit (pkgs.stdenv.hostPlatform.rust) cargoEnvVarTarget rustcTarget;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
"CC_${cargoEnvVarTarget}" = envVars.ccForHost;
|
|
|
|
"CXX_${cargoEnvVarTarget}" = envVars.cxxForHost;
|
|
|
|
"CARGO_TARGET_${cargoEnvVarTarget}_LINKER" = envVars.linkerForHost;
|
|
|
|
CARGO_BUILD_TARGET = rustcTarget;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
// (
|
|
|
|
let
|
|
|
|
inherit (pkgs.stdenv.buildPlatform.rust) cargoEnvVarTarget;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
"CC_${cargoEnvVarTarget}" = envVars.ccForBuild;
|
|
|
|
"CXX_${cargoEnvVarTarget}" = envVars.cxxForBuild;
|
|
|
|
"CARGO_TARGET_${cargoEnvVarTarget}_LINKER" = envVars.linkerForBuild;
|
|
|
|
HOST_CC = "${pkgs.buildPackages.stdenv.cc}/bin/cc";
|
|
|
|
HOST_CXX = "${pkgs.buildPackages.stdenv.cc}/bin/c++";
|
|
|
|
}
|
|
|
|
));
|
2024-01-23 06:56:00 +00:00
|
|
|
|
|
|
|
package = pkgs: builder pkgs {
|
2024-01-18 21:05:55 +00:00
|
|
|
src = nix-filter {
|
|
|
|
root = ./.;
|
|
|
|
include = [
|
|
|
|
"src"
|
|
|
|
"Cargo.toml"
|
|
|
|
"Cargo.lock"
|
|
|
|
];
|
|
|
|
};
|
2023-04-04 23:56:51 +00:00
|
|
|
|
2023-12-24 05:12:42 +00:00
|
|
|
# This is redundant with CI
|
|
|
|
doCheck = false;
|
|
|
|
|
2024-01-23 06:56:00 +00:00
|
|
|
env = env pkgs;
|
|
|
|
nativeBuildInputs = nativeBuildInputs pkgs;
|
2023-12-24 05:12:42 +00:00
|
|
|
|
|
|
|
meta.mainProgram = cargoToml.package.name;
|
2023-04-04 23:56:51 +00:00
|
|
|
};
|
2024-01-23 06:56:00 +00:00
|
|
|
in
|
|
|
|
{
|
2024-01-23 07:07:30 +00:00
|
|
|
packages = {
|
|
|
|
default = package pkgsHost;
|
|
|
|
|
|
|
|
oci-image =
|
|
|
|
let
|
|
|
|
package = self.packages.${system}.default;
|
|
|
|
in
|
|
|
|
pkgsHost.dockerTools.buildImage {
|
|
|
|
name = package.pname;
|
|
|
|
tag = "latest";
|
|
|
|
config = {
|
|
|
|
# Use the `tini` init system so that signals (e.g. ctrl+c/SIGINT)
|
|
|
|
# are handled as expected
|
|
|
|
Entrypoint = [
|
|
|
|
"${pkgsHost.lib.getExe' pkgsHost.tini "tini"}"
|
|
|
|
"--"
|
|
|
|
];
|
|
|
|
Cmd = [
|
|
|
|
"${pkgsHost.lib.getExe package}"
|
|
|
|
];
|
|
|
|
};
|
2024-01-18 21:31:46 +00:00
|
|
|
};
|
2024-01-23 19:22:18 +00:00
|
|
|
} // builtins.listToAttrs (
|
|
|
|
builtins.map
|
|
|
|
(crossSystem: {
|
|
|
|
name = "static-${crossSystem}";
|
|
|
|
value = package (import nixpkgs {
|
|
|
|
inherit system;
|
|
|
|
crossSystem = {
|
|
|
|
config = crossSystem;
|
|
|
|
};
|
|
|
|
}).pkgsStatic;
|
|
|
|
})
|
|
|
|
[
|
|
|
|
"x86_64-unknown-linux-musl"
|
2024-01-23 19:49:52 +00:00
|
|
|
"aarch64-unknown-linux-musl"
|
2024-01-23 19:22:18 +00:00
|
|
|
]
|
|
|
|
);
|
2024-01-18 21:31:46 +00:00
|
|
|
|
2024-01-23 06:25:08 +00:00
|
|
|
devShells.default = pkgsHost.mkShell {
|
2024-01-23 06:26:16 +00:00
|
|
|
env = env pkgsHost // {
|
2023-12-24 05:12:42 +00:00
|
|
|
# Rust Analyzer needs to be able to find the path to default crate
|
|
|
|
# sources, and it can read this environment variable to do so. The
|
|
|
|
# `rust-src` component is required in order for this to work.
|
2024-01-23 06:48:58 +00:00
|
|
|
RUST_SRC_PATH = "${toolchain}/lib/rustlib/src/rust/library";
|
2023-12-24 05:12:42 +00:00
|
|
|
};
|
2023-07-16 20:38:33 +00:00
|
|
|
|
2023-04-04 23:56:51 +00:00
|
|
|
# Development tools
|
2024-01-23 06:26:16 +00:00
|
|
|
nativeBuildInputs = nativeBuildInputs pkgsHost ++ [
|
2024-01-23 06:48:58 +00:00
|
|
|
# Always use nightly rustfmt because most of its options are unstable
|
|
|
|
#
|
|
|
|
# This needs to come before `toolchain` in this list, otherwise
|
|
|
|
# `$PATH` will have stable rustfmt instead.
|
|
|
|
fenix.packages.${system}.latest.rustfmt
|
|
|
|
|
|
|
|
toolchain
|
2024-01-23 06:25:08 +00:00
|
|
|
] ++ (with pkgsHost; [
|
2023-12-20 23:29:09 +00:00
|
|
|
engage
|
2023-04-04 23:56:51 +00:00
|
|
|
]);
|
|
|
|
};
|
|
|
|
});
|
2022-10-15 20:02:13 +00:00
|
|
|
}
|