e70f33741c
Also switch names to match the newer upstream nixpkgs code. Flake lock file updates: • Updated input 'attic': 'github:zhaofengli/attic/fbe252a5c21febbe920c025560cbd63b20e24f3b' (2024-01-18) → 'github:zhaofengli/attic/6eabc3f02fae3683bffab483e614bebfcd476b21' (2024-02-14) • Updated input 'fenix': 'github:nix-community/fenix/e132ea0eb0c799a2109a91688e499d7bf4962801' (2024-01-18) → 'github:nix-community/fenix/c8943ea9e98d41325ff57d4ec14736d330b321b2' (2024-03-05) • Updated input 'fenix/rust-analyzer-src': 'github:rust-lang/rust-analyzer/9d9b34354d2f13e33568c9c55b226dd014a146a0' (2024-01-17) → 'github:rust-lang/rust-analyzer/9f14343f9ee24f53f17492c5f9b653427e2ad15e' (2024-03-04) • Updated input 'flake-utils': 'github:numtide/flake-utils/1ef2e671c3b0c19053962c07dbda38332dcebf26' (2024-01-15) → 'github:numtide/flake-utils/d465f4819400de7c8d874d50b982301f28a84605' (2024-02-28) • Updated input 'nixpkgs': 'github:NixOS/nixpkgs/842d9d80cfd4560648c785f8a4e6f3b096790e19' (2024-01-17) → 'github:NixOS/nixpkgs/b8697e57f10292a6165a20f03d2f42920dfaf973' (2024-03-03)
264 lines
8.6 KiB
Nix
264 lines
8.6 KiB
Nix
{
|
|
inputs = {
|
|
nixpkgs.url = "github:NixOS/nixpkgs?ref=nixos-unstable";
|
|
flake-utils.url = "github:numtide/flake-utils";
|
|
nix-filter.url = "github:numtide/nix-filter";
|
|
flake-compat = {
|
|
url = "github:edolstra/flake-compat";
|
|
flake = false;
|
|
};
|
|
|
|
fenix = {
|
|
url = "github:nix-community/fenix";
|
|
inputs.nixpkgs.follows = "nixpkgs";
|
|
};
|
|
crane = {
|
|
# 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";
|
|
inputs.nixpkgs.follows = "nixpkgs";
|
|
};
|
|
attic.url = "github:zhaofengli/attic?ref=main";
|
|
};
|
|
|
|
outputs =
|
|
{ self
|
|
, nixpkgs
|
|
, flake-utils
|
|
, nix-filter
|
|
|
|
, fenix
|
|
, crane
|
|
, ...
|
|
}: flake-utils.lib.eachDefaultSystem (system:
|
|
let
|
|
pkgsHost = nixpkgs.legacyPackages.${system};
|
|
|
|
# Nix-accessible `Cargo.toml`
|
|
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
|
|
|
|
# The Rust toolchain to use
|
|
toolchain = fenix.packages.${system}.fromToolchainFile {
|
|
file = ./rust-toolchain.toml;
|
|
|
|
# See also `rust-toolchain.toml`
|
|
sha256 = "sha256-SXRtAuO4IqNOQq+nLbrsDFbVk+3aVA8NNpSZsKlVH/8=";
|
|
};
|
|
|
|
builder = pkgs:
|
|
((crane.mkLib pkgs).overrideToolchain toolchain).buildPackage;
|
|
|
|
nativeBuildInputs = pkgs: [
|
|
# 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.pkgsBuildHost.rustPlatform.bindgenHook
|
|
];
|
|
|
|
env = pkgs: {
|
|
ROCKSDB_INCLUDE_DIR = "${pkgs.rocksdb}/include";
|
|
ROCKSDB_LIB_DIR = "${pkgs.rocksdb}/lib";
|
|
}
|
|
// 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 != stdenv.hostPlatform.config)
|
|
["-l" "c"]
|
|
++ 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/5cdb38bb16c6d0a38779db14fcc766bc1b2394d6/pkgs/build-support/rust/lib/default.nix#L37-L40
|
|
(
|
|
# 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
|
|
)
|
|
[
|
|
"-l"
|
|
"stdc++"
|
|
"-L"
|
|
"${stdenv.cc.cc.lib}/${stdenv.hostPlatform.config}/lib"
|
|
]
|
|
);
|
|
}
|
|
|
|
# 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/5cdb38bb16c6d0a38779db14fcc766bc1b2394d6/pkgs/build-support/rust/lib/default.nix#L57-L80
|
|
// (
|
|
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.pkgsBuildHost.stdenv.cc}/bin/cc";
|
|
HOST_CXX = "${pkgs.pkgsBuildHost.stdenv.cc}/bin/c++";
|
|
}
|
|
));
|
|
|
|
package = pkgs: builder pkgs {
|
|
src = nix-filter {
|
|
root = ./.;
|
|
include = [
|
|
"src"
|
|
"Cargo.toml"
|
|
"Cargo.lock"
|
|
];
|
|
};
|
|
|
|
# This is redundant with CI
|
|
doCheck = false;
|
|
|
|
env = env pkgs;
|
|
nativeBuildInputs = nativeBuildInputs pkgs;
|
|
|
|
meta.mainProgram = cargoToml.package.name;
|
|
};
|
|
|
|
mkOciImage = pkgs: package:
|
|
pkgs.dockerTools.buildImage {
|
|
name = package.pname;
|
|
tag = "next";
|
|
copyToRoot = [
|
|
pkgs.dockerTools.caCertificates
|
|
];
|
|
config = {
|
|
# Use the `tini` init system so that signals (e.g. ctrl+c/SIGINT)
|
|
# are handled as expected
|
|
Entrypoint = [
|
|
"${pkgs.lib.getExe' pkgs.tini "tini"}"
|
|
"--"
|
|
];
|
|
Cmd = [
|
|
"${pkgs.lib.getExe package}"
|
|
];
|
|
};
|
|
};
|
|
in
|
|
{
|
|
packages = {
|
|
default = package pkgsHost;
|
|
oci-image = mkOciImage pkgsHost self.packages.${system}.default;
|
|
}
|
|
//
|
|
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"
|
|
]
|
|
)
|
|
);
|
|
|
|
devShells.default = pkgsHost.mkShell {
|
|
env = env pkgsHost // {
|
|
# 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.
|
|
RUST_SRC_PATH = "${toolchain}/lib/rustlib/src/rust/library";
|
|
};
|
|
|
|
# Development tools
|
|
nativeBuildInputs = nativeBuildInputs pkgsHost ++ [
|
|
# 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
|
|
] ++ (with pkgsHost; [
|
|
engage
|
|
|
|
# Needed for Complement
|
|
go
|
|
olm
|
|
|
|
# Needed for our script for Complement
|
|
jq
|
|
]);
|
|
};
|
|
});
|
|
}
|