Skip to content

cmake setup hook: Allow passing environment variables to cmake invocation #79302

@nh2

Description

@nh2

Some CMake settings cann only be properly set using environment variables, such as CC, CXX, CFLAGS and CXXFLAGS. CMake has no CLI argument to do that instead; it is designed to take this from environment variables.

In nixpkgs we don't really support that. You can do e.g.

{
  preConfigure = ''
    export CLFAGS="-march=haswell"
  '';
}

or

{
  CXXFLAGS = "-march=haswell";
}

but this will pollute the entire build environment, instead of being given specifically to CMake's configure phase.

NIX_CFLAGS_COMPILE = "-march=haswell"; is a workaround, but it is a hack that sneaks flags past cmake directly to the compiler, and I think it should be avoided (it has various drawbacks, such as CMake not seeing what your compiler flag wishes are; see also #77979 (comment)).

In general I think that CXXFLAGS = "-march=haswell"; is fine for most cases but there may be builds that require it not polluting the environment.

Proposal

I propose that we replace

cmake ${cmakeDir:-.} $cmakeFlags "${cmakeFlagsArray[@]}"

by something like

"${cmakeEnvVarsArray[@]}" cmake ${cmakeDir:-.} $cmakeFlags "${cmakeFlagsArray[@]}"

to allow using CMake as it is intended.

CC @ttuegel @LnL7

Metadata

Metadata

Assignees

No one assigned

    Labels

    0.kind: enhancementAdd something new or improve an existing system.2.status: stalehttps://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions