Skip to content

Conversation

@AzideCupric
Copy link
Contributor

@AzideCupric AzideCupric commented Sep 6, 2025

like PLUGIN_CONFIG_A=true nb run

@github-actions
Copy link
Contributor

github-actions bot commented Sep 6, 2025

🚀 Deployed to https://deploy-preview-3673--nonebot2.netlify.app

@yanyongyu
Copy link
Member

加载所有环境变量的意义是什么?为什么会有这个需求啊。。。插件就应该定义插件配置类通过 get_plugin_config 规范加载。

@AzideCupric
Copy link
Contributor Author

加载所有环境变量的意义是什么?为什么会有这个需求啊。。。插件就应该定义插件配置类通过 get_plugin_config 规范加载。

是为了不需要在.env 里声明这些配置,就能在nb启动时读取进去,并没有影响到插件原本通过 get_plugin_config 规范加载的行为

比如构建了一个Docker,里面安装了插件A,它有个配置A_X: int = 0
如果仅仅在启动的时候传入 -e A_X=1,nb启动的时候是不会自动去读取它的,需要在.env文件里重新声明一次这个配置项A_X
这个行为容易导致

  • 插件新增了一个新的配置项,docker启动时配置了配置项,但是因为没有在.env里声明而不生效
  • docker中整合了多个插件,为了能在后续使用时,能通过docker环境变量来修改相关配置,必须要把这些插件的配置项都声明到.env,所依赖的插件如果有配置变动,整合仓库也需要同步.env文件内容

@he0119
Copy link
Member

he0119 commented Sep 6, 2025

容器环境相对来说也比较可控,有这个配置确实会方便不少。不需要通过挂载 .env 文件来修改配置,也不需要每次添加新插件时都修改容器内的 .env 文件了,感觉确实是痛点。

比如 he0119/CoolQBot@4105380

@yanyongyu
Copy link
Member

如果是这种场景,我更倾向于修改 get_plugin_config,因为可以明确知道需要哪些环境变量。

@he0119
Copy link
Member

he0119 commented Sep 6, 2025

如果是这种场景,我更倾向于修改 get_plugin_config,因为可以明确知道需要哪些环境变量。

确实,很合理。

@AzideCupric AzideCupric changed the title ✨ 添加ENV_GREEDY_LOAD配置项以允许启动时读取所有环境变量 ✨ 允许插件从环境变量中读取配置项而不需要在envfile中声明 Sep 6, 2025
@AzideCupric
Copy link
Contributor Author

现在插件会在get_plugin_config时从环境变量里查找配置项了

@yanyongyu
Copy link
Member

为什么不直接通过获取配置类的字段对应环境变量 dict 与 driver.config 直接 validate。dotenv file 里的配置已经加载到 driver.config 里了。

@yanyongyu yanyongyu added the enhancement New feature or request label Sep 7, 2025
@AzideCupric
Copy link
Contributor Author

修改了亿点点
大抵应该是这样吧(x

@yanyongyu
Copy link
Member

我理解这东西完全不需要修改 config source 吧,难道不是直接通过 model_fields 获取 fields 然后读环境变量,和 model_dump(driver.config) 组成一个 dict 再 model_validate 就行?

@AzideCupric
Copy link
Contributor Author

我理解这东西完全不需要修改 config source 吧,难道不是直接通过 model_fields 获取 fields 然后读环境变量,和 model_dump(driver.config) 组成一个 dict 再 model_validate 就行?

不复用的话,插件 config fields 和 环境变量 之间的大小写敏感处理、配置项别名映射、嵌套字典解析到scope配置(A__B=1 => {A: {B: 1}})这些部分不是单纯model_validate能处理的吧

@yanyongyu yanyongyu changed the title ✨ 允许插件从环境变量中读取配置项而不需要在envfile中声明 Feature: 允许插件从环境变量中读取配置项并支持 alias Oct 18, 2025
@yanyongyu yanyongyu requested a review from Copilot October 18, 2025 10:08
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for plugins to read configuration from environment variables and supports field aliases. Previously, NoneBot wouldn't automatically read environment variables for plugin configurations unless declared in dotenv files. Now, get_plugin_config can directly read plugin configuration from environment variables following the same priority order as global configuration loading.

Key Changes:

  • Modified get_plugin_config to use BaseSettings._settings_build_values for reading configuration from both global config and environment variables
  • Enhanced DotEnvSettingsSource to support pydantic field aliases with higher priority over regular field names
  • Refactored BaseSettings initialization to store env file settings and made _settings_build_values a static method

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
nonebot/plugin/init.py Updated get_plugin_config to read from environment variables using BaseSettings._settings_build_values
nonebot/config.py Refactored settings sources to support aliases and made _settings_build_values static for reuse
tests/test_plugin/test_get.py Added test cases for plugin config with environment variables and aliases
tests/test_config.py Added test for aliased field in base config
tests/.env.example Added example aliased environment variable
website/docs/appendices/config.mdx Updated documentation to clarify plugin config loading behavior

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@yanyongyu yanyongyu merged commit efa3ac3 into nonebot:master Oct 18, 2025
39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Development

Successfully merging this pull request may close these issues.

3 participants