Both DNG and NEF are TIFF/EP containers, so the spec you're after is the TIFF/EP spec. On top of that there is custom tags in both formats: the DNG ones are described in its spec (surprised you couldn't find it), the NEF ones you can glimpse maybe from e.g. ExifTool, but I don't think it's published by Nikon. I think you pretty much have to come up with your own writer, or use some TIFF library and extend it.
Both DNG and NEF are TIFF/EP containers, so the spec you're after is the TIFF/EP spec. On top of that there is custom tags in both formats: the DNG ones are described in its spec (surprised you couldn't find it), the NEF ones you can glimpse maybe from e.g. ExifTool, but I don't think it's published by Nikon. I think you pretty much have to come up with your own writer, or use some TIFF library and extend it.