Created
December 3, 2024 04:29
-
-
Save getchoo/df684f6b14d2cc79a4a6e25c374abd6d to your computer and use it in GitHub Desktop.
Demo of using optimized CFLAGS with Nixpkgs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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