Use environment variables in config.toml or config.yaml or config.json

I’d like to use an environment variable like $HOME or $DROPBOX when I specify archetypeDir, contentDir, dataDir, layoutDir, publishDir, staticDir, and themesDir, but so far I haven’t been able to do that. There’s no example of this in https://gohugo.io/overview/configuration and I’m wondering if it’s possible. From the internet searching I’ve done, I think TOML files do not allow environment variables, but maybe this would work in config.yaml or config.json? Thanks for any tips about this.

The documentation shows how to define the title of the blog as an environment variable. This is an example and should apply to other config values.

$ env HUGO_TITLE="Some Title" hugo

What I want to know is how to define something in the params section as an environment variable:

[Params]                                                                                                                                                                                                                                    
  subtitle = "random"

I tried HUGO_PARAMS_SUBTITLE but that didn’t work.

I don’t think params can be arbitrarily set; maybe someone else know for sure.

If your [Params] section is in config.toml then you should be able to access it using the following:

env HUGO_PARAMS_subtitle="new subtitle" hugo

If your section is in another config file but still found on the default config path: ./config/_default/ then you need to specify the file as well. Let’s say you want to access a parameter (called subtitle) in the params.toml file, under [map]. Then you need the following command:

env HUGO_PARAMS_MAP_subtitle="new subtitle" Hugo
1 Like