@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,180 @@
|
|
|
1
|
+
// winix apply — generate .winix/out/ from config
|
|
2
|
+
import { isAbsolute, join, dirname, relative } from "node:path";
|
|
3
|
+
import { mkdir, writeFile, readFile, copyFile, cp, realpath, rm } from "node:fs/promises";
|
|
4
|
+
import { existsSync } from "node:fs";
|
|
5
|
+
import { loadWorkspace } from "../loader.js";
|
|
6
|
+
import { evaluate } from "../../evaluator/index.js";
|
|
7
|
+
import { generateNix } from "../../backends/nix/index.js";
|
|
8
|
+
import { activationCommand, platformForEvaluatedHost } from "../activation.js";
|
|
9
|
+
export async function applyWorkspace(cwd, opts) {
|
|
10
|
+
const { workspace, configDir } = await loadWorkspace(cwd);
|
|
11
|
+
let evaluated = evaluate(workspace);
|
|
12
|
+
if (opts.host) {
|
|
13
|
+
evaluated = evaluated.filter((h) => h.name === opts.host);
|
|
14
|
+
if (evaluated.length === 0) {
|
|
15
|
+
const all = evaluate(workspace);
|
|
16
|
+
throw new Error(`Host "${opts.host}" not found. Available hosts:\n` +
|
|
17
|
+
all.map((h) => ` - ${h.name}`).join("\n"));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const output = generateNix(workspace, evaluated);
|
|
21
|
+
printWarnings(output.warnings);
|
|
22
|
+
const outDir = join(configDir, ".winix", "out");
|
|
23
|
+
if (opts.dry) {
|
|
24
|
+
console.log("=== .winix/out/flake.nix ===");
|
|
25
|
+
console.log(output["flake.nix"]);
|
|
26
|
+
for (const [name, content] of Object.entries(output.hosts)) {
|
|
27
|
+
console.log(`\n=== .winix/out/hosts/${name} ===`);
|
|
28
|
+
console.log(content);
|
|
29
|
+
}
|
|
30
|
+
return resultFor(configDir, outDir, evaluated);
|
|
31
|
+
}
|
|
32
|
+
if (opts.diff) {
|
|
33
|
+
await showDiff(configDir, outDir, output);
|
|
34
|
+
return resultFor(configDir, outDir, evaluated);
|
|
35
|
+
}
|
|
36
|
+
await mkdir(join(outDir, "hosts"), { recursive: true });
|
|
37
|
+
await writeFile(join(outDir, "flake.nix"), output["flake.nix"]);
|
|
38
|
+
for (const [name, content] of Object.entries(output.hosts)) {
|
|
39
|
+
await writeFile(join(outDir, "hosts", name), content);
|
|
40
|
+
}
|
|
41
|
+
await copyRawModules(configDir, outDir, output.rawModules);
|
|
42
|
+
// Copy flake.lock from project root if it exists.
|
|
43
|
+
const rootLock = join(cwd, "flake.lock");
|
|
44
|
+
const outLock = join(outDir, "flake.lock");
|
|
45
|
+
if (existsSync(rootLock)) {
|
|
46
|
+
await rm(outLock, { force: true });
|
|
47
|
+
await copyFile(rootLock, outLock);
|
|
48
|
+
}
|
|
49
|
+
console.log(`✓ Generated ${Object.keys(output.hosts).length} host(s) in .winix/out/`);
|
|
50
|
+
for (const name of Object.keys(output.hosts)) {
|
|
51
|
+
console.log(` → hosts/${name}`);
|
|
52
|
+
}
|
|
53
|
+
printNextSteps(cwd, outDir, evaluated);
|
|
54
|
+
return resultFor(configDir, outDir, evaluated);
|
|
55
|
+
}
|
|
56
|
+
export async function apply(cwd, opts) {
|
|
57
|
+
await applyWorkspace(cwd, opts);
|
|
58
|
+
}
|
|
59
|
+
function resultFor(configDir, outDir, evaluated) {
|
|
60
|
+
return {
|
|
61
|
+
configDir,
|
|
62
|
+
outDir,
|
|
63
|
+
hostNames: evaluated.map((host) => host.name),
|
|
64
|
+
hostPlatforms: new Map(evaluated.map((host) => [host.name, platformForEvaluatedHost(host)])),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function printNextSteps(cwd, outDir, evaluated) {
|
|
68
|
+
const relativeOutDir = relative(cwd, outDir) || ".winix/out";
|
|
69
|
+
const flakeBase = `path:$(pwd)/${relativeOutDir}`;
|
|
70
|
+
const commands = evaluated.map((host) => activationCommand(platformForEvaluatedHost(host), `${flakeBase}#${host.name}`, false).join(" "));
|
|
71
|
+
if (commands.length === 1) {
|
|
72
|
+
console.log(`\nNext: ${commands[0]}`);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
console.log("\nNext:");
|
|
76
|
+
for (const command of commands) {
|
|
77
|
+
console.log(` ${command}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function printWarnings(warnings) {
|
|
81
|
+
for (const warning of warnings) {
|
|
82
|
+
console.warn(`Warning: ${warning}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
async function showDiff(configDir, outDir, output) {
|
|
86
|
+
const files = [
|
|
87
|
+
[join(outDir, "flake.nix"), output["flake.nix"]],
|
|
88
|
+
...Object.entries(output.hosts).map(([name, content]) => [join(outDir, "hosts", name), content]),
|
|
89
|
+
];
|
|
90
|
+
let hasDiff = false;
|
|
91
|
+
for (const [path, newContent] of files) {
|
|
92
|
+
try {
|
|
93
|
+
const existing = await readFile(path, "utf-8");
|
|
94
|
+
if (existing !== newContent) {
|
|
95
|
+
hasDiff = true;
|
|
96
|
+
console.log(`\n--- ${relative(process.cwd(), path)}`);
|
|
97
|
+
console.log(`+++ ${relative(process.cwd(), path)} (new)`);
|
|
98
|
+
console.log("(content differs)");
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
hasDiff = true;
|
|
103
|
+
console.log(`\n+++ ${relative(process.cwd(), path)} (new file)`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
for (const rawModule of output.rawModules) {
|
|
107
|
+
const source = await resolveRawModuleSource(configDir, rawModule.path);
|
|
108
|
+
const target = rawModuleTarget(outDir, rawModule.path);
|
|
109
|
+
const sourceContent = await readFile(source);
|
|
110
|
+
try {
|
|
111
|
+
const existing = await readFile(target);
|
|
112
|
+
if (!sourceContent.equals(existing)) {
|
|
113
|
+
hasDiff = true;
|
|
114
|
+
console.log(`\n--- ${relative(process.cwd(), target)}`);
|
|
115
|
+
console.log(`+++ ${relative(process.cwd(), target)} (new)`);
|
|
116
|
+
console.log("(content differs)");
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
hasDiff = true;
|
|
121
|
+
console.log(`\n+++ ${relative(process.cwd(), target)} (new file)`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (!hasDiff) {
|
|
125
|
+
console.log("No changes. Output is up to date.");
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
async function copyRawModules(configDir, outDir, rawModules) {
|
|
129
|
+
if (rawModules.length === 0)
|
|
130
|
+
return;
|
|
131
|
+
const rawModulesDir = join(outDir, "raw-modules");
|
|
132
|
+
await rm(rawModulesDir, { recursive: true, force: true });
|
|
133
|
+
const copiedRoots = new Set();
|
|
134
|
+
for (const rawModule of rawModules) {
|
|
135
|
+
const source = await resolveRawModuleSource(configDir, rawModule.path);
|
|
136
|
+
const segments = rawModule.path.split("/");
|
|
137
|
+
if (segments.length > 1) {
|
|
138
|
+
const root = segments[0];
|
|
139
|
+
if (!copiedRoots.has(root)) {
|
|
140
|
+
const sourceRoot = join(configDir, root);
|
|
141
|
+
const realConfigDir = await realpath(configDir);
|
|
142
|
+
const realSourceRoot = await realpath(sourceRoot);
|
|
143
|
+
if (!isPathInside(realConfigDir, realSourceRoot)) {
|
|
144
|
+
throw new Error(`rawModule("${rawModule.path}") resolves outside the workspace`);
|
|
145
|
+
}
|
|
146
|
+
await cp(sourceRoot, join(rawModulesDir, root), { recursive: true, force: true });
|
|
147
|
+
copiedRoots.add(root);
|
|
148
|
+
}
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
const target = rawModuleTarget(outDir, rawModule.path);
|
|
152
|
+
await mkdir(dirname(target), { recursive: true });
|
|
153
|
+
await copyFile(source, target);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
async function resolveRawModuleSource(configDir, rawModulePath) {
|
|
157
|
+
const realConfigDir = await realpath(configDir);
|
|
158
|
+
const expectedPath = join(configDir, rawModulePath);
|
|
159
|
+
let source;
|
|
160
|
+
try {
|
|
161
|
+
source = await realpath(expectedPath);
|
|
162
|
+
}
|
|
163
|
+
catch (err) {
|
|
164
|
+
if (err.code === "ENOENT") {
|
|
165
|
+
throw new Error(`rawModule("${rawModulePath}") file not found. Expected at: ${expectedPath}`);
|
|
166
|
+
}
|
|
167
|
+
throw err;
|
|
168
|
+
}
|
|
169
|
+
if (!isPathInside(realConfigDir, source)) {
|
|
170
|
+
throw new Error(`rawModule("${rawModulePath}") resolves outside the workspace`);
|
|
171
|
+
}
|
|
172
|
+
return source;
|
|
173
|
+
}
|
|
174
|
+
function rawModuleTarget(outDir, rawModulePath) {
|
|
175
|
+
return join(outDir, "raw-modules", ...rawModulePath.split("/"));
|
|
176
|
+
}
|
|
177
|
+
function isPathInside(parent, child) {
|
|
178
|
+
const rel = relative(parent, child);
|
|
179
|
+
return rel === "" || (!rel.startsWith("..") && !isAbsolute(rel));
|
|
180
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// winix check — validate configuration
|
|
2
|
+
import { loadWorkspace } from "../loader.js";
|
|
3
|
+
import { evaluate } from "../../evaluator/index.js";
|
|
4
|
+
import { generateNix } from "../../backends/nix/index.js";
|
|
5
|
+
import { analyzeWorkspace, collectEscapeReport, detectConflicts, findDuplicateHosts, } from "../analysis.js";
|
|
6
|
+
export async function check(cwd, opts) {
|
|
7
|
+
try {
|
|
8
|
+
const { workspace } = await loadWorkspace(cwd);
|
|
9
|
+
const duplicateHosts = findDuplicateHosts(workspace);
|
|
10
|
+
const evaluated = evaluate(workspace);
|
|
11
|
+
const output = generateNix(workspace, evaluated);
|
|
12
|
+
const analyses = analyzeWorkspace(workspace);
|
|
13
|
+
const conflicts = detectConflicts(analyses);
|
|
14
|
+
console.log(`✓ Configuration valid`);
|
|
15
|
+
console.log(` Hosts: ${evaluated.map((h) => h.name).join(", ")}`);
|
|
16
|
+
for (const warning of output.warnings) {
|
|
17
|
+
console.warn(`Warning: ${warning}`);
|
|
18
|
+
}
|
|
19
|
+
if (duplicateHosts.length > 0) {
|
|
20
|
+
console.warn(`Warning: duplicate host names: ${duplicateHosts.join(", ")}`);
|
|
21
|
+
}
|
|
22
|
+
if (conflicts.length > 0) {
|
|
23
|
+
console.warn(`Warning: ${conflicts.length} possible scalar conflict(s):`);
|
|
24
|
+
for (const conflict of conflicts) {
|
|
25
|
+
console.warn(` ${conflict.host}.${conflict.scope}.${conflict.path}: ` +
|
|
26
|
+
`${conflict.firstFragment} (${conflict.firstValue}) -> ` +
|
|
27
|
+
`${conflict.secondFragment} (${conflict.secondValue})`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (opts.escapeReport) {
|
|
31
|
+
const report = collectEscapeReport(analyses);
|
|
32
|
+
console.log(`\nEscape hatch report: ${report.length} item(s)`);
|
|
33
|
+
for (const item of report) {
|
|
34
|
+
console.log(` ${item.host} ${item.fragment} ${item.kind} ${item.scope}.${item.path}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (opts.strict && (duplicateHosts.length > 0 || conflicts.length > 0)) {
|
|
38
|
+
throw new Error("Strict check failed");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
console.error(`✗ Configuration error:`);
|
|
43
|
+
console.error(` ${err.message}`);
|
|
44
|
+
process.exit(1);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
export async function init(cwd, opts) {
|
|
5
|
+
await mkdir(cwd, { recursive: true });
|
|
6
|
+
await writeIfAllowed(join(cwd, "winix.config.ts"), CONFIG, opts.force);
|
|
7
|
+
await writeIfAllowed(join(cwd, "tsconfig.json"), TSCONFIG, opts.force);
|
|
8
|
+
await writeIfAllowed(join(cwd, "package.json"), PACKAGE_JSON, opts.force);
|
|
9
|
+
await writeIfAllowed(join(cwd, ".gitignore"), GITIGNORE, opts.force);
|
|
10
|
+
console.log("✓ Initialized Winix project");
|
|
11
|
+
console.log("");
|
|
12
|
+
console.log("Next steps:");
|
|
13
|
+
console.log(" npm install");
|
|
14
|
+
}
|
|
15
|
+
async function writeIfAllowed(path, content, force) {
|
|
16
|
+
if (existsSync(path) && !force) {
|
|
17
|
+
throw new Error(`${path} already exists. Re-run with --force to overwrite.`);
|
|
18
|
+
}
|
|
19
|
+
await writeFile(path, content);
|
|
20
|
+
}
|
|
21
|
+
const CONFIG = `import { account, host, nixos, platforms, workspace } from "@adrifer/winix";
|
|
22
|
+
|
|
23
|
+
export default workspace({
|
|
24
|
+
inputs: {
|
|
25
|
+
nixpkgs: "nixos-unstable",
|
|
26
|
+
homeManager: {
|
|
27
|
+
url: "github:nix-community/home-manager",
|
|
28
|
+
follows: { nixpkgs: "nixpkgs" },
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
hosts: [
|
|
32
|
+
host("my-host", platforms.nixos({ stateVersion: "25.05" }), [
|
|
33
|
+
account.user("adrifer", () => ({ admin: true, shell: "zsh", stateVersion: "25.05" }))(),
|
|
34
|
+
nixos.packages("git", "curl"),
|
|
35
|
+
]),
|
|
36
|
+
],
|
|
37
|
+
});
|
|
38
|
+
`;
|
|
39
|
+
const PACKAGE_JSON = `{
|
|
40
|
+
"type": "module",
|
|
41
|
+
"private": true,
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@adrifer/winix": "^0.1.0"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"check": "winix check",
|
|
47
|
+
"apply": "winix apply",
|
|
48
|
+
"switch": "winix switch"
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
`;
|
|
52
|
+
const GITIGNORE = `.winix/
|
|
53
|
+
node_modules/
|
|
54
|
+
`;
|
|
55
|
+
const TSCONFIG = `{
|
|
56
|
+
"compilerOptions": {
|
|
57
|
+
"strict": true,
|
|
58
|
+
"module": "esnext",
|
|
59
|
+
"moduleResolution": "bundler",
|
|
60
|
+
"target": "esnext",
|
|
61
|
+
"noEmit": true,
|
|
62
|
+
"skipLibCheck": true
|
|
63
|
+
},
|
|
64
|
+
"include": [
|
|
65
|
+
"**/*.ts",
|
|
66
|
+
"node_modules/@adrifer/winix/types/bundled/*.d.ts"
|
|
67
|
+
]
|
|
68
|
+
}
|
|
69
|
+
`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function inspect(cwd: string): Promise<void>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { analyzeWorkspace } from "../analysis.js";
|
|
2
|
+
import { loadWorkspace } from "../loader.js";
|
|
3
|
+
export async function inspect(cwd) {
|
|
4
|
+
const { workspace, configPath } = await loadWorkspace(cwd);
|
|
5
|
+
const analyses = analyzeWorkspace(workspace);
|
|
6
|
+
console.log(`Config: ${configPath}`);
|
|
7
|
+
console.log(`Hosts: ${analyses.length}`);
|
|
8
|
+
for (const analysis of analyses) {
|
|
9
|
+
console.log(`\n${analysis.name}`);
|
|
10
|
+
console.log(` platform: ${analysis.platform}`);
|
|
11
|
+
console.log(` fragments: ${analysis.fragments.map((f) => f.label).join(" -> ")}`);
|
|
12
|
+
for (const scope of ["nixos", "homeManager", "darwin"]) {
|
|
13
|
+
const keys = new Set();
|
|
14
|
+
for (const record of analysis.fragments) {
|
|
15
|
+
const data = record.fragment[scope];
|
|
16
|
+
if (data)
|
|
17
|
+
Object.keys(data).forEach((key) => keys.add(key));
|
|
18
|
+
}
|
|
19
|
+
if (keys.size > 0) {
|
|
20
|
+
console.log(` ${scope}: ${[...keys].join(", ")}`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
interface SwitchOptions {
|
|
2
|
+
host?: string;
|
|
3
|
+
dry: boolean;
|
|
4
|
+
}
|
|
5
|
+
export declare function switchCommand(cwd: string, opts: SwitchOptions): Promise<void>;
|
|
6
|
+
export declare function selectHost(hosts: string[], requested?: string, currentHostname?: string): string;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { hostname } from "node:os";
|
|
2
|
+
import { evaluate } from "../../evaluator/index.js";
|
|
3
|
+
import { loadWorkspace } from "../loader.js";
|
|
4
|
+
import { runCommand } from "../run.js";
|
|
5
|
+
import { activationCommand, platformForEvaluatedHost } from "../activation.js";
|
|
6
|
+
import { applyWorkspace } from "./apply.js";
|
|
7
|
+
export async function switchCommand(cwd, opts) {
|
|
8
|
+
const { workspace } = await loadWorkspace(cwd);
|
|
9
|
+
const evaluated = evaluate(workspace);
|
|
10
|
+
const hostName = selectHost(evaluated.map((host) => host.name), opts.host);
|
|
11
|
+
const selected = evaluated.find((host) => host.name === hostName);
|
|
12
|
+
if (!selected)
|
|
13
|
+
throw new Error(`Host "${hostName}" not found`);
|
|
14
|
+
const platform = platformForEvaluatedHost(selected);
|
|
15
|
+
const result = await applyWorkspace(cwd, {
|
|
16
|
+
host: hostName,
|
|
17
|
+
dry: false,
|
|
18
|
+
diff: false,
|
|
19
|
+
});
|
|
20
|
+
const flake = `path:${result.outDir}#${hostName}`;
|
|
21
|
+
const command = activationCommand(platform, flake);
|
|
22
|
+
console.log(`\nRunning: ${command.join(" ")}`);
|
|
23
|
+
await runCommand(command[0], command.slice(1), { dry: opts.dry });
|
|
24
|
+
}
|
|
25
|
+
export function selectHost(hosts, requested, currentHostname = hostname()) {
|
|
26
|
+
if (requested) {
|
|
27
|
+
if (!hosts.includes(requested)) {
|
|
28
|
+
throw new Error(`Host "${requested}" not found. Available hosts:\n` +
|
|
29
|
+
hosts.map((host) => ` - ${host}`).join("\n"));
|
|
30
|
+
}
|
|
31
|
+
return requested;
|
|
32
|
+
}
|
|
33
|
+
if (hosts.length === 1)
|
|
34
|
+
return hosts[0];
|
|
35
|
+
if (hosts.includes(currentHostname))
|
|
36
|
+
return currentHostname;
|
|
37
|
+
throw new Error(`Current hostname "${currentHostname}" does not match a configured host. ` +
|
|
38
|
+
"`winix switch` needs --host. Available hosts:\n" +
|
|
39
|
+
hosts.map((host) => ` - ${host}`).join("\n"));
|
|
40
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { cp, mkdtemp, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
4
|
+
import { runCommand } from "../run.js";
|
|
5
|
+
import { applyWorkspace } from "./apply.js";
|
|
6
|
+
export async function update(cwd, opts) {
|
|
7
|
+
const result = await applyWorkspace(cwd, { dry: false, diff: false });
|
|
8
|
+
const updateDir = await mkdtemp(join(tmpdir(), "winix-update-"));
|
|
9
|
+
await cp(result.outDir, updateDir, { recursive: true });
|
|
10
|
+
const args = [
|
|
11
|
+
"--extra-experimental-features",
|
|
12
|
+
"nix-command",
|
|
13
|
+
"--extra-experimental-features",
|
|
14
|
+
"flakes",
|
|
15
|
+
"flake",
|
|
16
|
+
"update",
|
|
17
|
+
...opts.inputs,
|
|
18
|
+
];
|
|
19
|
+
try {
|
|
20
|
+
await runCommand("nix", args, { cwd: updateDir, dry: opts.dry });
|
|
21
|
+
if (opts.dry)
|
|
22
|
+
return;
|
|
23
|
+
const outLock = join(updateDir, "flake.lock");
|
|
24
|
+
const rootLock = join(result.configDir, "flake.lock");
|
|
25
|
+
const tmpLock = join(result.configDir, ".flake.lock.tmp");
|
|
26
|
+
await writeFile(tmpLock, await readFile(outLock));
|
|
27
|
+
await rename(tmpLock, rootLock);
|
|
28
|
+
console.log("✓ Updated flake.lock");
|
|
29
|
+
}
|
|
30
|
+
finally {
|
|
31
|
+
await rm(updateDir, { recursive: true, force: true });
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
#!/usr/bin/env -S node --experimental-transform-types --no-warnings
|
|
2
|
+
// Winix CLI entry point
|
|
3
|
+
import { parseArgs } from "node:util";
|
|
4
|
+
import { apply } from "./commands/apply.js";
|
|
5
|
+
import { check } from "./commands/check.js";
|
|
6
|
+
import { init } from "./commands/init.js";
|
|
7
|
+
import { inspect } from "./commands/inspect.js";
|
|
8
|
+
import { switchCommand } from "./commands/switch.js";
|
|
9
|
+
import { update } from "./commands/update.js";
|
|
10
|
+
const { positionals, values } = parseArgs({
|
|
11
|
+
allowPositionals: true,
|
|
12
|
+
options: {
|
|
13
|
+
host: { type: "string" },
|
|
14
|
+
dry: { type: "boolean", default: false },
|
|
15
|
+
diff: { type: "boolean", default: false },
|
|
16
|
+
strict: { type: "boolean", default: false },
|
|
17
|
+
force: { type: "boolean", default: false },
|
|
18
|
+
"escape-report": { type: "boolean", default: false },
|
|
19
|
+
help: { type: "boolean", short: "h", default: false },
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
const command = positionals[0];
|
|
23
|
+
if (values.help || !command) {
|
|
24
|
+
console.log(`
|
|
25
|
+
winix - TypeScript-first system configuration
|
|
26
|
+
|
|
27
|
+
Commands:
|
|
28
|
+
init Scaffold a Winix project
|
|
29
|
+
apply Generate Nix output in .winix/out/
|
|
30
|
+
check Validate configuration
|
|
31
|
+
switch Generate output and run nixos-rebuild/darwin-rebuild
|
|
32
|
+
update Update generated flake.lock and copy it to project root
|
|
33
|
+
inspect Print host composition and fragment graph
|
|
34
|
+
|
|
35
|
+
Options:
|
|
36
|
+
--host <name> Target a specific host
|
|
37
|
+
--dry Show what would be generated (apply)
|
|
38
|
+
--diff Show diff against current output (apply)
|
|
39
|
+
--strict Treat conflicts as errors (check)
|
|
40
|
+
--escape-report Show escape hatch usage (check)
|
|
41
|
+
--force Overwrite files (init)
|
|
42
|
+
-h, --help Show this help
|
|
43
|
+
|
|
44
|
+
Examples:
|
|
45
|
+
winix init
|
|
46
|
+
winix apply
|
|
47
|
+
winix apply --dry
|
|
48
|
+
winix apply --host wsl-work
|
|
49
|
+
winix check --strict
|
|
50
|
+
winix check --escape-report
|
|
51
|
+
winix switch --host wsl-work
|
|
52
|
+
winix update
|
|
53
|
+
`);
|
|
54
|
+
process.exit(0);
|
|
55
|
+
}
|
|
56
|
+
const cwd = process.cwd();
|
|
57
|
+
switch (command) {
|
|
58
|
+
case "init":
|
|
59
|
+
try {
|
|
60
|
+
await init(cwd, { force: values.force });
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
console.error(`\u2717 Error: ${err.message}`);
|
|
64
|
+
process.exit(1);
|
|
65
|
+
}
|
|
66
|
+
break;
|
|
67
|
+
case "apply":
|
|
68
|
+
try {
|
|
69
|
+
await apply(cwd, {
|
|
70
|
+
host: values.host,
|
|
71
|
+
dry: values.dry,
|
|
72
|
+
diff: values.diff,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
console.error(`\u2717 Error: ${err.message}`);
|
|
77
|
+
process.exit(1);
|
|
78
|
+
}
|
|
79
|
+
break;
|
|
80
|
+
case "check":
|
|
81
|
+
await check(cwd, {
|
|
82
|
+
strict: values.strict,
|
|
83
|
+
escapeReport: values["escape-report"],
|
|
84
|
+
});
|
|
85
|
+
break;
|
|
86
|
+
case "switch":
|
|
87
|
+
try {
|
|
88
|
+
await switchCommand(cwd, {
|
|
89
|
+
host: values.host,
|
|
90
|
+
dry: values.dry,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
catch (err) {
|
|
94
|
+
console.error(`\u2717 Error: ${err.message}`);
|
|
95
|
+
process.exit(1);
|
|
96
|
+
}
|
|
97
|
+
break;
|
|
98
|
+
case "update":
|
|
99
|
+
try {
|
|
100
|
+
await update(cwd, {
|
|
101
|
+
dry: values.dry,
|
|
102
|
+
inputs: positionals.slice(1),
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
catch (err) {
|
|
106
|
+
console.error(`\u2717 Error: ${err.message}`);
|
|
107
|
+
process.exit(1);
|
|
108
|
+
}
|
|
109
|
+
break;
|
|
110
|
+
case "inspect":
|
|
111
|
+
try {
|
|
112
|
+
await inspect(cwd);
|
|
113
|
+
}
|
|
114
|
+
catch (err) {
|
|
115
|
+
console.error(`\u2717 Error: ${err.message}`);
|
|
116
|
+
process.exit(1);
|
|
117
|
+
}
|
|
118
|
+
break;
|
|
119
|
+
default:
|
|
120
|
+
console.error(`Unknown command: ${command}`);
|
|
121
|
+
process.exit(2);
|
|
122
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { WorkspaceDef } from "../core/types.js";
|
|
2
|
+
export interface LoadResult {
|
|
3
|
+
workspace: WorkspaceDef;
|
|
4
|
+
configDir: string;
|
|
5
|
+
configPath: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Find and load the workspace config file.
|
|
9
|
+
* Searches for winix.config.ts in cwd and parent dirs.
|
|
10
|
+
*/
|
|
11
|
+
export declare function loadWorkspace(cwd: string): Promise<LoadResult>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// Loader: finds and imports the user's winix.config.ts
|
|
2
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
3
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
4
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
5
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
return path;
|
|
9
|
+
};
|
|
10
|
+
import { resolve, dirname } from "node:path";
|
|
11
|
+
import { existsSync } from "node:fs";
|
|
12
|
+
import { pathToFileURL } from "node:url";
|
|
13
|
+
const CONFIG_NAMES = ["winix.config.ts", "winix.config.js", "winix.config.mjs"];
|
|
14
|
+
/**
|
|
15
|
+
* Find and load the workspace config file.
|
|
16
|
+
* Searches for winix.config.ts in cwd and parent dirs.
|
|
17
|
+
*/
|
|
18
|
+
export async function loadWorkspace(cwd) {
|
|
19
|
+
const configPath = findConfig(cwd);
|
|
20
|
+
if (!configPath) {
|
|
21
|
+
throw new Error(`Could not find winix.config.ts in ${cwd} or parent directories.\n` +
|
|
22
|
+
`Run \`winix init\` to create one.`);
|
|
23
|
+
}
|
|
24
|
+
const configDir = dirname(configPath);
|
|
25
|
+
// Import the config file (requires Node --experimental-strip-types or pre-compiled)
|
|
26
|
+
const fileUrl = pathToFileURL(configPath).href;
|
|
27
|
+
const mod = await import(__rewriteRelativeImportExtension(fileUrl));
|
|
28
|
+
const workspace = mod.default;
|
|
29
|
+
if (!workspace || !workspace.hosts) {
|
|
30
|
+
throw new Error(`${configPath} must export a default workspace() value.`);
|
|
31
|
+
}
|
|
32
|
+
return { workspace, configDir, configPath };
|
|
33
|
+
}
|
|
34
|
+
function findConfig(dir) {
|
|
35
|
+
let current = resolve(dir);
|
|
36
|
+
for (let i = 0; i < 10; i++) {
|
|
37
|
+
for (const name of CONFIG_NAMES) {
|
|
38
|
+
const candidate = resolve(current, name);
|
|
39
|
+
if (existsSync(candidate)) {
|
|
40
|
+
return candidate;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const parent = dirname(current);
|
|
44
|
+
if (parent === current)
|
|
45
|
+
break;
|
|
46
|
+
current = parent;
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
}
|
package/dist/cli/run.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
export async function runCommand(command, args, opts = {}) {
|
|
3
|
+
const printable = [command, ...args].join(" ");
|
|
4
|
+
if (opts.dry) {
|
|
5
|
+
console.log(printable);
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
await new Promise((resolve, reject) => {
|
|
9
|
+
const child = spawn(command, args, {
|
|
10
|
+
cwd: opts.cwd,
|
|
11
|
+
stdio: "inherit",
|
|
12
|
+
});
|
|
13
|
+
child.on("error", reject);
|
|
14
|
+
child.on("exit", (code, signal) => {
|
|
15
|
+
if (code === 0) {
|
|
16
|
+
resolve();
|
|
17
|
+
}
|
|
18
|
+
else if (signal) {
|
|
19
|
+
reject(new Error(`${command} exited from signal ${signal}`));
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
reject(new Error(`${command} exited with code ${code}`));
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface RawOptionEntry {
|
|
2
|
+
type?: string;
|
|
3
|
+
loc?: unknown;
|
|
4
|
+
visible?: boolean | string;
|
|
5
|
+
internal?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export interface ParsedOption {
|
|
8
|
+
path: string[];
|
|
9
|
+
tsType: string;
|
|
10
|
+
nixType: string;
|
|
11
|
+
}
|
|
12
|
+
export type OptionsJson = Record<string, RawOptionEntry>;
|
|
13
|
+
export declare function parseOptions(options: OptionsJson): ParsedOption[];
|
|
14
|
+
export declare function nixTypeToTs(type: string): string;
|