Debian Codegraph @

docs/content/en/configuration/introduction.md

raw
1 ---
2 title: Introduction
3 description: Configure your site using files, directories, and environment variables.
4 categories: []
5 keywords: []
6 weight: 10
7 ---
8
9 ## General settings and categories
10
11 Each top-level key in the project configuration is either a general setting or a configuration category.
12
13 A general setting is a single value, such as [`baseURL`][] or [`title`][]. A configuration category groups related, nested settings, such as [`markup`][], [`menus`][], or [`params`][].
14
15 {{< code-toggle file=hugo >}}
16 baseURL = 'https://example.org/'
17 title = 'My New Hugo Site'
18 [params]
19 subtitle = 'The Best Widgets on Earth'
20 {{< /code-toggle >}}
21
22 In this example, `baseURL` and `title` are general settings, and `params` is a configuration category.
23
24 ## Sensible defaults
25
26 Hugo offers many configuration settings, but its defaults are often sufficient. A new project requires only these settings:
27
28 {{< code-toggle file=hugo >}}
29 baseURL = 'https://example.org/'
30 locale = 'en-us'
31 title = 'My New Hugo Site'
32 {{< /code-toggle >}}
33
34 Only define settings that deviate from the defaults. A smaller configuration file is easier to read, understand, and debug. Keep your configuration concise.
35
36 > [!NOTE]
37 > The best configuration file is a short configuration file.
38
39 ## Configuration file
40
41 Create a project configuration file in the root of your project directory, naming it `hugo.toml`, `hugo.yaml`, or `hugo.json`, with that order of precedence.
42
43 ```tree
44 my-project/
45 └── hugo.toml
46 ```
47
48 A simple example:
49
50 {{< code-toggle file=hugo >}}
51 baseURL = 'https://example.org/'
52 locale = 'en-us'
53 title = 'ABC Widgets, Inc.'
54 [params]
55 subtitle = 'The Best Widgets on Earth'
56 [params.contact]
57 email = '[email protected]'
58 phone = '+1 202-555-1212'
59 {{< /code-toggle >}}
60
61 To use a different configuration file when building your project, use the `--config` flag:
62
63 ```sh
64 hugo build --config other.toml
65 ```
66
67 Combine two or more configuration files, with left-to-right precedence:
68
69 ```sh
70 hugo build --config a.toml,b.yaml,c.json
71 ```
72
73 Hugo loads the files in the order listed, and each file recursively overwrites matching keys from the files before it. In other words, for a conflicting key, the value from the last-listed file wins.
74
75 > [!NOTE]
76 > See the specifications for each file format: [TOML][], [YAML][], and [JSON][].
77
78 ## Configuration directory
79
80 Instead of a single project configuration file, split your configuration by [environment](g), configuration category, and language. Hugo loads the `_default` directory first, then the directory for the current environment, so for a conflicting key, the environment-specific value wins. For example:
81
82 ```tree
83 my-project/
84 └── config/
85 ├── _default/
86 │ ├── hugo.toml
87 │ ├── menus.en.toml
88 │ ├── menus.de.toml
89 │ └── params.toml
90 └── production/
91 └── params.toml
92 ```
93
94 The configuration categories are {{< configuration-categories >}}.
95
96 ### Omit or include a category
97
98 {{< new-in 0.162.0 />}}
99
100 When splitting the configuration by configuration category, you may omit or include the category name in the component file. For example, these are equivalent:
101
102 {{< code-toggle file=config/_default/hugo >}}
103 [params]
104 foo = 'bar'
105 {{< /code-toggle >}}
106
107 {{< code-toggle file=config/_default/params >}}
108 foo = 'bar'
109 {{< /code-toggle >}}
110
111 This also applies to keys whose values are maps of slices, such as `menus`. For example, these are equivalent:
112
113 {{< code-toggle file=config/_default/menus >}}
114 [[main]]
115 name = 'Home'
116 pageRef = '/'
117 weight = 10
118 {{< /code-toggle >}}
119
120 {{< code-toggle file=config/_default/menus >}}
121 [[menus.main]]
122 name = 'Home'
123 pageRef = '/'
124 weight = 10
125 {{< /code-toggle >}}
126
127 For pure slice-typed keys such as `cascade` and `permalinks`, including the category name is required. For example:
128
129 {{< code-toggle file=config/_default/cascade >}}
130 [[cascade]]
131 [cascade.params]
132 color = 'red'
133 [cascade.target]
134 path = '/articles/**'
135 {{< /code-toggle >}}
136
137 > [!NOTE]
138 > Hugo unwraps the category only when it is the sole key in the file and matches the file's basename.
139
140 ### Recursive parsing
141
142 Hugo parses the `config` directory recursively, allowing you to organize the files into subdirectories. For example:
143
144 ```tree
145 my-project/
146 └── config/
147 └── _default/
148 ├── navigation/
149 │ ├── menus.de.toml
150 │ └── menus.en.toml
151 └── hugo.toml
152 ```
153
154 ### Example
155
156 ```tree
157 my-project/
158 └── config/
159 ├── _default/
160 │ ├── hugo.toml
161 │ ├── menus.en.toml
162 │ ├── menus.de.toml
163 │ └── params.toml
164 ├── production/
165 │ ├── hugo.toml
166 │ └── params.toml
167 └── staging/
168 ├── hugo.toml
169 └── params.toml
170 ```
171
172 Considering the structure above, when running `hugo build --environment staging`, Hugo uses every setting from `config/_default`, then overwrites matching keys with settings from `staging`.
173
174 Let's take an example to understand this better. Let's say you are using Google Analytics for your website. This requires you to specify a [Google tag ID][] in your project configuration:
175
176 {{< code-toggle file=hugo >}}
177 [services.googleAnalytics]
178 ID = 'G-XXXXXXXXX'
179 {{< /code-toggle >}}
180
181 Now consider the following scenario:
182
183 1. You don't want to load the analytics code when running `hugo server`.
184 1. You want to use different Google tag IDs for your production and staging environments. For example:
185 - `G-PPPPPPPPP` for production
186 - `G-SSSSSSSSS` for staging
187
188 To satisfy these requirements, configure your site as follows:
189
190 1. `config/_default/hugo.toml`
191 - Exclude the `services.googleAnalytics` section. This prevents Hugo from loading the analytics code when you run `hugo server`.
192 - By default, Hugo sets its `environment` to `development` when running `hugo server`. In the absence of a `config/development` directory, Hugo uses the `config/_default` directory.
193 1. `config/production/hugo.toml`
194 - Include this section only:
195
196 {{< code-toggle file=hugo >}}
197 [services.googleAnalytics]
198 ID = 'G-PPPPPPPPP'
199 {{< /code-toggle >}}
200
201 - You do not need to include other parameters in this file. Include only those parameters that are specific to your production environment. Hugo overwrites the default configuration with these parameters.
202 - By default, Hugo sets its `environment` to `production` when running `hugo build`. The analytics code uses the `G-PPPPPPPPP` tag ID.
203
204 1. `config/staging/hugo.toml`
205
206 - Include this section only:
207
208 {{< code-toggle file=hugo >}}
209 [services.googleAnalytics]
210 ID = 'G-SSSSSSSSS'
211 {{< /code-toggle >}}
212
213 - You do not need to include other parameters in this file. Include only those parameters that are specific to your staging environment. Hugo overwrites the default configuration with these parameters.
214 - To build your staging site, run `hugo build --environment staging`. The analytics code uses the `G-SSSSSSSSS` tag ID.
215
216 ## Merge configuration settings
217
218 Hugo merges configuration settings from themes and modules, prioritizing the project's own settings. This is distinct from combining configuration files with the `--config` flag, or splitting configuration across a [configuration directory](#configuration-directory), both of which always overwrite keys of the same name. Given this simplified project structure with two themes:
219
220 ```tree
221 project/
222 ├── themes/
223 │ ├── theme-a/
224 │ │ └── hugo.toml
225 │ └── theme-b/
226 │ └── hugo.toml
227 └── hugo.toml
228 ```
229
230 and this project-level configuration:
231
232 {{< code-toggle file=hugo >}}
233 baseURL = 'https://example.org/'
234 locale = 'en-us'
235 title = 'My New Hugo Site'
236 theme = ['theme-a','theme-b']
237 {{< /code-toggle >}}
238
239 Hugo merges settings in this order:
240
241 1. Project configuration (`hugo.toml` in the project root)
242 1. `theme-a` configuration
243 1. `theme-b` configuration
244
245 ### Merge strategy
246
247 The `_merge` setting within each [configuration category](#general-settings-and-categories) controls _which_ settings are merged and _how_ they are merged.
248
249 You can set `_merge` at any level of nesting within a category, not only at its top level. When merging a nested table, Hugo uses the `_merge` value set on that table if present, or inherits the value from its nearest ancestor. For example, to change the merge strategy for a single Goldmark extension without affecting the rest of the `markup` category:
250
251 {{< code-toggle file=hugo >}}
252 [markup.goldmark.extensions.typographer]
253 _merge = 'deep'
254 {{< /code-toggle >}}
255
256 The value for `_merge` can be one of:
257
258 `none`
259 : Do not merge.
260
261 `shallow`
262 : Add values for new keys only.
263
264 `deep`
265 : Add values for new keys, and merge values for existing keys.
266
267 You don't need to be as verbose as the default setup below. A `_merge` value set higher up is inherited if not set lower down.
268
269 {{< code-toggle file=hugo dataKey="config_helpers.mergeStrategy" skipHeader=true />}}
270
271 ### Root-level merge strategy
272
273 You can also set `_merge` at the root of your project configuration, outside of any configuration category, to change this behavior for the whole project:
274
275 {{< code-toggle file=hugo >}}
276 _merge = 'none'
277 baseURL = 'https://example.org/'
278 locale = 'en-us'
279 title = 'My New Hugo Site'
280 theme = ['theme-a','theme-b']
281 {{< /code-toggle >}}
282
283 A root-level `_merge` set to `none` disables merging of theme and module configuration entirely, regardless of any `_merge` value set on individual configuration categories. A root-level `_merge` set to `shallow` or `deep` instead changes the default merge strategy for configuration categories that do not specify their own `_merge` value.
284
285 > [!NOTE]
286 > Hugo can merge map configuration values from modules and themes into the project configuration, but cannot merge slice values. This applies to slice-typed configuration categories such as `menus`, as well as to map keys whose values are slices, such as the per-kind format lists in `outputs`.
287
288 ### Security implications
289
290 Most configuration categories default to a `none` merge strategy specifically to protect your project from third-party themes and modules.
291
292 > [!CAUTION]
293 > Setting `_merge` to `shallow` or `deep` removes that protection, whether applied directly to a security-sensitive key such as `markup` or `security`, or set at the root of your configuration to change the default for every key. Only use a permissive `_merge` value for these keys if you trust every theme and module in your project.
294
295 ## Environment variables
296
297 You can also configure settings using operating system environment variables:
298
299 ```sh
300 export HUGO_BASEURL=https://example.org/
301 export HUGO_ENABLEGITINFO=true
302 hugo
303 ```
304
305 The above configures the [`baseURL`][] and [`enableGitInfo`][] settings and then builds your site.
306
307 > [!NOTE]
308 > An environment variable takes precedence over the values set in the configuration file. This means that if you set a configuration value with both an environment variable and in the configuration file, Hugo uses the value from the environment variable.
309
310 Environment variables simplify configuration for [CI/CD](g) platforms by allowing you to set values directly within their respective configuration and workflow files.
311
312 > [!NOTE]
313 > Environment variable names must be prefixed with `HUGO_`.
314 >
315 > To set custom site parameters, prefix the name with `HUGO_PARAMS_`.
316
317 For snake_case variable names, the standard `HUGO_` prefix won't work. Hugo infers the delimiter from the first character following `HUGO`. This allows for variations like `HUGOxPARAMSxAPI_KEY=abcdefgh` using any [permitted delimiter][].
318
319 In addition to configuring standard settings, environment variables may be used to override default values for certain internal settings:
320
321 `DART_SASS_BINARY`
322 : (`string`) The absolute path to the Dart Sass executable. By default, Hugo searches for the executable in each of the paths in the `PATH` environment variable.
323
324 `HUGO_ENVIRONMENT`
325 : (`string`) The build environment. Default is `production` when running `hugo build` and `development` when running `hugo server`.
326
327 `HUGO_FILE_LOG_FORMAT`
328 : (`string`) A format string for the file path, line number, and column number displayed when reporting errors, or when calling the `Position` method from a shortcode or Markdown render hook. Valid tokens are `:file`, `:line`, and `:col`. Default is `:file::line::col`.
329
330 `HUGO_MEMORYLIMIT`
331 : (`int`) The maximum amount of system memory, in gigabytes, that Hugo can use while rendering your site. Default is 25% of total system memory. Note that `HUGO_MEMORYLIMIT` is a "best effort" setting. Don't expect Hugo to build a million pages with only 1 GB of memory. You can get more information about how this behaves during the build by running `hugo build --logLevel info` and look for the `dynacache` label.
332
333 `HUGO_NUMWORKERMULTIPLIER`
334 : (`int`) The number of workers used in parallel processing. Default is the number of logical CPUs.
335
336 ## Current configuration
337
338 Display the complete project configuration with:
339
340 ```sh
341 hugo config
342 ```
343
344 Display a specific configuration setting with:
345
346 ```sh
347 hugo config | grep [key]
348 ```
349
350 Display the configured file mounts with:
351
352 ```sh
353 hugo config mounts
354 ```
355
356 [Google tag ID]: https://support.google.com/tagmanager/answer/12326985?hl=en
357 [JSON]: https://datatracker.ietf.org/doc/html/rfc7159
358 [TOML]: https://toml.io/en/latest
359 [YAML]: https://yaml.org/spec/
360 [`baseURL`]: /configuration/all#baseurl
361 [`enableGitInfo`]: /configuration/all#enablegitinfo
362 [`markup`]: /configuration/all#markup
363 [`menus`]: /configuration/all#menus
364 [`params`]: /configuration/all#params
365 [`title`]: /configuration/all#title
366 [permitted delimiter]: https://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html