@adrifer/winix 0.1.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/LICENSE +21 -0
- package/README.md +291 -0
- package/dist/backends/nix/index.d.ts +18 -0
- package/dist/backends/nix/index.js +442 -0
- package/dist/cli/activation.d.ts +6 -0
- package/dist/cli/activation.js +19 -0
- package/dist/cli/analysis.d.ts +30 -0
- package/dist/cli/analysis.js +170 -0
- package/dist/cli/commands/apply.d.ts +14 -0
- package/dist/cli/commands/apply.js +180 -0
- package/dist/cli/commands/check.d.ts +6 -0
- package/dist/cli/commands/check.js +46 -0
- package/dist/cli/commands/init.d.ts +5 -0
- package/dist/cli/commands/init.js +69 -0
- package/dist/cli/commands/inspect.d.ts +1 -0
- package/dist/cli/commands/inspect.js +24 -0
- package/dist/cli/commands/switch.d.ts +7 -0
- package/dist/cli/commands/switch.js +40 -0
- package/dist/cli/commands/update.d.ts +6 -0
- package/dist/cli/commands/update.js +33 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +122 -0
- package/dist/cli/loader.d.ts +11 -0
- package/dist/cli/loader.js +49 -0
- package/dist/cli/run.d.ts +4 -0
- package/dist/cli/run.js +26 -0
- package/dist/cli/types-gen/parser.d.ts +14 -0
- package/dist/cli/types-gen/parser.js +275 -0
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.js +1 -0
- package/dist/core/types.d.ts +106 -0
- package/dist/core/types.js +2 -0
- package/dist/evaluator/index.d.ts +20 -0
- package/dist/evaluator/index.js +184 -0
- package/dist/helpers/account.d.ts +46 -0
- package/dist/helpers/account.js +165 -0
- package/dist/helpers/darwin.d.ts +36 -0
- package/dist/helpers/darwin.js +38 -0
- package/dist/helpers/home.d.ts +40 -0
- package/dist/helpers/home.js +63 -0
- package/dist/helpers/index.d.ts +14 -0
- package/dist/helpers/index.js +7 -0
- package/dist/helpers/nix.d.ts +36 -0
- package/dist/helpers/nix.js +157 -0
- package/dist/helpers/nixos.d.ts +53 -0
- package/dist/helpers/nixos.js +86 -0
- package/dist/helpers/options.d.ts +10 -0
- package/dist/helpers/options.js +1 -0
- package/dist/helpers/overlay.d.ts +13 -0
- package/dist/helpers/overlay.js +29 -0
- package/dist/helpers/platforms.d.ts +21 -0
- package/dist/helpers/platforms.js +62 -0
- package/dist/helpers/utils.d.ts +6 -0
- package/dist/helpers/utils.js +8 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +5 -0
- package/dist/sdk/index.d.ts +26 -0
- package/dist/sdk/index.js +183 -0
- package/dist/types/common.d.ts +10 -0
- package/dist/types/common.js +1 -0
- package/dist/types/darwin.d.ts +86 -0
- package/dist/types/darwin.js +1 -0
- package/dist/types/home.d.ts +45 -0
- package/dist/types/home.js +1 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/index.js +5 -0
- package/dist/types/nixos.d.ts +225 -0
- package/dist/types/nixos.js +1 -0
- package/dist/types/programs.d.ts +67 -0
- package/dist/types/programs.js +1 -0
- package/index.d.ts +5 -0
- package/package.json +69 -0
- package/types/bundled/darwin.d.ts +476 -0
- package/types/bundled/home-manager.d.ts +4674 -0
- package/types/bundled/nixos.d.ts +19991 -0
- package/types/index.d.ts +8 -0
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
// Nix backend: generates .nix files from evaluated host IR
|
|
2
|
+
/**
|
|
3
|
+
* Generate Nix files from an evaluated workspace.
|
|
4
|
+
*/
|
|
5
|
+
export function generateNix(workspace, evaluatedHosts) {
|
|
6
|
+
const flakeNix = generateFlake(workspace, evaluatedHosts);
|
|
7
|
+
const hosts = {};
|
|
8
|
+
for (const host of evaluatedHosts) {
|
|
9
|
+
hosts[`${host.name}.nix`] = generateHostModule(host);
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
"flake.nix": flakeNix,
|
|
13
|
+
hosts,
|
|
14
|
+
rawModules: collectRawModules(evaluatedHosts),
|
|
15
|
+
warnings: validateWorkspaceInputs(workspace, evaluatedHosts),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function generateFlake(workspace, hosts) {
|
|
19
|
+
const inputLines = Object.entries(workspace.inputs)
|
|
20
|
+
.map(([name, def]) => formatInput(name, def))
|
|
21
|
+
.join("\n");
|
|
22
|
+
const nixosHosts = hosts
|
|
23
|
+
.filter((h) => Object.keys(h.nixos).length > 0)
|
|
24
|
+
.map((h) => ` nixosConfigurations.${h.name} = nixpkgs.lib.nixosSystem {\n` +
|
|
25
|
+
` system = "${systemForHost(h, "nixos")}";\n` +
|
|
26
|
+
` modules = [ ./hosts/${h.name}.nix ];\n` +
|
|
27
|
+
` specialArgs = { inherit inputs; };\n` +
|
|
28
|
+
` };`)
|
|
29
|
+
.join("\n\n");
|
|
30
|
+
const darwinHosts = hosts
|
|
31
|
+
.filter((h) => Object.keys(h.darwin).length > 0)
|
|
32
|
+
.map((h) => ` darwinConfigurations.${h.name} = inputs.nix-darwin.lib.darwinSystem {\n` +
|
|
33
|
+
` system = "${systemForHost(h, "darwin")}";\n` +
|
|
34
|
+
` modules = [ ./hosts/${h.name}.nix ];\n` +
|
|
35
|
+
` specialArgs = { inherit inputs; };\n` +
|
|
36
|
+
` };`)
|
|
37
|
+
.join("\n\n");
|
|
38
|
+
const outputs = [nixosHosts, darwinHosts].filter(Boolean).join("\n\n");
|
|
39
|
+
return `# Generated by Winix — do not edit
|
|
40
|
+
{
|
|
41
|
+
description = "Generated by Winix";
|
|
42
|
+
|
|
43
|
+
inputs = {
|
|
44
|
+
${inputLines}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
outputs = { self, nixpkgs, ... } @ inputs: {
|
|
48
|
+
${outputs}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
`;
|
|
52
|
+
}
|
|
53
|
+
function formatInput(name, def) {
|
|
54
|
+
const nixName = inputNixName(name, def);
|
|
55
|
+
if (typeof def === "string") {
|
|
56
|
+
const url = def.startsWith("github:") ? def : `github:NixOS/nixpkgs/${def}`;
|
|
57
|
+
return ` ${nixName}.url = "${url}";`;
|
|
58
|
+
}
|
|
59
|
+
const lines = [` ${nixName}.url = "${def.url}";`];
|
|
60
|
+
if (def.follows) {
|
|
61
|
+
for (const [key, value] of Object.entries(def.follows)) {
|
|
62
|
+
lines.push(` ${nixName}.inputs.${key}.follows = "${value}";`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return lines.join("\n");
|
|
66
|
+
}
|
|
67
|
+
// Known input name → NixOS module path mapping
|
|
68
|
+
const MODULE_MAP = {
|
|
69
|
+
"home-manager": "inputs.home-manager.nixosModules.home-manager",
|
|
70
|
+
"nixos-wsl": "inputs.nixos-wsl.nixosModules.wsl",
|
|
71
|
+
"nix-darwin": "inputs.nix-darwin.darwinModules.default",
|
|
72
|
+
};
|
|
73
|
+
function generateHostModule(host) {
|
|
74
|
+
const lines = [
|
|
75
|
+
`# Generated by Winix — do not edit`,
|
|
76
|
+
`{ config, lib, pkgs, inputs, ... }:`,
|
|
77
|
+
``,
|
|
78
|
+
`{`,
|
|
79
|
+
];
|
|
80
|
+
// Generate imports
|
|
81
|
+
const imports = getImports(host.nixos);
|
|
82
|
+
const nixosRaw = getRawBlocks(host.nixos);
|
|
83
|
+
if (imports.length > 0) {
|
|
84
|
+
lines.push(...importsToNix(imports, 1));
|
|
85
|
+
lines.push(``);
|
|
86
|
+
}
|
|
87
|
+
// Generate NixOS config (skip internal fields)
|
|
88
|
+
const skipKeys = ["imports", "__id", "__platform", "homeManager"];
|
|
89
|
+
if (Object.keys(host.nixos).length > 0) {
|
|
90
|
+
const nixLines = objectToNix(filterKeys(host.nixos, [...skipKeys, "__raw"]), 1, "nixos");
|
|
91
|
+
lines.push(...nixLines);
|
|
92
|
+
}
|
|
93
|
+
if (nixosRaw.length > 0) {
|
|
94
|
+
lines.push(...rawBlocksToNix(nixosRaw, 1));
|
|
95
|
+
}
|
|
96
|
+
// Emit home-manager settings
|
|
97
|
+
const hm = host.nixos.homeManager;
|
|
98
|
+
if (hm) {
|
|
99
|
+
lines.push(``);
|
|
100
|
+
for (const [key, value] of Object.entries(hm)) {
|
|
101
|
+
lines.push(` home-manager.${key} = ${formatNixValue(value)};`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// Generate darwin config
|
|
105
|
+
if (Object.keys(host.darwin).length > 0) {
|
|
106
|
+
const darwinImports = getImports(host.darwin);
|
|
107
|
+
const darwinRaw = getRawBlocks(host.darwin);
|
|
108
|
+
if (darwinImports.length > 0) {
|
|
109
|
+
lines.push(...importsToNix(darwinImports, 1));
|
|
110
|
+
lines.push(``);
|
|
111
|
+
}
|
|
112
|
+
const darwinLines = objectToNix(filterKeys(host.darwin, ["imports", "__raw"]), 1, "darwin");
|
|
113
|
+
lines.push(...darwinLines);
|
|
114
|
+
if (darwinRaw.length > 0) {
|
|
115
|
+
lines.push(...rawBlocksToNix(darwinRaw, 1));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
// Generate Home Manager config (inline)
|
|
119
|
+
if (Object.keys(host.homeManager).length > 0) {
|
|
120
|
+
const username = homeUsername(host.homeManager) ?? "user";
|
|
121
|
+
const homeManagerData = filterKeys(host.homeManager, ["username"]);
|
|
122
|
+
if (Object.keys(homeManagerData).length > 0) {
|
|
123
|
+
lines.push(``);
|
|
124
|
+
lines.push(` # Home Manager`);
|
|
125
|
+
lines.push(` home-manager.users.${username} = { config, lib, pkgs, ... }: {`);
|
|
126
|
+
const homeManagerImports = getImports(homeManagerData);
|
|
127
|
+
const homeManagerRaw = getRawBlocks(homeManagerData);
|
|
128
|
+
if (homeManagerImports.length > 0) {
|
|
129
|
+
lines.push(...importsToNix(homeManagerImports, 2));
|
|
130
|
+
}
|
|
131
|
+
const homeManagerLines = objectToNix(filterKeys(homeManagerData, ["imports", "__raw"]), 2, "home");
|
|
132
|
+
lines.push(...homeManagerLines);
|
|
133
|
+
if (homeManagerRaw.length > 0) {
|
|
134
|
+
lines.push(...rawBlocksToNix(homeManagerRaw, 2));
|
|
135
|
+
}
|
|
136
|
+
lines.push(` };`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
lines.push(`}`);
|
|
140
|
+
return lines.join("\n") + "\n";
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Convert a JS object tree to Nix attribute lines.
|
|
144
|
+
*/
|
|
145
|
+
function objectToNix(obj, indent, scope, path = []) {
|
|
146
|
+
const lines = [];
|
|
147
|
+
const prefix = " ".repeat(indent);
|
|
148
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
149
|
+
if (key === "__raw") {
|
|
150
|
+
throw new Error("Internal error: raw blocks must be extracted before objectToNix(). This is a Winix bug.");
|
|
151
|
+
}
|
|
152
|
+
if (key.startsWith("__"))
|
|
153
|
+
continue; // skip internal fields
|
|
154
|
+
if (value === undefined)
|
|
155
|
+
continue;
|
|
156
|
+
if (isRawModuleRef(value)) {
|
|
157
|
+
throw new Error("rawModule() references are only supported in imports arrays. " +
|
|
158
|
+
"Use rawModule() inside a fragment's imports field, e.g. { nixos: { imports: [rawModule('path.nix')] } }.");
|
|
159
|
+
}
|
|
160
|
+
const mappedPath = mapKnownOptionPath(scope, [...path, key]);
|
|
161
|
+
const currentPath = mappedPath;
|
|
162
|
+
const nixPath = formatAttrPath(currentPath);
|
|
163
|
+
if (Array.isArray(value)) {
|
|
164
|
+
const packageAttr = packageAttributeForPath(scope, nixPath);
|
|
165
|
+
if (packageAttr) {
|
|
166
|
+
// Packages need pkgs prefix, no quotes
|
|
167
|
+
const items = value.map((v) => formatPackageItem(v)).join(" ");
|
|
168
|
+
lines.push(`${prefix}${packageAttr} = with pkgs; [ ${items} ];`);
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
const items = value.map((v) => formatNixValue(v)).join(" ");
|
|
172
|
+
lines.push(`${prefix}${nixPath} = [ ${items} ];`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
else if (isPlainObject(value)) {
|
|
176
|
+
// Always recurse — never inline attr sets (avoids quoting/nesting issues)
|
|
177
|
+
lines.push(...objectToNix(value, indent, scope, currentPath));
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
lines.push(`${prefix}${nixPath} = ${formatNixValue(value)};`);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return lines;
|
|
184
|
+
}
|
|
185
|
+
function packageAttributeForPath(scope, nixPath) {
|
|
186
|
+
if (scope === "nixos") {
|
|
187
|
+
if (nixPath === "packages")
|
|
188
|
+
return "environment.systemPackages";
|
|
189
|
+
if (nixPath === "environment.systemPackages")
|
|
190
|
+
return nixPath;
|
|
191
|
+
}
|
|
192
|
+
if (scope === "darwin") {
|
|
193
|
+
if (nixPath === "packages")
|
|
194
|
+
return "environment.systemPackages";
|
|
195
|
+
if (nixPath === "environment.systemPackages")
|
|
196
|
+
return nixPath;
|
|
197
|
+
}
|
|
198
|
+
if (scope === "home") {
|
|
199
|
+
if (nixPath === "packages")
|
|
200
|
+
return "home.packages";
|
|
201
|
+
if (nixPath === "home.packages")
|
|
202
|
+
return nixPath;
|
|
203
|
+
}
|
|
204
|
+
return undefined;
|
|
205
|
+
}
|
|
206
|
+
function importsToNix(imports, indent) {
|
|
207
|
+
const prefix = " ".repeat(indent);
|
|
208
|
+
const itemPrefix = " ".repeat(indent + 1);
|
|
209
|
+
const importLines = uniqueBy(imports, formatImport).map((imp) => {
|
|
210
|
+
return `${itemPrefix}${formatImport(imp)}`;
|
|
211
|
+
});
|
|
212
|
+
return [`${prefix}imports = [`, ...importLines, `${prefix}];`];
|
|
213
|
+
}
|
|
214
|
+
function formatImport(imp) {
|
|
215
|
+
if (isRawModuleRef(imp)) {
|
|
216
|
+
return `../raw-modules/${imp.path}`;
|
|
217
|
+
}
|
|
218
|
+
if (typeof imp === "string") {
|
|
219
|
+
return MODULE_MAP[imp] ?? imp;
|
|
220
|
+
}
|
|
221
|
+
throw new Error("imports entries must be strings or rawModule() references");
|
|
222
|
+
}
|
|
223
|
+
function getImports(obj) {
|
|
224
|
+
const imports = obj.imports;
|
|
225
|
+
if (imports === undefined)
|
|
226
|
+
return [];
|
|
227
|
+
if (!Array.isArray(imports)) {
|
|
228
|
+
throw new Error("imports must be an array");
|
|
229
|
+
}
|
|
230
|
+
return imports.map((imp) => {
|
|
231
|
+
if (isNixExpr(imp)) {
|
|
232
|
+
throw new Error("nix.expr() expressions are not supported in imports arrays. " +
|
|
233
|
+
"Use a string for module paths. nix.expr() is for option values.");
|
|
234
|
+
}
|
|
235
|
+
if (typeof imp === "string" || isRawModuleRef(imp)) {
|
|
236
|
+
return imp;
|
|
237
|
+
}
|
|
238
|
+
throw new Error("imports entries must be strings or rawModule() references");
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
function getRawBlocks(obj) {
|
|
242
|
+
const rawBlocks = obj.__raw;
|
|
243
|
+
if (rawBlocks === undefined)
|
|
244
|
+
return [];
|
|
245
|
+
if (!Array.isArray(rawBlocks) || rawBlocks.some((block) => typeof block !== "string")) {
|
|
246
|
+
throw new Error("raw fragments must contain string bodies");
|
|
247
|
+
}
|
|
248
|
+
return rawBlocks;
|
|
249
|
+
}
|
|
250
|
+
function rawBlocksToNix(rawBlocks, indent) {
|
|
251
|
+
const prefix = " ".repeat(indent);
|
|
252
|
+
return rawBlocks.flatMap((block) => block
|
|
253
|
+
.trim()
|
|
254
|
+
.split("\n")
|
|
255
|
+
.map((line) => `${prefix}${line}`));
|
|
256
|
+
}
|
|
257
|
+
function collectRawModules(hosts) {
|
|
258
|
+
const rawModules = new Map();
|
|
259
|
+
for (const host of hosts) {
|
|
260
|
+
for (const scope of [host.nixos, host.homeManager, host.darwin]) {
|
|
261
|
+
for (const imp of getImports(scope)) {
|
|
262
|
+
if (isRawModuleRef(imp)) {
|
|
263
|
+
rawModules.set(imp.path, { path: imp.path });
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return [...rawModules.values()];
|
|
269
|
+
}
|
|
270
|
+
function uniqueBy(values, keyForValue) {
|
|
271
|
+
const seen = new Set();
|
|
272
|
+
const result = [];
|
|
273
|
+
for (const value of values) {
|
|
274
|
+
const key = keyForValue(value);
|
|
275
|
+
if (!seen.has(key)) {
|
|
276
|
+
seen.add(key);
|
|
277
|
+
result.push(value);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return result;
|
|
281
|
+
}
|
|
282
|
+
function formatNixValue(value) {
|
|
283
|
+
if (isNixExpr(value)) {
|
|
284
|
+
return value.expr;
|
|
285
|
+
}
|
|
286
|
+
if (isRawModuleRef(value)) {
|
|
287
|
+
throw new Error("rawModule() references are only supported in imports arrays. " +
|
|
288
|
+
"Use rawModule() inside a fragment's imports field, e.g. { nixos: { imports: [rawModule('path.nix')] } }.");
|
|
289
|
+
}
|
|
290
|
+
if (typeof value === "string") {
|
|
291
|
+
if (isPkgsReference(value))
|
|
292
|
+
return value;
|
|
293
|
+
return `"${value}"`;
|
|
294
|
+
}
|
|
295
|
+
if (typeof value === "number")
|
|
296
|
+
return String(value);
|
|
297
|
+
if (typeof value === "boolean")
|
|
298
|
+
return value ? "true" : "false";
|
|
299
|
+
if (Array.isArray(value)) {
|
|
300
|
+
return `[ ${value.map((item) => formatNixValue(item)).join(" ")} ]`;
|
|
301
|
+
}
|
|
302
|
+
if (isPlainObject(value)) {
|
|
303
|
+
return formatInlineAttrSet(value);
|
|
304
|
+
}
|
|
305
|
+
return `"${String(value)}"`;
|
|
306
|
+
}
|
|
307
|
+
function formatInlineAttrSet(obj) {
|
|
308
|
+
// Scope-specific package rewriting only applies to top-level fragment fields;
|
|
309
|
+
// inline attrsets are already inside a concrete option value.
|
|
310
|
+
const parts = objectToNix(obj, 0, "home").map((line) => line.trim());
|
|
311
|
+
return `{ ${parts.join(" ")} }`;
|
|
312
|
+
}
|
|
313
|
+
function formatPackageItem(value) {
|
|
314
|
+
if (isNixExpr(value))
|
|
315
|
+
return value.expr;
|
|
316
|
+
if (typeof value === "string")
|
|
317
|
+
return value;
|
|
318
|
+
throw new Error("package lists only support strings or nix.expr() expressions");
|
|
319
|
+
}
|
|
320
|
+
function isPkgsReference(value) {
|
|
321
|
+
return /^pkgs\.[A-Za-z_][A-Za-z0-9_'-]*(\.[A-Za-z_][A-Za-z0-9_'-]*)*$/.test(value);
|
|
322
|
+
}
|
|
323
|
+
function formatAttrPath(path) {
|
|
324
|
+
return path.map(formatAttrKey).join(".");
|
|
325
|
+
}
|
|
326
|
+
function formatAttrKey(key) {
|
|
327
|
+
return needsQuoting(key) ? JSON.stringify(key) : key;
|
|
328
|
+
}
|
|
329
|
+
function needsQuoting(key) {
|
|
330
|
+
if (NIX_KEYWORDS.has(key))
|
|
331
|
+
return true;
|
|
332
|
+
return !/^[a-zA-Z_][a-zA-Z0-9_'-]*$/.test(key);
|
|
333
|
+
}
|
|
334
|
+
const NIX_KEYWORDS = new Set([
|
|
335
|
+
"assert",
|
|
336
|
+
"else",
|
|
337
|
+
"false",
|
|
338
|
+
"if",
|
|
339
|
+
"in",
|
|
340
|
+
"inherit",
|
|
341
|
+
"let",
|
|
342
|
+
"null",
|
|
343
|
+
"or",
|
|
344
|
+
"rec",
|
|
345
|
+
"then",
|
|
346
|
+
"true",
|
|
347
|
+
"with",
|
|
348
|
+
]);
|
|
349
|
+
function mapKnownOptionPath(scope, path) {
|
|
350
|
+
if (scope === "home" && path[0] === "activation") {
|
|
351
|
+
return ["home", "activation", ...path.slice(1)];
|
|
352
|
+
}
|
|
353
|
+
const key = `${scope}:${path.join(".")}`;
|
|
354
|
+
const mapped = KNOWN_OPTION_PATHS[key];
|
|
355
|
+
if (mapped)
|
|
356
|
+
return mapped.split(".");
|
|
357
|
+
for (const prefix of KEBAB_NAME_PREFIXES[scope]) {
|
|
358
|
+
const prefixSegments = prefix.split(".");
|
|
359
|
+
if (path.length !== prefixSegments.length + 1)
|
|
360
|
+
continue;
|
|
361
|
+
if (!prefixSegments.every((segment, index) => path[index] === segment))
|
|
362
|
+
continue;
|
|
363
|
+
const name = path[prefixSegments.length];
|
|
364
|
+
if (hasCamelCaseBoundary(name)) {
|
|
365
|
+
return [...prefixSegments, toKebabCase(name)];
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return path;
|
|
369
|
+
}
|
|
370
|
+
const KNOWN_OPTION_PATHS = {
|
|
371
|
+
"nixos:nix.settings.experimentalFeatures": "nix.settings.experimental-features",
|
|
372
|
+
"darwin:nix.settings.experimentalFeatures": "nix.settings.experimental-features",
|
|
373
|
+
"home:nix.settings.experimentalFeatures": "nix.settings.experimental-features",
|
|
374
|
+
"home:sessionVariables": "home.sessionVariables",
|
|
375
|
+
"home:sessionPath": "home.sessionPath",
|
|
376
|
+
};
|
|
377
|
+
const KEBAB_NAME_PREFIXES = {
|
|
378
|
+
nixos: ["programs", "services", "nix.settings", "virtualisation"],
|
|
379
|
+
home: ["programs", "services"],
|
|
380
|
+
darwin: ["programs", "services", "nix.settings", "homebrew"],
|
|
381
|
+
};
|
|
382
|
+
function hasCamelCaseBoundary(value) {
|
|
383
|
+
return /[a-z][A-Z]/.test(value);
|
|
384
|
+
}
|
|
385
|
+
function systemForHost(host, scope) {
|
|
386
|
+
const config = scope === "nixos" ? host.nixos : host.darwin;
|
|
387
|
+
const fallback = scope === "nixos" ? "x86_64-linux" : "aarch64-darwin";
|
|
388
|
+
const hostPlatform = config.nixpkgs?.hostPlatform;
|
|
389
|
+
return typeof hostPlatform === "string" ? hostPlatform : fallback;
|
|
390
|
+
}
|
|
391
|
+
function validateWorkspaceInputs(workspace, hosts) {
|
|
392
|
+
const warnings = [];
|
|
393
|
+
if (hosts.some((host) => Object.keys(host.darwin).length > 0) && !hasInput(workspace, "nix-darwin")) {
|
|
394
|
+
warnings.push("darwin host detected but workspace inputs do not include nix-darwin");
|
|
395
|
+
}
|
|
396
|
+
return warnings;
|
|
397
|
+
}
|
|
398
|
+
function hasInput(workspace, nixName) {
|
|
399
|
+
return Object.entries(workspace.inputs).some(([name, def]) => inputNixName(name, def) === nixName);
|
|
400
|
+
}
|
|
401
|
+
function inputNixName(name, def) {
|
|
402
|
+
return typeof def === "object" && def.nixName ? def.nixName : toKebabCase(name);
|
|
403
|
+
}
|
|
404
|
+
function toKebabCase(str) {
|
|
405
|
+
return str.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
|
|
406
|
+
}
|
|
407
|
+
function filterKeys(obj, exclude) {
|
|
408
|
+
const result = {};
|
|
409
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
410
|
+
if (!exclude.includes(k)) {
|
|
411
|
+
result[k] = v;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
return result;
|
|
415
|
+
}
|
|
416
|
+
function homeUsername(home) {
|
|
417
|
+
const nestedHome = home.home;
|
|
418
|
+
const nestedUsername = nestedHome?.username;
|
|
419
|
+
if (typeof nestedUsername === "string")
|
|
420
|
+
return nestedUsername;
|
|
421
|
+
const topLevelUsername = home.username;
|
|
422
|
+
return typeof topLevelUsername === "string" ? topLevelUsername : undefined;
|
|
423
|
+
}
|
|
424
|
+
function isPlainObject(val) {
|
|
425
|
+
return (typeof val === "object" &&
|
|
426
|
+
val !== null &&
|
|
427
|
+
!Array.isArray(val) &&
|
|
428
|
+
!isNixExpr(val) &&
|
|
429
|
+
!isRawModuleRef(val));
|
|
430
|
+
}
|
|
431
|
+
function isRawModuleRef(val) {
|
|
432
|
+
return (typeof val === "object" &&
|
|
433
|
+
val !== null &&
|
|
434
|
+
val.__winixRawModule === true &&
|
|
435
|
+
typeof val.path === "string");
|
|
436
|
+
}
|
|
437
|
+
function isNixExpr(val) {
|
|
438
|
+
return (typeof val === "object" &&
|
|
439
|
+
val !== null &&
|
|
440
|
+
val.__winixNixExpr === true &&
|
|
441
|
+
typeof val.expr === "string");
|
|
442
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type ActivationPlatform = "nixos" | "darwin";
|
|
2
|
+
export declare function activationCommand(platform: ActivationPlatform, flake: string, isRoot?: boolean): string[];
|
|
3
|
+
export declare function platformForEvaluatedHost(host: {
|
|
4
|
+
nixos: Record<string, unknown>;
|
|
5
|
+
darwin: Record<string, unknown>;
|
|
6
|
+
}): ActivationPlatform;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function activationCommand(platform, flake, isRoot = isCurrentProcessRoot()) {
|
|
2
|
+
const command = platform === "darwin"
|
|
3
|
+
? ["darwin-rebuild", "switch", "--flake", flake]
|
|
4
|
+
: ["nixos-rebuild", "switch", "--flake", flake];
|
|
5
|
+
return withSudo(command, isRoot);
|
|
6
|
+
}
|
|
7
|
+
export function platformForEvaluatedHost(host) {
|
|
8
|
+
return Object.keys(host.darwin).length > 0 && Object.keys(host.nixos).length === 0
|
|
9
|
+
? "darwin"
|
|
10
|
+
: "nixos";
|
|
11
|
+
}
|
|
12
|
+
function withSudo(command, isRoot) {
|
|
13
|
+
if (isRoot)
|
|
14
|
+
return command;
|
|
15
|
+
return ["sudo", ...command];
|
|
16
|
+
}
|
|
17
|
+
function isCurrentProcessRoot() {
|
|
18
|
+
return typeof process.getuid === "function" && process.getuid() === 0;
|
|
19
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Fragment, WorkspaceDef } from "../core/types.js";
|
|
2
|
+
export interface FragmentRecord {
|
|
3
|
+
label: string;
|
|
4
|
+
fragment: Fragment;
|
|
5
|
+
}
|
|
6
|
+
export interface HostAnalysis {
|
|
7
|
+
name: string;
|
|
8
|
+
platform: string;
|
|
9
|
+
fragments: FragmentRecord[];
|
|
10
|
+
}
|
|
11
|
+
export interface Conflict {
|
|
12
|
+
host: string;
|
|
13
|
+
scope: string;
|
|
14
|
+
path: string;
|
|
15
|
+
firstFragment: string;
|
|
16
|
+
secondFragment: string;
|
|
17
|
+
firstValue: string;
|
|
18
|
+
secondValue: string;
|
|
19
|
+
}
|
|
20
|
+
export interface EscapeReportItem {
|
|
21
|
+
host: string;
|
|
22
|
+
fragment: string;
|
|
23
|
+
scope: string;
|
|
24
|
+
path: string;
|
|
25
|
+
kind: "escape" | "raw" | "rawModule";
|
|
26
|
+
}
|
|
27
|
+
export declare function analyzeWorkspace(workspace: WorkspaceDef): HostAnalysis[];
|
|
28
|
+
export declare function findDuplicateHosts(workspace: WorkspaceDef): string[];
|
|
29
|
+
export declare function detectConflicts(analyses: HostAnalysis[]): Conflict[];
|
|
30
|
+
export declare function collectEscapeReport(analyses: HostAnalysis[]): EscapeReportItem[];
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { collectActiveIds } from "../evaluator/index.js";
|
|
2
|
+
import { restoreEvalContext, setEvalContext } from "../sdk/index.js";
|
|
3
|
+
export function analyzeWorkspace(workspace) {
|
|
4
|
+
return workspace.hosts.map((host) => analyzeHost(host));
|
|
5
|
+
}
|
|
6
|
+
export function findDuplicateHosts(workspace) {
|
|
7
|
+
const seen = new Set();
|
|
8
|
+
const duplicates = new Set();
|
|
9
|
+
for (const host of workspace.hosts) {
|
|
10
|
+
if (seen.has(host.name))
|
|
11
|
+
duplicates.add(host.name);
|
|
12
|
+
seen.add(host.name);
|
|
13
|
+
}
|
|
14
|
+
return [...duplicates];
|
|
15
|
+
}
|
|
16
|
+
export function detectConflicts(analyses) {
|
|
17
|
+
const conflicts = [];
|
|
18
|
+
for (const analysis of analyses) {
|
|
19
|
+
const seen = new Map();
|
|
20
|
+
for (const record of analysis.fragments) {
|
|
21
|
+
for (const scope of ["nixos", "homeManager", "darwin"]) {
|
|
22
|
+
const data = record.fragment[scope];
|
|
23
|
+
if (!data)
|
|
24
|
+
continue;
|
|
25
|
+
for (const leaf of walkLeaves(data)) {
|
|
26
|
+
if (Array.isArray(leaf.value))
|
|
27
|
+
continue;
|
|
28
|
+
if (leaf.path[0] === "__raw" || leaf.path[0] === "imports")
|
|
29
|
+
continue;
|
|
30
|
+
const key = `${scope}:${leaf.path.join(".")}`;
|
|
31
|
+
const formatted = formatValue(leaf.value);
|
|
32
|
+
const previous = seen.get(key);
|
|
33
|
+
if (previous && previous.formatted !== formatted) {
|
|
34
|
+
conflicts.push({
|
|
35
|
+
host: analysis.name,
|
|
36
|
+
scope,
|
|
37
|
+
path: leaf.path.join("."),
|
|
38
|
+
firstFragment: previous.fragment,
|
|
39
|
+
secondFragment: record.label,
|
|
40
|
+
firstValue: previous.formatted,
|
|
41
|
+
secondValue: formatted,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
else if (!previous) {
|
|
45
|
+
seen.set(key, { fragment: record.label, value: leaf.value, formatted });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return conflicts;
|
|
52
|
+
}
|
|
53
|
+
export function collectEscapeReport(analyses) {
|
|
54
|
+
const items = [];
|
|
55
|
+
for (const analysis of analyses) {
|
|
56
|
+
for (const record of analysis.fragments) {
|
|
57
|
+
for (const scope of ["nixos", "homeManager", "darwin"]) {
|
|
58
|
+
const data = record.fragment[scope];
|
|
59
|
+
if (!data)
|
|
60
|
+
continue;
|
|
61
|
+
collectEscapeItems(analysis.name, record.label, scope, [], data, items);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return items;
|
|
66
|
+
}
|
|
67
|
+
function analyzeHost(host) {
|
|
68
|
+
const platform = host.platform.__id;
|
|
69
|
+
const activeIds = collectActiveIds(host);
|
|
70
|
+
const ctx = { platform, hostname: host.name, activeIds };
|
|
71
|
+
const prev = setEvalContext(ctx);
|
|
72
|
+
const fragments = [];
|
|
73
|
+
try {
|
|
74
|
+
resolveEntry(host.platform, host.platform.__id, fragments);
|
|
75
|
+
host.fragments.forEach((entry, index) => {
|
|
76
|
+
resolveEntry(entry, labelForEntry(entry, index), fragments);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
finally {
|
|
80
|
+
restoreEvalContext(prev);
|
|
81
|
+
}
|
|
82
|
+
return { name: host.name, platform, fragments };
|
|
83
|
+
}
|
|
84
|
+
function resolveEntry(entry, label, out) {
|
|
85
|
+
if (isLazy(entry)) {
|
|
86
|
+
const resolved = entry.__resolve();
|
|
87
|
+
if (Array.isArray(resolved)) {
|
|
88
|
+
resolved.forEach((item, index) => resolveEntry(item, `${entry.__id}[${index}]`, out));
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
resolveEntry(resolved, entry.__id, out);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else if (Array.isArray(entry)) {
|
|
95
|
+
entry.forEach((item, index) => resolveEntry(item, `${label}[${index}]`, out));
|
|
96
|
+
}
|
|
97
|
+
else if (typeof entry === "object" && entry !== null) {
|
|
98
|
+
const fragment = entry;
|
|
99
|
+
out.push({ label: fragment.__id ?? label, fragment });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
function labelForEntry(entry, index) {
|
|
103
|
+
if (isLazy(entry))
|
|
104
|
+
return entry.__id;
|
|
105
|
+
if (Array.isArray(entry))
|
|
106
|
+
return `inline[${index}]`;
|
|
107
|
+
return entry.__id ?? `inline[${index}]`;
|
|
108
|
+
}
|
|
109
|
+
function walkLeaves(value, path = []) {
|
|
110
|
+
if (Array.isArray(value) || isNixExpr(value) || isRawModuleRef(value) || !isPlainObject(value)) {
|
|
111
|
+
return [{ path, value }];
|
|
112
|
+
}
|
|
113
|
+
return Object.entries(value).flatMap(([key, child]) => walkLeaves(child, [...path, key]));
|
|
114
|
+
}
|
|
115
|
+
function collectEscapeItems(host, fragment, scope, path, value, out) {
|
|
116
|
+
if (isNixExpr(value)) {
|
|
117
|
+
out.push({ host, fragment, scope, path: path.join(".") || "<root>", kind: "escape" });
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (isRawModuleRef(value)) {
|
|
121
|
+
out.push({ host, fragment, scope, path: path.join(".") || "<root>", kind: "rawModule" });
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (Array.isArray(value)) {
|
|
125
|
+
value.forEach((item, index) => collectEscapeItems(host, fragment, scope, [...path, String(index)], item, out));
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (!isPlainObject(value))
|
|
129
|
+
return;
|
|
130
|
+
const rawBlocks = value.__raw;
|
|
131
|
+
if (Array.isArray(rawBlocks)) {
|
|
132
|
+
rawBlocks.forEach((_, index) => {
|
|
133
|
+
out.push({
|
|
134
|
+
host,
|
|
135
|
+
fragment,
|
|
136
|
+
scope,
|
|
137
|
+
path: [...path, "__raw", String(index)].join("."),
|
|
138
|
+
kind: "raw",
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
for (const [key, child] of Object.entries(value)) {
|
|
143
|
+
if (key === "__raw")
|
|
144
|
+
continue;
|
|
145
|
+
collectEscapeItems(host, fragment, scope, [...path, key], child, out);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function formatValue(value) {
|
|
149
|
+
if (isNixExpr(value))
|
|
150
|
+
return value.expr;
|
|
151
|
+
if (isRawModuleRef(value))
|
|
152
|
+
return `rawModule:${value.path}`;
|
|
153
|
+
return JSON.stringify(value);
|
|
154
|
+
}
|
|
155
|
+
function isLazy(entry) {
|
|
156
|
+
return typeof entry === "object" && entry !== null && entry.__lazy === true;
|
|
157
|
+
}
|
|
158
|
+
function isPlainObject(value) {
|
|
159
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
160
|
+
}
|
|
161
|
+
function isNixExpr(value) {
|
|
162
|
+
return (typeof value === "object" &&
|
|
163
|
+
value !== null &&
|
|
164
|
+
value.__winixNixExpr === true);
|
|
165
|
+
}
|
|
166
|
+
function isRawModuleRef(value) {
|
|
167
|
+
return (typeof value === "object" &&
|
|
168
|
+
value !== null &&
|
|
169
|
+
value.__winixRawModule === true);
|
|
170
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface ApplyOptions {
|
|
2
|
+
host?: string;
|
|
3
|
+
dry: boolean;
|
|
4
|
+
diff: boolean;
|
|
5
|
+
}
|
|
6
|
+
export interface ApplyResult {
|
|
7
|
+
configDir: string;
|
|
8
|
+
outDir: string;
|
|
9
|
+
hostNames: string[];
|
|
10
|
+
hostPlatforms: Map<string, "nixos" | "darwin">;
|
|
11
|
+
}
|
|
12
|
+
export declare function applyWorkspace(cwd: string, opts: ApplyOptions): Promise<ApplyResult>;
|
|
13
|
+
export declare function apply(cwd: string, opts: ApplyOptions): Promise<void>;
|
|
14
|
+
export {};
|