Files
nix-js/nix-js/runtime-ts/src/builtins/misc.ts

173 lines
4.4 KiB
TypeScript

/**
* Miscellaneous builtin functions
*/
import { force } from "../thunk";
import { CatchableError } from "../types";
import type { NixBool, NixStrictValue, NixValue } from "../types";
import { forceList, forceAttrs, forceFunction } from "../type-assert";
import * as context from "./context";
export const addErrorContext =
(e1: NixValue) =>
(e2: NixValue): never => {
throw new Error("Not implemented: addErrorContext");
};
export const appendContext = context.appendContext;
export const getContext = context.getContext;
export const hasContext = context.hasContext;
export const hashFile =
(type: NixValue) =>
(p: NixValue): never => {
throw new Error("Not implemented: hashFile");
};
export const hashString =
(type: NixValue) =>
(p: NixValue): never => {
throw new Error("Not implemented: hashString");
};
export const convertHash = (args: NixValue): never => {
throw new Error("Not implemented: convertHash");
};
export const unsafeDiscardOutputDependency = context.unsafeDiscardOutputDependency;
export const unsafeDiscardStringContext = context.unsafeDiscardStringContext;
export const unsafeGetAttrPos = (s: NixValue): never => {
throw new Error("Not implemented: unsafeGetAttrPos");
};
export const addDrvOutputDependencies = context.addDrvOutputDependencies;
export const compareVersions =
(s1: NixValue) =>
(s2: NixValue): never => {
throw new Error("Not implemented: compareVersions");
};
export const dirOf = (s: NixValue): never => {
throw new Error("Not implemented: dirOf");
};
export const flakeRefToString = (attrs: NixValue): never => {
throw new Error("Not implemented: flakeRefToString");
};
export const functionArgs = (f: NixValue): never => {
throw new Error("Not implemented: functionArgs");
};
export const genericClosure = (args: NixValue): never => {
throw new Error("Not implemented: genericClosure");
};
export const getFlake = (attrs: NixValue): never => {
throw new Error("Not implemented: getFlake");
};
export const match =
(regex: NixValue) =>
(str: NixValue): never => {
throw new Error("Not implemented: match");
};
export const outputOf =
(drv: NixValue) =>
(out: NixValue): never => {
throw new Error("Not implemented: outputOf");
};
export const parseDrvName = (s: NixValue): never => {
throw new Error("Not implemented: parseDrvName");
};
export const parseFlakeName = (s: NixValue): never => {
throw new Error("Not implemented: parseFlakeName");
};
export const parseFlakeRef = (s: NixValue): never => {
throw new Error("Not implemented: parseFlakeRef");
};
export const placeholder = (output: NixValue): never => {
throw new Error("Not implemented: placeholder");
};
export const replaceStrings =
(from: NixValue) =>
(to: NixValue) =>
(s: NixValue): never => {
throw new Error("Not implemented: replaceStrings");
};
export const split = (regex: NixValue, str: NixValue): never => {
throw new Error("Not implemented: split");
};
export const splitVersion = (s: NixValue): never => {
throw new Error("Not implemented: splitVersion");
};
export const traceVerbose = (e1: NixValue, e2: NixValue): never => {
throw new Error("Not implemented: traceVerbose");
};
export const tryEval = (e: NixValue): { success: NixBool; value: NixStrictValue } => {
try {
return {
success: true,
value: force(e),
};
} catch (err) {
if (err instanceof CatchableError) {
return {
success: false,
value: false,
};
} else {
throw err;
}
}
};
export const zipAttrsWith =
(f: NixValue) =>
(list: NixValue): NixValue => {
const listForced = forceList(list);
// Map to collect all values for each attribute name
const attrMap = new Map<string, NixValue[]>();
// Iterate through each attribute set in the list
for (const item of listForced) {
const attrs = forceAttrs(force(item) as NixValue);
// Collect all attribute names and their values
for (const [key, value] of Object.entries(attrs)) {
if (!attrMap.has(key)) {
attrMap.set(key, []);
}
attrMap.get(key)!.push(value);
}
}
// Build the result attribute set
const result: Record<string, NixValue> = {};
for (const [name, values] of attrMap.entries()) {
// Apply f to name and values list
// f is curried: f name values
const fWithName = forceFunction(f)(name);
result[name] = forceFunction(fWithName)(values);
}
return result;
};