Skip to content
TypeScript

fix(server): honor relocated systemd user configuration (#29244)

Lakshmi Tanmayb1913b99c8c7
diff --git a/apps/server/src/cloud/bootService.test.ts b/apps/server/src/cloud/bootService.test.ts
index 8fcb026d4a4..4979f8d4f34 100644
--- a/apps/server/src/cloud/bootService.test.ts
+++ b/apps/server/src/cloud/bootService.test.ts
@@ -90,10 +90,19 @@ const macInstallerPath =
const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
platform: NodeJS.Platform = "linux",
installerPath = macInstallerPath,
+ xdgConfigHome?: string,
+ managerXdgConfigHome = xdgConfigHome,
) {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-boot-service-test-" });
+ const managerUnitDir = path.join(
+ managerXdgConfigHome !== undefined && path.isAbsolute(managerXdgConfigHome)
+ ? managerXdgConfigHome
+ : path.join(home, ".config"),
+ "systemd",
+ "user",
+ );
const baseDir = path.join(home, ".t3");
const statePath = path.join(baseDir, "runtime", "service-state.json");
const runtime = pinnedRuntimePaths(path, baseDir, "1.2.3", platform);
@@ -140,15 +149,26 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
}
return {
stdout:
- input.args[0] === "--version"
- ? `t3 v${/versions\/([^/]+)\//.exec(input.command)?.[1] ?? "1.2.3"}\n`
- : input.command === "loginctl" && input.args[0] === "show-user"
- ? `${control.linger}\n`
- : input.args[1] === "is-enabled"
- ? control.enabled
- ? "enabled\n"
- : "disabled\n"
- : "",
+ input.command === "busctl"
+ ? JSON.stringify({
+ type: "as",
+ data: [
+ "/usr/lib/systemd/user",
+ `${managerUnitDir}.control`,
+ "/run/user/501/systemd/transient",
+ managerUnitDir,
+ "/etc/systemd/user",
+ ],
+ })
+ : input.args[0] === "--version"
+ ? `t3 v${/versions\/([^/]+)\//.exec(input.command)?.[1] ?? "1.2.3"}\n`
+ : input.command === "loginctl" && input.args[0] === "show-user"
+ ? `${control.linger}\n`
+ : input.args[1] === "is-enabled"
+ ? control.enabled
+ ? "enabled\n"
+ : "disabled\n"
+ : "",
stderr: "",
code: ChildProcessSpawner.ExitCode(
failed ||
@@ -198,6 +218,7 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
ConfigProvider.fromEnv({
env: {
HOME: home,
+ ...(xdgConfigHome === undefined ? {} : { XDG_CONFIG_HOME: xdgConfigHome }),
...(environmentPath === undefined || environmentPath === ""
? {}
: { PATH: environmentPath }),
@@ -213,6 +234,7 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
service,
makeService,
fs,
+ home,
baseDir,
statePath,
commands,
@@ -315,10 +337,52 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
}),
);
- it.effect("installs, reports current state, and uninstalls", () =>
+ it.effect.each([
+ "unset",
+ "empty",
+ "relative",
+ "relocated",
+ "shell-only",
+ "manager-only",
+ "manager-unavailable",
+ ] as const)("installs, discovers, restarts, and uninstalls with $XDG_CONFIG_HOME %s", (setting) =>
Effect.gen(function* () {
- const { service, fs, statePath, timeouts, runtime } = yield* makeHarness();
+ const path = yield* Path.Path;
+ const fs = yield* FileSystem.FileSystem;
+ const configRoot = yield* fs.makeTempDirectoryScoped({ prefix: "t3-xdg-config-" });
+ const xdgConfigHome = {
+ unset: undefined,
+ empty: "",
+ relative: "relative-config",
+ relocated: path.join(configRoot, "config root"),
+ "shell-only": path.join(configRoot, "config root"),
+ "manager-only": undefined,
+ "manager-unavailable": path.join(configRoot, "config root"),
+ }[setting];
+ const managerXdgConfigHome =
+ setting === "shell-only"
+ ? ""
+ : setting === "manager-only"
+ ? path.join(configRoot, "config root")
+ : undefined;
+ const { service, makeService, home, statePath, timeouts, runtime, control } =
+ yield* makeHarness("linux", undefined, xdgConfigHome, managerXdgConfigHome);
+ const unitPath = path.join(
+ setting === "relocated" || setting === "manager-only" || setting === "manager-unavailable"
+ ? path.join(configRoot, "config root")
+ : path.join(home, ".config"),
+ "systemd",
+ "user",
+ "t3code.service",
+ );
+ expect(yield* service.status).toMatchObject({ installed: false, unitPath });
const plan = yield* service.install();
+ expect(plan.unitPath).toBe(unitPath);
+ if (setting === "manager-unavailable") {
+ control.failCommand =
+ "busctl --user --json=short get-property org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager UnitPath";
+ }
+ const discoveredService = yield* makeService();
expect(parseServiceState(yield* fs.readFileString(statePath))).toEqual({
protocol: SERVICE_LAUNCHER_PROTOCOL,
@@ -332,6 +396,12 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
current: true,
installedVersion: "1.2.3",
});
+ expect(yield* discoveredService.status).toMatchObject({
+ installed: true,
+ current: true,
+ unitPath,
+ });
+ expect(yield* discoveredService.restart).toBe("restarted");
const pendingState = JSON.stringify({
protocol: SERVICE_LAUNCHER_PROTOCOL,
activeVersion: "1.2.3",
@@ -345,8 +415,9 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
});
yield* fs.writeFileString(statePath, pendingState);
expect((yield* service.status).current).toBe(false);
- expect(yield* service.uninstall).toBe(true);
- expect((yield* service.status).installed).toBe(false);
+ expect(yield* discoveredService.uninstall).toBe(true);
+ expect(yield* fs.exists(unitPath)).toBe(false);
+ expect((yield* discoveredService.status).installed).toBe(false);
// systemd can take 90s to stop, exceeding the runner's 60s default.
expect(timeouts.get("systemctl --user disable --now t3code.service")).toEqual(
Duration.seconds(120),
diff --git a/apps/server/src/cloud/bootService.ts b/apps/server/src/cloud/bootService.ts
index efa18413d97..49e748e004d 100644
--- a/apps/server/src/cloud/bootService.ts
+++ b/apps/server/src/cloud/bootService.ts
@@ -213,15 +213,9 @@ export interface BootServiceManager {
function systemdManager(input: {
readonly path: Path.Path;
- readonly homeDir: string;
+ readonly unitDir: string;
}): BootServiceManager {
- const unitPath = input.path.join(
- input.homeDir,
- ".config",
- "systemd",
- "user",
- BOOT_SERVICE_UNIT_FILE,
- );
+ const unitPath = input.path.join(input.unitDir, BOOT_SERVICE_UNIT_FILE);
const prepareActivation = [
{
step: "reloading systemd user units",
@@ -358,6 +352,8 @@ function launchdManager(input: {
function selectBootServiceManager(input: {
readonly platform: NodeJS.Platform;
readonly homeDir: string;
+ readonly xdgConfigHome: string;
+ readonly systemdUnitDir: string | undefined;
readonly uid: number | undefined;
readonly path: Path.Path;
readonly environmentPath: string;
@@ -366,7 +362,18 @@ function selectBootServiceManager(input: {
return undefined;
}
if (input.platform === "linux") {
- return systemdManager({ path: input.path, homeDir: input.homeDir });
+ return systemdManager({
+ path: input.path,
+ unitDir:
+ input.systemdUnitDir ??
+ input.path.join(
+ input.path.isAbsolute(input.xdgConfigHome)
+ ? input.xdgConfigHome
+ : input.path.join(input.homeDir, ".config"),
+ "systemd",
+ "user",
+ ),
+ });
}
if (input.platform === "darwin" && input.uid !== undefined) {
return launchdManager({
@@ -379,6 +386,42 @@ function selectBootServiceManager(input: {
return undefined;
}
+const decodeSystemdUnitPaths = Schema.decodeUnknownEffect(
+ Schema.fromJsonString(Schema.Struct({ data: Schema.Array(Schema.String) })),
+);
+
+// The manager may not share the installer's environment; JSON preserves spaces in its paths.
+const readSystemdUnitDirectory = Effect.fn("cloud.boot_service.read_systemd_unit_directory")(
+ function* () {
+ const runner = yield* ProcessRunner.ProcessRunner;
+ const path = yield* Path.Path;
+ const result = yield* runner.run({
+ command: "busctl",
+ args: [
+ "--user",
+ "--json=short",
+ "get-property",
+ "org.freedesktop.systemd1",
+ "/org/freedesktop/systemd1",
+ "org.freedesktop.systemd1.Manager",
+ "UnitPath",
+ ],
+ timeout: Duration.seconds(5),
+ });
+ if (result.code !== 0) return undefined;
+ const { data } = yield* decodeSystemdUnitPaths(result.stdout);
+ // The persistent control directory identifies the manager's config root despite UnitPath overrides.
+ const controlDirectory = data.find(
+ (directory) =>
+ path.isAbsolute(directory) &&
+ directory.endsWith("/systemd/user.control") &&
+ data.includes(directory.slice(0, -".control".length)),
+ );
+ return controlDirectory?.slice(0, -".control".length);
+ },
+ Effect.orElseSucceed(() => undefined),
+);
+
export class BootServiceUnsupportedError extends Schema.TaggedError<BootServiceUnsupportedError>()(
"BootServiceUnsupportedError",
{ platform: Schema.String },
@@ -536,6 +579,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
yield* Config.String(CLI_RELEASE_BASE_URL_ENV).pipe(Config.option),
);
const homeDir = yield* Config.String("HOME").pipe(Config.withDefault(""));
+ const xdgConfigHome = yield* Config.String("XDG_CONFIG_HOME").pipe(Config.withDefault(""));
const installerPath = yield* Config.String("PATH").pipe(Config.withDefault(""));
const runningInsideService =
(yield* Config.String(BOOT_SERVICE_UNIT_ENV).pipe(Config.withDefault(""))) ===
@@ -573,9 +617,12 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
),
).join(":");
+ const systemdUnitDir = platform === "linux" ? yield* readSystemdUnitDirectory() : undefined;
const detectedManager = selectBootServiceManager({
platform,
homeDir,
+ xdgConfigHome,
+ systemdUnitDir,
uid,
path,
environmentPath,