fix: musl builds

This commit is contained in:
Shuroii 2025-07-01 21:38:48 +02:00
commit 5dba10af21
No known key found for this signature in database
2 changed files with 100 additions and 135 deletions

View file

@ -40,9 +40,8 @@
mkScope = mkScope =
pkgs: pkgs:
pkgs.lib.makeScope pkgs.newScope (self: { pkgs.lib.makeScope pkgs.newScope (self: {
inherit pkgs; inherit pkgs inputs;
craneLib = ((inputs.crane.mkLib pkgs).overrideToolchain (_: toolchain)); craneLib = (inputs.crane.mkLib pkgs).overrideToolchain (_: toolchain);
inherit inputs;
main = self.callPackage ./nix/pkgs/main { }; main = self.callPackage ./nix/pkgs/main { };
liburing = pkgs.liburing.overrideAttrs { liburing = pkgs.liburing.overrideAttrs {
# Tests weren't building # Tests weren't building
@ -53,14 +52,10 @@
]; ];
buildFlags = [ "library" ]; buildFlags = [ "library" ];
}; };
rocksdb = pkgs.rocksdb_9_10.overrideAttrs (oldAttrs: { rocksdb-custom = pkgs.rocksdb_9_10.overrideAttrs (oldAttrs: {
version = "v9.11.1"; version = "v9.11.1";
src = inputs.rocksdb; src = inputs.rocksdb;
# We have this already at https://forgejo.ellis.link/continuwuation/rocksdb/commit/a935c0273e1ba44eacf88ce3685a9b9831486155
# Unsetting this so we don't have to revert it and make this nix exclusive
patches = [ ];
cmakeFlags = cmakeFlags =
pkgs.lib.subtractLists [ pkgs.lib.subtractLists [
# No real reason to have snappy or zlib, no one uses this # No real reason to have snappy or zlib, no one uses this
@ -106,7 +101,8 @@
# preInstall hooks has stuff for messing with ldb/sst_dump which we don't need or use # preInstall hooks has stuff for messing with ldb/sst_dump which we don't need or use
preInstall = ""; preInstall = "";
# # We have this already at https://forgejo.ellis.link/continuwuation/rocksdb/commit/a935c0273e1ba44eacf88ce3685a9b9831486155
patches = [ ];
postPatch = '' postPatch = ''
# Fix gcc-13 build failures due to missing <cstdint> and # Fix gcc-13 build failures due to missing <cstdint> and
# <system_error> includes, fixed upstream since 8.x # <system_error> includes, fixed upstream since 8.x

View file

@ -10,11 +10,12 @@
, rust , rust
, rust-jemalloc-sys , rust-jemalloc-sys
, stdenv , stdenv
, # Options (keep sorted)
all_features ? false # Options (keep sorted)
, all_features ? false
, default_features ? true , default_features ? true
, # default list of disabled features # default list of disabled features
disable_features ? [ , disable_features ? [
# dont include experimental features # dont include experimental features
"experimental" "experimental"
# jemalloc profiling/stats features are expensive and shouldn't # jemalloc profiling/stats features are expensive and shouldn't
@ -29,34 +30,28 @@
, disable_release_max_log_level ? false , disable_release_max_log_level ? false
, features ? [ ] , features ? [ ]
, profile ? "release" , profile ? "release"
, # rocksdb compiled with -march=haswell and target-cpu=haswell rustflag # rocksdb compiled with -march=haswell and target-cpu=haswell rustflag
# haswell is pretty much any x86 cpu made in the last 12 years, and # haswell is pretty much any x86 cpu made in the last 12 years, and
# supports modern CPU extensions that rocksdb can make use of. # supports modern CPU extensions that rocksdb can make use of.
# disable if trying to make a portable x86_64 build for very old hardware # disable if trying to make a portable x86_64 build for very old hardware
x86_64_haswell_target_optimised ? false , x86_64_haswell_target_optimised ? false
,
}: }:
let let
# We perform default-feature unification in nix, because some of the dependencies # We perform default-feature unification in nix, because some of the dependencies
# on the nix side depend on feature values. # on the nix side depend on feature values.
crateFeatures = crateFeatures = path:
path: let manifest = lib.importTOML "${path}/Cargo.toml"; in
let
manifest = lib.importTOML "${path}/Cargo.toml";
in
lib.remove "default" (lib.attrNames manifest.features); lib.remove "default" (lib.attrNames manifest.features);
crateDefaultFeatures = path: (lib.importTOML "${path}/Cargo.toml").features.default; crateDefaultFeatures = path:
(lib.importTOML "${path}/Cargo.toml").features.default;
allDefaultFeatures = crateDefaultFeatures "${inputs.self}/src/main"; allDefaultFeatures = crateDefaultFeatures "${inputs.self}/src/main";
allFeatures = crateFeatures "${inputs.self}/src/main"; allFeatures = crateFeatures "${inputs.self}/src/main";
features' = lib.unique ( features' = lib.unique
features (features ++
++ lib.optionals default_features allDefaultFeatures lib.optionals default_features allDefaultFeatures ++
++ lib.optionals all_features allFeatures lib.optionals all_features allFeatures);
); disable_features' = disable_features ++ lib.optionals disable_release_max_log_level [ "release_max_log_level" ];
disable_features' =
disable_features
++ lib.optionals disable_release_max_log_level [ "release_max_log_level" ];
features'' = lib.subtractLists disable_features' features'; features'' = lib.subtractLists disable_features' features';
featureEnabled = feature: builtins.elem feature features''; featureEnabled = feature: builtins.elem feature features'';
@ -68,69 +63,54 @@ let
# own. In order for this to work, we need to set flags on the build that match # own. In order for this to work, we need to set flags on the build that match
# whatever flags tikv-jemalloc-sys was going to use. These are dependent on # whatever flags tikv-jemalloc-sys was going to use. These are dependent on
# which features we enable in tikv-jemalloc-sys. # which features we enable in tikv-jemalloc-sys.
rust-jemalloc-sys' = rust-jemalloc-sys' = (rust-jemalloc-sys.override {
(rust-jemalloc-sys.override { # tikv-jemalloc-sys/unprefixed_malloc_on_supported_platforms feature
# tikv-jemalloc-sys/unprefixed_malloc_on_supported_platforms feature unprefixed = true;
unprefixed = true; }).overrideAttrs (old: {
}).overrideAttrs configureFlags = old.configureFlags ++
(old: { # we dont need docs
configureFlags = [ "--disable-doc" ] ++
old.configureFlags # we dont need cxx/C++ integration
++ [ "--disable-cxx" ] ++
# we dont need docs # tikv-jemalloc-sys/profiling feature
[ "--disable-doc" ] lib.optional (featureEnabled "jemalloc_prof") "--enable-prof" ++
++ # tikv-jemalloc-sys/stats feature
# we dont need cxx/C++ integration (if (featureEnabled "jemalloc_stats") then [ "--enable-stats" ] else [ "--disable-stats" ]);
[ "--disable-cxx" ] });
++
# tikv-jemalloc-sys/profiling feature
lib.optional (featureEnabled "jemalloc_prof") "--enable-prof"
++
# tikv-jemalloc-sys/stats feature
(if (featureEnabled "jemalloc_stats") then [ "--enable-stats" ] else [ "--disable-stats" ]);
});
buildDepsOnlyEnv = buildDepsOnlyEnv =
let let
rocksdb' = rocksdb' = (rocksdb.override {
(rocksdb.override { jemalloc = lib.optional (featureEnabled "jemalloc") rust-jemalloc-sys';
jemalloc = lib.optional (featureEnabled "jemalloc") rust-jemalloc-sys'; # rocksdb fails to build with prefixed jemalloc, which is required on
# rocksdb fails to build with prefixed jemalloc, which is required on # darwin due to [1]. In this case, fall back to building rocksdb with
# darwin due to [1]. In this case, fall back to building rocksdb with # libc malloc. This should not cause conflicts, because all of the
# libc malloc. This should not cause conflicts, because all of the # jemalloc symbols are prefixed.
# jemalloc symbols are prefixed. #
# # [1]: https://github.com/tikv/jemallocator/blob/ab0676d77e81268cd09b059260c75b38dbef2d51/jemalloc-sys/src/env.rs#L17
# [1]: https://github.com/tikv/jemallocator/blob/ab0676d77e81268cd09b059260c75b38dbef2d51/jemalloc-sys/src/env.rs#L17 enableJemalloc = featureEnabled "jemalloc" && !stdenv.hostPlatform.isDarwin;
enableJemalloc = featureEnabled "jemalloc" && !stdenv.hostPlatform.isDarwin;
# for some reason enableLiburing in nixpkgs rocksdb is default true # for some reason enableLiburing in nixpkgs rocksdb is default true
# which breaks Darwin entirely # which breaks Darwin entirely
inherit enableLiburing; inherit enableLiburing;
}).overrideAttrs }).overrideAttrs (old: {
(old: { inherit enableLiburing;
inherit enableLiburing; cmakeFlags = (if x86_64_haswell_target_optimised then
cmakeFlags = (lib.subtractLists [
( # dont make a portable build if x86_64_haswell_target_optimised is enabled
if x86_64_haswell_target_optimised then "-DPORTABLE=1"
( ]
lib.subtractLists [ old.cmakeFlags
# dont make a portable build if x86_64_haswell_target_optimised is enabled ++ [ "-DPORTABLE=haswell" ]) else [ "-DPORTABLE=1" ]
"-DPORTABLE=1" )
] ++ old.cmakeFlags;
old.cmakeFlags
++ [ "-DPORTABLE=haswell" ]
)
else
[ "-DPORTABLE=1" ]
)
++ old.cmakeFlags;
# outputs has "tools" which we dont need or use # outputs has "tools" which we dont need or use
outputs = [ "out" ]; outputs = [ "out" ];
# preInstall hooks has stuff for messing with ldb/sst_dump which we dont need or use # preInstall hooks has stuff for messing with ldb/sst_dump which we dont need or use
preInstall = ""; preInstall = "";
}); });
in in
{ {
# https://crane.dev/faq/rebuilds-bindgen.html # https://crane.dev/faq/rebuilds-bindgen.html
@ -140,32 +120,27 @@ let
ROCKSDB_INCLUDE_DIR = "${rocksdb'}/include"; ROCKSDB_INCLUDE_DIR = "${rocksdb'}/include";
ROCKSDB_LIB_DIR = "${rocksdb'}/lib"; ROCKSDB_LIB_DIR = "${rocksdb'}/lib";
} }
// (import ./cross-compilation-env.nix { //
(import ./cross-compilation-env.nix {
# Keep sorted # Keep sorted
inherit inherit
lib lib
pkgsBuildHost pkgsBuildHost
rust rust
stdenv stdenv;
;
}); });
buildPackageEnv = buildPackageEnv = {
{ GIT_COMMIT_HASH = inputs.self.rev or inputs.self.dirtyRev or "";
GIT_COMMIT_HASH = inputs.self.rev or inputs.self.dirtyRev or ""; GIT_COMMIT_HASH_SHORT = inputs.self.shortRev or inputs.self.dirtyShortRev or "";
GIT_COMMIT_HASH_SHORT = inputs.self.shortRev or inputs.self.dirtyShortRev or ""; } // buildDepsOnlyEnv // {
} # Only needed in static stdenv because these are transitive dependencies of rocksdb
// buildDepsOnlyEnv CARGO_BUILD_RUSTFLAGS = buildDepsOnlyEnv.CARGO_BUILD_RUSTFLAGS
// { + lib.optionalString (enableLiburing && stdenv.hostPlatform.isStatic)
# Only needed in static stdenv because these are transitive dependencies of rocksdb " -L${lib.getLib liburing}/lib -luring"
CARGO_BUILD_RUSTFLAGS = + lib.optionalString x86_64_haswell_target_optimised
buildDepsOnlyEnv.CARGO_BUILD_RUSTFLAGS " -Ctarget-cpu=haswell";
+ lib.optionalString };
(
enableLiburing && stdenv.hostPlatform.isStatic
) " -L${lib.getLib liburing}/lib -luring"
+ lib.optionalString x86_64_haswell_target_optimised " -Ctarget-cpu=haswell";
};
commonAttrs = { commonAttrs = {
inherit inherit
@ -173,46 +148,40 @@ let
cargoToml = "${inputs.self}/Cargo.toml"; cargoToml = "${inputs.self}/Cargo.toml";
}) })
pname pname
version version;
;
src = src = let filter = inputs.nix-filter.lib; in filter {
let root = inputs.self;
filter = inputs.nix-filter.lib;
in
filter {
root = inputs.self;
# Keep sorted # Keep sorted
include = [ include = [
".cargo" ".cargo"
"Cargo.lock" "Cargo.lock"
"Cargo.toml" "Cargo.toml"
"src" "src"
]; ];
}; };
doCheck = true; doCheck = true;
cargoExtraArgs = cargoExtraArgs = "--no-default-features --locked "
"--no-default-features --locked " + lib.optionalString
+ lib.optionalString (features'' != [ ]) "--features " (features'' != [ ])
+ (builtins.concatStringsSep "," features''); "--features " + (builtins.concatStringsSep "," features'');
dontStrip = profile == "dev" || profile == "test"; dontStrip = profile == "dev" || profile == "test";
dontPatchELF = profile == "dev" || profile == "test"; dontPatchELF = profile == "dev" || profile == "test";
buildInputs = buildInputs = lib.optional (featureEnabled "jemalloc") rust-jemalloc-sys'
lib.optional (featureEnabled "jemalloc") rust-jemalloc-sys'
# needed to build Rust applications on macOS # needed to build Rust applications on macOS
++ lib.optionals stdenv.hostPlatform.isDarwin [ ++ lib.optionals stdenv.hostPlatform.isDarwin [
# https://github.com/NixOS/nixpkgs/issues/206242 # https://github.com/NixOS/nixpkgs/issues/206242
# ld: library not found for -liconv # ld: library not found for -liconv
libiconv libiconv
# https://stackoverflow.com/questions/69869574/properly-adding-darwin-apple-sdk-to-a-nix-shell # https://stackoverflow.com/questions/69869574/properly-adding-darwin-apple-sdk-to-a-nix-shell
# https://discourse.nixos.org/t/compile-a-rust-binary-on-macos-dbcrossbar/8612 # https://discourse.nixos.org/t/compile-a-rust-binary-on-macos-dbcrossbar/8612
pkgsBuildHost.darwin.apple_sdk.frameworks.Security pkgsBuildHost.darwin.apple_sdk.frameworks.Security
]; ];
nativeBuildInputs = [ nativeBuildInputs = [
# bindgen needs the build platform's libclang. Apparently due to "splicing # bindgen needs the build platform's libclang. Apparently due to "splicing