Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/meta: optimise platform constraint algebra #2

Open
wants to merge 1 commit into
base: lib/meta-platform-constraints
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/meta.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
let
inherit (lib) matchAttrs any all isDerivation getBin assertMsg;
inherit (lib.attrsets) mapAttrs' filterAttrs;
inherit (lib.meta.platform) evalConstraints;
inherit (builtins) isString match typeOf;

in
Expand Down Expand Up @@ -289,8 +290,8 @@ rec {
:::
*/
availableOn = platform: pkg:
((!pkg?meta.platforms) || any (lib.meta.platform.evalConstraints platform) pkg.meta.platforms) &&
all (elem: !lib.meta.platform.evalConstraints platform elem) (pkg.meta.badPlatforms or []);
((!pkg?meta.platforms) || any (evalConstraints platform) pkg.meta.platforms) &&
all (elem: !evalConstraints platform elem) (pkg.meta.badPlatforms or []);

/**
Mapping of SPDX ID to the attributes in lib.licenses.
Expand Down
61 changes: 38 additions & 23 deletions lib/platform-constraints.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,46 @@
# (NOT isMusl)
# ]

{
let
inherit (lib.meta) platformMatch;
inherit (lib) head;

operations = {
OR = lib.any lib.id;
AND = lib.all lib.id;
NOT = x: !(head x);
NONE = _: false;
ANY = _: true;
};

in

rec {
constraints =
{
OR = value: {
OR = constraints: {
__operation = "OR";
inherit value;
value = constraints;
};
AND = value: {

AND = constraints: {
__operation = "AND";
inherit value;
value = constraints;
};
NOT = value: {

NOT = constraint: {
__operation = "NOT";
value = [ value ]; # Also make this a list for simplicity
value = [ constraint ]; # Also make this a list for simplicity
};

NONE = {
__operation = "NONE";
value = [ ];
};

ANY = {
__operation = "ANY";
value = [ ];
};
}
# Put patterns in this set for convenient use
Expand All @@ -51,20 +77,9 @@
# Because this uses lib.meta.platformMatch internally it supports the same set
# of platform constraints.
evalConstraints =
platform: initialValue:
let
operations = {
OR = lib.any lib.id;
AND = lib.all lib.id;
NOT = x: !(lib.head x); # We have a list with one value
};
platformMatch' = lib.meta.platformMatch platform;
recurse =
expression:
if expression ? __operation then
operations.${expression.__operation} (map recurse expression.value)
else
platformMatch' expression;
in
recurse initialValue;
platform: constraint:
if constraint ? __operation then
operations.${constraint.__operation} (map (evalConstraints platform) constraint.value)
else
platformMatch platform constraint;
}