idiosyn/stdenv.nix
2024-07-25 22:09:04 +02:00

54 lines
1.8 KiB
Nix

pkgs: stdenv:
let
inherit (pkgs) addAttrsToDerivation;
inherit (pkgs.lib) optionalAttrs optionals toList;
inherit (stdenv) buildPlatform hostPlatform;
cflagsC = [
"-pipe" # Prefer pipes over temporary files between stages
"-O2" # Safe compiler optimisations
] ++ optionals buildPlatform.useLLVM [
"-flto=thin"
];
ldflags = [
"-O2" # Enable tail merging of strings
"--hash-style=gnu" # Produce only DT_GNU_HASH
] ++ optionals buildPlatform.useLLVM [
"--icf=safe" # Fold identical code where safe
"--lto-O2"
"--pack-dyn-relocs=relr"
];
cflagsL = map (flag: "-Wl,${flag}") ldflags;
rustflags = [
"-C opt-level=2"
] ++ optionals buildPlatform.useLLVM [
"-C linker-flavor=lld"
"-C lto=thin"
"-C linker-plugin-lto"
] ++ optionals (hostPlatform.isx86_64 && hostPlatform ? gcc.arch) [
"-C target-cpu=${hostPlatform.gcc.arch}"
] ++ map (flag: "-C link-arg=${flag}") ldflags;
in addAttrsToDerivation (base: {
env = (base.env or { }) // optionalAttrs (!base ? NIX_CFLAGS_COMPILE) {
NIX_CFLAGS_COMPILE =
toString (toList base.env.NIX_CFLAGS_COMPILE or [ ] ++ cflagsC);
} // optionalAttrs (base ? env.NIX_CFLAGS_LINK) {
NIX_CFLAGS_LINK =
toString (toList base.NIX_CFLAGS_LINK or [ ] ++ cflagsL);
} // 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 [ ] ++ cflagsC;
} // optionalAttrs (!base ? env.NIX_CFLAGS_LINK) {
NIX_CFLAGS_LINK = toList base.NIX_CFLAGS_LINK or [ ] ++ cflagsL;
} // optionalAttrs (!base ? env.NIX_LDFLAGS) {
NIX_LDFLAGS = toList base.NIX_LDFLAGS or [ ] ++ ldflags;
}) stdenv