neoidiosyn/stdenv.nix
2024-11-20 14:55:01 +01:00

53 lines
2.1 KiB
Nix

{ lib, pkgs, ... }: { ... }@attrs: let
stdenv = attrs.stdenv or attrs.baseStdenv;
pkgs' = attrs.pkgs or attrs.buildPackages;
inherit (pkgs'.stdenv.cc) isClang;
inherit (pkgs'.stdenv.cc.bintools) isLLVM;
in pkgs.addAttrsToDerivation (prevAttrs: let
inherit (lib) optionals optionalAttrs toList;
inherit (stdenv) targetPlatform;
ffpContract = prevAttrs.ffpContract or true;
autoVarInit = prevAttrs.autoVarInit or null;
boundsCheck = prevAttrs.boundsCheck or false;
overrideAlloc = prevAttrs.overrideAlloc or true;
inputs = optionals overrideAlloc [ pkgs.mimalloc ];
cflags = [
"-pipe"
"-fno-semantic-interposition"
]
++ optionals (isClang && ffpContract) [ "-ffp-contract=fast-honor-pragmas" ]
++ optionals isLLVM [ "-flto" ]
++ optionals (autoVarInit != null) [ "-ftrivial-auto-var-init=${autoVarInit}" ]
++ optionals boundsCheck [ "-fsanitize-trap=bounds,object-size,vla-bound" ];
ldflags = [ "-O2" "--hash-style=gnu" ]
++ optionals isLLVM [ "--icf=safe" "--lto-O2" ]
++ optionals overrideAlloc [ "-lmimalloc" ];
rustflags = [
"-C" "opt-level=2"
"-C" "linker-flavor=ld.lld"
"-C" "lto"
"-C" "linker-plugin-lto"
"-C" "target-cpu=${targetPlatform.gcc.arch}"
] ++ (map (flag: [ "-C" "link-arg=${flag}" ]) ldflags |> lib.flatten);
goflags = [ "-ldflags=-linkmode=external" ];
in {
buildInputs = prevAttrs.buildInputs or [ ] ++ inputs;
env = prevAttrs.env or { } // optionalAttrs (!prevAttrs ? NIX_CFLAGS_COMPILE) {
NIX_CFLAGS_COMPILE = toList prevAttrs.env.NIX_CFLAGS_COMPILE or [ ] ++ cflags |> toString;
} // optionalAttrs (prevAttrs ? env.NIX_LDFLAGS) {
NIX_LDFLAGS = toList prevAttrs.env.NIX_LDFLAGS or [ ] ++ ldflags |> toString;
};
NIX_RUSTFLAGS = prevAttrs.NIX_RUSTFLAGS or [ ] ++ rustflags;
GOFLAGS = prevAttrs.GOFLAGS or [ ] ++ goflags;
} // optionalAttrs (prevAttrs ? NIX_CFLAGS_COMPILE) {
NIX_CFLAGS_COMPILE = toList prevAttrs.NIX_CFLAGS_COMPILE or [ ] ++ cflags;
} // optionalAttrs (!prevAttrs ? env.NIX_LDFLAGS) {
NIX_LDFLAGS = lib.toList prevAttrs.NIX_LDFLAGS or [ ] ++ ldflags;
}) stdenv