cmakeFlags and spaces in option values

I’m trying to build a library which accepts custom flags via a CMake option. Unfortunately I hit a wall when I needed Nix to correctly tokenize cmakeFlags. I have essentialy the following in my derivation:

cmakeFlags = [
  "-DSOMELIB_CXX_FLAGS=\"-O3 -march=native\""

I have tried different escaping variants, but it seems that somewhere inside that string gets split at spaces, as I’m getting unknown argument -march=native" error from CMake. I haven’t found an analogous example in Nixpkgs itself or its manual.

How this problem can be fixed? I’d like to still retain the default CMake arguments.

1 Like

cmakeFlags are currently passed to CMake as follows:

so Bash will tokenize them on spaces and there is no way to prevent it.

The only alternative is adding the flag to cmakeFlagsArray in a Bash code. For example:

preConfigure = ''
    "-DSOMELIB_CXX_FLAGS=\"-O3 -march=native\""

In the future, this might be possible from Nix through __structuredAttrs.


Thank you very much for your answer! I’ll try the cmakeFlagsArray way.

EDIT: in what way could __structuredAttrs help here? I skimmed through that blog post earlier, but it seemed to me that it refers only to environment variables, isn’t it?

Currently, variables passed to derivation are passed to the builder through environment variables, which can only contain strings. __structuredAttrs will allow passing list of strings as well so we will no longer need to let bash tokenize them.

1 Like