add nested environment handling to copy_env and unit tests