import copy


def combine(to_merge, extend_by):
    """Merge nested dictionaries."""

    def _combine(to_merge, extend_by):
        for key, value in extend_by.items():
            if key in to_merge:
                if isinstance(to_merge[key], dict):
                    _combine(to_merge[key], value)
                else:
                    to_merge[key] = value
            else:
                to_merge[key] = value

    to_merge = copy.deepcopy(to_merge)

    _combine(to_merge, extend_by)

    return to_merge