Skip to content

Commit 6f04a5c

Browse files
fix(desktop): Niri/Hyprland - Linux secret storage backend (pingdotgg#2916)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 30c9622 commit 6f04a5c

24 files changed

Lines changed: 1186 additions & 31 deletions

‎apps/desktop/src/app/DesktopApp.ts‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as Crypto from "effect/Crypto";
99
import * as ElectronApp from "../electron/ElectronApp.ts";
1010
import * as ElectronDialog from "../electron/ElectronDialog.ts";
1111
import * as ElectronProtocol from "../electron/ElectronProtocol.ts";
12+
import * as ElectronSafeStorage from "../electron/ElectronSafeStorage.ts";
1213
import { installDesktopIpcHandlers } from "../ipc/DesktopIpcHandlers.ts";
1314
import * as DesktopAppIdentity from "./DesktopAppIdentity.ts";
1415
import * as DesktopClerk from "./DesktopClerk.ts";
@@ -19,6 +20,7 @@ import * as DesktopEnvironment from "./DesktopEnvironment.ts";
1920
import * as DesktopLifecycle from "./DesktopLifecycle.ts";
2021
import * as DesktopLinuxUrlHandler from "./DesktopLinuxUrlHandler.ts";
2122
import * as DesktopObservability from "./DesktopObservability.ts";
23+
import * as DesktopPreReadyPlatform from "./DesktopPreReadyPlatform.ts";
2224
import * as DesktopShutdown from "./DesktopShutdown.ts";
2325
import * as DesktopServerExposure from "../backend/DesktopServerExposure.ts";
2426
import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
@@ -225,17 +227,45 @@ const startup = Effect.gen(function* () {
225227
const clerk = yield* DesktopClerk.DesktopClerk;
226228
const shellEnvironment = yield* DesktopShellEnvironment.DesktopShellEnvironment;
227229
const desktopSettings = yield* DesktopAppSettings.DesktopAppSettings;
230+
const preReadyElectronOptions = yield* DesktopPreReadyPlatform.DesktopPreReadyElectronOptions;
231+
const safeStorage = yield* ElectronSafeStorage.ElectronSafeStorage;
228232
const updates = yield* DesktopUpdates.DesktopUpdates;
229233
const environment = yield* DesktopEnvironment.DesktopEnvironment;
230234

231235
yield* shellEnvironment.installIntoProcess;
236+
const hasCommandLinePasswordStore =
237+
preReadyElectronOptions.linuxPasswordStoreCommandLine !== null;
238+
const linuxElectronOptions =
239+
environment.platform === "linux" && !hasCommandLinePasswordStore
240+
? DesktopPreReadyPlatform.resolveEarlyLinuxElectronOptionsFromProcess()
241+
: preReadyElectronOptions.linux;
242+
if (linuxElectronOptions !== null && !hasCommandLinePasswordStore) {
243+
if (
244+
linuxElectronOptions.passwordStore !== null ||
245+
preReadyElectronOptions.linux?.passwordStore !== null
246+
) {
247+
yield* electronApp.removeCommandLineSwitch("password-store");
248+
}
249+
if (linuxElectronOptions.passwordStore !== null) {
250+
yield* electronApp.appendCommandLineSwitch(
251+
"password-store",
252+
linuxElectronOptions.passwordStore,
253+
);
254+
}
255+
}
232256
const userDataPath = yield* appIdentity.resolveUserDataPath;
233257
yield* electronApp.setPath("userData", userDataPath);
234258
yield* logStartupInfo("runtime logging configured", { logDir: environment.logDir });
235259
yield* desktopSettings.load;
236260

237-
if (environment.platform === "linux") {
238-
yield* electronApp.appendCommandLineSwitch("class", environment.linuxWmClass);
261+
if (linuxElectronOptions !== null) {
262+
yield* logStartupInfo("linux password store configured", {
263+
passwordStore: hasCommandLinePasswordStore
264+
? "command-line"
265+
: (linuxElectronOptions.passwordStore ?? "electron-default"),
266+
xdgCurrentDesktop: process.env.XDG_CURRENT_DESKTOP ?? null,
267+
xdgSessionDesktop: process.env.XDG_SESSION_DESKTOP ?? null,
268+
});
239269
}
240270

241271
yield* appIdentity.configure;
@@ -247,6 +277,12 @@ const startup = Effect.gen(function* () {
247277
Effect.catchCause((cause) => fatalStartupCause("whenReady", cause)),
248278
);
249279
yield* logStartupInfo("app ready");
280+
if (environment.platform === "linux") {
281+
const selectedBackend = yield* safeStorage.selectedStorageBackend;
282+
yield* logStartupInfo("safe storage ready", {
283+
backend: Option.getOrElse(selectedBackend, () => "unknown"),
284+
});
285+
}
250286
yield* appIdentity.configure;
251287
yield* applicationMenu.configure;
252288
yield* updates.configure;

‎apps/desktop/src/app/DesktopAppIdentity.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const makeElectronAppLayer = (calls: ElectronAppCalls) =>
6464
}),
6565
appendCommandLineSwitch: () => Effect.void,
6666
onBeforeQuitForUpdate: () => Effect.void,
67+
removeCommandLineSwitch: () => Effect.void,
6768
on: () => Effect.void,
6869
} satisfies ElectronApp.ElectronApp["Service"]);
6970

‎apps/desktop/src/app/DesktopConnectionCatalogStore.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function makeSafeStorageLayer(available: boolean, failDecrypt: Ref.Ref<boolean>
3939
return decoded.slice("encrypted:".length);
4040
});
4141
},
42+
selectedStorageBackend: Effect.succeed(Option.none()),
4243
} satisfies ElectronSafeStorage.ElectronSafeStorage["Service"]);
4344
}
4445

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - tests use POSIX path joining to match the Linux startup boundary.
2+
import * as NodePath from "node:path";
3+
import { assert, describe, it } from "@effect/vitest";
4+
5+
import {
6+
resolveEarlyLinuxElectronOptions,
7+
resolveEarlyLinuxPasswordStorePreference,
8+
} from "./DesktopEarlyElectronStartup.ts";
9+
10+
describe("DesktopEarlyElectronStartup", () => {
11+
const joinPath = NodePath.posix.join;
12+
13+
it("reads the persisted linux password-store preference before Electron is ready", () => {
14+
const preference = resolveEarlyLinuxPasswordStorePreference({
15+
env: { T3CODE_HOME: "/home/user/.t3-test" },
16+
homeDirectory: "/home/user",
17+
joinPath,
18+
readFileString: (path) => {
19+
assert.equal(path, "/home/user/.t3-test/userdata/desktop-settings.json");
20+
return JSON.stringify({ linuxPasswordStore: "kwallet6" });
21+
},
22+
});
23+
24+
assert.equal(preference, "kwallet6");
25+
});
26+
27+
it("accepts JSONC in the early desktop settings file", () => {
28+
const preference = resolveEarlyLinuxPasswordStorePreference({
29+
env: { T3CODE_HOME: "/home/user/.t3-test" },
30+
homeDirectory: "/home/user",
31+
joinPath,
32+
readFileString: () => `{
33+
// manually edited setting
34+
"linuxPasswordStore": "gnome-libsecret",
35+
}`,
36+
});
37+
38+
assert.equal(preference, "gnome-libsecret");
39+
});
40+
41+
it("falls back to auto when the early settings document is missing or invalid", () => {
42+
const preference = resolveEarlyLinuxPasswordStorePreference({
43+
env: {},
44+
homeDirectory: "/home/user",
45+
joinPath,
46+
readFileString: () => {
47+
throw new Error("missing");
48+
},
49+
});
50+
51+
assert.equal(preference, "auto");
52+
});
53+
54+
it("preserves absolute root paths when resolving early settings", () => {
55+
const preference = resolveEarlyLinuxPasswordStorePreference({
56+
env: { T3CODE_HOME: "/" },
57+
homeDirectory: "/home/user",
58+
joinPath,
59+
readFileString: (path) => {
60+
assert.equal(path, "/userdata/desktop-settings.json");
61+
return JSON.stringify({ linuxPasswordStore: "kwallet6" });
62+
},
63+
});
64+
65+
assert.equal(preference, "kwallet6");
66+
});
67+
68+
it("resolves the early linux Electron switches", () => {
69+
const options = resolveEarlyLinuxElectronOptions({
70+
env: {
71+
T3CODE_HOME: "/home/user/.t3-test",
72+
XDG_CURRENT_DESKTOP: "niri",
73+
VITE_DEV_SERVER_URL: "http://127.0.0.1:5173",
74+
},
75+
homeDirectory: "/home/user",
76+
joinPath,
77+
readFileString: (path) => {
78+
assert.equal(path, "/home/user/.t3-test/userdata/desktop-settings.json");
79+
return JSON.stringify({ linuxPasswordStore: "auto" });
80+
},
81+
});
82+
83+
assert.deepEqual(options, {
84+
linuxWmClass: "t3code-dev",
85+
passwordStore: "gnome-libsecret",
86+
});
87+
});
88+
89+
it("keeps implicit development state under ~/.t3/dev when T3CODE_HOME is unset", () => {
90+
const preference = resolveEarlyLinuxPasswordStorePreference({
91+
env: {
92+
VITE_DEV_SERVER_URL: "http://127.0.0.1:5173",
93+
},
94+
homeDirectory: "/home/user",
95+
joinPath,
96+
readFileString: (path) => {
97+
assert.equal(path, "/home/user/.t3/dev/desktop-settings.json");
98+
return JSON.stringify({ linuxPasswordStore: "kwallet" });
99+
},
100+
});
101+
102+
assert.equal(preference, "kwallet");
103+
});
104+
105+
it("treats whitespace-only T3CODE_HOME as unconfigured in development", () => {
106+
const preference = resolveEarlyLinuxPasswordStorePreference({
107+
env: {
108+
T3CODE_HOME: " ",
109+
VITE_DEV_SERVER_URL: "http://127.0.0.1:5173",
110+
},
111+
homeDirectory: "/home/user",
112+
joinPath,
113+
readFileString: (path) => {
114+
assert.equal(path, "/home/user/.t3/dev/desktop-settings.json");
115+
return JSON.stringify({ linuxPasswordStore: "gnome-libsecret" });
116+
},
117+
});
118+
119+
assert.equal(preference, "gnome-libsecret");
120+
});
121+
});
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { fromLenientJson } from "@t3tools/shared/schemaJson";
2+
import * as Option from "effect/Option";
3+
import * as Schema from "effect/Schema";
4+
5+
import {
6+
DEFAULT_LINUX_PASSWORD_STORE,
7+
normalizeLinuxPasswordStorePreference,
8+
resolveLinuxPasswordStoreSwitch,
9+
type LinuxPasswordStoreSwitch,
10+
type LinuxPasswordStorePreference,
11+
} from "../linuxSecretStorage.ts";
12+
import {
13+
resolveDesktopBaseDir,
14+
resolveDesktopStateDir,
15+
type JoinPath,
16+
} from "./DesktopStatePaths.ts";
17+
18+
interface EarlyDesktopSettingsInput {
19+
readonly env: NodeJS.ProcessEnv;
20+
readonly homeDirectory: string;
21+
readonly joinPath: JoinPath;
22+
readonly readFileString: (path: string) => string;
23+
}
24+
25+
type EarlyLinuxElectronOptionsInput = EarlyDesktopSettingsInput;
26+
27+
export interface EarlyLinuxElectronOptions {
28+
readonly linuxWmClass: string;
29+
readonly passwordStore: LinuxPasswordStoreSwitch | null;
30+
}
31+
32+
const trimNonEmpty = (value: string | undefined): string | null => {
33+
const trimmed = value?.trim();
34+
return trimmed && trimmed.length > 0 ? trimmed : null;
35+
};
36+
37+
const EarlyDesktopSettingsJson = fromLenientJson(
38+
Schema.Struct({
39+
linuxPasswordStore: Schema.optionalKey(Schema.Unknown),
40+
}),
41+
);
42+
const decodeEarlyDesktopSettingsJson = Schema.decodeSync(EarlyDesktopSettingsJson);
43+
44+
const isDevelopmentEnvironment = (env: NodeJS.ProcessEnv): boolean =>
45+
trimNonEmpty(env.VITE_DEV_SERVER_URL) !== null;
46+
47+
function resolveEarlyDesktopSettingsPath(input: {
48+
readonly env: NodeJS.ProcessEnv;
49+
readonly homeDirectory: string;
50+
readonly joinPath: JoinPath;
51+
}): string {
52+
const t3Home = Option.fromUndefinedOr(input.env.T3CODE_HOME);
53+
const baseDir = resolveDesktopBaseDir({
54+
homeDirectory: input.homeDirectory,
55+
joinPath: input.joinPath,
56+
t3Home,
57+
});
58+
const stateDir = resolveDesktopStateDir({
59+
baseDir,
60+
isDevelopment: isDevelopmentEnvironment(input.env),
61+
joinPath: input.joinPath,
62+
t3Home,
63+
});
64+
return input.joinPath(stateDir, "desktop-settings.json");
65+
}
66+
67+
export function resolveEarlyLinuxPasswordStorePreference(
68+
input: EarlyDesktopSettingsInput,
69+
): LinuxPasswordStorePreference {
70+
const settingsPath = resolveEarlyDesktopSettingsPath(input);
71+
try {
72+
const parsed = decodeEarlyDesktopSettingsJson(input.readFileString(settingsPath));
73+
return normalizeLinuxPasswordStorePreference(parsed.linuxPasswordStore);
74+
} catch {
75+
return DEFAULT_LINUX_PASSWORD_STORE;
76+
}
77+
}
78+
79+
export function resolveEarlyLinuxElectronOptions(
80+
input: EarlyLinuxElectronOptionsInput,
81+
): EarlyLinuxElectronOptions {
82+
const preference = resolveEarlyLinuxPasswordStorePreference(input);
83+
return {
84+
linuxWmClass: isDevelopmentEnvironment(input.env) ? "t3code-dev" : "t3code",
85+
passwordStore: resolveLinuxPasswordStoreSwitch({
86+
preference,
87+
env: input.env,
88+
}),
89+
};
90+
}

‎apps/desktop/src/app/DesktopEnvironment.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as Path from "effect/Path";
1313

1414
import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
1515
import * as DesktopConfig from "./DesktopConfig.ts";
16+
import { resolveDesktopBaseDir, resolveDesktopStateDir } from "./DesktopStatePaths.ts";
1617
import { isNightlyDesktopVersion } from "../updates/updateChannels.ts";
1718

1819
export interface MakeDesktopEnvironmentInput {
@@ -149,19 +150,24 @@ const make = Effect.fn("desktop.environment.make")(function* (
149150
: input.platform === "darwin"
150151
? path.join(homeDirectory, "Library", "Application Support")
151152
: Option.getOrElse(config.xdgConfigHome, () => path.join(homeDirectory, ".config"));
152-
const configuredBaseDir = config.t3Home;
153-
const baseDir = Option.getOrElse(configuredBaseDir, () => path.join(homeDirectory, ".t3"));
153+
const baseDir = resolveDesktopBaseDir({
154+
homeDirectory,
155+
joinPath: path.join,
156+
t3Home: config.t3Home,
157+
});
154158
const rootDir = path.resolve(input.dirname, "../../..");
155159
const appRoot = input.isPackaged ? input.appPath : rootDir;
156160
const branding = resolveDesktopAppBranding({
157161
isDevelopment,
158162
appVersion: input.appVersion,
159163
});
160164
const displayName = branding.displayName;
161-
const stateDir = path.join(
165+
const stateDir = resolveDesktopStateDir({
162166
baseDir,
163-
isDevelopment && Option.isNone(configuredBaseDir) ? "dev" : "userdata",
164-
);
167+
isDevelopment,
168+
joinPath: path.join,
169+
t3Home: config.t3Home,
170+
});
165171
const userDataDirName = isDevelopment ? "t3code-dev" : "t3code";
166172
const legacyUserDataDirName = isDevelopment ? "T3 Code (Dev)" : "T3 Code (Alpha)";
167173
const linuxApplicationsDir = path.join(

‎apps/desktop/src/app/DesktopLifecycle.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ describe("DesktopLifecycle", () => {
3535
setDesktopName: () => Effect.void,
3636
setDockIcon: () => Effect.void,
3737
appendCommandLineSwitch: () => Effect.void,
38+
removeCommandLineSwitch: () => Effect.void,
3839
onBeforeQuitForUpdate: (listener) =>
3940
Effect.acquireRelease(
4041
Effect.sync(() => {

0 commit comments

Comments
 (0)