2022-10-15 22:02:13 +02:00
|
|
|
{
|
2023-04-05 01:56:51 +02:00
|
|
|
inputs = {
|
2023-06-18 02:02:10 +02:00
|
|
|
nixpkgs.url = "github:NixOS/nixpkgs?ref=nixos-unstable";
|
2023-04-05 01:56:51 +02:00
|
|
|
flake-utils.url = "github:numtide/flake-utils";
|
2024-01-18 22:05:55 +01:00
|
|
|
nix-filter.url = "github:numtide/nix-filter";
|
2024-02-02 03:50:49 +01:00
|
|
|
flake-compat = {
|
|
|
|
url = "github:edolstra/flake-compat";
|
|
|
|
flake = false;
|
|
|
|
};
|
2023-04-05 01:56:51 +02:00
|
|
|
|
|
|
|
fenix = {
|
|
|
|
url = "github:nix-community/fenix";
|
|
|
|
inputs.nixpkgs.follows = "nixpkgs";
|
|
|
|
};
|
2023-04-05 02:30:30 +02:00
|
|
|
crane = {
|
2024-03-05 07:37:23 +01:00
|
|
|
# Pin latest crane that's not affected by the following bugs:
|
|
|
|
#
|
|
|
|
# * <https://github.com/ipetkov/crane/issues/527#issuecomment-1978079140>
|
|
|
|
# * <https://github.com/toml-rs/toml/issues/691>
|
|
|
|
# * <https://github.com/toml-rs/toml/issues/267>
|
|
|
|
url = "github:ipetkov/crane?rev=2c653e4478476a52c6aa3ac0495e4dea7449ea0e";
|
2023-04-05 01:56:51 +02:00
|
|
|
inputs.nixpkgs.follows = "nixpkgs";
|
2023-01-27 22:10:21 +01:00
|
|
|
};
|
2024-01-25 20:34:46 +01:00
|
|
|
attic.url = "github:zhaofengli/attic?ref=main";
|
2023-04-05 01:56:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
outputs =
|
|
|
|
{ self
|
|
|
|
, nixpkgs
|
|
|
|
, flake-utils
|
2024-01-18 22:05:55 +01:00
|
|
|
, nix-filter
|
2023-04-05 01:56:51 +02:00
|
|
|
|
|
|
|
, fenix
|
2023-04-05 02:30:30 +02:00
|
|
|
, crane
|
2024-01-25 20:34:46 +01:00
|
|
|
, ...
|
2023-04-05 01:56:51 +02:00
|
|
|
}: flake-utils.lib.eachDefaultSystem (system:
|
|
|
|
let
|
2024-01-23 07:25:08 +01:00
|
|
|
pkgsHost = nixpkgs.legacyPackages.${system};
|
2023-04-05 01:56:51 +02:00
|
|
|
|
|
|
|
# Nix-accessible `Cargo.toml`
|
|
|
|
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
|
|
|
|
|
|
|
|
# The Rust toolchain to use
|
2024-01-23 07:48:58 +01:00
|
|
|
toolchain = fenix.packages.${system}.fromToolchainFile {
|
|
|
|
file = ./rust-toolchain.toml;
|
2023-04-05 01:56:51 +02:00
|
|
|
|
2024-01-23 07:48:58 +01:00
|
|
|
# See also `rust-toolchain.toml`
|
2024-01-26 05:33:15 +01:00
|
|
|
sha256 = "sha256-SXRtAuO4IqNOQq+nLbrsDFbVk+3aVA8NNpSZsKlVH/8=";
|
2023-04-05 01:56:51 +02:00
|
|
|
};
|
|
|
|
|
2024-01-23 07:26:16 +01:00
|
|
|
builder = pkgs:
|
2024-01-23 07:48:58 +01:00
|
|
|
((crane.mkLib pkgs).overrideToolchain toolchain).buildPackage;
|
2023-07-16 22:38:33 +02:00
|
|
|
|
2024-01-23 07:26:16 +01:00
|
|
|
nativeBuildInputs = pkgs: [
|
2024-01-23 20:22:18 +01: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.
|
2024-03-05 07:40:47 +01:00
|
|
|
pkgs.pkgsBuildHost.rustPlatform.bindgenHook
|
2024-01-23 19:45:02 +01:00
|
|
|
];
|
2023-04-05 02:52:15 +02:00
|
|
|
|
2024-01-23 07:26:16 +01:00
|
|
|
env = pkgs: {
|
|
|
|
ROCKSDB_INCLUDE_DIR = "${pkgs.rocksdb}/include";
|
|
|
|
ROCKSDB_LIB_DIR = "${pkgs.rocksdb}/lib";
|
2024-01-23 20:22:18 +01: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
|
2024-01-26 07:00:32 +01:00
|
|
|
(stdenv.buildPlatform.config != stdenv.hostPlatform.config)
|
2024-01-23 20:22:18 +01:00
|
|
|
["-l" "c"]
|
2024-01-23 20:49:52 +01: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.
|
|
|
|
#
|
2024-03-05 07:40:47 +01:00
|
|
|
# [0]: https://github.com/NixOS/nixpkgs/blob/5cdb38bb16c6d0a38779db14fcc766bc1b2394d6/pkgs/build-support/rust/lib/default.nix#L37-L40
|
2024-01-23 20:49:52 +01:00
|
|
|
(
|
2024-01-26 07:00:32 +01:00
|
|
|
# Nixpkgs doesn't check for x86_64 here but we do, because I
|
|
|
|
# observed a failure building statically for x86_64 without
|
|
|
|
# including it here. Linkers are weird.
|
|
|
|
(stdenv.hostPlatform.isAarch64 || stdenv.hostPlatform.isx86_64)
|
|
|
|
&& stdenv.hostPlatform.isStatic
|
|
|
|
&& !stdenv.isDarwin
|
|
|
|
&& !stdenv.cc.bintools.isLLVM
|
2024-01-23 20:49:52 +01:00
|
|
|
)
|
|
|
|
[
|
|
|
|
"-l"
|
|
|
|
"stdc++"
|
|
|
|
"-L"
|
|
|
|
"${stdenv.cc.cc.lib}/${stdenv.hostPlatform.config}/lib"
|
|
|
|
]
|
2024-01-23 20:22:18 +01: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).
|
|
|
|
#
|
2024-03-05 07:40:47 +01:00
|
|
|
# [0]: https://github.com/NixOS/nixpkgs/blob/5cdb38bb16c6d0a38779db14fcc766bc1b2394d6/pkgs/build-support/rust/lib/default.nix#L57-L80
|
2024-01-23 20:22:18 +01:00
|
|
|
// (
|
|
|
|
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;
|
2024-03-05 07:40:47 +01:00
|
|
|
HOST_CC = "${pkgs.pkgsBuildHost.stdenv.cc}/bin/cc";
|
|
|
|
HOST_CXX = "${pkgs.pkgsBuildHost.stdenv.cc}/bin/c++";
|
2024-01-23 20:22:18 +01:00
|
|
|
}
|
|
|
|
));
|
2024-01-23 07:56:00 +01:00
|
|
|
|
|
|
|
package = pkgs: builder pkgs {
|
2024-01-18 22:05:55 +01:00
|
|
|
src = nix-filter {
|
|
|
|
root = ./.;
|
|
|
|
include = [
|
|
|
|
"src"
|
|
|
|
"Cargo.toml"
|
|
|
|
"Cargo.lock"
|
|
|
|
];
|
|
|
|
};
|
2023-04-05 01:56:51 +02:00
|
|
|
|
2023-12-24 06:12:42 +01:00
|
|
|
# This is redundant with CI
|
|
|
|
doCheck = false;
|
|
|
|
|
2024-01-23 07:56:00 +01:00
|
|
|
env = env pkgs;
|
|
|
|
nativeBuildInputs = nativeBuildInputs pkgs;
|
2023-12-24 06:12:42 +01:00
|
|
|
|
|
|
|
meta.mainProgram = cargoToml.package.name;
|
2023-04-05 01:56:51 +02:00
|
|
|
};
|
2024-01-23 08:07:30 +01:00
|
|
|
|
2024-01-26 04:36:17 +01:00
|
|
|
mkOciImage = pkgs: package:
|
|
|
|
pkgs.dockerTools.buildImage {
|
2024-01-23 08:07:30 +01:00
|
|
|
name = package.pname;
|
2024-01-27 06:38:13 +01:00
|
|
|
tag = "next";
|
2024-01-27 21:23:57 +01:00
|
|
|
copyToRoot = [
|
|
|
|
pkgs.dockerTools.caCertificates
|
|
|
|
];
|
2024-01-23 08:07:30 +01:00
|
|
|
config = {
|
|
|
|
# Use the `tini` init system so that signals (e.g. ctrl+c/SIGINT)
|
|
|
|
# are handled as expected
|
|
|
|
Entrypoint = [
|
2024-01-26 04:36:17 +01:00
|
|
|
"${pkgs.lib.getExe' pkgs.tini "tini"}"
|
2024-01-23 08:07:30 +01:00
|
|
|
"--"
|
|
|
|
];
|
|
|
|
Cmd = [
|
2024-01-26 04:36:17 +01:00
|
|
|
"${pkgs.lib.getExe package}"
|
2024-01-23 08:07:30 +01:00
|
|
|
];
|
|
|
|
};
|
2024-01-18 22:31:46 +01:00
|
|
|
};
|
2024-01-26 04:36:17 +01:00
|
|
|
in
|
|
|
|
{
|
|
|
|
packages = {
|
|
|
|
default = package pkgsHost;
|
|
|
|
oci-image = mkOciImage pkgsHost self.packages.${system}.default;
|
2024-01-30 00:34:16 +01:00
|
|
|
}
|
|
|
|
//
|
|
|
|
builtins.listToAttrs
|
|
|
|
(builtins.concatLists
|
|
|
|
(builtins.map
|
|
|
|
(crossSystem:
|
|
|
|
let
|
|
|
|
binaryName = "static-${crossSystem}";
|
|
|
|
pkgsCrossStatic =
|
|
|
|
(import nixpkgs {
|
|
|
|
inherit system;
|
|
|
|
crossSystem = {
|
|
|
|
config = crossSystem;
|
|
|
|
};
|
|
|
|
}).pkgsStatic;
|
|
|
|
in
|
|
|
|
[
|
|
|
|
# An output for a statically-linked binary
|
|
|
|
{
|
|
|
|
name = binaryName;
|
|
|
|
value = package pkgsCrossStatic;
|
|
|
|
}
|
|
|
|
|
|
|
|
# An output for an OCI image based on that binary
|
|
|
|
{
|
|
|
|
name = "oci-image-${crossSystem}";
|
|
|
|
value = mkOciImage
|
|
|
|
pkgsCrossStatic
|
|
|
|
self.packages.${system}.${binaryName};
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
[
|
|
|
|
"x86_64-unknown-linux-musl"
|
|
|
|
"aarch64-unknown-linux-musl"
|
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
2024-01-18 22:31:46 +01:00
|
|
|
|
2024-01-23 07:25:08 +01:00
|
|
|
devShells.default = pkgsHost.mkShell {
|
2024-01-23 07:26:16 +01:00
|
|
|
env = env pkgsHost // {
|
2023-12-24 06:12:42 +01: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 07:48:58 +01:00
|
|
|
RUST_SRC_PATH = "${toolchain}/lib/rustlib/src/rust/library";
|
2023-12-24 06:12:42 +01:00
|
|
|
};
|
2023-07-16 22:38:33 +02:00
|
|
|
|
2023-04-05 01:56:51 +02:00
|
|
|
# Development tools
|
2024-01-23 07:26:16 +01:00
|
|
|
nativeBuildInputs = nativeBuildInputs pkgsHost ++ [
|
2024-01-23 07:48:58 +01: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 07:25:08 +01:00
|
|
|
] ++ (with pkgsHost; [
|
2023-12-21 00:29:09 +01:00
|
|
|
engage
|
2023-12-24 09:51:26 +01:00
|
|
|
|
|
|
|
# Needed for Complement
|
|
|
|
go
|
|
|
|
olm
|
|
|
|
|
|
|
|
# Needed for our script for Complement
|
|
|
|
jq
|
2023-04-05 01:56:51 +02:00
|
|
|
]);
|
|
|
|
};
|
|
|
|
});
|
2022-10-15 22:02:13 +02:00
|
|
|
}
|