An NDA does not need to be in an employment contract to be enforceable. It can be part of a contract with an independent contractor, or in a settlement agreement, among other types of agreements.
On the other hand, if a party is compelled to testify, then it is highly unlikely that a court will order a return of the consideration or otherwise find the testifying party in breach. An NDA will likely be found unenforceable, to the extent it requires a party to disobey a court order or subpoena, or otherwise violate the law. Many NDAs contains provisions that explain how the parties must cooperate with each other when there is such a court order or subpoena, in order to best protect both sides.
NDA's are fully enforceable agreements as long as both sides have provided consideration for the promises made. That said, contracts made for an illegal purpose are unenforceable and an NDA with a purpose that would be contrary to the law would therefore be unenforceable. Most well-drafted NDAs will have exceptions written into them to avoid a finding of unenforceability.
Keep in mind that an agreement between two people is neither enforceable by nor binding on any other third party. If someone is compelled to testify about something that is the subject of an NDA, the party to the NDA cannot use the NDA to avoid testifying unless some other basis is available, like privacy rights or trade secret protection. And if the person does testify, that person can be subject to liability to the other party to the NDA.
Good luck to you.
This answer should not be construed to create any attorney-client relationship. Such a relationship can be formed only through the mutual execution of an attorney-client agreement. The answer given is based on the extremely limited facts provided and the proper course of action might change significantly with the introduction of other facts. All who read this answer should not rely on the answer to govern their conduct. Please seek the advice of competent counsel after disclosing all facts to that attorney. This answer is intended for California residents only. The answering party is only licensed to practice in the State of California.
Nondisclosure agreements may be necessary to protect confidential information when working with third parties. For example, if I hire a data-recovery technician because my computer crashed, I have to make sure that he does not disclose any client information that he recovers.
A provision that required return of consideration for the agreement if one party was legally compelled to disclose the information would more than likely be unenforceable. On the other hand, if I paid someone hush money, I could not get it back by claiming that the agreement was illegal. Paying a snitch not to tell the police is criminal for both parties.