@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.
Files changed (76) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +291 -0
  3. package/dist/backends/nix/index.d.ts +18 -0
  4. package/dist/backends/nix/index.js +442 -0
  5. package/dist/cli/activation.d.ts +6 -0
  6. package/dist/cli/activation.js +19 -0
  7. package/dist/cli/analysis.d.ts +30 -0
  8. package/dist/cli/analysis.js +170 -0
  9. package/dist/cli/commands/apply.d.ts +14 -0
  10. package/dist/cli/commands/apply.js +180 -0
  11. package/dist/cli/commands/check.d.ts +6 -0
  12. package/dist/cli/commands/check.js +46 -0
  13. package/dist/cli/commands/init.d.ts +5 -0
  14. package/dist/cli/commands/init.js +69 -0
  15. package/dist/cli/commands/inspect.d.ts +1 -0
  16. package/dist/cli/commands/inspect.js +24 -0
  17. package/dist/cli/commands/switch.d.ts +7 -0
  18. package/dist/cli/commands/switch.js +40 -0
  19. package/dist/cli/commands/update.d.ts +6 -0
  20. package/dist/cli/commands/update.js +33 -0
  21. package/dist/cli/index.d.ts +2 -0
  22. package/dist/cli/index.js +122 -0
  23. package/dist/cli/loader.d.ts +11 -0
  24. package/dist/cli/loader.js +49 -0
  25. package/dist/cli/run.d.ts +4 -0
  26. package/dist/cli/run.js +26 -0
  27. package/dist/cli/types-gen/parser.d.ts +14 -0
  28. package/dist/cli/types-gen/parser.js +275 -0
  29. package/dist/core/index.d.ts +1 -0
  30. package/dist/core/index.js +1 -0
  31. package/dist/core/types.d.ts +106 -0
  32. package/dist/core/types.js +2 -0
  33. package/dist/evaluator/index.d.ts +20 -0
  34. package/dist/evaluator/index.js +184 -0
  35. package/dist/helpers/account.d.ts +46 -0
  36. package/dist/helpers/account.js +165 -0
  37. package/dist/helpers/darwin.d.ts +36 -0
  38. package/dist/helpers/darwin.js +38 -0
  39. package/dist/helpers/home.d.ts +40 -0
  40. package/dist/helpers/home.js +63 -0
  41. package/dist/helpers/index.d.ts +14 -0
  42. package/dist/helpers/index.js +7 -0
  43. package/dist/helpers/nix.d.ts +36 -0
  44. package/dist/helpers/nix.js +157 -0
  45. package/dist/helpers/nixos.d.ts +53 -0
  46. package/dist/helpers/nixos.js +86 -0
  47. package/dist/helpers/options.d.ts +10 -0
  48. package/dist/helpers/options.js +1 -0
  49. package/dist/helpers/overlay.d.ts +13 -0
  50. package/dist/helpers/overlay.js +29 -0
  51. package/dist/helpers/platforms.d.ts +21 -0
  52. package/dist/helpers/platforms.js +62 -0
  53. package/dist/helpers/utils.d.ts +6 -0
  54. package/dist/helpers/utils.js +8 -0
  55. package/dist/index.d.ts +9 -0
  56. package/dist/index.js +5 -0
  57. package/dist/sdk/index.d.ts +26 -0
  58. package/dist/sdk/index.js +183 -0
  59. package/dist/types/common.d.ts +10 -0
  60. package/dist/types/common.js +1 -0
  61. package/dist/types/darwin.d.ts +86 -0
  62. package/dist/types/darwin.js +1 -0
  63. package/dist/types/home.d.ts +45 -0
  64. package/dist/types/home.js +1 -0
  65. package/dist/types/index.d.ts +5 -0
  66. package/dist/types/index.js +5 -0
  67. package/dist/types/nixos.d.ts +225 -0
  68. package/dist/types/nixos.js +1 -0
  69. package/dist/types/programs.d.ts +67 -0
  70. package/dist/types/programs.js +1 -0
  71. package/index.d.ts +5 -0
  72. package/package.json +69 -0
  73. package/types/bundled/darwin.d.ts +476 -0
  74. package/types/bundled/home-manager.d.ts +4674 -0
  75. package/types/bundled/nixos.d.ts +19991 -0
  76. package/types/index.d.ts +8 -0
@@ -0,0 +1,275 @@
1
+ export function parseOptions(options) {
2
+ return Object.entries(options)
3
+ .flatMap(([name, option]) => {
4
+ if (option.internal === true || option.visible === false || option.visible === "false") {
5
+ return [];
6
+ }
7
+ const path = getOptionPath(name, option);
8
+ if (path.length === 0)
9
+ return [];
10
+ const nixType = option.type ?? "unknown";
11
+ return [{ path, tsType: nixTypeToTs(nixType), nixType }];
12
+ })
13
+ .sort((a, b) => a.path.join(".").localeCompare(b.path.join(".")));
14
+ }
15
+ export function nixTypeToTs(type) {
16
+ const normalized = normalizeType(type);
17
+ // Null wrapper: "null or X"
18
+ const nullInner = matchWrapper(normalized, "null or ");
19
+ if (nullInner)
20
+ return `${nixTypeToTs(nullInner)} | null`;
21
+ // List wrapper: "list of X"
22
+ const listInner = matchWrapper(normalized, "list of ");
23
+ if (listInner) {
24
+ const innerType = nixTypeToTs(listInner);
25
+ const listType = `${parenthesizeUnion(innerType)}[]`;
26
+ return innerType === "PackageRef" ? `${listType} | NixExpr` : listType;
27
+ }
28
+ // Non-empty list: "non-empty (list of X)"
29
+ const nonEmptyList = /^non-empty \(list of (.+)\)$/.exec(normalized);
30
+ if (nonEmptyList) {
31
+ const innerType = nixTypeToTs(nonEmptyList[1]);
32
+ return `${parenthesizeUnion(innerType)}[]`;
33
+ }
34
+ // Attribute set: "attribute set of X" / "lazy attribute set of X"
35
+ const attrsInner = matchAttrSet(normalized);
36
+ if (attrsInner) {
37
+ if (attrsInner.includes("submodule"))
38
+ return "Record<string, unknown>";
39
+ return `Record<string, ${nixTypeToTs(attrsInner)}>`;
40
+ }
41
+ // Enum: "one of ..."
42
+ const enumType = parseEnum(normalized);
43
+ if (enumType)
44
+ return enumType;
45
+ // Singular enum: 'value "X" (singular enum)'
46
+ const singularEnum = /^value "([^"]+)" \(singular enum\)$/.exec(normalized);
47
+ if (singularEnum)
48
+ return JSON.stringify(singularEnum[1]);
49
+ // "X or Y" unions (must come after null/list/attrs which also use "or")
50
+ const orUnion = parseOrUnion(normalized);
51
+ if (orUnion)
52
+ return orUnion;
53
+ // "X convertible to it" — strip the suffix and parse the base type
54
+ const convertible = /^(.+?)\s+convertible to it$/.exec(normalized);
55
+ if (convertible)
56
+ return nixTypeToTs(convertible[1]);
57
+ // Boolean
58
+ if (normalized === "boolean")
59
+ return "boolean";
60
+ // Integer variants
61
+ if (normalized === "signed integer" ||
62
+ normalized === "unsigned integer" ||
63
+ /^(positive|nonnegative|nonzero)\s+(signed |unsigned )?integer/.test(normalized) ||
64
+ /^\d+ bit unsigned integer/.test(normalized) ||
65
+ /^integer of at least/.test(normalized) ||
66
+ normalized.includes("integer between") ||
67
+ /^unsigned integer, meaning/.test(normalized) ||
68
+ /^positive integer, meaning/.test(normalized) ||
69
+ /^nonzero signed integer/.test(normalized)) {
70
+ return "number";
71
+ }
72
+ // Float variants
73
+ if (normalized === "floating point number" ||
74
+ /^(positive |nonnegative )?floating point number/.test(normalized) ||
75
+ /^(positive |nonnegative )?integer or floating point number/.test(normalized) ||
76
+ normalized === "signed integer or floating point number" ||
77
+ /^integer or floating point number between/.test(normalized) ||
78
+ /^floating point number or/.test(normalized)) {
79
+ return "number";
80
+ }
81
+ // String variants
82
+ if (normalized === "string" ||
83
+ /^strings concatenated with/.test(normalized) ||
84
+ normalized === "absolute path" ||
85
+ normalized === "path" ||
86
+ /^string matching the pattern/.test(normalized) ||
87
+ /^string \(with check:/.test(normalized) ||
88
+ /^string containing/.test(normalized) ||
89
+ /^string of the form/.test(normalized) ||
90
+ /^string starting with/.test(normalized) ||
91
+ /^single-line string/.test(normalized) ||
92
+ /^\(optionally newline-terminated\) single-line string$/.test(normalized) ||
93
+ normalized === "non-empty string" ||
94
+ /^non-empty string/.test(normalized) ||
95
+ /^string, not containing/.test(normalized) ||
96
+ /^printable string/.test(normalized) ||
97
+ /^absolute path,/.test(normalized) ||
98
+ /^absolute path not in/.test(normalized) ||
99
+ normalized === "file mode string" ||
100
+ normalized === "session name" ||
101
+ /^Go duration/.test(normalized) ||
102
+ normalized === "IPv4 or IPv6 address" ||
103
+ normalized === "Network prefix in CIDR notation" ||
104
+ /^key ID prefixed with/.test(normalized) ||
105
+ /^tmpfiles\.d/.test(normalized)) {
106
+ return "string";
107
+ }
108
+ // Package variants
109
+ if (normalized === "package")
110
+ return "PackageRef";
111
+ if (/^package or absolute path$/.test(normalized))
112
+ return "PackageRef";
113
+ // Serialized value types (JSON, YAML, TOML, etc.) → Record is more useful than unknown
114
+ if (normalized === "JSON value" ||
115
+ normalized === "YAML 1.1 value" ||
116
+ normalized === "TOML value" ||
117
+ normalized === "HOCON value" ||
118
+ normalized === "raw value" ||
119
+ normalized === "Elixir value" ||
120
+ normalized === "nixpkgs config" ||
121
+ /config type/.test(normalized) ||
122
+ /^section of an INI/.test(normalized) ||
123
+ normalized === "systemd option" ||
124
+ /^value coercible to/.test(normalized) ||
125
+ /config stanza/.test(normalized) ||
126
+ /records value/.test(normalized) ||
127
+ /^libconfig configuration/.test(normalized) ||
128
+ /^`.*` configuration type/.test(normalized)) {
129
+ return "Record<string, unknown>";
130
+ }
131
+ // Submodule
132
+ if (normalized.includes("submodule"))
133
+ return "Record<string, unknown>";
134
+ // Attribute set (bare)
135
+ if (normalized === "attribute set")
136
+ return "Record<string, unknown>";
137
+ // "anything" / "unspecified value"
138
+ if (normalized === "anything")
139
+ return "unknown";
140
+ if (normalized === "unspecified value")
141
+ return "unknown";
142
+ // Catch-all patterns that are "complex" but essentially record-like
143
+ if (/^Toplevel NixOS config$/.test(normalized))
144
+ return "Record<string, unknown>";
145
+ if (/^An evaluation of Nixpkgs/.test(normalized))
146
+ return "unknown";
147
+ if (/^attribute-tagged union/.test(normalized))
148
+ return "Record<string, unknown>";
149
+ if (/^(function that evaluates to|Json value or lambda)/.test(normalized))
150
+ return "unknown";
151
+ // Pair types → tuples (approximate as arrays)
152
+ if (/^pair of/.test(normalized))
153
+ return "number[]";
154
+ // Secret value → string (it's always a path or string reference)
155
+ if (normalized === "secret value")
156
+ return "string";
157
+ // "one of the available ..." → string (dynamic enum)
158
+ if (/^one of the available/.test(normalized))
159
+ return "string";
160
+ // "boolean or null or X" patterns
161
+ if (/^boolean or null/.test(normalized))
162
+ return "boolean | null | string";
163
+ // Fallback
164
+ return "unknown";
165
+ }
166
+ /**
167
+ * Parse "X or Y" style unions that aren't handled by the specific prefix matchers.
168
+ * Returns null if this doesn't look like a parseable union.
169
+ */
170
+ function parseOrUnion(normalized) {
171
+ // Common patterns: "string or boolean", "absolute path or string", "boolean or string"
172
+ // "string or list of string", "string or signed integer", "package or absolute path"
173
+ // "string or (attribute set)", "(list of string) or string"
174
+ // Must exclude things like "one of ..." and "attribute set of ..."
175
+ // Skip if it starts with known prefixes that use "or" differently
176
+ if (/^(null or |list of |non-empty |attribute set|lazy attribute|one of )/.test(normalized)) {
177
+ return null;
178
+ }
179
+ // Handle paren-wrapped types that start with "(" — these are unions like
180
+ // "(list of string) or string", "(attribute set of X) or Y"
181
+ if (normalized.startsWith("(")) {
182
+ const parts = splitOr(normalized);
183
+ if (parts.length >= 2) {
184
+ const tsTypes = parts.map((p) => nixTypeToTs(p.trim()));
185
+ if (!tsTypes.some((t) => t === "unknown")) {
186
+ return [...new Set(tsTypes)].join(" | ");
187
+ }
188
+ }
189
+ return null;
190
+ }
191
+ // Try to split on " or " but be careful of nested parens
192
+ const parts = splitOr(normalized);
193
+ if (parts.length < 2)
194
+ return null;
195
+ // Parse each part
196
+ const tsTypes = parts.map((p) => nixTypeToTs(p.trim()));
197
+ // If any part resolved to "unknown", don't emit a broken union
198
+ if (tsTypes.some((t) => t === "unknown"))
199
+ return null;
200
+ const deduped = [...new Set(tsTypes)];
201
+ return deduped.join(" | ");
202
+ }
203
+ /**
204
+ * Split on top-level " or " (not inside parentheses).
205
+ */
206
+ function splitOr(type) {
207
+ const parts = [];
208
+ let depth = 0;
209
+ let current = "";
210
+ const words = type.split(" ");
211
+ for (let i = 0; i < words.length; i++) {
212
+ const word = words[i];
213
+ if (word === "or" && depth === 0 && current.trim()) {
214
+ parts.push(current.trim());
215
+ current = "";
216
+ continue;
217
+ }
218
+ for (const ch of word) {
219
+ if (ch === "(")
220
+ depth++;
221
+ else if (ch === ")")
222
+ depth--;
223
+ }
224
+ current += (current ? " " : "") + word;
225
+ }
226
+ if (current.trim())
227
+ parts.push(current.trim());
228
+ return parts.length >= 2 ? parts : [];
229
+ }
230
+ function getOptionPath(name, option) {
231
+ if (Array.isArray(option.loc) && option.loc.every((segment) => typeof segment === "string")) {
232
+ return option.loc;
233
+ }
234
+ return name.split(".").filter(Boolean);
235
+ }
236
+ function normalizeType(type) {
237
+ return type.replace(/\s+/g, " ").trim();
238
+ }
239
+ function matchWrapper(type, prefix) {
240
+ if (!type.startsWith(prefix))
241
+ return null;
242
+ return stripOuterParens(type.slice(prefix.length).trim());
243
+ }
244
+ function matchAttrSet(type) {
245
+ const prefixes = ["attribute set of ", "attribute sets of ", "lazy attribute set of "];
246
+ for (const prefix of prefixes) {
247
+ if (type.startsWith(prefix)) {
248
+ return stripOuterParens(type.slice(prefix.length).trim());
249
+ }
250
+ }
251
+ return null;
252
+ }
253
+ function stripOuterParens(value) {
254
+ if (value.startsWith("(") && value.endsWith(")")) {
255
+ return value.slice(1, -1).trim();
256
+ }
257
+ return value;
258
+ }
259
+ function parenthesizeUnion(type) {
260
+ return type.includes(" | ") ? `(${type})` : type;
261
+ }
262
+ function parseEnum(type) {
263
+ const match = /^one of (.+)$/.exec(type);
264
+ if (!match)
265
+ return null;
266
+ // Try quoted strings first
267
+ const quotedValues = [...match[1].matchAll(/"((?:\\.|[^"\\])*)"/g)].map((value) => JSON.stringify(value[1].replace(/\\"/g, "\"")));
268
+ if (quotedValues.length > 0)
269
+ return [...new Set(quotedValues)].sort().join(" | ");
270
+ // Try numeric values: "one of 0, 1, 2"
271
+ const numericValues = [...match[1].matchAll(/\b(\d+)\b/g)].map((m) => m[1]);
272
+ if (numericValues.length > 0)
273
+ return "number";
274
+ return null;
275
+ }
@@ -0,0 +1 @@
1
+ export * from "./types.js";
@@ -0,0 +1 @@
1
+ export * from "./types.js";
@@ -0,0 +1,106 @@
1
+ import type { DarwinOptions, HomeOptions, NixosOptions } from "../types/index.js";
2
+ /**
3
+ * The output shape of any fragment. Keys correspond to target scopes.
4
+ * Values are deep-merged by the evaluator according to merge rules.
5
+ */
6
+ export interface Fragment {
7
+ nixos?: NixosOptions;
8
+ homeManager?: HomeOptions;
9
+ darwin?: DarwinOptions;
10
+ /** Internal: platform/feature ID for .isActive resolution */
11
+ __id?: string;
12
+ /** Internal: marks this as a platform fragment */
13
+ __platform?: boolean;
14
+ }
15
+ /**
16
+ * Internal reference to a workspace-relative raw Nix module file.
17
+ */
18
+ export interface RawModuleRef {
19
+ __winixRawModule: true;
20
+ path: string;
21
+ }
22
+ /**
23
+ * Internal reference to a verbatim Nix expression.
24
+ */
25
+ export interface NixExpr {
26
+ __winixNixExpr: true;
27
+ expr: string;
28
+ }
29
+ export type ImportRef = string | RawModuleRef;
30
+ /**
31
+ * What goes into a host/profile fragment list. Arrays are recursive so profiles
32
+ * can be nested without spread boilerplate.
33
+ */
34
+ export type FragmentEntry = LazyFragment | Fragment | readonly FragmentEntry[];
35
+ /**
36
+ * What a lazy fragment can resolve into.
37
+ */
38
+ export type FragmentResult = FragmentEntry;
39
+ /**
40
+ * A lazy fragment descriptor: holds the factory + args for deferred evaluation.
41
+ */
42
+ export interface LazyFragment {
43
+ __lazy: true;
44
+ __id: string;
45
+ __platform?: boolean;
46
+ __resolve: () => FragmentResult;
47
+ }
48
+ /**
49
+ * A platform lazy fragment: branded type to enforce exactly-one-platform in host().
50
+ */
51
+ export interface PlatformLazyFragment extends LazyFragment {
52
+ __platform: true;
53
+ }
54
+ /**
55
+ * A fragment factory: callable to produce a LazyFragment, with .isActive getter.
56
+ */
57
+ export interface FragmentFactory<T extends unknown[] = []> {
58
+ (...args: T): LazyFragment;
59
+ readonly isActive: boolean;
60
+ readonly id: string;
61
+ }
62
+ /**
63
+ * A profile factory: a semantic alias for composite fragments.
64
+ */
65
+ export interface ProfileFactory<T extends unknown[] = []> extends FragmentFactory<T> {
66
+ }
67
+ /**
68
+ * A platform factory: callable to produce a PlatformLazyFragment, with .isActive getter.
69
+ */
70
+ export interface PlatformFactory<T extends unknown[] = []> {
71
+ (...args: T): PlatformLazyFragment;
72
+ readonly isActive: boolean;
73
+ readonly id: string;
74
+ }
75
+ /**
76
+ * Input definition: either a URL string or an input() with options.
77
+ */
78
+ export type InputDef = string | InputWithOptions;
79
+ export interface InputWithOptions {
80
+ url: string;
81
+ follows?: Record<string, string>;
82
+ nixName?: string;
83
+ }
84
+ /**
85
+ * Workspace definition: the top-level configuration.
86
+ */
87
+ export interface WorkspaceDef {
88
+ inputs: Record<string, InputDef>;
89
+ hosts: HostDef[];
90
+ }
91
+ /**
92
+ * Host definition: a named target with a platform and its fragment list.
93
+ */
94
+ export interface HostDef {
95
+ name: string;
96
+ platform: PlatformLazyFragment;
97
+ fragments: readonly FragmentEntry[];
98
+ }
99
+ /**
100
+ * Evaluation context: set by the evaluator before running fragments.
101
+ */
102
+ export interface EvalContext {
103
+ platform: string;
104
+ hostname: string;
105
+ activeIds: Set<string>;
106
+ }
@@ -0,0 +1,2 @@
1
+ // Core types for Winix fragments and composition
2
+ export {};
@@ -0,0 +1,20 @@
1
+ import type { HostDef, WorkspaceDef } from "../core/types.js";
2
+ /**
3
+ * Evaluated host: the merged result of all fragments for one host.
4
+ */
5
+ export interface EvaluatedHost {
6
+ name: string;
7
+ nixos: Record<string, unknown>;
8
+ homeManager: Record<string, unknown>;
9
+ darwin: Record<string, unknown>;
10
+ }
11
+ /**
12
+ * Evaluate a workspace: process each host's fragments and merge them.
13
+ */
14
+ export declare function evaluate(workspace: WorkspaceDef): EvaluatedHost[];
15
+ export declare function collectActiveIds(host: HostDef): Set<string>;
16
+ /**
17
+ * Deep merge two objects. Arrays are appended + deduped.
18
+ * Scalars: last wins.
19
+ */
20
+ export declare function deepMerge(target: Record<string, unknown>, source: Record<string, unknown>): Record<string, unknown>;
@@ -0,0 +1,184 @@
1
+ // Evaluator: takes a workspace config, evaluates fragments per host, produces merged IR
2
+ import { setEvalContext, restoreEvalContext } from "../sdk/index.js";
3
+ /**
4
+ * Evaluate a workspace: process each host's fragments and merge them.
5
+ */
6
+ export function evaluate(workspace) {
7
+ return workspace.hosts.map((host) => evaluateHost(host));
8
+ }
9
+ function evaluateHost(host) {
10
+ // The platform is always host.platform (exactly one, guaranteed by type system)
11
+ const platformEntry = host.platform;
12
+ const allEntries = [platformEntry, ...host.fragments];
13
+ const platformId = platformEntry.__id;
14
+ const activeIds = collectActiveIds(host);
15
+ // Pass 2: set context → resolve lazy fragments (now .isActive works)
16
+ const ctx = {
17
+ platform: platformId,
18
+ hostname: host.name,
19
+ activeIds,
20
+ };
21
+ const prevCtx = setEvalContext(ctx);
22
+ const resolvedFragments = [];
23
+ try {
24
+ for (const entry of allEntries) {
25
+ resolveEntry(entry, resolvedFragments);
26
+ }
27
+ }
28
+ finally {
29
+ restoreEvalContext(prevCtx);
30
+ }
31
+ // Pass 3: merge all fragments
32
+ const result = {
33
+ name: host.name,
34
+ nixos: {},
35
+ homeManager: {},
36
+ darwin: {},
37
+ };
38
+ for (const fragment of resolvedFragments) {
39
+ if (fragment.nixos && shouldMergeScope(resolvedFragments, "nixos")) {
40
+ result.nixos = deepMerge(result.nixos, fragment.nixos);
41
+ }
42
+ if (fragment.homeManager) {
43
+ result.homeManager = deepMerge(result.homeManager, fragment.homeManager);
44
+ }
45
+ if (fragment.darwin && shouldMergeScope(resolvedFragments, "darwin")) {
46
+ result.darwin = deepMerge(result.darwin, fragment.darwin);
47
+ }
48
+ }
49
+ return result;
50
+ }
51
+ function shouldMergeScope(fragments, scope) {
52
+ const platformFragments = fragments.filter((fragment) => fragment.__platform);
53
+ if (platformFragments.length === 0)
54
+ return true;
55
+ return platformFragments.some((fragment) => Boolean(fragment[scope]));
56
+ }
57
+ function isLazy(entry) {
58
+ return typeof entry === "object" && entry !== null && "__lazy" in entry && entry.__lazy === true;
59
+ }
60
+ export function collectActiveIds(host) {
61
+ const activeIds = new Set([host.platform.__id]);
62
+ let previousSize = -1;
63
+ let iterations = 0;
64
+ while (activeIds.size !== previousSize) {
65
+ previousSize = activeIds.size;
66
+ iterations += 1;
67
+ if (iterations > 100) {
68
+ throw new Error(`Could not stabilize active fragment IDs for host "${host.name}". ` +
69
+ "Check for fragments that generate new fragment IDs dynamically.");
70
+ }
71
+ const ctx = {
72
+ platform: host.platform.__id,
73
+ hostname: host.name,
74
+ activeIds,
75
+ };
76
+ const prevCtx = setEvalContext(ctx);
77
+ try {
78
+ collectIds(host.platform, activeIds);
79
+ for (const entry of host.fragments) {
80
+ collectIds(entry, activeIds);
81
+ }
82
+ }
83
+ finally {
84
+ restoreEvalContext(prevCtx);
85
+ }
86
+ }
87
+ return activeIds;
88
+ }
89
+ function collectIds(entry, activeIds) {
90
+ if (isLazy(entry)) {
91
+ activeIds.add(entry.__id);
92
+ collectIds(entry.__resolve(), activeIds);
93
+ }
94
+ else if (Array.isArray(entry)) {
95
+ for (const item of entry) {
96
+ collectIds(item, activeIds);
97
+ }
98
+ }
99
+ else if (typeof entry === "object" && entry !== null) {
100
+ const fragment = entry;
101
+ if (fragment.__id) {
102
+ activeIds.add(fragment.__id);
103
+ }
104
+ }
105
+ }
106
+ /**
107
+ * Recursively resolve a fragment entry. Handles:
108
+ * - LazyFragment → resolve, then recurse (may return Fragment[] with more lazies)
109
+ * - Fragment[] → recurse each
110
+ * - Fragment → push directly
111
+ */
112
+ function resolveEntry(entry, out) {
113
+ if (isLazy(entry)) {
114
+ const result = entry.__resolve();
115
+ if (Array.isArray(result)) {
116
+ for (const item of result) {
117
+ resolveEntry(item, out);
118
+ }
119
+ }
120
+ else {
121
+ resolveEntry(result, out);
122
+ }
123
+ }
124
+ else if (Array.isArray(entry)) {
125
+ for (const item of entry) {
126
+ resolveEntry(item, out);
127
+ }
128
+ }
129
+ else if (typeof entry === "object" && entry !== null) {
130
+ out.push(entry);
131
+ }
132
+ }
133
+ /**
134
+ * Deep merge two objects. Arrays are appended + deduped.
135
+ * Scalars: last wins.
136
+ */
137
+ export function deepMerge(target, source) {
138
+ const result = { ...target };
139
+ for (const key of Object.keys(source)) {
140
+ const targetVal = target[key];
141
+ const sourceVal = source[key];
142
+ if (sourceVal === undefined) {
143
+ continue;
144
+ }
145
+ if (Array.isArray(sourceVal)) {
146
+ if (Array.isArray(targetVal)) {
147
+ // Append + dedupe
148
+ const merged = [...targetVal, ...sourceVal];
149
+ result[key] = [...new Set(merged)];
150
+ }
151
+ else {
152
+ result[key] = sourceVal;
153
+ }
154
+ }
155
+ else if (isPlainObject(sourceVal) && isPlainObject(targetVal)) {
156
+ // Deep merge objects
157
+ result[key] = deepMerge(targetVal, sourceVal);
158
+ }
159
+ else {
160
+ // Scalar: last wins
161
+ result[key] = sourceVal;
162
+ }
163
+ }
164
+ return result;
165
+ }
166
+ function isPlainObject(val) {
167
+ return (typeof val === "object" &&
168
+ val !== null &&
169
+ !Array.isArray(val) &&
170
+ !isNixExpr(val) &&
171
+ !isRawModuleRef(val));
172
+ }
173
+ function isNixExpr(val) {
174
+ return (typeof val === "object" &&
175
+ val !== null &&
176
+ val.__winixNixExpr === true &&
177
+ typeof val.expr === "string");
178
+ }
179
+ function isRawModuleRef(val) {
180
+ return (typeof val === "object" &&
181
+ val !== null &&
182
+ val.__winixRawModule === true &&
183
+ typeof val.path === "string");
184
+ }
@@ -0,0 +1,46 @@
1
+ import type { Fragment, FragmentFactory } from "../core/types.js";
2
+ import type { PackageRef } from "../types/index.js";
3
+ export interface AccountOpts {
4
+ admin?: boolean;
5
+ shell?: string | PackageRef;
6
+ description?: string;
7
+ homeDirectory?: string;
8
+ home?: string;
9
+ stateVersion?: string;
10
+ sessionVariables?: Record<string, string>;
11
+ groups?: string[];
12
+ extraGroups?: string[];
13
+ uid?: number;
14
+ gid?: number;
15
+ group?: string;
16
+ isNormalUser?: boolean;
17
+ isSystemUser?: boolean;
18
+ packages?: PackageRef[];
19
+ openssh?: {
20
+ authorizedKeys?: string[];
21
+ authorizedKeyFiles?: string[];
22
+ };
23
+ hashedPasswordFile?: string;
24
+ homeManager?: Fragment;
25
+ wslDefault?: boolean;
26
+ }
27
+ export interface AccountUserRef {
28
+ readonly kind: "account.user";
29
+ readonly name: string;
30
+ }
31
+ export type AccountGroupMember = AccountUserRef | string;
32
+ export interface AccountUserFactory<T extends unknown[] = []> extends FragmentFactory<T>, AccountUserRef {
33
+ }
34
+ export interface AccountGroupFactory<T extends unknown[] = []> extends FragmentFactory<T> {
35
+ readonly kind: "account.group";
36
+ readonly name: string;
37
+ }
38
+ export interface AccountGroupOpts extends Record<string, unknown> {
39
+ gid?: number;
40
+ members?: AccountGroupMember[];
41
+ }
42
+ export interface AccountNamespace {
43
+ user<T extends unknown[]>(name: string, factory: (...args: T) => AccountOpts): AccountUserFactory<T>;
44
+ group<T extends unknown[]>(name: string, factory?: (...args: T) => AccountGroupOpts): AccountGroupFactory<T>;
45
+ }
46
+ export declare const account: AccountNamespace;