Mike Richards
Mike Richards

Reputation: 21

Hydra defaults using YAML: How to handle optional user configuration?

I'm distributing an application which includes a several commands.

What I'd like to do is

  1. Ensure the search_path contains both the user's homedir and the package where the default configuration is
  2. Write the default configuration like
# default_config.yaml
defaults:
  - _self_
  - .user-config

default1: value1
default2: value2
# ...

But this doesn't work - if .user-config isn't on a search_path Hydra errors with "Could not load '.user-config'."

The way I see to do this with Hydra is

  1. Ensure the search_path contains both the user's homedir and the package where the default configuration is
  2. Write the user configuration as
# .user-config.yaml
defaults:
  - default_config
  - _self_

additional_config: foo
# ...

This makes the user configuration mandatory and also requires the defaults: stanza at the top.

Is there a configuration-based way of doing this or should I just load and merge the configuration in code?

Upvotes: 1

Views: 2620

Answers (1)

Omry Yadan
Omry Yadan

Reputation: 33646

There are a few things you can try:

optional config in the defaults list

Using the optional keyword in your defaults list. Unfortunately this is only supported for configs in a config group.

# default_config.yaml
defaults:
  - _self_
  - optional user: config

# see https://hydra.cc/docs/advanced/search_path/
# Note that this is only supported in the primary config
hydra:
  searchpath:
    - file://${oc.env:HOME}/.my_app

A user can now place ~/my_app/user/config.yaml in the search path. # @package _global_ will ensure that the config here overrides the global scope and not things under the config group node user.

# ~/my_app/user/config.yaml
# @package _global_

additional_config: foo

(Avoid the temptation of adding your home directory directly to the search path, in some cases Hydra scans the search path and it will cause slowdown).

Generate a config dir from a template

You can do something similar to what configen is doing. (IIRC it predated the ability to add the search path to the primary config, which can make things even better).

Basically, it has a Structured Config schema it's using as a default config. It can generate a config dir for the user (with the initial config that contains the defaults list boilerplate).

Upvotes: 1

Related Questions