Skip to content

Configuration ​

Source Graph is configured in four layers, each with a different owner and lifetime:

LayerWhereChanged byTakes effect
Environment variablesProcess environment, for example Fly.io secrets or a container env fileOperatorOn restart
Deployment settingsconfig/deployment.json, embedded at build timeWhoever builds the imageOn the next build
Site settingsDatabase, edited on the Settings pageAdministratorsImmediately, with revision checks
Repository policyDatabase, set per repository at registration or laterAdministratorsOn the next synchronization

Secrets — Git tokens, OAuth client secrets, the service token, the credential key and database URLs with passwords — belong only in environment variables or the database, never in the deployment file or the image. For deployment steps see Self-hosting.

Environment variables ​

VariableDefaultPurpose
SOURCEGRAPH_LISTEN0.0.0.0:8080Listen address
SOURCEGRAPH_ORIGINhttp://localhost:8080Public origin, used for sign-in callbacks and links
SOURCEGRAPH_DATA_ROOT/dataData directory; must be absolute
SOURCEGRAPH_DATABASEsqlitesqlite, postgres or mongodb
DATABASE_URL<data root>/sourcegraph.db for SQLiteDatabase location or connection string
SOURCEGRAPH_MIN_FREE_BYTES1073741824 (1 GiB)Free-space floor below which fetching and indexing stop with INSUFFICIENT_STORAGE
SOURCEGRAPH_API_TOKENUnsetOptional service token of at least 32 characters; a matching bearer token gets full HTTP API access, except site settings, which require a browser session. The CLI reads the same variable
SOURCEGRAPH_CREDENTIAL_KEYGenerated code-credentials.key in the data rootBase64 of 32 bytes; encrypts stored code-host credentials
SOURCEGRAPH_GIT_TOKENUnsetLegacy global GitHub credential, used only while no global github credential has been saved
GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRETUnsetGoogle sign-in
GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRETUnsetGitHub sign-in
SOURCEGRAPH_SEED_DEMOUnsettrue registers context4ai/context and context4ai/sourcegraph at startup if they are missing

Changing SOURCEGRAPH_CREDENTIAL_KEY after credentials have been stored makes them unreadable; keep the key with the database backup. With PostgreSQL or MongoDB, several instances can share one database while each keeps its own data directory.

Deployment settings ​

config/deployment.json is compiled into the binaries and describes what this deployment is, not how it is secured:

FieldCommunity defaultMeaning
code_hosthttps://github.comCode host that registrations and credentials target
example_repocontext4ai/contextRepository used in interface examples
public_urlhttps://context4ai.org/sourcegraphPublic address shown in links and connection snippets
source_repositoryhttps://github.com/context4ai/sourcegraphSource link shown in the interface
ssoGoogle issuer and endpoints, empty client IDSign-in provider metadata, plus registration_url, the provider console where an operator creates the OAuth client; no secrets

A fork that deploys under its own name or address edits this file and rebuilds; sign-in client IDs and secrets still come from the environment.

Site settings ​

Administrators edit two sections on the Settings page or through /api/admin/settings. Each save carries the section's current revision; a stale revision is rejected so two administrators cannot silently overwrite each other.

SectionFieldDefaultMeaning
repository_defaultsretention_modedaysDefault retention for new registrations: days or count
retention_days30Days of history kept, 1–365
retention_count20Commits kept per branch, 1–4,096
accesspublic_readtrueWhether anonymous visitors can search and read public repositories
allow_registrationfalseWhether a first-time sign-in creates an account
registration_notice_zh, registration_notice_enA built-in noticeText shown to people who cannot register

Requests to /api/admin/settings must use a browser session; requests carrying an Authorization header are refused. The Storage page separately moves where Git mirrors are kept; see Operations.

Repository policy ​

Each registration has its own policy, initialized from the repository defaults:

SettingRangeDefault
Monitored branches1–8The default branch
RetentionDays 1–365 or commits 1–4,096, not bothSite default
Sync interval (minutes)1, 3, 5, 10, 30, 60, 180, 360, 720 or 14405
Index modeWhole repository, or registered path groups for a monorepoWhole repository
Public readOn or offOn
Credential kind (code_provider)github or git; the registrant's own credential of that kind is tried first, then the global onegithub

A repository with public read off is hidden from anonymous visitors only; every signed-in user can still read it. See Registering repositories for how branches and retention decide which commits are searchable.

Plugin limits ​

Wasm evidence plugins run inside the service under resource guards set by SOURCEGRAPH_PLUGINS_* variables. Invalid values stop startup rather than removing a guard.

Variable suffixDefaultLimits
DISABLEDUnset1 disables plugins
TIMEOUT10sWall time per run (Go duration)
MEMORY_MIB256Memory per instance, at most 4,096
ARTIFACT_BYTES32 MiBPlugin module size
METADATA_BYTES256 KiBPlugin metadata size
FILE_BYTES32 MiBSize of one file the plugin reads
READ_BYTES128 MiBTotal bytes read per run
READ_CALLS1,024File reads per run
OUTPUT_BYTES2 MiBOutput size
CONCURRENCY8Concurrent runs
CACHE_ENTRIES16Compiled modules kept
IDLE_INSTANCES8Warm instances kept

Built-in limits ​

Some limits are fixed in code and cannot be configured: request bodies up to 64 KiB, a 30-second request deadline, HTTP server timeouts of 5 seconds for headers, 120 seconds for reading and 125 seconds for writing, and the search, read, list and diff page sizes described in HTTP and MCP. The search engine runs as a local helper on 127.0.0.1:6070 and is never exposed.

Data directory ​

Path under the data rootContentsRebuildable
sourcegraph.dbSQLite database: users, keys, settings, registrations, credentialsNo
code-credentials.keyGenerated credential key, when SOURCEGRAPH_CREDENTIAL_KEY is unsetNo
storage.jsonWhere Git mirrors are stored on this machineNo
space/Bare Git mirrors, unless moved on the Storage pageYes, from the remotes
index/, manifests/Search index shards and the manifests that describe themYes, by indexing again

The container runs as an unprivileged user (uid 10001) and needs write access to the whole data root.

配置 ​

Source Graph 的配置分为四层,每层的负责人和生效周期各不相同:

层级位置修改者生效时机
环境变量进程环境,例如 Fly.io secrets 或容器 env 文件运维人员重启后
部署设置config/deployment.json,构建时嵌入构建镜像的人下次构建后
站点设置数据库,在 Settings(设置)页面编辑管理员立即生效,带版本校验
仓库策略数据库,按仓库在登记时或之后设置管理员下次同步时

机密信息,包括 Git 令牌、OAuth 客户端密钥、服务令牌、凭据加密密钥以及带密码的数据库 URL,只能放在环境变量或数据库中,绝不能放进部署文件或镜像。部署步骤见自部署与数据库选择。

环境变量 ​

变量默认值用途
SOURCEGRAPH_LISTEN0.0.0.0:8080监听地址
SOURCEGRAPH_ORIGINhttp://localhost:8080公开源地址,用于登录回调和链接
SOURCEGRAPH_DATA_ROOT/data数据目录;必须是绝对路径
SOURCEGRAPH_DATABASEsqlitesqlite、postgres 或 mongodb
DATABASE_URLSQLite 为 <data root>/sourcegraph.db数据库位置或连接字符串
SOURCEGRAPH_MIN_FREE_BYTES1073741824(1 GiB)剩余空间下限,低于该值时拉取和建索引会停止,并返回 INSUFFICIENT_STORAGE
SOURCEGRAPH_API_TOKEN未设置可选的服务令牌,至少 32 个字符;匹配的 Bearer 令牌拥有完整的 HTTP API 访问权限,但站点设置除外,站点设置需要浏览器会话。CLI 也读取同一变量
SOURCEGRAPH_CREDENTIAL_KEY在数据根目录中生成的 code-credentials.key32 字节的 Base64;用于加密已存储的代码托管平台凭据
SOURCEGRAPH_GIT_TOKEN未设置旧版全局 GitHub 凭据,仅在尚未保存全局 github 凭据时使用
GOOGLE_CLIENT_ID、GOOGLE_CLIENT_SECRET未设置Google 登录
GITHUB_CLIENT_ID、GITHUB_CLIENT_SECRET未设置GitHub 登录
SOURCEGRAPH_SEED_DEMO未设置设为 true 时,若 context4ai/context 和 context4ai/sourcegraph 尚未登记,会在启动时登记它们

在已存储凭据后修改 SOURCEGRAPH_CREDENTIAL_KEY 会导致这些凭据无法读取;请将该密钥与数据库备份一同保管。使用 PostgreSQL 或 MongoDB 时,多个实例可以共享同一个数据库,同时各自保留自己的数据目录。

部署设置 ​

config/deployment.json 会被编译进二进制文件,描述的是这个部署是什么,而不是如何保障其安全:

字段社区版默认值含义
code_hosthttps://github.com登记和凭据所针对的代码托管平台
example_repocontext4ai/context界面示例中使用的仓库
public_urlhttps://context4ai.org/sourcegraph在链接和连接代码片段中显示的公开地址
source_repositoryhttps://github.com/context4ai/sourcegraph界面中显示的源码链接
ssoGoogle 签发方和端点,客户端 ID 为空登录提供方元数据,另含 registration_url,即运维人员创建 OAuth 客户端的提供方控制台;不含任何机密

以自己的名称或地址部署的 fork 需要修改此文件并重新构建;登录所用的客户端 ID 和密钥仍来自环境变量。

站点设置 ​

管理员可以在 Settings 页面或通过 /api/admin/settings 编辑两个分区。每次保存都会携带该分区的当前版本;过期的版本会被拒绝,避免两位管理员在不知情的情况下互相覆盖。

分区字段默认值含义
repository_defaultsretention_modedays新登记仓库的默认保留策略:days 或 count
retention_days30保留的历史天数,1–365
retention_count20每个分支保留的提交数,1–4,096
accesspublic_readtrue匿名访客能否搜索和阅读公开仓库
allow_registrationfalse首次登录时是否创建账号
registration_notice_zh、registration_notice_en内置提示向无法注册的人显示的文本

对 /api/admin/settings 的请求必须使用浏览器会话;携带 Authorization 请求头的请求会被拒绝。Storage(存储)页面另外负责迁移 Git 镜像的存放位置;见运维。

仓库策略 ​

每份登记信息都有自己的策略,初始值取自仓库默认值:

设置范围默认值
监控分支1–8 个默认分支
保留策略天数 1–365 或提交数 1–4,096,二者不可同时设置站点默认值
同步间隔(分钟)1、3、5、10、30、60、180、360、720 或 14405
索引模式整个仓库,或 Monorepo 中已登记的路径组整个仓库
公开读取开或关开
凭据类型(code_provider)github 或 git;先尝试登记人自己的该类型凭据,再尝试全局凭据github

关闭公开读取的仓库只对匿名访客隐藏;所有已登录用户仍然可以阅读。分支和保留策略如何决定哪些提交可被搜索,见登记仓库。

插件限制 ​

Wasm 证据插件在服务内部运行,受 SOURCEGRAPH_PLUGINS_* 变量设置的资源防护约束。无效值会导致启动失败,而不是移除对应的防护。

变量后缀默认值限制内容
DISABLED未设置设为 1 时禁用插件
TIMEOUT10s每次运行的墙钟时间(Go duration 格式)
MEMORY_MIB256每个实例的内存,最多 4,096
ARTIFACT_BYTES32 MiB插件模块大小
METADATA_BYTES256 KiB插件元数据大小
FILE_BYTES32 MiB插件读取的单个文件大小
READ_BYTES128 MiB每次运行读取的总字节数
READ_CALLS1,024每次运行的文件读取次数
OUTPUT_BYTES2 MiB输出大小
CONCURRENCY8并发运行数
CACHE_ENTRIES16保留的已编译模块数
IDLE_INSTANCES8保留的预热实例数

内置限制 ​

有些限制在代码中写死,无法配置:请求体最大 64 KiB,请求截止时间 30 秒,HTTP 服务器超时分别为请求头 5 秒、读取 120 秒、写入 125 秒,以及HTTP 与 MCP 接口中描述的搜索、读取、列目录和差异分页大小。搜索引擎作为本地辅助进程运行在 127.0.0.1:6070 上,从不对外暴露。

数据目录 ​

数据根目录下的路径内容可否重建
sourcegraph.dbSQLite 数据库:用户、Key、设置、登记信息、凭据否
code-credentials.key未设置 SOURCEGRAPH_CREDENTIAL_KEY 时生成的凭据加密密钥否
storage.json本机上 Git 镜像的存放位置否
space/裸 Git 镜像,除非已在 Storage 页面迁移可以,从远端重新拉取
index/、manifests/搜索索引分片及描述它们的清单可以,重新建索引即可

容器以非特权用户(uid 10001)运行,需要对整个数据根目录拥有写权限。