canada
It would be prima facie setting a trap or causing bodily harm with a trap
The act of deploying Material X would be the offence of setting a trap likely to cause bodily harm. If Material X is triggered then it would also be the offence of causing bodily harm with a trap. See s. 247 Criminal Code.
Self defence or defence of another require that the illegal act be reasonable in the circumstances
For self-defence or defence-of-another to excuse or justify this act, one needs to satisfy the elements of s. 34 of the Criminal Code:
34 (1) A person is not guilty of an offence if
(a) they believe on reasonable grounds that force is being used against them or another person or that a threat of force is being made against them or another person;
(b) the act that constitutes the offence is committed for the purpose of defending or protecting themselves or the other person from that use or threat of force; and
(c) the act committed is reasonable in the circumstances.
(2) In determining whether the act committed is reasonable in the
circumstances, the court shall consider the relevant circumstances of
the person, the other parties and the act, including, but not limited
to, the following factors:
(a) the nature of the force or threat;
(b) the extent to which the use of force was imminent and whether
there were other means available to respond to the potential use of
force;
(c) the person’s role in the incident;
(d) whether any party to the incident used or threatened to use a
weapon;
(e) the size, age, gender and physical capabilities of the parties to
the incident;
(f) the nature, duration and history of any relationship between the
parties to the incident, including any prior use or threat of force
and the nature of that force or threat;
(f.1) any history of interaction or communication between the parties
to the incident;
(g) the nature and proportionality of the person’s response to the use
or threat of force; and
(h) whether the act committed was in response to a use or threat of
force that the person knew was lawful.
The Supreme Court has said (R. v. Khill, 2021 SCC 37):
it is for the trier of fact to assess and weigh the factors and determine whether or not the act was reasonable. This is a global, holistic exercise. No single factor is necessarily determinative of the outcome.
The use of Material X would not be reasonable in many circumstances
The fact that Material X will "instantly cause serious bodily harm towards the aggressor", triggered by any harm with intent, would in many circumstances take the use of Material X outside of what would be found to be a reasonable act in self-defence or defence of another.