Home Explore Blog Models CI



nixpkgs

19th chunk of `doc/languages-frameworks/haskell.section.md`
06018586a7d45bed81a3fd87791d8f3787562058ad42f9720000000100000dbd
the package set from pulling in GHC from `prev`.

The result is two overlays like the ones shown below. Adjustable parts are
annotated with comments, as are any optional or alternative ways to achieve
the desired profiling settings without causing too many rebuilds.

<!-- TODO(@sternenseemann): buildHaskellPackages != haskellPackages with this overlay,
affected by https://github.com/NixOS/nixpkgs/issues/235960 which needs to be fixed
properly still.
-->

```nix
let
  # Name of the compiler and package set you want to change. If you are using
  # the default package set `haskellPackages`, you need to look up what version
  # of GHC it currently uses (note that this is subject to change).
  ghcName = "ghc910";
  # Desired new setting
  enableProfiling = true;

in
[
  # The first overlay modifies the GHC derivation so that it does or does not
  # build profiling versions of the core libraries bundled with it. It is
  # recommended to only use such an overlay if you are enabling profiling on a
  # platform that doesn't by default, because compiling GHC from scratch is
  # quite expensive.
  (
    final: prev:
    let
      inherit (final) lib;

    in
    {
      haskell = prev.haskell // {
        compiler = prev.haskell.compiler // {
          ${ghcName} = prev.haskell.compiler.${ghcName}.override {
            # Unfortunately, the GHC setting is named differently for historical reasons
            enableProfiledLibs = enableProfiling;
          };
        };
      };
    }
  )

  (
    final: prev:
    let
      inherit (final) lib;
      haskellLib = final.haskell.lib.compose;

    in
    {
      haskell = prev.haskell // {
        packages = prev.haskell.packages // {
          ${ghcName} = prev.haskell.packages.${ghcName}.override {
            overrides = hfinal: hprev: {
              mkDerivation =
                args:
                hprev.mkDerivation (
                  args
                  // {
                    # Since we are forcing our ideas upon mkDerivation, this change will
                    # affect every package in the package set.
                    enableLibraryProfiling = enableProfiling;

                    # To actually use profiling on an executable, executable profiling
                    # needs to be enabled for the executable you want to profile. You
                    # can either do this globally or…
                    enableExecutableProfiling = enableProfiling;
                  }
                );

              # …only for the package that contains an executable you want to profile.
              # That saves on unnecessary rebuilds for packages that you only depend
              # on for their library, but also contain executables (e.g. pandoc).
              my-executable = haskellLib.enableExecutableProfiling hprev.my-executable;

              # If you are disabling profiling to save on build time, but want to
              # retain the ability to substitute from the binary cache. Drop the
              # override for mkDerivation above and instead have an override like
              # this for the specific packages you are building locally and want
              # to make cheaper to build.
              my-library = haskellLib.disableLibraryProfiling hprev.my-library;
            };
          };
        };
      };
    }
  )
]
```

<!-- TODO(@sternenseemann): write overriding mkDerivation, overriding GHC, and
overriding the entire package set sections and link to them from here where
relevant.
-->


Title: Nixpkgs Overlays for Global Haskell Profiling Configuration
Summary
This chunk provides a detailed Nixpkgs overlay configuration for globally enabling or disabling Haskell profiling. It outlines a two-part approach: one overlay modifies the specified GHC compiler (e.g., `ghc910`) to build profiling versions of its core libraries via `enableProfiledLibs`, and a second overlay modifies the Haskell package set. This second overlay globally overrides `mkDerivation` to set `enableLibraryProfiling` and `enableExecutableProfiling` for all packages. The text also mentions more granular alternatives for `enableExecutableProfiling` on specific executables and `disableLibraryProfiling` for individual libraries to optimize build times and preserve binary cache substitution.