Overriding MFE .env values via plugin

Hi all
Is it possible to override the values in the .ENV configs by using the plugin system?
For example in frontend-app-authoring/.env at master · openedx/frontend-app-authoring · GitHub there are a variety of variables there.
I’ve tried patching like this:

from tutor import hooks

hooks.Filters.ENV_PATCHES.add_items(
    [
        (
            "mfe-lms-common-settings",
            """
MFE_CONFIG["VARIABLE_NAME"] = VALUE
            """
        ),
    ],
)

which does add the value to ~/.local/share/tutor/env/apps/openedx/settings/lms/production.py however it appears to have no effect in my instance. So far the only way I can apply any change is to fork the MFE and manually edit the .env file. Surely there must be a way to patch these envs somehow? or am I being stupid? (evidently i am)

@joel.edwards Hi, depending on where you are trying to use this value, you might have to use different patches. It’s one of the confusing things about the MFE customization process. I would recommend going through the Template patch catalog of tutor-mfe to identify the right patch.

For example, the patch you have used above is a runtime config patch that provide config to MFEs via an API call. If you look for an API call to /api/mfe_config/ from your MFE (for example: http://apps.local.openedx.io:1996/api/mfe_config/v1?mfe=learner-dashboard) , you should see the VARIABLE_NAME as one of the keys.

Now if you are setting something that will be used during the build time, you might have to use "mfe-dockerfile-pre-npm-build" patch. There is an example at the bottom of the Customizing MFEs section of the tutor-mfe’s README.

1 Like

Thanks for your response @arunmozhi it was very helpful and pointed me in the right direction.
Those patches were a little confusing to wrap my head around but think I’ve got a working method.

from tutor import hooks

hooks.Filters.ENV_PATCHES.add_items([
    (
        "mfe-dockerfile-pre-npm-build-authoring",
        """
ENV VAR=VAL
"""
    ),
])

Following a rebuild and restart of the MFE it seems to take effect.

Is there an equivalent way I can set env vars outside of the build process? Such that I’d be able to apply a new env var (either with or without restarting the instance) without having to rebuild the entire image? I can live with spending 15mins on a rebuild if that’s what it takes, but would be nice to be able to simply restart for new env to take effect… But if not then you’ve resolved my query perfectly :slight_smile:

@joel.edwards I am a bit iffy on the distintion between the build time and run time ENV VARs myself. I am glad your problem is resolved for now.

If you are looking for a quickly changing these, I think looking into the frontend-platform repo/docs to see how it handles config parsing could be useful. And also the docs on the /api/mfe_config endpoint itself. I believe, there is a way to set the config in something like Site Configurations or something and you could change the values in the DB and have it take effect. I could be mis-remembering. But, I know those 2 pieces could help here.