Skip to content

cmake: setup-hooks: take structural cmakeEntries#442017

Open
ShamrockLee wants to merge 7 commits intoNixOS:stagingfrom
ShamrockLee:cmake-definitions
Open

cmake: setup-hooks: take structural cmakeEntries#442017
ShamrockLee wants to merge 7 commits intoNixOS:stagingfrom
ShamrockLee:cmake-definitions

Conversation

@ShamrockLee
Copy link
Contributor

@ShamrockLee ShamrockLee commented Sep 11, 2025

This PR provides structural access to the CMake flags specifying CMake variable cache entries.

If applied, we'll be able to do

{
  stdenv,
  cmake,
}:
stdenv.mkDerivation {
  __structuredAttrs = true;
  nativeBuildInputs = [ cmake ];
  cmakeEntries = {
    CMAKE_BUILD_TYPE = "RelWithDebInfo";
    WITH_PYTHON_BINDINGS = false;
  };
}

Aside from

{
  lib,
  stdenv,
  cmake,
}:
stdenv.mkDerivation {
  nativeBuildInputs = [ cmake ];
  cmakeFlags = [
    (lib.cmakeFeature "CMAKE_BUILD_TYPE" "RelWithDebInfo")
    (lib.cmakeBool "WITH_PYTHON_BINDINGS" false)
  ];
}

Things done

  • Built on platform:
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • Tested, as applicable:
  • Ran nixpkgs-review on this PR. See nixpkgs-review usage.
  • Tested basic functionality of all binary files, usually in ./result/bin/.
  • Nixpkgs Release Notes
    • Package update: when the change is major or breaking.
  • NixOS Release Notes
    • Module addition: when adding a new NixOS module.
    • Module update: when the change is significant.
  • Fits CONTRIBUTING.md, pkgs/README.md, maintainers/README.md and other READMEs.

Add a 👍 reaction to pull requests you find important.

@nixpkgs-ci nixpkgs-ci bot added 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin-stdenv This PR causes stdenv to rebuild on Darwin and must target a staging branch. 10.rebuild-darwin: 5001+ This PR causes many rebuilds on Darwin and must target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. 8.has: changelog This PR adds or changes release notes 8.has: documentation This PR adds or changes documentation labels Sep 11, 2025
@LunNova
Copy link
Member

LunNova commented Sep 11, 2025

How important is being able to set :BOOL automatically?

@ShamrockLee
Copy link
Contributor Author

ShamrockLee commented Sep 11, 2025

How important is being able to set :BOOL automatically?

The flag would be more human-readable. If cmakeDefinitionTypes[WITH_FOO]=BOOL is set, the resulting flag would be -DWITH_FOO:BOOL=ON instead of -DWITH_FOO=1 (or -DWITH_FOO:BOOL=OFF instead of -DWITH_FOO=)

In the mean time, I'm also considering if we should auto-convert

{
  # WITH_FOO takes, e.g., "auto", true-like, and false-like
  cmakeDefinitions.WIITH_FOO = true;
  cmakeDefinitionTypes.WITH_FOO = "STRING";
}

to cmakeDefinitions[WITH_FOO]=ON (instead of cmakeDefinitions[WITH_FOO]="").

Update: Just implemented the above feature.

@ShamrockLee ShamrockLee force-pushed the cmake-definitions branch 2 times, most recently from b124393 to 1717365 Compare September 11, 2025 15:54
@ShamrockLee ShamrockLee changed the title cmake: setup-hooks: take structural cmakeDefinitions and cmakeDefinitionTypes cmake: setup-hooks: take structural cmakeEntries and cmakeEntryTypes Sep 11, 2025
@ShamrockLee ShamrockLee force-pushed the cmake-definitions branch 4 times, most recently from aab158f to 183cc44 Compare September 11, 2025 18:05
@ShamrockLee ShamrockLee marked this pull request as ready for review September 11, 2025 18:05
@ShamrockLee
Copy link
Contributor Author

-GNinja
@ofborg build aws-lc
@ofborg build bencode

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/structured-cmakeflags-attrs/6261/5

@nixpkgs-ci nixpkgs-ci bot added the 2.status: merge conflict This PR has merge conflicts with the target branch label Sep 21, 2025
Copy link
Contributor

@RossSmyth RossSmyth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the idea, I'd love to see this in-tree. Unsure about the two disconnected attrset API, but it works well enough.

@nixpkgs-ci nixpkgs-ci bot added the 12.approvals: 1 This PR was reviewed and approved by one person. label Sep 28, 2025
@ShamrockLee ShamrockLee changed the title cmake: setup-hooks: take structural cmakeEntries and cmakeEntryTypes cmake: setup-hooks: take structural cmakeEntries Nov 3, 2025
@ShamrockLee ShamrockLee force-pushed the cmake-definitions branch 2 times, most recently from 4e6138f to c058a5b Compare November 3, 2025 11:19
github-actions[bot]

This comment was marked as resolved.

@ShamrockLee
Copy link
Contributor Author

ShamrockLee commented Nov 3, 2025

Sorry for the wrong rebase. Fell free to unsubscribe.

@github-actions github-actions bot dismissed their stale review November 3, 2025 11:24

All good now, thank you!

@nixpkgs-ci nixpkgs-ci bot removed the 2.status: merge conflict This PR has merge conflicts with the target branch label Nov 3, 2025
@ShamrockLee
Copy link
Contributor Author

I simplified the implementation and dropped cmakeEntryTypes.

Now, CMake boolean values are expected to be either cmakeEntries[enableFoo] == ON or cmakEntries[enableFoo] == OFF. Values from { cmakeEntries.enableFoo = true; } will be canonicalized at the setup-hook source time. Other supported values supported by CMake are also acceptable. Helper Bash functions canonicalizeCMakeBool, cmakeBoolToBash, and testCMakeBool helps users handle them.

@ShamrockLee
Copy link
Contributor Author

nixpkgs-review result

Generated using nixpkgs-review-gha

Command: nixpkgs-review pr 442017 -p cmakeMinimal -p cmake -p bcc
Commit: 182e3ab18e0da37cd575c8dd823a8dc968b8128d (subsequent changes)
Merge: 5ccef07aff19527b94145ca080d073effd244e03

Logs: https://github.com/ShamrockLee/nixpkgs-review-gha/actions/runs/19035963338


x86_64-linux

❌ 2 packages failed to build:
  • bcc
  • bcc.man (bcc.man.man)
✅ 4 packages built:
  • cmake
  • cmake.debug (cmake.debug.debug)
  • cmakeMinimal
  • cmakeMinimal.debug (cmakeMinimal.debug.debug)

aarch64-linux

❌ 2 packages failed to build:
  • bcc
  • bcc.man (bcc.man.man)
✅ 4 packages built:
  • cmake
  • cmake.debug (cmake.debug.debug)
  • cmakeMinimal
  • cmakeMinimal.debug (cmakeMinimal.debug.debug)

x86_64-darwin (sandbox = relaxed)

❌ 2 packages failed to build:
  • cmake
  • cmakeMinimal

aarch64-darwin (sandbox = relaxed)

❌ 2 packages failed to build:
  • cmake
  • cmakeMinimal

@@ -16,9 +16,35 @@ You can disable this hook’s behavior by setting `configurePhase` to a custom v

### CMake Exclusive Variables {#cmake-exclusive-variables}

#### `cmakeEntries` {#cmake-entries}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there prior art in Nixpkgs that we can refer to here, such as for another build system?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT, this PR is the first one that implements structured flags attributes for a build system. The previous pattern is cmakeFlags and cmakeFlagsArray.

Comment on lines 27 to 29
If `cmakeEntries[WITH_FOO]` is defined and `[[ cmakeEntryTypes[WITH_FOO] == BOOL ]]`,
one can determine the condition with `[[ -n "${cmakeEntries[WITH_FOO]}" ]]`.
When prepending to the flags, however, the value will become converted to either `ON` or `OFF`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a motivating example in mind where we would use this in Bash?

Copy link
Contributor Author

@ShamrockLee ShamrockLee Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! This paragraph is for the previous attempt of this PR (where we have cmakeEntryTypes) and is no longer relevant.

In that design, Bash function can construct a conditional expression from a boolean cmakeEntries element using test -n or test -z, which is what we expect when passing a Nix derivation argument to the Bash builder:

if [[ -n "${cmakeEntries[ENABLE_FOO]}" ]]; then
  # Do something when using -DENBLE_FOO=ON
else
  # Do something when using -DENBLE_FOO=OFF
fi

In the current design, one simply assigns ON or OFF to "${cmakeEntries[ENABLE_FOO]}". { cmakeEntries.ENABLE_FOO = true; } is translated to cmakeEntries[ENABLE_FOO] when jq is available. However, we don't strongly enforce this policy, but provide tooling to accommodate other CMake-supported boolean values. For example. one can use testCMakeBool to construct a Bash conditional expression:

if testCmakeBool "${cmakeEntries[ENABLE_FOO]}"; then
  # Do something when using -DENBLE_FOO=ON
else
  # Do something when using -DENBLE_FOO=OFF
fi

testCMakeBool will fail with non-zero exit status.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this paragraph.

@nixpkgs-ci nixpkgs-ci bot added the 2.status: merge conflict This PR has merge conflicts with the target branch label Feb 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.status: merge conflict This PR has merge conflicts with the target branch 8.has: changelog This PR adds or changes release notes 8.has: documentation This PR adds or changes documentation 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 5001+ This PR causes many rebuilds on Darwin and must target the staging branches. 10.rebuild-darwin-stdenv This PR causes stdenv to rebuild on Darwin and must target a staging branch. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. 12.approvals: 1 This PR was reviewed and approved by one person.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants