Skip to content

lib/meta: add platform constraints algebra - #348238

Draft
Atemu wants to merge 6 commits into
NixOS:masterfrom
Atemu:lib/meta-platform-constraints
Draft

Atemu wants to merge 6 commits into
NixOS:masterfrom
Atemu:lib/meta-platform-constraints

Conversation

@Atemu

@Atemu Atemu commented Oct 13, 2024 •

Copy link
Copy Markdown
Member

Previously, we were able to express platform constraints in packages using e.g.

meta.platforms = with lib.platforms; linux ++ darwin;

These platform checks only worked on a coarse level using lists of pre-defined platform strings. It was not possible to express that only e.g. 64bit platforms or that only GNU libcs are supported previously.

This introduces an algebra based on platform patterns which allows you to express arbitrarily complex platform constraints using simple expressions without needing to rely on stdenv.*Platform.is* bools.

Once this is in wide-spread use, it can be used for accurate rendering of platform support in manuals and search sites.

It also removes the need for dependency on a package's stdenv for expressing which platforms are broken as this can now be expressed using platform constraints instead. This makes progress of potentially evaluating a package's meta information independently of the package or its inputs.

Things we still need to discuss:

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 24.11 Release Notes (or backporting 23.11 and 24.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions Bot added the 6.topic: lib The Nixpkgs function library label Oct 13, 2024
@Atemu Atemu added the 9.needs: community feedback This needs feedback from more community members. label Oct 13, 2024
@ofborg ofborg Bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. labels Oct 13, 2024
@Atemu

Atemu commented Oct 13, 2024

Copy link
Copy Markdown
Member Author

The commit I just pushed will be made redundant in a follow-up but I want to see ofBorg eval perf because @adisbladis suggested that even seemingly tiny changes can have an immense impact there because this is on the hot path for all packages.

@Atemu

Atemu commented Oct 13, 2024

Copy link
Copy Markdown
Member Author

They have also provided an alternative implementation that has much nicer properties but trades off the ability to introspect.

Atemu#1

@Atemu

Atemu commented Oct 13, 2024

Copy link
Copy Markdown
Member Author

Hm, I have also realised that their approach trades off the ability to insert arbitrary custom constraints too as all constraints must be wrapped inside of a function that takes the platform as a parameter.

This is an admittedly fringe case and could be remedied using a simple helper function around the custom constraint that applies the same wrapping.

Comment thread lib/tests/misc.nix Resolved
Comment thread lib/platform-constraints.nix Resolved
@Atemu

Atemu commented Oct 13, 2024

Copy link
Copy Markdown
Member Author

I'm far from an expert in interpreting these stats but the eval perf report appears to show a slight increase in function activity and thunks but a slight decrease in GC heap size?

Anyhow, that's with the functionality not really in use by any packages yet. Real-world testing would likely prove to be difficult to realise. Perhaps we could migrate a critical mass of packages via automation somehow and extrapolate the effect.

@ofborg ofborg Bot added 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. and removed 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. labels Nov 15, 2024
@wegank wegank added the 2.status: merge conflict This PR has merge conflicts with the target branch label Feb 15, 2025
@nixpkgs-ci nixpkgs-ci Bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 25, 2025
@stale stale Bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jul 1, 2025
@Atemu
Atemu force-pushed the lib/meta-platform-constraints branch from ede4b66 to 41a3da4 Compare July 1, 2025 15:42
@ofborg ofborg Bot removed the 2.status: merge conflict This PR has merge conflicts with the target branch label Jul 1, 2025
@nixpkgs-ci nixpkgs-ci Bot removed the 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. label Jul 1, 2025
@Atemu

Atemu commented Jul 1, 2025

Copy link
Copy Markdown
Member Author

I did a quick and dirty hack by sed-patching the most common trivial cases of all, unix, linux++darwin, linux and darwin.

Merge base:

$ NIXPKGS_ALLOW_UNSUPPORTED_SYSTEM=1 hyperfine --warmup 10 -- "nix-instantiate nixos/release.nix -A iso_minimal.x86_64-linux"
Benchmark 1: nix-instantiate nixos/release.nix -A iso_minimal.x86_64-linux
  Time (mean ± σ):     13.437 s ±  0.119 s    [User: 5.799 s, System: 2.197 s]
  Range (min … max):   13.283 s … 13.706 s    10 runs

My PR with 12902 packages hacked:

$ NIXPKGS_ALLOW_UNSUPPORTED_SYSTEM=1 hyperfine --warmup 10 -- "nix-instantiate nixos/release.nix -A iso_minimal.x86_64-linux"
Benchmark 1: nix-instantiate nixos/release.nix -A iso_minimal.x86_64-linux
  Time (mean ± σ):     13.465 s ±  0.119 s    [User: 5.823 s, System: 2.187 s]
  Range (min … max):   13.293 s … 13.667 s    10 runs

(also: wth, 13s eval for the minimal ISO‽ It was 6s on the previous merge base..)

Sed script used:

s/platforms =\s*(with (lib)?(platforms;\s*))?(\S+all);/platforms = lib.platforms.all; platformConstraints = with lib.meta.platform.constraints; ANY;/g
s/platforms =\s*(with (lib)?(platforms;\s*))?(\S+linux);/platforms = lib.platforms.all; platformConstraints = with lib.meta.platform.constraints; isLinux;/g
s/platforms =\s*(with (lib)?(platforms;\s*))?(\S+darwin);/platforms = lib.platforms.all; platformConstraints = with lib.meta.platform.constraints; isDarwin;/g
s/platforms =\s*(with (lib)?(platforms;\s*))?(\S+unix);/platforms = lib.platforms.all; platformConstraints = with lib.meta.platform.constraints; isUnix;/g
s/platforms = (\S+linux)\s*\+\+\s*(\S+darwin);/platforms = lib.platforms.all; platformConstraints = with lib.meta.platform.constraints; OR [ isLinux isDarwin ];/g
s/platforms = (\S+darwin)\s*\+\+\s*(\S+linux);/platforms = lib.platforms.all; platformConstraints = with lib.meta.platform.constraints; OR [ isLinux isDarwin ];/g

cmdline to hack:

fd --extension nix . pkgs/ | xargs -P 10 sed -i -E -f 'constraints.sed' 

@nixpkgs-ci nixpkgs-ci Bot added the 6.topic: stdenv Standard environment label Jul 2, 2025
@Atemu
Atemu force-pushed the lib/meta-platform-constraints branch from a0a5813 to 0e467a3 Compare July 2, 2025 12:12
@nixpkgs-ci nixpkgs-ci Bot added the 6.topic: policy discussion Discuss policies to work in and around Nixpkgs label Jul 2, 2025
Atemu and others added 4 commits July 2, 2025 16:47
Previously, we were able to express platform constraints in packages using e.g.

    meta.platforms = with lib.platforms; linux ++ darwin;

These platform checks only worked on a coarse level using lists of pre-defined
platform strings. It was not possible to express that only e.g. 64bit platforms
or that only GNU libcs are supported previously.

This introduces an algebra based on platform patterns which allows you to
express arbitrarily complex platform constraints using simple expressions
without needing to rely on `stdenv.*Platform.is*` bools.

Once this is in wide-spread use, it can be used for accurate rendering of
platform support in manuals and search sites.

It also removes the need for dependency on a package's `stdenv` for expressing
which platforms are broken as this can now be expressed using platform
constraints instead. This makes progress of potentially evaluating a package's
meta information independently of the package or its inputs.
It would be trivially possible to support platformConstraints in meta.platforms
and meta.badPlatforms by simply replacing all occurrences of platformMatch with
evalConstraint but that would require wrapping the platform constraints inside
of a list when declaring them in packages which doesn't make a whole lot of
sense given that they have native support for combining multiple conditions.

It would be possible to allow lists or non-lists or even choose which method to
use based on whether the value passed is a list but I think it's simpler if we
just introduced a new meta attribute instead to make a clean cut between legacy
platform declarations and platform constraints declaration.
This originally had some more opinionated changes and was based on an older
state of my patches. I only kept the performance optimisations.

I was not able to measure a significant change in perf but these look like
they'd be better for perf to me and also improve the code.

Co-authored-by: Atemu <[email protected]>
@Atemu
Atemu force-pushed the lib/meta-platform-constraints branch from 0e467a3 to ead9589 Compare July 2, 2025 14:48
@nixpkgs-ci nixpkgs-ci Bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: documentation This PR adds or changes documentation labels Jul 2, 2025
@nixpkgs-ci nixpkgs-ci Bot added the 2.status: merge conflict This PR has merge conflicts with the target branch label Aug 28, 2025
@Atemu Atemu mentioned this pull request Dec 6, 2025
13 tasks
@nixpkgs-ci nixpkgs-ci Bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Dec 30, 2025
@Atemu

Atemu commented Aug 26, 2026 •

Copy link
Copy Markdown
Member Author

In talking with @llakala about the current eval perf of the platforms check, I had the idea that we could declare platforms as a function like this:

platform = constraints: with constraints; OR [ isLinux isDarwin ]

This declares the same meaning but is no longer dependent on the specific definitions in lib, so it could be substituted with a set of pre-computed values for e.g. isLinux instead which might be much faster to eval. Of course you could also still plug in the constraints values from lib and work with them as data.

@nixpkgs-ci nixpkgs-ci Bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Aug 26, 2026
@Atemu

Atemu commented Aug 26, 2026

Copy link
Copy Markdown
Member Author

They also suggested that we could just make the constraints a package input injected via callPackage. That way, you could just create your scope to make the set contain cached values for fast-pathing the platform check and we remove one (potentially quite perf costly) function invocation.

@hsjobeki hsjobeki moved this to 👀 To Review in Nix documentation Oct 21, 2026
@Atemu

Atemu commented Nov 14, 2026

Copy link
Copy Markdown
Member Author

A conceptual difficulty I've just run into is that adding an attribute for memoisation causes false negatives platformMatch because it's just a dumb matchAttrs.

But tbh. we have a custom data format here anyway, so we can just make it { constraint = <raw value>; memoised = <bool>; } all the way through.

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 6.topic: lib The Nixpkgs function library 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 6.topic: policy discussion Discuss policies to work in and around Nixpkgs 6.topic: stdenv Standard environment 8.has: documentation This PR adds or changes documentation 9.needs: community feedback This needs feedback from more community members. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux.

Projects

Status: 👀 To Review
Status: No status
Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants