from typing import Iterable


def write_nested_dict_value(nested_dict, nested_keys: Iterable, value):
    """
    Safely write a value into a series of nested dictionaries, making intermediate layers where required

    :param nested_dict: The nested dictionaries (configuration map) to write to
    :param nested_keys: An Iterable containing the sequence of keys to follow to store the value
    :param value: The value to store in the nested dictionary
    :returns: value
    """
    head_key, *tail_keys = nested_keys
    if not tail_keys:
        # Base case, no more layers to go down, store our value here
        nested_dict[head_key] = value
        return value
    # Need to recurse down a level, but if the next level doesn't already exist we need to create it first
    if head_key not in nested_dict:
        nested_dict[head_key] = {}
    return write_nested_dict_value(nested_dict[head_key], tail_keys, value)