Qtbase with NIX_CFLAGS_COMPILE overlay in stdenv

Similar to my previous thread, I have the following overlay to my configuration, which qtbase fails to build with.

(
  self: super: {
    .
    .
    .
    stdenv = super.stdenv // {
      mkDerivation = args: super.stdenv.mkDerivation (
        args // {
          NIX_CFLAGS_COMPILE = toString (args.NIX_CFLAGS_COMPILE or "") + " -pipe - march=skylake -O3";
        }
      );
    };
    .
    .
    .
  }
)

qtbase’s derivation has this in its env:

"NIX_CFLAGS_COMPILE": "-Wno-error=sign-compare -DNIXPKGS_QTCOMPOSE=\"/nix/store/myn5q5p002n997p2g19x4mf01cjwp1w2-libX11-1.6.12/share/X11/locale\" -DLIBRESOLV_SO=\"/nix/store/iz8q8dbqxkhsmjn9mf86wmrxj8bfpiyd-glibc-2.31/lib/libresolv\" -DNIXPKGS_LIBXCURSOR=\"/nix/store/d4440walblw18jayilyg6iy9zi7xjx2y-libXcursor-1.2.0/lib/libXcursor\" -DNIXPKGS_MESA_GL=\"/nix/store/awldp19kp9d8i1kbnc87h5md0n4h40h2-libGL-1.3.2/lib/libGL\" -DNIXPKGS_QGTK3_XDG_DATA_DIRS=\"/nix/store/b9n6f0hpmgg3ajwq19mdgp60fs9fqlpv-gtk+3-3.24.21/share/gsettings-schemas/gtk+3-3.24.21\" -DNIXPKGS_QGTK3_GIO_EXTRA_MODULES=\"/nix/store/a6xb7whs613zf2d4rsda9r4c9jddgy4p-dconf-0.36.0-lib/lib/gio/modules\" -pipe -march=skylake -O3"

Qtbase is provided by qt5.qtbase defined here. After spending days trying different things, I have been unable to stop -O3 from getting added to qtbase.

Would an overlay like this work?

(
  self: super: {
    qt5.qtbase = super.qt5.qtbase.overrideAttrs (oa: {
      NIX_CFLAGS_COMPILE = oa.NIX_CFLAGS_COMPILE + " -O2";
    });
  }
)

You can check what’s being used with

nix eval nixpkgs.qt5.qtbase.NIX_CFLAGS_COMPILE

I think I had tried this before. Here’s what I get:

$ sudo nix-instantiate '<nixpkgs/nixos>' -A system --show-trace
error: while evaluating the attribute 'activationScript' of the derivation 'nixos-system-illustris-thinkpad-20.09.2016.19db3e5ea27' at /nix/var/nix/profiles/per-user/root/channels/nixos/nixos/modules/system/activation/top-level.nix:95:5:
while evaluating the attribute 'system.activationScripts.script' at /nix/var/nix/profiles/per-user/root/channels/nixos/nixos/modules/system/activation/activation-script.nix:68:9:
while evaluating 'textClosureMap' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/strings-with-deps.nix:70:35, called from /nix/var/nix/profiles/per-user/root/channels/nixos/nixos/modules/system/activation/activation-script.nix:89:18:
while evaluating 'id' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/trivial.nix:14:5, called from undefined position:
while evaluating the attribute 'text' at /nix/var/nix/profiles/per-user/root/channels/nixos/nixos/modules/system/activation/activation-script.nix:9:5:
while evaluating the attribute 'text' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/strings-with-deps.nix:77:38:
while evaluating the attribute 'sources' of the derivation 'etc' at /nix/var/nix/profiles/per-user/root/channels/nixos/nixos/modules/system/etc/etc.nix:12:5:
while evaluating anonymous function at /nix/var/nix/profiles/per-user/root/channels/nixos/nixos/modules/system/etc/etc.nix:20:20, called from undefined position:
while evaluating the attribute 'source' at undefined position:
while evaluating 'g' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/attrsets.nix:276:19, called from undefined position:
while evaluating anonymous function at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:98:72, called from /nix/var/nix/profiles/per-user/root/channels/nixos/lib/attrsets.nix:279:20:
while evaluating the attribute 'value' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:465:9:
while evaluating the option `environment.etc.dbus-1.source':
while evaluating the attribute 'mergedValue' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:497:5:
while evaluating anonymous function at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:499:17, called from /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:499:12:
while evaluating 'check' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/types.nix:256:15, called from /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:499:22:
while evaluating the attribute 'serviceDirectories' of the derivation 'dbus-1' at /nix/var/nix/profiles/per-user/root/channels/nixos/pkgs/build-support/trivial-builders.nix:7:7:
while evaluating anonymous function at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/types.nix:265:14, called from undefined position:
while evaluating the attribute 'value' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:510:27:
while evaluating anonymous function at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:499:17, called from /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:499:12:
while evaluating 'check' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/types.nix:256:15, called from /nix/var/nix/profiles/per-user/root/channels/nixos/lib/modules.nix:499:22:
while evaluating the attribute 'passAsFile' of the derivation 'system-path' at /nix/var/nix/profiles/per-user/root/channels/nixos/pkgs/build-support/trivial-builders.nix:7:7:
while evaluating the attribute 'out.outPath' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/customisation.nix:156:13:
while evaluating the attribute 'buildInputs' of the derivation 'virt-manager-2.2.1' at /nix/var/nix/profiles/per-user/root/channels/nixos/pkgs/development/interpreters/python/mk-python-derivation.nix:108:5:
while evaluating the attribute 'propagatedBuildInputs' of the derivation 'spice-gtk-0.37' at /nix/var/nix/profiles/per-user/root/channels/nixos/pkgs/stdenv/generic/make-derivation.nix:192:11:
while evaluating the attribute 'buildInputs' of the derivation 'gst-plugins-good-1.16.2' at /nix/var/nix/profiles/per-user/root/channels/nixos/pkgs/stdenv/generic/make-derivation.nix:192:11:
while evaluating 'getOutput' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/attrsets.nix:464:23, called from undefined position:
while evaluating anonymous function at /nix/var/nix/profiles/per-user/root/channels/nixos/pkgs/stdenv/generic/make-derivation.nix:143:17, called from undefined position:
while evaluating 'addMetaAttrs' at /nix/var/nix/profiles/per-user/root/channels/nixos/lib/meta.nix:15:28, called from /nix/var/nix/profiles/per-user/root/channels/nixos/pkgs/top-level/all-packages.nix:14107:12:
attribute 'callPackage' missing, at /nix/var/nix/profiles/per-user/root/channels/nixos/pkgs/top-level/all-packages.nix:18577:15

The line mentioned in the error is pkgs/top-level/all-packages.nix:18577:

v4l-utils = qt5.callPackage ../os-specific/linux/v4l-utils { };

I’m not sure if I’m understanding this correctly, but it looks like overriding qt5.qtbase removes callPackage in qt5.

qt5.callPackage also appears to be the same as callPackage

nix-repl> pkgs = import <nixpkgs> {}

nix-repl> pkgs.callPackage
«lambda @ /nix/var/nix/profiles/per-user/root/channels/nixos/lib/customisation.nix:117:31»

nix-repl> pkgs.qt5.callPackage
«lambda @ /nix/var/nix/profiles/per-user/root/channels/nixos/lib/customisation.nix:117:31»

Ah you’re right, maybe this will work?

(
  self: super: {
    qt5 = super.qt5 // {
      qtbase = super.qt5.qtbase.overrideAttrs (oa: {
        NIX_CFLAGS_COMPILE = oa.NIX_CFLAGS_COMPILE + " -O2";
      });
    };
  }
)

But if you’re modifying nixpkgs this extensively, it will probably be easier to use a modified nixpkgs directly rather than try to make the changes you need via overlays

Thanks! This made some progress.
Instead of appending O2 (which would be ignored in favor of the O3 already present), I replaced it with the value it would otherwise have

qt5 = super.qt5 //{
    qtbase = super.qt5.qtbase.overrideAttrs (oa: {
        NIX_CFLAGS_COMPILE = "-Wno-error=sign-compare -DNIXPKGS_QTCOMPOSE=\"/nix/store/myn5q5p002n997p2g19x4mf01cjwp1w2-libX11-1.6.12/share/X11/locale\" -DLIBRESOLV_SO=\"/nix/store/iz8q8dbqxkhsmjn9mf86wmrxj8bfpiyd-glibc-2.31/lib/libresolv\" -DN............. -march=skylake"
    });
};

This partially works. Now there are two instances of qtbase-5.15.0.drv, one with “-march=skylake -O3” and the other with “-march=skylake”.
The immediate parents of the O3 one are:

/nix/store/mcs6i4dz5pbgj787p4hj13k35dbmcxps-qtbase-5.15.0.drv
NAR Size: 15.07 KiB
Closure Size: 3.15 MiB
Immediate Parents (6): hook.drv, qtdeclarative-5.15.0.drv, qtsvg-5.15.0.drv, pinentry-1.1.0.drv, hook.drv, qtwayland-5.15.0.drv

and the one without O3:

/nix/store/0aj5qb08m7s2rwb05p24azafx64bw85h-qtbase-5.15.0.drv
NAR Size: 14.85 KiB
Closure Size: 3.15 MiB
Immediate Parents (1): rescuetime-2.16.3.1.drv

I think rescuetime is referring to qtbase in a different way than pinentry (added by programs.gnupg.agent.enable = true;) and other packages.

Also, by modifying nixpkgs do you mean editing /nix/var/nix/profiles/per-user/...?

I mean git cloning the nixpkgs repo, making the changes you need locally, and setting export NIX_PATH=nixpkgs=/path/to/your/checkout so that nix commands use your local clone instead of whatever channel you are currently using.