Skip to content

Remove NIX_CFLAGS_COMPILE where possible #79303

Open

Description

@nh2

Problem

NIX_CFLAGS_COMPILE is a low-level hack that sneaks compiler arguments past packages' build systems.

It has various drawbacks:

  • the build system cannot see the flag, thus intended upstream logic in the build system is potentially not activated
  • generated files, such as pkg-config .pc files, may lack the necessary entries, making problems for downstream packages
  • the flag does not show up in the build logs, making it unclear what's happening

Because many packages use NIX_CFLAGS_COMPILE, more packages get it added when people copy this style.

Solution

It is better to pass CFLAGS, CXXFLAGS and so on directly to the build system via the method intended to pass compiler flags, for example (feel free to edit this to add more build systems):

Delivery

I think we should run a nixpkgs-wide sprint (like #79064) to do this switch for as many packages as possible.

We should also update the documentation to recommend better alternatives to this hack (like above).

Activity

  1. added
    3.skill: sprintable
    on Feb 6, 2020
  2. changed the title [-]Get rid of NIX_CFLAGS_COMPILE where possible[/-] [+]Remove NIX_CFLAGS_COMPILE where possible[/+] on Feb 6, 2020
  3. jtojnar commented on Feb 6, 2020

    @jtojnar
    Member

    Note that many of these are actually upstream bugs so in addition to changing the expressions, we should report the issues. See, for example, #36468

  4. added
    9.needs: clean-up
    on Feb 6, 2020
  5. added this to the 20.09 milestone on Feb 6, 2020
  6. stale commented on Aug 4, 2020

    @stale
  7. added
    2.status: stale
    on Aug 4, 2020
  8. nh2 commented on Aug 12, 2020

    @nh2
    Author
  9. removed
    2.status: stale
    on Aug 12, 2020
  10. blaggacao commented on Nov 2, 2020

    @blaggacao
    Contributor

    I was just about to copy one such instance of NIX_CFLAGS_COMPILE it is so absolutely widespread that it appears to be a "dominant" pattern.

    $ rg 'NIX_CFLAGS_COMPILE' | wc -l
    705
  11. jtojnar commented on Nov 2, 2020

    @jtojnar
    Member

    @blaggacao Yes, it is a very common hack. It would still be better if maintainers went upstream for their packages and opened issues or merge requests with a proper fix, like we do with #36468.

  12. nh2 commented on Nov 2, 2020

    @nh2
    ContributorAuthor

    Often it's also not an upstream problem but straight-out unnecessarily done in nixpkgs, as described in the Solution section in the issue description.

  13. blaggacao commented on Nov 2, 2020

    @blaggacao
    Contributor

    @nh2 Would you want to do a regex mass commit (quickly) that at least prepends the ocurrances with a comment and link to this issue? That would at least stop further propagation. Good/bad idea?

  14. nh2 commented on Nov 2, 2020

    @nh2
    ContributorAuthor

    @blaggacao That sounds like a great idea to me!

  15. edolstra commented on Nov 2, 2020

    @edolstra
    Member

    I'm not convinced getting rid of NIX_CFLAGS_COMPILE is a good idea. NIX_CFLAGS_COMPILE is consistent and reliable, unlike figuring out the magic incantation (often silently ignored) to tell the build system to pass a flag to the C compiler.

  16. 33 remaining items

  17. allsey87 commented on Apr 29, 2024

    @allsey87

    While I understand the arguments for not using NIX_CFLAGS_COMPILE in recipes, I do not think it should be removed since it provides a way of consistently applying flags across all libraries and binaries during cross compilation (for example, march etc).

  18. nh2 commented on Apr 29, 2024

    @nh2
    ContributorAuthor

    While I understand the arguments for not using NIX_CFLAGS_COMPILE in recipes, I do not think it should be removed

    I don't think anybody is suggesting for this feature to be removed.

    The idea is that if there's a clean way to pass an option to the build system, that should be the default.

  19. LunNova commented on Oct 12, 2024

    @LunNova
    Member

    Is there a correct way to disable includes getting added to NIX_CFLAGS_COMPILE for buildInputs in a stdenv or in a derivation?

    I'm working on packaging rocm 6.2.2. I'd like to turn off the automatic include paths since cmake and pkg-config should be handling all that anyway - in practice we just get a giant pile of duplicate includes passed to clang.

    So far I didn't find the correct way to turn it off. I'm assuming there is one since nix hook stuff is pretty flexible and I'm just doing it wrong.

    Adding this to the compiler wrapper allows turning it off for that specific wrapper but it's super jank :(

    substituteInPlace $out/nix-support/setup-hook --replace-fail '1/include"' '1/include-DONOTUSE"'
    
  20. gregorburger commented on Jun 16, 2025

    @gregorburger

    @LunNova have you found out anything about that. I'm currently facing a problem in a project where both Qt5 and Qt6 are used. Qt has include files that are called the same in Qt5 and Qt6, but there is no way to segregate the include paths. Whichever is first in the NIX_CFLAGS_COMPILE is picked and results in compile errors.

  21. 7c6f434c commented on Jun 16, 2025

    @7c6f434c
    Member

    Maybe you shouldn't add neither Qt5 nor Qt6 to buildInputs and pass the paths manually where needed?

  22. added
    2.status: stale
    on Dec 13, 2025
  23. removed
    2.status: stale
    on Dec 24, 2025
  24. added
    2.status: stale
    on Dec 24, 2025
  25. ju1m commented on Jun 26, 2026

    @ju1m
    Contributor

    If it can help others, to add flags to BINDGEN_EXTRA_CLANG_ARGS, NIX_CFLAGS_COMPILE is the only envvar proposed:

    BINDGEN_EXTRA_CLANG_ARGS="$(< @clang@/nix-support/cc-cflags) $(< @clang@/nix-support/libc-cflags) $(< @clang@/nix-support/libcxx-cxxflags) $NIX_CFLAGS_COMPILE"

    But this can be done with:

    preConfigure = ''
      BINDGEN_EXTRA_CLANG_ARGS+=" $(pkg-config opus --cflags)"
    '';

    Besides, I am not able to check with NIX_DEBUG=1, but $NIX_CFLAGS_COMPILE seems to be appended twice

    1. in BINDGEN_EXTRA_CLANG_ARGS in populateBindgenEnv (as seen above);
    2. and in bindgen's command line arguments:
      exec -a "$0" @unwrapped@/bin/bindgen "$@" $sep $cxxflags @cincludes@ $NIX_CFLAGS_COMPILE
  26. removed
    2.status: stale
    on Jun 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.skill: sprintable9.needs: clean-up

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions