@adrifer/winix 0.2.0-preview.4 → 0.2.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.
- package/dist/backends/nix/index.js +9 -8
- package/dist/backends/windows/index.js +209 -7
- package/dist/backends/windows/yaml.js +7 -0
- package/dist/cli/commands/check.d.ts +8 -0
- package/dist/cli/commands/check.js +25 -0
- package/dist/helpers/home.d.ts +4 -4
- package/dist/helpers/home.js +4 -6
- package/dist/helpers/index.d.ts +1 -1
- package/dist/helpers/nix.js +17 -18
- package/dist/helpers/windows.d.ts +45 -1
- package/dist/helpers/windows.js +495 -1
- package/dist/index.d.ts +2 -2
- package/dist/nix/serialize.d.ts +8 -0
- package/dist/nix/serialize.js +21 -0
- package/dist/types/home.d.ts +4 -4
- package/dist/types/windows.d.ts +61 -1
- package/package.json +1 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Nix backend: generates .nix files from evaluated host IR
|
|
2
2
|
import { posix as pathPosix } from "node:path";
|
|
3
|
+
import { nixStringLiteral } from "../../nix/serialize.js";
|
|
3
4
|
/**
|
|
4
5
|
* Generate Nix files from an evaluated workspace.
|
|
5
6
|
*/
|
|
@@ -23,7 +24,7 @@ function generateFlake(workspace, hosts) {
|
|
|
23
24
|
const nixosHosts = hosts
|
|
24
25
|
.filter((h) => Object.keys(h.nixos).length > 0)
|
|
25
26
|
.map((h) => ` nixosConfigurations.${h.name} = nixpkgs.lib.nixosSystem {\n` +
|
|
26
|
-
` system =
|
|
27
|
+
` system = ${nixStringLiteral(systemForHost(h, "nixos"))};\n` +
|
|
27
28
|
` modules = [ ${nixRelativePath("hosts", `${h.name}.nix`)} ];\n` +
|
|
28
29
|
` specialArgs = { inherit inputs; };\n` +
|
|
29
30
|
` };`)
|
|
@@ -31,7 +32,7 @@ function generateFlake(workspace, hosts) {
|
|
|
31
32
|
const darwinHosts = hosts
|
|
32
33
|
.filter((h) => Object.keys(h.darwin).length > 0)
|
|
33
34
|
.map((h) => ` darwinConfigurations.${h.name} = inputs.nix-darwin.lib.darwinSystem {\n` +
|
|
34
|
-
` system =
|
|
35
|
+
` system = ${nixStringLiteral(systemForHost(h, "darwin"))};\n` +
|
|
35
36
|
` modules = [ ${nixRelativePath("hosts", `${h.name}.nix`)} ];\n` +
|
|
36
37
|
` specialArgs = { inherit inputs; };\n` +
|
|
37
38
|
` };`)
|
|
@@ -55,12 +56,12 @@ function formatInput(name, def) {
|
|
|
55
56
|
const nixName = inputNixName(name, def);
|
|
56
57
|
if (typeof def === "string") {
|
|
57
58
|
const url = def.startsWith("github:") ? def : `github:NixOS/nixpkgs/${def}`;
|
|
58
|
-
return ` ${nixName}.url =
|
|
59
|
+
return ` ${nixName}.url = ${nixStringLiteral(url)};`;
|
|
59
60
|
}
|
|
60
|
-
const lines = [` ${nixName}.url =
|
|
61
|
+
const lines = [` ${nixName}.url = ${nixStringLiteral(def.url)};`];
|
|
61
62
|
if (def.follows) {
|
|
62
63
|
for (const [key, value] of Object.entries(def.follows)) {
|
|
63
|
-
lines.push(` ${nixName}.inputs.${key}.follows =
|
|
64
|
+
lines.push(` ${nixName}.inputs.${key}.follows = ${nixStringLiteral(value)};`);
|
|
64
65
|
}
|
|
65
66
|
}
|
|
66
67
|
return lines.join("\n");
|
|
@@ -285,7 +286,7 @@ function formatNixValue(value) {
|
|
|
285
286
|
if (typeof value === "string") {
|
|
286
287
|
if (isPkgsReference(value))
|
|
287
288
|
return value;
|
|
288
|
-
return
|
|
289
|
+
return nixStringLiteral(value);
|
|
289
290
|
}
|
|
290
291
|
if (typeof value === "number")
|
|
291
292
|
return String(value);
|
|
@@ -297,7 +298,7 @@ function formatNixValue(value) {
|
|
|
297
298
|
if (isPlainObject(value)) {
|
|
298
299
|
return formatInlineAttrSet(value);
|
|
299
300
|
}
|
|
300
|
-
return
|
|
301
|
+
return nixStringLiteral(String(value));
|
|
301
302
|
}
|
|
302
303
|
function formatInlineAttrSet(obj) {
|
|
303
304
|
// Scope-specific package rewriting only applies to top-level fragment fields;
|
|
@@ -319,7 +320,7 @@ function formatAttrPath(path) {
|
|
|
319
320
|
return path.map(formatAttrKey).join(".");
|
|
320
321
|
}
|
|
321
322
|
function formatAttrKey(key) {
|
|
322
|
-
return needsQuoting(key) ?
|
|
323
|
+
return needsQuoting(key) ? nixStringLiteral(key) : key;
|
|
323
324
|
}
|
|
324
325
|
function needsQuoting(key) {
|
|
325
326
|
if (NIX_KEYWORDS.has(key))
|
|
@@ -32,6 +32,7 @@ export { isWindowsHost } from "./host.js";
|
|
|
32
32
|
const DSC_SCHEMA = "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/config/document.json";
|
|
33
33
|
const WINGET_PACKAGE_TYPE = "Microsoft.WinGet/Package";
|
|
34
34
|
const RUN_COMMAND_ON_SET_TYPE = "Microsoft.DSC.Transitional/RunCommandOnSet";
|
|
35
|
+
const WINDOWS_SETTINGS_MODULE_COMMAND_NAME = "Ensure Microsoft Windows Settings module";
|
|
35
36
|
/**
|
|
36
37
|
* Generate Windows bundles for every Windows host in the evaluated workspace.
|
|
37
38
|
* Non-Windows hosts are skipped.
|
|
@@ -55,8 +56,9 @@ export function generateWindows(evaluatedHosts, lock = emptyWindowsLock()) {
|
|
|
55
56
|
*/
|
|
56
57
|
function generateConfiguration(hostName, win, lock) {
|
|
57
58
|
const packages = sortedPackages(win.packages ?? {});
|
|
58
|
-
const
|
|
59
|
-
const
|
|
59
|
+
const prepared = prepareWindowsDscResources(win.commands ?? [], win.dsc ?? []);
|
|
60
|
+
const commands = prepared.commands;
|
|
61
|
+
const dscResources = prepared.dscResources;
|
|
60
62
|
const lines = [];
|
|
61
63
|
lines.push(`# yaml-language-server: $schema=${DSC_SCHEMA}`);
|
|
62
64
|
lines.push(`# Generated by Winix for host "${hostName}". Do not edit.`);
|
|
@@ -87,6 +89,191 @@ function generateConfiguration(hostName, win, lock) {
|
|
|
87
89
|
}
|
|
88
90
|
return lines.join("\n") + "\n";
|
|
89
91
|
}
|
|
92
|
+
function prepareWindowsDscResources(commands, resources) {
|
|
93
|
+
const envResolvedResources = resolveManagedEnvironmentReferences(resources);
|
|
94
|
+
if (!envResolvedResources.some((resource) => resource.winix?.kind === "setting")) {
|
|
95
|
+
return { commands: [...commands], dscResources: envResolvedResources };
|
|
96
|
+
}
|
|
97
|
+
const ensureModule = createWindowsSettingsEnsureCommand();
|
|
98
|
+
const ensureRef = { kind: "dsc", token: ensureModule.token };
|
|
99
|
+
return {
|
|
100
|
+
commands: [...commands],
|
|
101
|
+
dscResources: [
|
|
102
|
+
ensureModule,
|
|
103
|
+
...envResolvedResources.map((resource) => {
|
|
104
|
+
if (resource.winix?.kind !== "setting")
|
|
105
|
+
return resource;
|
|
106
|
+
return cloneDscResource(resource, resource.properties, [
|
|
107
|
+
ensureRef,
|
|
108
|
+
...(resource.dependsOn ?? []),
|
|
109
|
+
]);
|
|
110
|
+
}),
|
|
111
|
+
],
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
function createWindowsSettingsEnsureCommand() {
|
|
115
|
+
// Microsoft.Windows.Settings currently publishes the WindowsSettings resource
|
|
116
|
+
// as a prerelease module (the upstream sample uses v0.1.0-alpha), so the
|
|
117
|
+
// ensure step must opt into prerelease discovery for now. Revisit/remove
|
|
118
|
+
// -Prerelease once a stable module exists so future alphas are not preferred.
|
|
119
|
+
const resource = {
|
|
120
|
+
name: WINDOWS_SETTINGS_MODULE_COMMAND_NAME,
|
|
121
|
+
resourceType: RUN_COMMAND_ON_SET_TYPE,
|
|
122
|
+
properties: {
|
|
123
|
+
executable: "pwsh",
|
|
124
|
+
arguments: {
|
|
125
|
+
"0": "-NoProfile",
|
|
126
|
+
"1": "-NoLogo",
|
|
127
|
+
"2": "-Command",
|
|
128
|
+
"3": [
|
|
129
|
+
"$ErrorActionPreference = 'Stop';",
|
|
130
|
+
"if (-not (Get-Module -ListAvailable -Name Microsoft.Windows.Settings)) {",
|
|
131
|
+
" Install-PSResource -Name Microsoft.Windows.Settings -Prerelease -TrustRepository -AcceptLicense | Out-Null",
|
|
132
|
+
"}",
|
|
133
|
+
].join(" "),
|
|
134
|
+
treatAsArray: true,
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
Object.defineProperty(resource, "token", {
|
|
139
|
+
value: Symbol("winix.windows-settings-module"),
|
|
140
|
+
enumerable: false,
|
|
141
|
+
configurable: true,
|
|
142
|
+
});
|
|
143
|
+
return resource;
|
|
144
|
+
}
|
|
145
|
+
const WINDOWS_ENV_TOKEN = /%([^%]+)%/g;
|
|
146
|
+
function canonicalEnvName(name) {
|
|
147
|
+
return name.toLocaleUpperCase("en-US");
|
|
148
|
+
}
|
|
149
|
+
function resolveManagedEnvironmentReferences(resources) {
|
|
150
|
+
if (resources.length === 0)
|
|
151
|
+
return [];
|
|
152
|
+
const managedEnv = new Map();
|
|
153
|
+
for (const resource of resources) {
|
|
154
|
+
const metadata = resource.winix;
|
|
155
|
+
if (metadata?.kind === "env" && metadata.action === "set") {
|
|
156
|
+
managedEnv.set(canonicalEnvName(metadata.name), {
|
|
157
|
+
name: metadata.name,
|
|
158
|
+
value: metadata.value ?? "",
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (managedEnv.size === 0)
|
|
163
|
+
return [...resources];
|
|
164
|
+
const resolvedEnv = new Map();
|
|
165
|
+
const resolveEnv = (key, stack) => {
|
|
166
|
+
const cached = resolvedEnv.get(key);
|
|
167
|
+
if (cached !== undefined)
|
|
168
|
+
return cached;
|
|
169
|
+
const entry = managedEnv.get(key);
|
|
170
|
+
if (!entry)
|
|
171
|
+
return `%${key}%`;
|
|
172
|
+
const nextStack = [...stack, key];
|
|
173
|
+
const resolved = entry.value.replace(WINDOWS_ENV_TOKEN, (token, name) => {
|
|
174
|
+
const depKey = canonicalEnvName(name);
|
|
175
|
+
if (!managedEnv.has(depKey))
|
|
176
|
+
return token;
|
|
177
|
+
const cycleStart = nextStack.indexOf(depKey);
|
|
178
|
+
if (cycleStart !== -1) {
|
|
179
|
+
const cycle = [...nextStack.slice(cycleStart), depKey]
|
|
180
|
+
.map((cycleKey) => managedEnv.get(cycleKey)?.name ?? cycleKey)
|
|
181
|
+
.join(" -> ");
|
|
182
|
+
throw new Error(`Circular Windows environment variable reference: ${cycle}`);
|
|
183
|
+
}
|
|
184
|
+
return resolveEnv(depKey, nextStack);
|
|
185
|
+
});
|
|
186
|
+
resolvedEnv.set(key, resolved);
|
|
187
|
+
return resolved;
|
|
188
|
+
};
|
|
189
|
+
for (const key of managedEnv.keys()) {
|
|
190
|
+
resolveEnv(key, []);
|
|
191
|
+
}
|
|
192
|
+
const resolveString = (value) => value.replace(WINDOWS_ENV_TOKEN, (token, name) => {
|
|
193
|
+
const key = canonicalEnvName(name);
|
|
194
|
+
return managedEnv.has(key) ? resolveEnv(key, []) : token;
|
|
195
|
+
});
|
|
196
|
+
return resources.map((resource) => {
|
|
197
|
+
const metadata = resource.winix;
|
|
198
|
+
if (metadata?.kind === "env" && metadata.action === "set") {
|
|
199
|
+
const resolvedValue = resolveString(metadata.value ?? "");
|
|
200
|
+
const properties = rewriteEnvValue(resource.properties, resolvedValue);
|
|
201
|
+
return cloneDscResource(resource, properties);
|
|
202
|
+
}
|
|
203
|
+
if (metadata?.kind === "path") {
|
|
204
|
+
const resolvedValue = resolveString(metadata.value);
|
|
205
|
+
if (resolvedValue === metadata.value)
|
|
206
|
+
return resource;
|
|
207
|
+
const properties = rewritePathValue(resource.properties, metadata.value, resolvedValue);
|
|
208
|
+
return cloneDscResource(resource, properties);
|
|
209
|
+
}
|
|
210
|
+
return resource;
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
function rewriteEnvValue(properties, value) {
|
|
214
|
+
if (!properties)
|
|
215
|
+
return properties;
|
|
216
|
+
const valueData = properties.valueData;
|
|
217
|
+
if (!valueData || typeof valueData !== "object" || Array.isArray(valueData)) {
|
|
218
|
+
return properties;
|
|
219
|
+
}
|
|
220
|
+
return {
|
|
221
|
+
...properties,
|
|
222
|
+
valueData: {
|
|
223
|
+
...valueData,
|
|
224
|
+
String: value,
|
|
225
|
+
},
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
function rewritePathValue(properties, originalValue, resolvedValue) {
|
|
229
|
+
if (!properties)
|
|
230
|
+
return properties;
|
|
231
|
+
const original = psSingleQuoted(originalValue);
|
|
232
|
+
const resolved = psSingleQuoted(resolvedValue);
|
|
233
|
+
const rewritten = {};
|
|
234
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
235
|
+
rewritten[key] = rewritePathPropertyValue(value, original, resolved);
|
|
236
|
+
}
|
|
237
|
+
return rewritten;
|
|
238
|
+
}
|
|
239
|
+
function rewritePathPropertyValue(value, original, resolved) {
|
|
240
|
+
if (typeof value === "string") {
|
|
241
|
+
return value.replaceAll(original, resolved);
|
|
242
|
+
}
|
|
243
|
+
if (Array.isArray(value)) {
|
|
244
|
+
return value.map((entry) => rewritePathPropertyValue(entry, original, resolved));
|
|
245
|
+
}
|
|
246
|
+
if (value && typeof value === "object") {
|
|
247
|
+
const rewritten = {};
|
|
248
|
+
for (const [key, child] of Object.entries(value)) {
|
|
249
|
+
rewritten[key] = rewritePathPropertyValue(child, original, resolved);
|
|
250
|
+
}
|
|
251
|
+
return rewritten;
|
|
252
|
+
}
|
|
253
|
+
return value;
|
|
254
|
+
}
|
|
255
|
+
function cloneDscResource(resource, properties, dependsOn = resource.dependsOn) {
|
|
256
|
+
const cloned = { ...resource, properties };
|
|
257
|
+
if (dependsOn)
|
|
258
|
+
cloned.dependsOn = dependsOn;
|
|
259
|
+
else
|
|
260
|
+
delete cloned.dependsOn;
|
|
261
|
+
if (resource.token) {
|
|
262
|
+
Object.defineProperty(cloned, "token", {
|
|
263
|
+
value: resource.token,
|
|
264
|
+
enumerable: false,
|
|
265
|
+
configurable: true,
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
if (resource.winix) {
|
|
269
|
+
Object.defineProperty(cloned, "winix", {
|
|
270
|
+
value: resource.winix,
|
|
271
|
+
enumerable: false,
|
|
272
|
+
configurable: true,
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
return cloned;
|
|
276
|
+
}
|
|
90
277
|
/**
|
|
91
278
|
* Convert an arbitrary identifier into a DSC v3 instance name, which the schema
|
|
92
279
|
* restricts to `^[a-zA-Z0-9 ]+$` (letters, numbers, spaces only). Non-matching
|
|
@@ -166,10 +353,13 @@ function assignResourceNames(hostName, packages, commands, dscResources) {
|
|
|
166
353
|
return plan;
|
|
167
354
|
}
|
|
168
355
|
/**
|
|
169
|
-
* Resolve a resource's `dependsOn` references into
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
356
|
+
* Resolve a resource's `dependsOn` references into WinGet Configuration
|
|
357
|
+
* instance names. The underlying DSC v3 schema documents `resourceId(...)`
|
|
358
|
+
* lookups, but `winget configure`'s dscv3 processor currently resolves
|
|
359
|
+
* dependencies by simple resource name. Throws when a referenced resource is
|
|
360
|
+
* not present in this host's naming plan, which happens if a handle from
|
|
361
|
+
* another host is passed to `dependsOn` (cross-host ordering is not expressible
|
|
362
|
+
* in a single document).
|
|
173
363
|
*/
|
|
174
364
|
function resolveDependsOnRefs(refs, naming) {
|
|
175
365
|
if (!refs || refs.length === 0)
|
|
@@ -195,7 +385,7 @@ function resolveDependsOnRefs(refs, naming) {
|
|
|
195
385
|
`"${naming.hostName}". Resources can only depend on other resources ` +
|
|
196
386
|
`within the same host.`);
|
|
197
387
|
}
|
|
198
|
-
ids.push(
|
|
388
|
+
ids.push(target.name);
|
|
199
389
|
}
|
|
200
390
|
return ids;
|
|
201
391
|
}
|
|
@@ -266,6 +456,10 @@ function renderRawCommandResource(command, plan, naming) {
|
|
|
266
456
|
*/
|
|
267
457
|
function renderDscResource(resource, plan, naming) {
|
|
268
458
|
const out = [];
|
|
459
|
+
if (resource.winix?.kind === "setting") {
|
|
460
|
+
out.push(" # Some Windows settings (including DeveloperMode) require");
|
|
461
|
+
out.push(" # running winget configure from an elevated shell to change.");
|
|
462
|
+
}
|
|
269
463
|
out.push(` - name: ${yamlScalar(plan.name)}`);
|
|
270
464
|
out.push(` type: ${yamlScalar(resource.resourceType)}`);
|
|
271
465
|
const deps = resolveDependsOnRefs(resource.dependsOn, naming);
|
|
@@ -287,6 +481,11 @@ function renderDscResource(resource, plan, naming) {
|
|
|
287
481
|
}
|
|
288
482
|
}
|
|
289
483
|
}
|
|
484
|
+
if (resource.elevated) {
|
|
485
|
+
out.push(` metadata:`);
|
|
486
|
+
out.push(` winget:`);
|
|
487
|
+
out.push(` securityContext: elevated`);
|
|
488
|
+
}
|
|
290
489
|
return out;
|
|
291
490
|
}
|
|
292
491
|
function generateApplyScript() {
|
|
@@ -340,3 +539,6 @@ function yamlQuoted(value) {
|
|
|
340
539
|
function yamlStringArray(values) {
|
|
341
540
|
return `[${values.map((value) => yamlQuoted(value)).join(", ")}]`;
|
|
342
541
|
}
|
|
542
|
+
function psSingleQuoted(value) {
|
|
543
|
+
return `'${value.replace(/'/g, "''")}'`;
|
|
544
|
+
}
|
|
@@ -77,6 +77,13 @@ export function yamlEntry(key, value, depth) {
|
|
|
77
77
|
}
|
|
78
78
|
return out;
|
|
79
79
|
}
|
|
80
|
+
if (typeof value === "string" && /[\r\n]/.test(value)) {
|
|
81
|
+
const childPad = INDENT.repeat(depth + 1);
|
|
82
|
+
return [
|
|
83
|
+
`${pad}${k}: |-`,
|
|
84
|
+
...value.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n").map((line) => `${childPad}${line}`),
|
|
85
|
+
];
|
|
86
|
+
}
|
|
80
87
|
return [`${pad}${k}: ${yamlScalar(value)}`];
|
|
81
88
|
}
|
|
82
89
|
/**
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
+
import { type NixOutput } from "../../backends/nix/index.js";
|
|
1
2
|
interface CheckOptions {
|
|
2
3
|
strict: boolean;
|
|
3
4
|
escapeReport: boolean;
|
|
4
5
|
}
|
|
5
6
|
export declare function check(cwd: string, opts: CheckOptions): Promise<void>;
|
|
7
|
+
interface NixParseResult {
|
|
8
|
+
error?: NodeJS.ErrnoException;
|
|
9
|
+
status: number | null;
|
|
10
|
+
stderr: string;
|
|
11
|
+
}
|
|
12
|
+
type NixParser = (source: string) => NixParseResult;
|
|
13
|
+
export declare function validateGeneratedNixSyntax(output: NixOutput, parse?: NixParser): void;
|
|
6
14
|
export {};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// winix check — validate configuration
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
2
3
|
import { loadWorkspace } from "../loader.js";
|
|
3
4
|
import { evaluate } from "../../evaluator/index.js";
|
|
4
5
|
import { generateNix } from "../../backends/nix/index.js";
|
|
@@ -9,6 +10,7 @@ export async function check(cwd, opts) {
|
|
|
9
10
|
const duplicateHosts = findDuplicateHosts(workspace);
|
|
10
11
|
const evaluated = evaluate(workspace);
|
|
11
12
|
const output = generateNix(workspace, evaluated);
|
|
13
|
+
validateGeneratedNixSyntax(output);
|
|
12
14
|
const analyses = analyzeWorkspace(workspace);
|
|
13
15
|
const conflicts = detectConflicts(analyses);
|
|
14
16
|
console.log(`✓ Configuration valid`);
|
|
@@ -44,3 +46,26 @@ export async function check(cwd, opts) {
|
|
|
44
46
|
process.exit(1);
|
|
45
47
|
}
|
|
46
48
|
}
|
|
49
|
+
export function validateGeneratedNixSyntax(output, parse = parseNixSource) {
|
|
50
|
+
const sources = [
|
|
51
|
+
["flake.nix", output["flake.nix"]],
|
|
52
|
+
...Object.entries(output.hosts).map(([name, source]) => [`hosts/${name}`, source]),
|
|
53
|
+
];
|
|
54
|
+
for (const [name, source] of sources) {
|
|
55
|
+
const result = parse(source);
|
|
56
|
+
if (result.error?.code === "ENOENT")
|
|
57
|
+
return;
|
|
58
|
+
if (result.error)
|
|
59
|
+
throw result.error;
|
|
60
|
+
if (result.status !== 0) {
|
|
61
|
+
const detail = result.stderr.trim() || `nix-instantiate exited with status ${result.status}`;
|
|
62
|
+
throw new Error(`Generated Nix syntax is invalid in ${name}:\n${detail}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function parseNixSource(source) {
|
|
67
|
+
return spawnSync("nix-instantiate", ["--parse", "-"], {
|
|
68
|
+
input: source,
|
|
69
|
+
encoding: "utf8",
|
|
70
|
+
});
|
|
71
|
+
}
|
package/dist/helpers/home.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ProgramOptions, ServiceOptions } from "./options.js";
|
|
2
|
-
import type { Fragment } from "../core/types.js";
|
|
2
|
+
import type { Fragment, NixExpr } from "../core/types.js";
|
|
3
3
|
import type { HomeFile, HomeOptions, PackageRef } from "../types/index.js";
|
|
4
4
|
/**
|
|
5
5
|
* Map of Home Manager program names to their option types.
|
|
@@ -19,9 +19,9 @@ export interface HomeHelper {
|
|
|
19
19
|
imports(...imports: string[]): Fragment;
|
|
20
20
|
program<const K extends string>(name: K, opts?: ProgramOptions<HomeProgramOptions, K>): Fragment;
|
|
21
21
|
service<const K extends string>(name: K, opts?: ServiceOptions<HomeServiceOptions, K>): Fragment;
|
|
22
|
-
env(vars: Record<string, string>): Fragment;
|
|
23
|
-
path(paths: string[]): Fragment;
|
|
24
|
-
path(...paths: string[]): Fragment;
|
|
22
|
+
env(vars: Record<string, string | NixExpr>): Fragment;
|
|
23
|
+
path(paths: (string | NixExpr)[]): Fragment;
|
|
24
|
+
path(...paths: (string | NixExpr)[]): Fragment;
|
|
25
25
|
packages(packages: PackageRef[]): Fragment;
|
|
26
26
|
packages(...packages: PackageRef[]): Fragment;
|
|
27
27
|
files(files: Record<string, HomeFile>): Fragment;
|
package/dist/helpers/home.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { normalizeArgs } from "./utils.js";
|
|
2
|
+
import { escapeNixDoubleQuoted, nixStringLiteral } from "../nix/serialize.js";
|
|
2
3
|
export const home = Object.assign((config) => ({ homeManager: config }), {
|
|
3
4
|
program: (name, opts = {}) => ({
|
|
4
5
|
homeManager: { programs: { [name]: { enable: true, ...opts } } },
|
|
@@ -37,7 +38,7 @@ export const home = Object.assign((config) => ({ homeManager: config }), {
|
|
|
37
38
|
raw: (config) => ({ homeManager: { __raw: [config] } }),
|
|
38
39
|
activation: (name, opts) => {
|
|
39
40
|
const after = opts.after ?? ["writeBoundary"];
|
|
40
|
-
const afterList = after.map((s) =>
|
|
41
|
+
const afterList = after.map((s) => nixStringLiteral(s)).join(" ");
|
|
41
42
|
return {
|
|
42
43
|
homeManager: {
|
|
43
44
|
home: {
|
|
@@ -54,10 +55,7 @@ export const home = Object.assign((config) => ({ homeManager: config }), {
|
|
|
54
55
|
});
|
|
55
56
|
function homePathToNixString(path) {
|
|
56
57
|
if (path.startsWith("~/")) {
|
|
57
|
-
return
|
|
58
|
+
return '"${config.home.homeDirectory}/' + escapeNixDoubleQuoted(path.slice(2)) + '"';
|
|
58
59
|
}
|
|
59
|
-
return
|
|
60
|
-
}
|
|
61
|
-
function escapeNixString(value) {
|
|
62
|
-
return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
|
|
60
|
+
return nixStringLiteral(path);
|
|
63
61
|
}
|
package/dist/helpers/index.d.ts
CHANGED
|
@@ -13,4 +13,4 @@ export type { NixosHelper, NixosProgramOptions, NixosServiceOptions, SysctlSetti
|
|
|
13
13
|
export { darwin } from "./darwin.js";
|
|
14
14
|
export type { DarwinHelper, DarwinProgramOptions, DarwinServiceOptions, LaunchdHelper } from "./darwin.js";
|
|
15
15
|
export { windows } from "./windows.js";
|
|
16
|
-
export type { WindowsHelper, WinDscSpec, WinEnvNamespace, WinEnvOpts, WinEnvScope, WinPackageArg, WinPackageSpec, WinPathNamespace, WinRawCommandArg, WinRawCommandSpec, } from "./windows.js";
|
|
16
|
+
export type { WindowsHelper, WinDscSpec, WinEnvNamespace, WinEnvOpts, WinEnvScope, WinFileEncoding, WinFileNamespace, WinFileOpts, WinPackageArg, WinPackageSpec, WinPathNamespace, WinRawCommandArg, WinRawCommandSpec, WinSettingOpts, } from "./windows.js";
|
package/dist/helpers/nix.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { escapeNixDoubleQuoted, nixStringLiteral } from "../nix/serialize.js";
|
|
1
2
|
const expr = (value) => ({ __winixNixExpr: true, expr: value });
|
|
2
3
|
const pkg = Object.assign((name) => expr(`pkgs.${name}`), {
|
|
3
4
|
stable: (name) => expr(`pkgs.stable.${name}`),
|
|
@@ -123,12 +124,16 @@ function binaryRelease(opts) {
|
|
|
123
124
|
const sourcesAttrs = filtered
|
|
124
125
|
.map(([arch, platform]) => {
|
|
125
126
|
const parts = [
|
|
126
|
-
`file = ${
|
|
127
|
-
`hash = ${
|
|
128
|
-
`binary = ${
|
|
127
|
+
`file = ${nixStringLiteral(platform.file)};`,
|
|
128
|
+
`hash = ${nixStringLiteral(platform.hash)};`,
|
|
129
|
+
`binary = ${nixStringLiteral(platform.binary ?? opts.binary)};`,
|
|
129
130
|
];
|
|
130
131
|
if (usesPlatformPlaceholder) {
|
|
131
|
-
|
|
132
|
+
const platformName = platform.platform;
|
|
133
|
+
if (platformName === undefined) {
|
|
134
|
+
throw new Error(`nix.binaryRelease: "${arch}" must define \`platform\` because \`urlTemplate\` uses \`{platform}\``);
|
|
135
|
+
}
|
|
136
|
+
parts.push(`platform = ${nixStringLiteral(platformName)};`);
|
|
132
137
|
}
|
|
133
138
|
return ` ${arch} = { ${parts.join(" ")} };`;
|
|
134
139
|
})
|
|
@@ -142,15 +147,15 @@ function binaryRelease(opts) {
|
|
|
142
147
|
? license.expr
|
|
143
148
|
: `pkgs.lib.licenses.${license}`;
|
|
144
149
|
const metaLines = [
|
|
145
|
-
` description = ${
|
|
150
|
+
` description = ${nixStringLiteral(opts.meta.description)};`,
|
|
146
151
|
];
|
|
147
152
|
if (opts.meta.homepage !== undefined) {
|
|
148
|
-
metaLines.push(` homepage = ${
|
|
153
|
+
metaLines.push(` homepage = ${nixStringLiteral(opts.meta.homepage)};`);
|
|
149
154
|
}
|
|
150
155
|
if (licenseExpr !== undefined) {
|
|
151
156
|
metaLines.push(` license = ${licenseExpr};`);
|
|
152
157
|
}
|
|
153
|
-
metaLines.push(` mainProgram = ${
|
|
158
|
+
metaLines.push(` mainProgram = ${nixStringLiteral(mainProgram)};`);
|
|
154
159
|
metaLines.push(` platforms = builtins.attrNames sources;`);
|
|
155
160
|
const extraInstallBlock = opts.extraInstall
|
|
156
161
|
? `\n ${dedent(opts.extraInstall).split("\n").join("\n ")}`
|
|
@@ -181,13 +186,13 @@ function binaryRelease(opts) {
|
|
|
181
186
|
? `\n\n dontStrip = pkgs.stdenv.hostPlatform.isDarwin;`
|
|
182
187
|
: "";
|
|
183
188
|
const text = `(let
|
|
184
|
-
version = ${
|
|
189
|
+
version = ${nixStringLiteral(opts.version)};
|
|
185
190
|
sources = {
|
|
186
191
|
${sourcesAttrs}
|
|
187
192
|
};
|
|
188
193
|
source = sources.\${pkgs.stdenv.hostPlatform.system};
|
|
189
194
|
in pkgs.stdenvNoCC.mkDerivation {
|
|
190
|
-
pname = ${
|
|
195
|
+
pname = ${nixStringLiteral(opts.name)};
|
|
191
196
|
inherit version;
|
|
192
197
|
|
|
193
198
|
src = pkgs.fetchurl {
|
|
@@ -289,7 +294,7 @@ function dedent(value) {
|
|
|
289
294
|
return lines.map((line) => (line.length >= min ? line.slice(min) : line)).join("\n");
|
|
290
295
|
}
|
|
291
296
|
function stringPartToNix(value) {
|
|
292
|
-
return isNixExpr(value) ? value.expr :
|
|
297
|
+
return isNixExpr(value) ? value.expr : nixStringLiteral(value);
|
|
293
298
|
}
|
|
294
299
|
function conditionToNix(condition) {
|
|
295
300
|
if (isNixExpr(condition))
|
|
@@ -302,7 +307,7 @@ function nixLiteral(value) {
|
|
|
302
307
|
if (isNixExpr(value))
|
|
303
308
|
return value.expr;
|
|
304
309
|
if (typeof value === "string")
|
|
305
|
-
return
|
|
310
|
+
return nixStringLiteral(value);
|
|
306
311
|
if (typeof value === "number")
|
|
307
312
|
return String(value);
|
|
308
313
|
if (typeof value === "boolean")
|
|
@@ -324,7 +329,7 @@ function formatAttrs(attrs) {
|
|
|
324
329
|
.join(" ");
|
|
325
330
|
}
|
|
326
331
|
function formatAttrKey(key) {
|
|
327
|
-
return needsQuoting(key) ?
|
|
332
|
+
return needsQuoting(key) ? nixStringLiteral(key) : key;
|
|
328
333
|
}
|
|
329
334
|
function needsQuoting(key) {
|
|
330
335
|
if (NIX_KEYWORDS.has(key))
|
|
@@ -354,12 +359,6 @@ function joinRawTemplate(strings, values, formatValue) {
|
|
|
354
359
|
function trimScript(body) {
|
|
355
360
|
return body.replace(/^\n/, "").replace(/\n\s*$/, "");
|
|
356
361
|
}
|
|
357
|
-
function escapeNixDoubleQuoted(value) {
|
|
358
|
-
return value
|
|
359
|
-
.replace(/\\/g, "\\\\")
|
|
360
|
-
.replace(/"/g, '\\"')
|
|
361
|
-
.replace(/\$\{/g, "\\${");
|
|
362
|
-
}
|
|
363
362
|
function isNixExpr(value) {
|
|
364
363
|
return (typeof value === "object" &&
|
|
365
364
|
value !== null &&
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ResourceHandle } from "../core/types.js";
|
|
2
|
-
import type { WinDscProperties, WinPackageSource } from "../types/index.js";
|
|
2
|
+
import type { WinDscProperties, WinPackageSource, WinSettings } from "../types/index.js";
|
|
3
3
|
/**
|
|
4
4
|
* Accepted call shapes for `windows.package(...)`.
|
|
5
5
|
*
|
|
@@ -43,6 +43,10 @@ export interface WinDscSpec {
|
|
|
43
43
|
properties?: WinDscProperties;
|
|
44
44
|
dependsOn?: ResourceHandle | ResourceHandle[];
|
|
45
45
|
}
|
|
46
|
+
/** Options for `windows.setting(...)`. */
|
|
47
|
+
export interface WinSettingOpts {
|
|
48
|
+
dependsOn?: ResourceHandle | ResourceHandle[];
|
|
49
|
+
}
|
|
46
50
|
/** Scope for Windows environment and PATH writes. */
|
|
47
51
|
export type WinEnvScope = "user" | "machine";
|
|
48
52
|
/** Options shared by `windows.env.*` and `windows.path.*` methods. */
|
|
@@ -50,6 +54,16 @@ export interface WinEnvOpts {
|
|
|
50
54
|
scope?: WinEnvScope;
|
|
51
55
|
dependsOn?: ResourceHandle | ResourceHandle[];
|
|
52
56
|
}
|
|
57
|
+
export type WinFileEncoding = "utf8" | "utf8bom" | "ascii";
|
|
58
|
+
/** Options shared by `windows.file.*` methods. */
|
|
59
|
+
export interface WinFileOpts {
|
|
60
|
+
force?: boolean;
|
|
61
|
+
backup?: boolean;
|
|
62
|
+
recursive?: boolean;
|
|
63
|
+
elevate?: boolean;
|
|
64
|
+
encoding?: WinFileEncoding;
|
|
65
|
+
dependsOn?: ResourceHandle | ResourceHandle[];
|
|
66
|
+
}
|
|
53
67
|
export interface WinEnvNamespace {
|
|
54
68
|
set(name: string, value: string, opts?: WinEnvOpts): ResourceHandle;
|
|
55
69
|
remove(name: string, opts?: WinEnvOpts): ResourceHandle;
|
|
@@ -58,6 +72,12 @@ export interface WinPathNamespace {
|
|
|
58
72
|
add(value: string, opts?: WinEnvOpts): ResourceHandle;
|
|
59
73
|
remove(value: string, opts?: WinEnvOpts): ResourceHandle;
|
|
60
74
|
}
|
|
75
|
+
export interface WinFileNamespace {
|
|
76
|
+
text(target: string, content: string, opts?: WinFileOpts): ResourceHandle;
|
|
77
|
+
symlink(target: string, source: string, opts?: WinFileOpts): ResourceHandle;
|
|
78
|
+
copy(target: string, source: string, opts?: WinFileOpts): ResourceHandle;
|
|
79
|
+
remove(target: string, opts?: WinFileOpts): ResourceHandle;
|
|
80
|
+
}
|
|
61
81
|
export interface WindowsHelper {
|
|
62
82
|
/**
|
|
63
83
|
* Declare a winget/msstore package.
|
|
@@ -98,6 +118,18 @@ export interface WindowsHelper {
|
|
|
98
118
|
* ```
|
|
99
119
|
*/
|
|
100
120
|
dsc(arg: WinDscSpec): ResourceHandle;
|
|
121
|
+
/**
|
|
122
|
+
* Manage Windows OS settings through the native
|
|
123
|
+
* `Microsoft.Windows.Settings/WindowsSettings` DSC resource. The emitter
|
|
124
|
+
* automatically ensures the required `Microsoft.Windows.Settings` module is
|
|
125
|
+
* installed and wires settings resources to depend on it.
|
|
126
|
+
*
|
|
127
|
+
* ```ts
|
|
128
|
+
* windows.setting({ DeveloperMode: true });
|
|
129
|
+
* windows.setting({ SystemColorMode: "Dark", TaskbarAlignment: "Left" });
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
setting(settings: WinSettings, opts?: WinSettingOpts): ResourceHandle;
|
|
101
133
|
/**
|
|
102
134
|
* Manage a user/machine environment variable declaratively via the native
|
|
103
135
|
* `Microsoft.Windows/Registry` DSC resource.
|
|
@@ -118,5 +150,17 @@ export interface WindowsHelper {
|
|
|
118
150
|
* ```
|
|
119
151
|
*/
|
|
120
152
|
path: WinPathNamespace;
|
|
153
|
+
/**
|
|
154
|
+
* Manage files, copies, and dotfile symlinks declaratively via an idempotent
|
|
155
|
+
* `Microsoft.DSC.Transitional/WindowsPowerShellScript` resource.
|
|
156
|
+
*
|
|
157
|
+
* ```ts
|
|
158
|
+
* windows.file.text("%USERPROFILE%\\.gitconfig", "[user]\n");
|
|
159
|
+
* windows.file.symlink("%LOCALAPPDATA%\\nvim", "%USERPROFILE%\\dotfiles\\nvim");
|
|
160
|
+
* windows.file.copy("%APPDATA%\\tool\\config.json", ".\\config.json");
|
|
161
|
+
* windows.file.remove("%USERPROFILE%\\.oldrc");
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
file: WinFileNamespace;
|
|
121
165
|
}
|
|
122
166
|
export declare const windows: WindowsHelper;
|