@adrifer/winix 0.2.2 → 0.2.4-preview.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +374 -2
  2. package/dist/backends/windows/index.d.ts +3 -1
  3. package/dist/backends/windows/index.js +99 -7
  4. package/dist/backends/windows/lockfile.d.ts +36 -1
  5. package/dist/backends/windows/lockfile.js +168 -0
  6. package/dist/backends/windows/paths.d.ts +2 -0
  7. package/dist/backends/windows/paths.js +17 -0
  8. package/dist/backends/windows/powershell.d.ts +6 -0
  9. package/dist/backends/windows/powershell.js +51 -0
  10. package/dist/backends/windows/wsl-setup-runtime.d.ts +32 -0
  11. package/dist/backends/windows/wsl-setup-runtime.js +387 -0
  12. package/dist/backends/windows/wsl-setup.d.ts +12 -0
  13. package/dist/backends/windows/wsl-setup.js +108 -0
  14. package/dist/backends/windows/wsl.d.ts +9 -0
  15. package/dist/backends/windows/wsl.js +194 -0
  16. package/dist/cli/commands/apply.d.ts +7 -1
  17. package/dist/cli/commands/apply.js +39 -15
  18. package/dist/cli/commands/check.js +3 -0
  19. package/dist/cli/commands/init.js +2 -0
  20. package/dist/cli/commands/install.d.ts +7 -0
  21. package/dist/cli/commands/install.js +41 -0
  22. package/dist/cli/commands/switch.d.ts +6 -0
  23. package/dist/cli/commands/switch.js +53 -13
  24. package/dist/cli/commands/update.d.ts +6 -1
  25. package/dist/cli/commands/update.js +41 -10
  26. package/dist/cli/index.js +65 -3
  27. package/dist/cli/windows-state.d.ts +56 -0
  28. package/dist/cli/windows-state.js +341 -0
  29. package/dist/helpers/index.d.ts +1 -1
  30. package/dist/helpers/utils.d.ts +1 -0
  31. package/dist/helpers/utils.js +8 -0
  32. package/dist/helpers/windows.d.ts +53 -7
  33. package/dist/helpers/windows.js +351 -28
  34. package/dist/index.d.ts +4 -2
  35. package/dist/skill.d.ts +2 -0
  36. package/dist/skill.js +503 -0
  37. package/dist/types/windows.d.ts +82 -1
  38. package/package.json +3 -1
  39. package/scripts/bootstrap-nixos.sh +51 -0
  40. package/scripts/bootstrap-windows.ps1 +216 -0
@@ -1,5 +1,7 @@
1
1
  import { existsSync, readFileSync, writeFileSync } from "node:fs";
2
2
  import { join } from "node:path";
3
+ import { validWslImage } from "./wsl.js";
4
+ import { validWslRepository, wslRepositoryKey, } from "./wsl-setup.js";
3
5
  export const WINDOWS_LOCKFILE_NAME = "winix-windows.lock";
4
6
  export const WINDOWS_LOCK_VERSION = 1;
5
7
  export function readWindowsLock(configDir) {
@@ -51,6 +53,7 @@ export function reconcileInlinePins(lock, evaluatedWindowsHosts, now = new Date(
51
53
  }
52
54
  }
53
55
  return {
56
+ ...lock,
54
57
  version: WINDOWS_LOCK_VERSION,
55
58
  generatedAt: changed ? resolvedAt : lock.generatedAt,
56
59
  packages,
@@ -83,6 +86,7 @@ export function resolveMissingWindowsLockEntries(lock, evaluatedWindowsHosts, re
83
86
  export function resolveWindowsLockEntries(lock, packages, resolveVersion, now = new Date()) {
84
87
  const resolvedAt = now.toISOString();
85
88
  const next = {
89
+ ...lock,
86
90
  version: WINDOWS_LOCK_VERSION,
87
91
  generatedAt: lock.generatedAt,
88
92
  packages: cloneLockPackages(lock.packages),
@@ -135,8 +139,130 @@ export function serializeWindowsLock(lock) {
135
139
  resolvedAt: entry.resolvedAt,
136
140
  };
137
141
  }
142
+ if (lock.modules !== undefined) {
143
+ sorted.modules = {};
144
+ for (const name of Object.keys(lock.modules).sort(comparePackageIds)) {
145
+ sorted.modules[name] = { ...lock.modules[name] };
146
+ }
147
+ }
148
+ if (lock.wslImages !== undefined) {
149
+ sorted.wslImages = {};
150
+ for (const key of Object.keys(lock.wslImages).sort(comparePackageIds)) {
151
+ sorted.wslImages[key] = { ...lock.wslImages[key] };
152
+ }
153
+ }
154
+ if (lock.wslRepositories !== undefined) {
155
+ sorted.wslRepositories = {};
156
+ for (const key of Object.keys(lock.wslRepositories).sort(comparePackageIds)) {
157
+ sorted.wslRepositories[key] = { ...lock.wslRepositories[key] };
158
+ }
159
+ }
138
160
  return JSON.stringify(sorted, null, 2) + "\n";
139
161
  }
162
+ export function collectWindowsModules(hosts) {
163
+ const modules = new Map();
164
+ for (const host of hosts) {
165
+ for (const resource of host.windows.dsc ?? []) {
166
+ if (resource.winix?.kind !== "powershell-module")
167
+ continue;
168
+ const module = resource.winix.module;
169
+ const key = module.name.toLowerCase();
170
+ const existing = modules.get(key);
171
+ if (existing && existing.repository.toLowerCase() !== module.repository.toLowerCase()) {
172
+ throw new Error(`PowerShell module "${module.name}" is declared with conflicting repositories.`);
173
+ }
174
+ modules.set(key, module);
175
+ }
176
+ }
177
+ return modules;
178
+ }
179
+ export function hasWindowsWslDistros(hosts) {
180
+ return hosts.some((host) => host.windows.dsc?.some((resource) => resource.winix?.kind === "wsl-distro"));
181
+ }
182
+ export function resolveWindowsWslImages(lock, resolve, now = new Date(), missingOnly = false) {
183
+ const images = { ...lock.wslImages };
184
+ let changed = false;
185
+ for (const architecture of ["x64", "arm64"]) {
186
+ const key = `nixos-wsl/${architecture}`;
187
+ if (missingOnly && images[key])
188
+ continue;
189
+ const image = { ...resolve(architecture), resolvedAt: now.toISOString() };
190
+ if (!validWslImage(image) || image.architecture !== architecture) {
191
+ throw new Error(`Invalid NixOS-WSL image resolution for ${architecture}`);
192
+ }
193
+ const previous = images[key];
194
+ if (previous?.version === image.version && previous.url === image.url && previous.sha256 === image.sha256)
195
+ continue;
196
+ images[key] = image;
197
+ changed = true;
198
+ }
199
+ return {
200
+ lock: changed ? { ...lock, generatedAt: now.toISOString(), wslImages: images } : lock,
201
+ changed,
202
+ };
203
+ }
204
+ export function resolveWindowsWslRepositories(lock, hosts, resolve, now = new Date(), missingOnly = false) {
205
+ const repositories = { ...lock.wslRepositories };
206
+ const visited = new Set();
207
+ let changed = false;
208
+ for (const host of hosts) {
209
+ for (const resource of host.windows.dsc ?? []) {
210
+ if (resource.winix?.kind !== "wsl-distro" || !resource.winix.distro.setup)
211
+ continue;
212
+ const setup = resource.winix.distro.setup;
213
+ const key = wslRepositoryKey(setup);
214
+ if (visited.has(key))
215
+ continue;
216
+ visited.add(key);
217
+ if (missingOnly && repositories[key])
218
+ continue;
219
+ const pin = {
220
+ repository: setup.repository, ref: setup.ref ?? "HEAD",
221
+ revision: resolve(setup.repository, setup.ref ?? "HEAD"), resolvedAt: now.toISOString(),
222
+ };
223
+ if (!validWslRepository(pin))
224
+ throw new Error("Invalid WSL setup repository revision");
225
+ if (repositories[key]?.revision === pin.revision)
226
+ continue;
227
+ repositories[key] = pin;
228
+ changed = true;
229
+ }
230
+ }
231
+ return { lock: changed ? { ...lock, generatedAt: now.toISOString(), wslRepositories: repositories } : lock, changed };
232
+ }
233
+ export function resolveWindowsModuleLockEntries(lock, modules, resolveVersion, now = new Date(), missingOnly = false) {
234
+ const next = { ...lock, modules: { ...lock.modules } };
235
+ const resolutions = [];
236
+ let changed = false;
237
+ for (const module of modules) {
238
+ const key = module.name.toLowerCase();
239
+ const previous = next.modules[key];
240
+ if (previous && previous.repository.toLowerCase() !== module.repository.toLowerCase()) {
241
+ if (missingOnly) {
242
+ throw new Error(`PowerShell module "${module.name}" changed repository. Run winix update --windows ${module.name}.`);
243
+ }
244
+ }
245
+ else if (missingOnly && previous) {
246
+ continue;
247
+ }
248
+ const version = resolveVersion(module.name, module.repository);
249
+ if (!/^\d+\.\d+\.\d+(?:\.\d+)?$/.test(version)) {
250
+ throw new Error(`PowerShell module "${module.name}" resolver did not return a stable version: ${version}`);
251
+ }
252
+ const unchanged = previous?.version === version &&
253
+ previous.repository.toLowerCase() === module.repository.toLowerCase();
254
+ resolutions.push({
255
+ id: module.name, version, previousVersion: previous?.version,
256
+ status: unchanged ? "up-to-date" : previous ? "updated" : "new",
257
+ });
258
+ if (unchanged)
259
+ continue;
260
+ next.modules[key] = { repository: module.repository, version, resolvedAt: now.toISOString() };
261
+ next.generatedAt = now.toISOString();
262
+ changed = true;
263
+ }
264
+ return { lock: changed ? next : lock, resolutions, changed };
265
+ }
140
266
  function cloneLockPackages(packages) {
141
267
  const out = {};
142
268
  for (const [id, entry] of Object.entries(packages)) {
@@ -181,10 +307,52 @@ function validateWindowsLock(value) {
181
307
  resolvedAt: entry.resolvedAt,
182
308
  };
183
309
  }
310
+ const modules = {};
311
+ if (value.modules !== undefined) {
312
+ if (!isObject(value.modules))
313
+ throw new Error(`${WINDOWS_LOCKFILE_NAME} modules must be an object.`);
314
+ for (const [name, entry] of Object.entries(value.modules)) {
315
+ if (!/^[a-z0-9][a-z0-9._-]*$/i.test(name) || !isObject(entry) ||
316
+ typeof entry.repository !== "string" || !/^[a-z0-9][a-z0-9._-]*$/i.test(entry.repository) ||
317
+ typeof entry.version !== "string" || !/^\d+\.\d+\.\d+(?:\.\d+)?$/.test(entry.version) ||
318
+ typeof entry.resolvedAt !== "string") {
319
+ throw new Error(`${WINDOWS_LOCKFILE_NAME} module "${name}" has an invalid repository, stable version, or resolvedAt.`);
320
+ }
321
+ const key = name.toLowerCase();
322
+ if (modules[key])
323
+ throw new Error(`${WINDOWS_LOCKFILE_NAME} has duplicate module "${name}".`);
324
+ modules[key] = { repository: entry.repository, version: entry.version, resolvedAt: entry.resolvedAt };
325
+ }
326
+ }
327
+ const wslImages = {};
328
+ if (value.wslImages !== undefined) {
329
+ if (!isObject(value.wslImages))
330
+ throw new Error(`${WINDOWS_LOCKFILE_NAME} wslImages must be an object.`);
331
+ for (const [key, image] of Object.entries(value.wslImages)) {
332
+ if (!validWslImage(image) || key !== `nixos-wsl/${image.architecture}`) {
333
+ throw new Error(`${WINDOWS_LOCKFILE_NAME} WSL image "${key}" is invalid or is not an immutable official release asset.`);
334
+ }
335
+ wslImages[key] = { ...image };
336
+ }
337
+ }
338
+ const wslRepositories = {};
339
+ if (value.wslRepositories !== undefined) {
340
+ if (!isObject(value.wslRepositories))
341
+ throw new Error(`${WINDOWS_LOCKFILE_NAME} wslRepositories must be an object`);
342
+ for (const [key, pin] of Object.entries(value.wslRepositories)) {
343
+ if (!validWslRepository(pin) || key !== wslRepositoryKey({ repository: pin.repository, ref: pin.ref, host: "validate", configDirectory: "." })) {
344
+ throw new Error(`${WINDOWS_LOCKFILE_NAME} WSL repository pin "${key}" is invalid`);
345
+ }
346
+ wslRepositories[key] = { ...pin };
347
+ }
348
+ }
184
349
  return {
185
350
  version: WINDOWS_LOCK_VERSION,
186
351
  generatedAt: value.generatedAt,
187
352
  packages,
353
+ ...(value.modules === undefined ? {} : { modules }),
354
+ ...(value.wslImages === undefined ? {} : { wslImages }),
355
+ ...(value.wslRepositories === undefined ? {} : { wslRepositories }),
188
356
  };
189
357
  }
190
358
  function isObject(value) {
@@ -0,0 +1,2 @@
1
+ /** DSC consumes Windows paths even when the configuration is generated in WSL. */
2
+ export declare function windowsSourcePath(path: string, osPlatform?: NodeJS.Platform, wslDistribution?: string | undefined, convert?: (source: string) => string): string;
@@ -0,0 +1,17 @@
1
+ import { execFileSync } from "node:child_process";
2
+ /** DSC consumes Windows paths even when the configuration is generated in WSL. */
3
+ export function windowsSourcePath(path, osPlatform = process.platform, wslDistribution = process.env.WSL_DISTRO_NAME, convert = (source) => execFileSync("wslpath", ["-w", source], { encoding: "utf8" })) {
4
+ if (osPlatform !== "linux" || !wslDistribution || !path.startsWith("/"))
5
+ return path;
6
+ try {
7
+ const converted = convert(path).trim();
8
+ if (!/^(?:[a-z]:\\|\\\\)/i.test(converted)) {
9
+ throw new Error("wslpath did not return an absolute Windows path");
10
+ }
11
+ return converted;
12
+ }
13
+ catch (error) {
14
+ throw new Error(`Cannot translate Windows file source "${path}" from WSL: ` +
15
+ (error instanceof Error ? error.message : String(error)), { cause: error });
16
+ }
17
+ }
@@ -0,0 +1,6 @@
1
+ import type { WinDscProperties, WinPowerShellModule } from "../../types/index.js";
2
+ import type { WindowsLockModule } from "./lockfile.js";
3
+ export type PowerShellGalleryRunner = (script: string) => string;
4
+ /** Find-PSResource returns the newest stable release unless -Prerelease is requested. */
5
+ export declare function resolvePowerShellModuleVersion(name: string, repository: string, run?: PowerShellGalleryRunner): string;
6
+ export declare function powerShellModuleProperties(module: WinPowerShellModule, pin: WindowsLockModule): WinDscProperties;
@@ -0,0 +1,51 @@
1
+ import { execFileSync } from "node:child_process";
2
+ import { powerShellLiteral } from "../../helpers/utils.js";
3
+ /** Find-PSResource returns the newest stable release unless -Prerelease is requested. */
4
+ export function resolvePowerShellModuleVersion(name, repository, run = (script) => execFileSync("pwsh", ["-NoLogo", "-NoProfile", "-NonInteractive", "-EncodedCommand", encode(script)], { encoding: "utf8" })) {
5
+ const script = [
6
+ "$ErrorActionPreference = 'Stop'",
7
+ `$found = @(Find-PSResource -Name ${powerShellLiteral(name)} -Repository ${powerShellLiteral(repository)} -ErrorAction Stop)`,
8
+ `if ($found.Count -ne 1 -or $found[0].Name -ine ${powerShellLiteral(name)} -or $found[0].Prerelease) { throw 'Expected one stable module release' }`,
9
+ "$found[0].Version.ToString() | ConvertTo-Json -Compress",
10
+ ].join("\n");
11
+ try {
12
+ const version = JSON.parse(run(script).trim());
13
+ if (typeof version !== "string" || !/^\d+\.\d+\.\d+(?:\.\d+)?$/.test(version)) {
14
+ throw new Error("Repository did not return a stable module version");
15
+ }
16
+ return version;
17
+ }
18
+ catch (error) {
19
+ throw new Error(`Failed to resolve PowerShell module "${name}" from "${repository}". ` +
20
+ `PowerShell 7 with PSResourceGet and the registered repository is required: ` +
21
+ (error instanceof Error ? error.message : String(error)), { cause: error });
22
+ }
23
+ }
24
+ function encode(script) {
25
+ return Buffer.from(script, "utf16le").toString("base64");
26
+ }
27
+ export function powerShellModuleProperties(module, pin) {
28
+ const variables = [
29
+ `$name = ${powerShellLiteral(module.name)}`,
30
+ `$version = ${powerShellLiteral(pin.version)}`,
31
+ ];
32
+ const present = "@(Get-Module -ListAvailable -Name $name | Where-Object { $_.Version -eq [version]$version -and -not $_.PrivateData.PSData.Prerelease }).Count -gt 0";
33
+ const prelude = [
34
+ "$ErrorActionPreference = 'Stop'",
35
+ "if ($PSVersionTable.PSVersion.Major -lt 7) { throw 'PowerShell 7 is required' }",
36
+ ...variables,
37
+ ];
38
+ return {
39
+ getScript: [
40
+ ...prelude,
41
+ "Get-Module -ListAvailable -Name $name | Select-Object Name,@{Name='Version';Expression={$_.Version.ToString()}}",
42
+ ].join("\n"),
43
+ testScript: [...prelude, present].join("\n"),
44
+ setScript: [
45
+ ...prelude,
46
+ `if (${present}) { return }`,
47
+ `Install-PSResource -Name $name -Version $version -Repository ${powerShellLiteral(pin.repository)} -Scope CurrentUser -TrustRepository -AcceptLicense -Reinstall -ErrorAction Stop | Out-Null`,
48
+ `if (-not (${present})) { throw "Module $name $version was not installed" }`,
49
+ ].join("\n"),
50
+ };
51
+ }
@@ -0,0 +1,32 @@
1
+ import type { EvaluatedHost } from "../../evaluator/index.js";
2
+ import type { WinWslSetup } from "../../types/index.js";
3
+ interface Request {
4
+ setup: WinWslSetup;
5
+ revision: string;
6
+ fingerprint: string;
7
+ }
8
+ interface Plan {
9
+ user: string;
10
+ home: string;
11
+ checkout: string;
12
+ host: string;
13
+ }
14
+ interface CommandOptions {
15
+ cwd?: string;
16
+ user?: string;
17
+ home?: string;
18
+ env?: NodeJS.ProcessEnv;
19
+ }
20
+ export type SetupRunner = (command: string, args: string[], options?: CommandOptions) => string;
21
+ export declare function createWslAccountPlan(host: EvaluatedHost, setup: WinWslSetup, architecture: string): {
22
+ plan: Plan;
23
+ accountHost: EvaluatedHost;
24
+ };
25
+ export declare function prepareWslAccount(configDirectory: string, accountDirectory: string, setup: WinWslSetup): Promise<Plan>;
26
+ export declare function runWslSetup(request: Request, options?: {
27
+ stateDirectory?: string;
28
+ run?: SetupRunner;
29
+ isRoot?: () => boolean;
30
+ readWslConfig?: () => Promise<string>;
31
+ }): Promise<void>;
32
+ export {};