Mishnah: A Nazirite vow of unspecified duration [remains in force] thirty days. Gemara: From where is this rule derived? Rabbi Mattena said: The text reads: He shall be [yihyeh] holy, and the numerical value of the word "yihyeh" is thirty. [Nazir 5a]
I thought we were not allowed to derive halacha from gematria. Are we?
(As an aside, wouldn't the gematria of "holy" be more appropriate?)
Your motivating example is not necessarily a case of “deriving” halachah from gematria.
Rambam writes in is Commentary to Nazir 1:3 that the thirty-day nazir rule is a received tradition, which the Rabbis supported using the gematria as a hint.
I speculate that Rambam says this as he is loathe to view a gematria as the primary source for this halachah. This would suggest that he is of the opinion that one cannot straightforwardly derive laws from gematria.