From daf3285584c3f51a6c57e3e14b2124b453a9a577 Mon Sep 17 00:00:00 2001 From: Priyanshubhartistm Date: Thu, 3 Dec 2026 20:47:40 +0530 Subject: [PATCH] fix(cli): mount the configuration directory the CLI set up into the relay Signed-off-by: Priyanshubhartistm --- .changeset/compose-mount-nostr-config-dir.md | 5 ++ README.md | 1 + docker-compose.yml | 4 +- src/cli/utils/docker.ts | 11 +++- test/unit/cli/docker.spec.ts | 69 +++++++++++++++++++- 5 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 .changeset/compose-mount-nostr-config-dir.md diff --git a/.changeset/compose-mount-nostr-config-dir.md b/.changeset/compose-mount-nostr-config-dir.md new file mode 100644 index 000000000..417898508 --- /dev/null +++ b/.changeset/compose-mount-nostr-config-dir.md @@ -0,0 +1,5 @@ +--- +"nostream": patch +--- + +`nostream start` now mounts the configuration directory it prepared settings in (`NOSTR_CONFIG_DIR`) into the relay container. The compose file mounted `.nostr` in the project whatever `NOSTR_CONFIG_DIR` was, so a relay started with another directory read the default settings from the project instead, without the admission restrictions saved in the directory the CLI had set up. A plain `docker compose up` still mounts `.nostr` in the project. diff --git a/README.md b/README.md index 0398d141a..537116324 100644 --- a/README.md +++ b/README.md @@ -206,6 +206,7 @@ walkthrough, see the development quick start in ## Configuration You can change the default folder by setting the `NOSTR_CONFIG_DIR` environment variable to a different path. +The `nostream` CLI mounts that folder into the relay container, so `nostream start` runs with the settings in it. Run nostream using one of the quick-start guides at least once and `nostream/.nostr/settings.yaml` will be created. Any changes made to the settings file will be read on the next start. diff --git a/docker-compose.yml b/docker-compose.yml index 719832a9c..5f6fb24cc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -70,7 +70,9 @@ services: # DEBUG: "knex:query" user: node:node volumes: - - ${PWD}/.nostr:/home/node/.nostr + # The nostream CLI sets NOSTR_CONFIG_HOST_DIR to the directory it prepared settings in, so the relay reads those + # settings. A separate name keeps this off NOSTR_CONFIG_DIR, which here is the path inside the container. + - ${NOSTR_CONFIG_HOST_DIR:-${PWD}/.nostr}:/home/node/.nostr ports: - 127.0.0.1:8008:8008 depends_on: diff --git a/src/cli/utils/docker.ts b/src/cli/utils/docker.ts index 8cf24fce8..287abcfba 100644 --- a/src/cli/utils/docker.ts +++ b/src/cli/utils/docker.ts @@ -1,8 +1,8 @@ import fs from 'fs' import os from 'os' -import { join } from 'path' +import { join, resolve } from 'path' -import { getProjectPath } from './paths' +import { getConfigBaseDir, getProjectPath } from './paths' import { runCommand } from './process' export type ComposeOptions = { @@ -26,9 +26,14 @@ export const buildComposeArgs = (files: string[], args: string[]): string[] => { return [...out, ...args] } +// The directory the CLI prepares settings in (NOSTR_CONFIG_DIR, or .nostr in the project) on the host. The compose file +// mounts it into the relay under another name, because NOSTR_CONFIG_DIR is the path inside the container there, so a +// relay started from a different configuration directory does not read the defaults from the project's .nostr instead. +export const composeConfigEnv = (): NodeJS.ProcessEnv => ({ NOSTR_CONFIG_HOST_DIR: resolve(getConfigBaseDir()) }) + export const runDockerCompose = async ({ files, args, env }: ComposeOptions): Promise => { const composeArgs = buildComposeArgs(files, args) - return runCommand('docker', ['compose', ...composeArgs], { env }) + return runCommand('docker', ['compose', ...composeArgs], { env: { ...composeConfigEnv(), ...env } }) } export const createPortOverrideComposeFile = (port: number): string => { diff --git a/test/unit/cli/docker.spec.ts b/test/unit/cli/docker.spec.ts index 964b257da..e40b6a2a5 100644 --- a/test/unit/cli/docker.spec.ts +++ b/test/unit/cli/docker.spec.ts @@ -1,10 +1,17 @@ import { expect } from 'chai' import fs from 'fs' +import yaml from 'js-yaml' import os from 'os' import path from 'path' import sinon from 'sinon' -import { buildComposeArgs, createPortOverrideComposeFile } from '../../../src/cli/utils/docker' +import * as processUtils from '../../../src/cli/utils/process' +import { + buildComposeArgs, + composeConfigEnv, + createPortOverrideComposeFile, + runDockerCompose, +} from '../../../src/cli/utils/docker' describe('cli docker utils', () => { afterEach(() => { @@ -12,7 +19,9 @@ describe('cli docker utils', () => { }) it('includes only existing compose files', () => { - const existsSyncStub = sinon.stub(fs, 'existsSync').callsFake((input) => String(input).includes('docker-compose.yml')) + const existsSyncStub = sinon + .stub(fs, 'existsSync') + .callsFake((input) => String(input).includes('docker-compose.yml')) const args = buildComposeArgs(['docker-compose.yml', 'docker-compose.tor.yml'], ['up']) @@ -31,4 +40,60 @@ describe('cli docker utils', () => { fs.unlinkSync(tempFile) }) + + describe('the configuration directory the relay is started with', () => { + let previousConfigDir: string | undefined + + beforeEach(() => { + previousConfigDir = process.env.NOSTR_CONFIG_DIR + }) + + afterEach(() => { + if (previousConfigDir === undefined) { + delete process.env.NOSTR_CONFIG_DIR + } else { + process.env.NOSTR_CONFIG_DIR = previousConfigDir + } + }) + + it('is the one NOSTR_CONFIG_DIR names', () => { + process.env.NOSTR_CONFIG_DIR = '/srv/relay-conf' + + expect(composeConfigEnv()).to.deep.equal({ NOSTR_CONFIG_HOST_DIR: '/srv/relay-conf' }) + }) + + it('is an absolute path, since the compose file mounts it', () => { + process.env.NOSTR_CONFIG_DIR = 'conf/relay' + + expect(composeConfigEnv()).to.deep.equal({ NOSTR_CONFIG_HOST_DIR: path.resolve('conf/relay') }) + }) + + it('is .nostr in the project when NOSTR_CONFIG_DIR is not set', () => { + delete process.env.NOSTR_CONFIG_DIR + + expect(composeConfigEnv()).to.deep.equal({ NOSTR_CONFIG_HOST_DIR: path.join(process.cwd(), '.nostr') }) + }) + + it('reaches docker compose next to the variables the command adds', async () => { + process.env.NOSTR_CONFIG_DIR = '/srv/relay-conf' + const runCommandStub = sinon.stub(processUtils, 'runCommand').resolves(0) + + await runDockerCompose({ files: [], args: ['up'], env: { DEBUG: 'primary:*' } }) + + expect(runCommandStub.firstCall.args[0]).to.equal('docker') + expect(runCommandStub.firstCall.args[2]).to.deep.equal({ + env: { NOSTR_CONFIG_HOST_DIR: '/srv/relay-conf', DEBUG: 'primary:*' }, + }) + }) + }) + + it('mounts the configuration directory the CLI prepared into the relay, and .nostr when there is none', () => { + const compose = yaml.load(fs.readFileSync(path.join(process.cwd(), 'docker-compose.yml'), 'utf-8')) as { + services: { nostream: { volumes: string[] } } + } + + const mounts = compose.services.nostream.volumes.filter((volume) => volume.endsWith(':/home/node/.nostr')) + + expect(mounts).to.deep.equal(['${NOSTR_CONFIG_HOST_DIR:-${PWD}/.nostr}:/home/node/.nostr']) + }) })