Files

138 lines
4.1 KiB
Nix

{
lib,
buildNpmPackage,
fetchFromGitHub,
fetchurl,
nix-update-script,
versionCheckHook,
writableTmpDirAsHomeHook,
ripgrep,
fd,
makeBinaryWrapper,
stdenvNoCC,
}:
buildNpmPackage (finalAttrs: {
pname = "pi-coding-agent";
version = "0.81.1";
src = fetchFromGitHub {
owner = "earendil-works";
repo = "pi";
tag = "v${finalAttrs.version}";
hash = "sha256-xo3uoR7HceOCL3wqoMcacOe8WXP1o7ReAXne5t6Hgao=";
};
npmDepsHash = "sha256-lzKQZbnITzgV9koucsMno6f61ubBLYUcwQEXtak1r1s=";
# The provider model catalog (packages/ai/src/providers/data/) is generated by
# a network fetch (models.dev plus provider APIs) and is gitignored upstream,
# so it is absent from the source tarball. Restore it from the matching
# published @earendil-works/pi-ai npm package, which ships the hydrated catalog
# under dist/providers/data/. Bump this hash alongside version.
modelData = fetchurl {
url = "https://registry.npmjs.org/@earendil-works/pi-ai/-/pi-ai-${finalAttrs.version}.tgz";
hash = "sha256-x53MD5DU370ZdNoz36P+OWZjGVpoM5sfVcEU2/ckDy8=";
};
preConfigure = ''
mkdir -p packages/ai/src/providers/data
tar --extract --gzip --file=${finalAttrs.modelData} \
--directory=packages/ai/src/providers/data \
--strip-components=4 \
package/dist/providers/data
'';
npmWorkspace = "packages/coding-agent";
# Skip native module rebuild for unneeded workspaces (e.g. canvas from web-ui)
npmRebuildFlags = [ "--ignore-scripts" ];
nativeBuildInputs = [
makeBinaryWrapper
];
# Build workspace dependencies in order, then the coding-agent.
# We invoke tsgo directly for workspace deps to skip pi-ai's
# generate-models script, which requires network access; the model
# catalog it would produce is supplied via modelData above.
buildPhase = ''
runHook preBuild
npx tsgo -p packages/ai/tsconfig.build.json
npx tsgo -p packages/tui/tsconfig.build.json
npx tsgo -p packages/agent/tsconfig.build.json
npm run build --workspace=packages/coding-agent
runHook postBuild
'';
# npm workspace symlinks in the output point into packages/ which
# doesn't exist there. Replace runtime deps with built content and
# delete the rest.
postInstall = ''
local nm="$out/lib/node_modules/pi-monorepo/node_modules"
# Replace workspace deps needed at runtime with real copies
for ws in @earendil-works/pi-ai:packages/ai \
@earendil-works/pi-agent-core:packages/agent \
@earendil-works/pi-tui:packages/tui; do
IFS=: read -r pkg src <<< "$ws"
rm "$nm/$pkg"
cp -r "$src" "$nm/$pkg"
done
# Delete remaining workspace symlinks
find "$nm" -type l -lname '*/packages/*' -delete
# Clean up now-dangling .bin symlinks
find "$nm/.bin" -xtype l -delete
''
+ lib.optionalString stdenvNoCC.hostPlatform.isDarwin ''
# Remove foreign Linux binaries that make audit-tmpdir try to inspect ELF
# RPATHs with patchelf
rm -rf \
"$nm/@anthropic-ai/sandbox-runtime/dist/vendor/seccomp" \
"$nm/@anthropic-ai/sandbox-runtime/vendor/seccomp"
'';
postFixup = ''
wrapProgram $out/bin/pi --prefix PATH : ${
lib.makeBinPath [
ripgrep
fd
]
} \
--set-default PI_SKIP_VERSION_CHECK 1 \
--set-default PI_TELEMETRY 0
'';
doInstallCheck = true;
nativeInstallCheckInputs = [
writableTmpDirAsHomeHook
versionCheckHook
];
versionCheckKeepEnvironment = [ "HOME" ];
versionCheckProgram = "${placeholder "out"}/bin/pi";
versionCheckProgramArg = "--version";
passthru.updateScript = nix-update-script {
extraArgs = [
"--custom-dep"
"modelData"
];
};
meta = {
description = "Coding agent CLI with read, bash, edit, write tools and session management";
homepage = "https://pi.dev/";
downloadPage = "https://www.npmjs.com/package/@earendil-works/pi-coding-agent";
changelog = "https://github.com/earendil-works/pi/blob/v${finalAttrs.version}/packages/coding-agent/CHANGELOG.md";
license = lib.licenses.mit;
maintainers = with lib.maintainers; [
munksgaard
bryanhonof
];
mainProgram = "pi";
};
})