idiosyn/stdenv.nix
2024-07-29 23:22:45 +02:00

56 lines
1.8 KiB
Nix

pkgs: stdenv:
let
inherit (pkgs) addAttrsToDerivation mimalloc overrideCC;
inherit (pkgs.lib) getLib optionalAttrs optionals toList;
inherit (pkgs.llvmPackages_latest) clangUseLLVM bintools;
inherit (stdenv) hostPlatform targetPlatform;
cflags = [
"-pipe" # Prefer pipes over temporary files between stages
"-O2" # Safe compiler optimisations
"-flto"
];
ldflags = [
"-O2" # Enable tail merging of strings
"--hash-style=gnu" # Produce only DT_GNU_HASH
"--icf=safe" # Fold identical code where safe
"--lto-O2"
"-L${getLib mimalloc}/lib" "-lmimalloc"
];
rustflags = [
"-C opt-level=2"
"-C linker-flavor=lld"
"-C lto"
"-C linker-plugin-lto"
] ++ optionals (targetPlatform.isx86_64 && targetPlatform ? gcc.arch) [
"-C target-cpu=${targetPlatform.gcc.arch}"
] ++ map (flag: "-C link-arg=${flag}") ldflags;
pname = base:
if base ? pname
then "package ${base.pname}"
else "unknown package";
in addAttrsToDerivation (base:
if base ? idiosynStdenvGuard
then builtins.traceVerbose "stdenv override applied repeatedly for ${pname base}," { }
else {
idiosynStdenvGuard = null;
env = (base.env or { }) // optionalAttrs (!base ? NIX_CFLAGS_COMPILE) {
NIX_CFLAGS_COMPILE =
toString (toList base.env.NIX_CFLAGS_COMPILE or [ ] ++ cflags);
} // optionalAttrs (base ? env.NIX_LDFLAGS) {
NIX_LDFLAGS =
toString (toList base.env.NIX_LDFLAGS or [ ] ++ ldflags);
};
NIX_RUSTFLAGS = toList base.NIX_RUSTFLAGS or [ ] ++ rustflags;
} // optionalAttrs (base ? env.NIX_CFLAGS) {
NIX_CFLAGS_COMPILE = toList base.NIX_CFLAGS_COMPILE or [ ] ++ cflags;
} // optionalAttrs (!base ? env.NIX_LDFLAGS) {
NIX_LDFLAGS = toList base.NIX_LDFLAGS or [ ] ++ ldflags;
}) (overrideCC stdenv (clangUseLLVM.override { inherit bintools; }))