Selectors
Selectors determine which nodes a rule or query targets. They can be used as the is key in a rule, or passed to select functions.
Selector forms
Section titled “Selector forms”Trait reference
Section titled “Trait reference”Match nodes that have a specific trait in their expanded is list:
nest.host # single traitnest.admin # marker traitList (AND compound)
Section titled “List (AND compound)”A list of selectors — all must match:
[ nest.host nest.web ] # has both host AND web[ nest.host (nest.has nest.admin) ] # host AND has admin child[ nest.host (nest.attrs { env = "prod"; }) ] # host AND env=prod attributeString (CSS syntax)
Section titled “String (CSS syntax)”A CSS selector string — parsed at evaluation time:
"*" # star"#lb-prod" # by name".nixos" # by entity class"[env=prod]" # attribute equality"[system]" # attribute presence"web, lb" # or (comma)"prod > web" # child combinator"prod web" # descendant combinator (space-separated — uses +)":not(.staging)" # negation":has(.admin)" # has child":within(.prod)" # within ancestorSee CSS Syntax for full string selector syntax.
Programmatic constructors
Section titled “Programmatic constructors”All constructors are available as nest.<name> inside rules and the nest module.
Matches any node:
nest.starattrs { key = val; … }
Section titled “attrs { key = val; … }”Matches nodes where all listed attributes equal their expected values:
nest.attrs { env = "prod"; }nest.attrs { env = "staging"; system = "aarch64-linux"; }Uses builtins.toString for comparison — works with strings, booleans, and integers.
or [ sel … ]
Section titled “or [ sel … ]”Matches nodes that satisfy at least one selector in the list:
nest.or [ nest.web nest.lb ]nest.or [ (nest.attrs { env = "prod"; }) (nest.attrs { env = "staging"; }) ]not sel
Section titled “not sel”Matches nodes that do NOT match sel. sel may be a list (which is AND):
nest.not nest.stagingnest.not [ nest.host nest.staging ]has sel
Section titled “has sel”Matches nodes that have at least one direct child matching sel:
nest.has nest.admin # host has a child with admin traitnest.has nest.user # node has at least one user childwithin sel
Section titled “within sel”Matches nodes that have at least one ancestor matching sel:
nest.within nest.host # node is nested somewhere under a hostnest.within "prod" # node is nested under a node named prodwhen fn
Section titled “when fn”Matches nodes for which fn returns true. The function receives the same args as a rule function:
nest.when ({ host, ... }: host.httpPort > 1024)nest.when ({ select, ... }: select.node.env == "prod")nest.when ({ select, ... }: builtins.length (select.children nest.user) > 0)class name
Section titled “class name”Matches nodes whose entity trait exposes a class with the given name:
nest.class "nixos" # entity produces nixosSystem outputnest.class "homeManager" # entity produces homeManager outputchild parentSel childSel
Section titled “child parentSel childSel”Matches nodes that satisfy childSel AND whose direct parent satisfies parentSel:
nest.child nest.cluster nest.webnest.child "#prod" nest.hostEquivalent to CSS prod > web.
descendant ancestorSel descendantSel
Section titled “descendant ancestorSel descendantSel”Matches nodes that satisfy descendantSel AND have some ancestor satisfying ancestorSel:
nest.descendant nest.cluster nest.webnest.descendant "#prod" nest.hostEquivalent to CSS descendant combinator.
Where selectors are used
Section titled “Where selectors are used”Rule is key
Section titled “Rule is key”nest.rules = [ { is = nest.host; nixos = …; } { is = [ nest.host (nest.has nest.admin) ]; nixos = …; } { is = "*.web"; nixos = …; }];select filter functions
Section titled “select filter functions”# Inside a rule or synth function:select nest.web # filter allNodes by nest.webselect.siblings nest.host # filter siblings by traitselect.children nest.user # filter direct children by traitselect.parent nest.cluster # filter direct parentselect.parents nest.host # filter all ancestors matchingselect.within clusterNode nest.web # descendants of clusterNode matchingSee Select API for the full context API.
Matching semantics
Section titled “Matching semantics”A node matches a selector if:
| Selector type | Match condition |
|---|---|
| Trait | __traitName appears in any item of node.is |
| List | All selectors in the list match |
| String | Parsed as CSS, then matched |
star | Always true |
id / name | node.name == sel.name |
attr | toString node.key == sel.val |
attrExists | node ? key |
attrs | All key-value pairs match |
or | Any selector in list matches |
not | No selector in list matches |
has | Any direct child matches inner selector |
within | Any ancestor matches inner selector |
when | Predicate function returns true |
class | Entity trait has a class with this name |
child | Node matches childSel AND parent matches parentSel |
descendant | Node matches descendantSel AND some ancestor matches ancestorSel |