Repository navigation
Open
Labels
3.skill: sprintableA larger issue which is split into distinct actionable tasks9.needs: clean-upSomebody please clean up this mess!
Description
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
.pcfiles, 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):
- Autoconf: By setting environment variables like
CFLAGSbefore the./configureinvocation (e.g. by writingCFLAGS = "...";intomkDerivation) - CMake: By setting environment variables like
CFLAGSbefore thecmakeinvocation (see also cmake setup hook: Allow passing environment variables to cmake invocation #79302) - Meson:
-Dc_args(see also cinnamon.nemo: init at 4.4.1 #77979 (comment) and Report undeclared gio-unix-2.0 upstream #36468 (comment))
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).
Reacted by Oleh Stolyar, Bjørn Forsman, Dmitry Kalinkin, Jan Tojnar, Juliano Solanho, Moritz Hedtke, Bob van der Linden, Armeen Mahdian, Laurent Giroud, zopsicle and 10 more
Reacted by AleXoundOS, Xerz, Cathal and Phani Rithvij
Metadata
Metadata
Assignees
Labels
3.skill: sprintableA larger issue which is split into distinct actionable tasks9.needs: clean-upSomebody please clean up this mess!
Activity
[-]Get rid of NIX_CFLAGS_COMPILE where possible[/-][+]Remove NIX_CFLAGS_COMPILE where possible[/+]jtojnar commentedon Feb 6, 2020
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
stale commentedon Aug 4, 2020
nh2 commentedon Aug 12, 2020
blaggacao commentedon Nov 2, 2020
I was just about to copy one such instance of
NIX_CFLAGS_COMPILEit is so absolutely widespread that it appears to be a "dominant" pattern.jtojnar commentedon Nov 2, 2020
@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.
nh2 commentedon Nov 2, 2020
Often it's also not an upstream problem but straight-out unnecessarily done in nixpkgs, as described in the
Solutionsection in the issue description.blaggacao commentedon Nov 2, 2020
@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?
nh2 commentedon Nov 2, 2020
@blaggacao That sounds like a great idea to me!
edolstra commentedon Nov 2, 2020
I'm not convinced getting rid of
NIX_CFLAGS_COMPILEis a good idea.NIX_CFLAGS_COMPILEis 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.33 remaining items
allsey87 commentedon Apr 29, 2024
While I understand the arguments for not using
NIX_CFLAGS_COMPILEin 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,marchetc).nh2 commentedon Apr 29, 2024
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.
LunNova commentedon Oct 12, 2024
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 :(
gregorburger commentedon Jun 16, 2025
@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.
7c6f434c commentedon Jun 16, 2025
Maybe you shouldn't add neither Qt5 nor Qt6 to buildInputs and pass the paths manually where needed?
ju1m commentedon Jun 26, 2026
If it can help others, to add flags to
BINDGEN_EXTRA_CLANG_ARGS,NIX_CFLAGS_COMPILEis the only envvar proposed:nixpkgs/pkgs/build-support/rust/hooks/rust-bindgen-hook.sh
Line 9 in c43d9bd
But this can be done with:
Besides, I am not able to check with
NIX_DEBUG=1, but$NIX_CFLAGS_COMPILEseems to be appended twiceBINDGEN_EXTRA_CLANG_ARGSinpopulateBindgenEnv(as seen above);bindgen's command line arguments:nixpkgs/pkgs/development/tools/rust/bindgen/wrapper.sh
Line 32 in 410a3a0
fix(ox.nix): use CFLAGS for cflags
fix(ox.nix): use CFLAGS for cflags (#16565)
fix(ox.nix): use CFLAGS for cflags
fix(ox.nix): use CFLAGS for cflags
fix(ox.nix): use CFLAGS for cflags