Skip to content

Instantly share code, notes, and snippets.

@getchoo
Created December 3, 2024 04:29
Show Gist options
  • Save getchoo/df684f6b14d2cc79a4a6e25c374abd6d to your computer and use it in GitHub Desktop.
Save getchoo/df684f6b14d2cc79a4a6e25c374abd6d to your computer and use it in GitHub Desktop.
Demo of using optimized CFLAGS with Nixpkgs
let
pkgs = import <nixpkgs> {
config = { };
overlays = [ ];
};
inherit (pkgs) lib;
# Packages we want to build optimized versions of
packages = lib.getAttrs [
"hello" # Test package
"jdk8"
"jdk17"
"jdk21"
] pkgs;
# Microarchitectures we want to optimize for
microarchitectures = lib.attrNames lib.systems.architectures.features;
# Do something for each microarchitecture
forAllMicroarchitectures = lib.genAttrs microarchitectures;
# Create an optimized stdenv for each microarchitecture
stdenvFor = forAllMicroarchitectures (
march:
pkgs.stdenvAdapters.withCFlags [
"-O3"
"-march=${march}"
"-mtune=${march}"
] pkgs.stdenv
);
# Override a given package with the stdenv optimized for the given microarchitecture
applyOptimizations = march: package: package.override { stdenv = stdenvFor.${march}; };
in
# For each microarchitecture
forAllMicroarchitectures (
march:
# Apply optimizations for each package
lib.mapAttrs (lib.const (applyOptimizations march)) packages
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment