Package overriding 1

A powerful feature if Nix is the option to override any packages inputs (docs).

Let's use this to compare multiple builds of our benchmark application. Create the following multibench_v1.nix shell file:

{ pkgs ? import <nixpkgs> { }
, lib ? pkgs.lib
}:

let
  args = lib.cartesianProductOfSets {
    cxxstd = [ "11" "20" ];
    stdenv = [ pkgs.gcc13Stdenv pkgs.llvmPackages_17.stdenv ];
  };

  overrideBoost = { cxxstd, stdenv }: {
    name = "std${cxxstd}-${stdenv.cc.name}";
    stdenv = stdenv;
    boost = pkgs.boost.override {
      extraB2Args = [ "cxxstd=${cxxstd}" ];
      stdenv = stdenv;
    };
  };

  boostList = map overrideBoost args;

  customizeBench = { name, stdenv, boost }:
    pkgs.writeShellApplication {
      name = "bench-${name}";
      runtimeInputs = [
        (pkgs.callPackage ./bench.nix {
          inherit stdenv boost;
        })
      ];
      text = "bench";
    };
in

pkgs.mkShell rec {
  packages = map customizeBench boostList;

  shellHook =
    let
      runTest = test: ''
        echo "Running ${test.name}:"
        ${test.name}
      '';
    in
    lib.concatMapStringsSep "\n" runTest packages;
}

And test it:

nix-shell multibench_v1.nix

bench-std11-clang-wrapper-17.0.6
bench-std20-gcc-wrapper-13.2.0