haskellPackages.crypton Build Failing with nixpkgs.hostPlatform.gcc.arch and tune set

I am trying to build my whole system for my CPU architecture as per Build flags - NixOS Wiki . In my configuration this looks like:

  nixpkgs.hostPlatform = { # If you get errors about not having build featu>
    gcc.arch = "icelake-client";
    gcc.tune = "icelake-client";
    system = "x86_64-linux";
  };

This makes the package haskellPackages.crypton fail to build – no idea why. It says the tests suite has failed, despite individual tests all passing. Here is the build log (single package build but it encounters the same issue when I nixos-rebuild) below for reference although it is not necessarily relevant. I am on the unstable channel btw.

build.nix
let
  pkgs = import <nixpkgs> {
    localSystem = {
      gcc.arch = "icelake-client";
      gcc.tune = "icelake-client";
      system = "x86_64-linux";
    };
  };
in
   pkgs.haskellPackages.crypton
Truncated Build Log for haskellPackages.crypton (build.nix)
Running phase: checkPhase
Running 1 test suites... 

...

Test suite logged to: dist/test/crypton-0.34-test-crypton.log
0 of 1 test suites (0 of 1 test cases) passed.
error: builder for '/nix/store/zq8b354v159v0ck66ms7pv03iazsx48f-crypton-0.34.drv' failed with exit code 1;
       last 25 log lines:
       >       2:                                     OK
       >       3:                                     OK
       >       4:                                     OK
       >   Ed448
       >     gen secretkey:                           OK
       >     gen publickey
       >       0:                                     OK
       >       1:                                     OK
       >       2:                                     OK
       >       3:                                     OK
       >       4:                                     OK
       >       5:                                     OK
       >       6:                                     OK
       >       7:                                     OK
       >     gen signature
       >       0:                                     OK
       >       1:                                     OK
       >       2:                                     OK
       >       3:                                     OK
       >       4:                                     OK
       >       5:                                     OK
       >       6:                                     OK
       >       7:                                     Test suite test-crypton: FAIL
       > Test suite logged to: dist/test/crypton-0.34-test-crypton.log
       > 0 of 1 test suites (0 of 1 test cases) passed.
       For full logs, run 'nix-store -l /nix/store/zq8b354v159v0ck66ms7pv03iazsx48f-crypton-0.34.drv'.

It is not important to build this one particular package for my exact architecture when almost everything else works fine (although sometimes builds fail for no apparent reason, and I have to build them on their own with nix-build similar to the crypton build.nix example above, before I can continue with my nixos-rebuild).

I had an issue with gsl and was able to build it for skylake instead of icelake-client using this overlay:

  nixpkgs.overlays = [
    (self: super: {
      gsl = super.gsl.overrideDerivation (oldAttrs: {
        NIX_CFLAGS_COMPILE = pkgs.lib.concatStringsSep " " [
          "-march=skylake"
          "-mtune=icelake-client"
          (oldAttrs.NIX_CFLAGS_COMPILE or "")
        ];
      });
    })
  ];

I could try something similar with crypton:

  nixpkgs.overlays = [
    (self: super: {
      haskellPackages = super.haskellPackages.extend (hself: hsuper: {
        crypton = self.haskell.lib.compose.overrideCabal (oldAttrs: {
          NIX_CFLAGS_COMPILE = pkgs.lib.concatStringsSep " " [
            "-march=skylake"
            "-mtune=icelake-client"
            (oldAttrs.NIX_CFLAGS_COMPILE or "")
          ];
        }) hsuper.crypton;
      });
    })
  ];

but that results in an error (below), probably because NIX_CFLAGS_COMPILE isn’t an attribute in the derivation for haskellPackages.crypton.

Error
error:
       … while calling the 'head' builtin
         at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/attrsets.nix:1:35741:
       … while evaluating the attribute 'value'
         at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:1:34707:
       … while evaluating the option `system.build.toplevel':

       … while evaluating definitions from `/nix/var/nix/profiles/per-user/root/channels/nixos/nixos/modules/system/activation/top-level.nix':

       … while evaluating the option `warnings':

       … while evaluating definitions from `/nix/var/nix/profiles/per-user/root/channels/nixos/nixos/modules/system/boot/systemd.nix':

       … while evaluating the option `systemd.services.libvirtd-config.serviceConfig':

       … while evaluating definitions from `/nix/var/nix/profiles/per-user/root/channels/nixos/nixos/modules/system/boot/systemd.nix':

       … while evaluating the option `systemd.services.libvirtd-config.script':

       … while evaluating definitions from `/nix/var/nix/profiles/per-user/root/channels/nixos/nixos/modules/virtualisation/libvirtd.nix':

       (stack trace truncated; use '--show-trace' to show the full, detailed trace)

       error: function 'anonymous lambda' called with unexpected argument 'NIX_CFLAGS_COMPILE'
       at /nix/store/djn9abzck18b8ihiyjjvzq17ykrqw4nk-nixos/nixos/pkgs/development/haskell-modules/generic-builder.nix:32:1:
           31|
           32| { pname
             | ^
           33| , dontStrip ? outputsJS

I was trying to come up with a solution using the configureFlags attribute with no luck – it seemed like there might be a way to pass -march flags to the compiler via GHC, although whether this would correctly override the ones stated by nixpkgs.hostPlatform is unclear. I wasn’t able to find any haskell documentation on this, and it seems like haskell packages are a bit convoluted in general to work with using Nix. I definitely just do not know enough.

I assume nixpkgs.hostPlatform gcc.arch and gcc.tune apply their changes in stdenv, so it would be good to be able to provide different settings on this level, using the nixpkgs.hostPlatform option, to certain packages that inevitably break with these settings. Set to a slighty older architecture to make them work, or to default to grab a package from the binary cache.

If anybody knows how to change the -march settings, exclude the package from the nixpkgs.hostPlatform settings, or fix whatever is causing the build to fail another way, I would really appreciate it.

It’s not an argument of the Haskell package builder, so you actually want to override the resulting mkDerivation call with .overrideAttrs.
(sorry for the roundabout, but usually one wants to override a Haskell package with the former)

  nixpkgs.overlays = [
    (self: super: {
      haskellPackages = super.haskellPackages.extend (hself: hsuper: {
        crypton =  hsuper.crypton.overrideAttrs (oldAttrs: {
          NIX_CFLAGS_COMPILE = self.lib.concatStringsSep " " [
            "-march=skylake"
            "-mtune=icelake-client"
          ];
        });
      });
    })
  ];

This seems to work because I get 129 Illegal instruction (core dumped) when trying to build it on my non-skylake machine.

Thank you for your help again!

For anybody else in the same situation, -march=skylake and -march=x86-64-v3 failed, but -march=x86-64-v2 allowed the package to build. -mtune did not seem to be the issue.