- Download and extract the engine binary from codebuff.com releases - Use patchelf to set correct glibc interpreter path - Create wrapper launcher that copies patched binary to user config - Pre-fetch all npm dependencies (no network needed in sandbox) - Set dontStrip/dontPatchelf to prevent fixupPhase corruption
212 lines
6.1 KiB
Nix
212 lines
6.1 KiB
Nix
{
|
|
lib,
|
|
stdenv,
|
|
fetchurl,
|
|
nodejs,
|
|
makeWrapper,
|
|
patchelf,
|
|
glibc,
|
|
}:
|
|
|
|
let
|
|
version = "0.0.85";
|
|
|
|
freebuffSrc = fetchurl {
|
|
url = "https://registry.npmjs.org/freebuff/-/freebuff-${version}.tgz";
|
|
hash = "sha256-1x593yLMkoFIO5O+k5NKeEpi729VhINQlW1xFbYGnXM=";
|
|
};
|
|
|
|
pkgTar = fetchurl {
|
|
url = "https://registry.npmjs.org/tar/-/tar-7.5.15.tgz";
|
|
hash = "sha256-hl60jJtM1W2THQyGZj8G72GEG8QvVrHvNnr7EU1liBw=";
|
|
};
|
|
|
|
pkgFsMinipass = fetchurl {
|
|
url = "https://registry.npmjs.org/@isaacs/fs-minipass/-/fs-minipass-4.0.1.tgz";
|
|
hash = "sha256-esKG48zMHqiYDnniA53va7l9MYLheVHNkJTwQA7ZgjY=";
|
|
};
|
|
|
|
pkgChownr = fetchurl {
|
|
url = "https://registry.npmjs.org/chownr/-/chownr-3.0.0.tgz";
|
|
hash = "sha256-TCT12qYwFCJS2oneZV998JDqR5RQqIJYl3UdeDIbE2A=";
|
|
};
|
|
|
|
pkgMinipass = fetchurl {
|
|
url = "https://registry.npmjs.org/minipass/-/minipass-7.1.3.tgz";
|
|
hash = "sha256-UqxhvnQ3VeP9yY5WAIbQ1KHix/02Qzh3kts44yLSfRI=";
|
|
};
|
|
|
|
pkgMinizlib = fetchurl {
|
|
url = "https://registry.npmjs.org/minizlib/-/minizlib-3.1.0.tgz";
|
|
hash = "sha256-mb8uKWGBct1x8GVHN0kMaPcbx+I3nUc+OPn+8tvs4uM=";
|
|
};
|
|
|
|
pkgYallist = fetchurl {
|
|
url = "https://registry.npmjs.org/yallist/-/yallist-5.0.0.tgz";
|
|
hash = "sha256-fJ1D26t8qzsxM7Dmpa8UAUSCKFMWs57J9QjvrdnrzpU=";
|
|
};
|
|
|
|
# Pre-built binary release from codebuff.com
|
|
binarySrc = fetchurl {
|
|
url = "https://codebuff.com/api/releases/download/${version}/freebuff-linux-x64.tar.gz";
|
|
hash = "sha256-WRTEXqKDww4ZPAnDLAAkAd0jxl+z6+dRbcQORmN7QfM=";
|
|
};
|
|
|
|
in
|
|
|
|
stdenv.mkDerivation rec {
|
|
pname = "freebuff";
|
|
version = "0.0.85";
|
|
|
|
src = freebuffSrc;
|
|
|
|
nativeBuildInputs = [ makeWrapper patchelf ];
|
|
|
|
dontStrip = true;
|
|
dontPatchelf = true;
|
|
|
|
installPhase = ''
|
|
runHook preInstall
|
|
|
|
# Extract and patch the pre-built binary for NixOS compatibility
|
|
mkdir -p "$out/bin" /tmp/fb-engine
|
|
tar xzf "${binarySrc}" -C /tmp/fb-engine --strip-components=0
|
|
cp /tmp/fb-engine/freebuff "$out/bin/freebuff-engine"
|
|
chmod 755 "$out/bin/freebuff-engine"
|
|
|
|
# Patch ELF interpreter and rpath for glibc on NixOS
|
|
patchelf \
|
|
--set-interpreter "${glibc}/lib/ld-linux-x86-64.so.2" \
|
|
--set-rpath "${glibc}/lib:" \
|
|
"$out/bin/freebuff-engine"
|
|
|
|
# Copy tree-sitter.wasm next to the engine binary
|
|
if [ -f /tmp/fb-engine/tree-sitter.wasm ]; then
|
|
cp /tmp/fb-engine/tree-sitter.wasm "$out/bin/"
|
|
fi
|
|
|
|
rm -rf /tmp/fb-engine
|
|
|
|
# Set up JS launcher and npm dependencies
|
|
mkdir -p "$out/lib/node_modules/freebuff"
|
|
cp -r * "$out/lib/node_modules/freebuff/"
|
|
|
|
# Extract npm dependencies from pre-fetched tarballs
|
|
extractNpmPkg() {
|
|
local src="$1" target="$2"
|
|
mkdir -p "$target"
|
|
tar xzf "$src" -C "$target" --strip-components=1
|
|
}
|
|
|
|
extractNpmPkg "${pkgTar}" "$out/lib/node_modules/tar"
|
|
extractNpmPkg "${pkgChownr}" "$out/lib/node_modules/chownr"
|
|
extractNpmPkg "${pkgMinipass}" "$out/lib/node_modules/minipass"
|
|
extractNpmPkg "${pkgMinizlib}" "$out/lib/node_modules/minizlib"
|
|
extractNpmPkg "${pkgYallist}" "$out/lib/node_modules/yallist"
|
|
mkdir -p "$out/lib/node_modules/@isaacs"
|
|
extractNpmPkg "${pkgFsMinipass}" "$out/lib/node_modules/@isaacs/fs-minipass"
|
|
|
|
# Create wrapper launcher that uses the pre-patched binary instead of downloading
|
|
cat > "$out/lib/node_modules/freebuff/wrapper-launcher.js" << WRAPPER_EOF
|
|
#!/usr/bin/env node
|
|
const { spawn } = require('child_process');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
// Use the Nix-store patched binary directly
|
|
const enginePath = process.env.FREEBUFF_ENGINE || '${placeholder "out"}/bin/freebuff-engine';
|
|
|
|
// Metadata path for version tracking
|
|
const configDir = path.join(process.env.HOME || '/', '.config', 'manicode');
|
|
const metadataPath = path.join(configDir, 'freebuff-metadata.json');
|
|
|
|
function resetTerminal() {
|
|
try {
|
|
if (process.stdin.isTTY && process.stdin.setRawMode) {
|
|
process.stdin.setRawMode(false);
|
|
}
|
|
} catch {}
|
|
try {
|
|
const seqs = '\\x1b[?1049l\\x1b[?1000l\\x1b[?1002l\\x1b[?1003l\\x1b[?1006l\\x1b[?2004l\\x1b[?25h';
|
|
if (process.stdout.isTTY) process.stdout.write(seqs);
|
|
} catch {}
|
|
}
|
|
|
|
async function main() {
|
|
// Copy patched binary to user config dir so the engine finds tree-sitter.wasm there too
|
|
fs.mkdirSync(configDir, { recursive: true });
|
|
const targetBinary = path.join(configDir, 'freebuff');
|
|
const targetWasm = path.join(configDir, 'tree-sitter.wasm');
|
|
|
|
// Install patched binary if not present or version mismatch
|
|
let needsInstall = !fs.existsSync(targetBinary);
|
|
if (!needsInstall) {
|
|
try {
|
|
const meta = JSON.parse(fs.readFileSync(metadataPath, 'utf8'));
|
|
needsInstall = (meta.version !== '${version}');
|
|
} catch {}
|
|
}
|
|
|
|
if (needsInstall) {
|
|
try { fs.unlinkSync(targetBinary); } catch {}
|
|
fs.copyFileSync(enginePath, targetBinary);
|
|
fs.chmodSync(targetBinary, 0o755);
|
|
fs.writeFileSync(metadataPath, JSON.stringify({ version: '${version}' }, null, 2));
|
|
|
|
// Copy tree-sitter.wasm if available
|
|
const wasmSrc = '${placeholder "out"}/bin/tree-sitter.wasm';
|
|
if (fs.existsSync(wasmSrc)) {
|
|
try { fs.copyFileSync(wasmSrc, targetWasm); } catch {}
|
|
}
|
|
}
|
|
|
|
const child = spawn(targetBinary, process.argv.slice(2), {
|
|
stdio: 'inherit',
|
|
env: { ...process.env },
|
|
});
|
|
|
|
child.on('exit', (code, signal) => {
|
|
resetTerminal();
|
|
process.exit(signal ? 1 : (code || 0));
|
|
});
|
|
|
|
child.on('error', (err) => {
|
|
console.error('Failed to start freebuff:', err.message);
|
|
process.exit(1);
|
|
});
|
|
}
|
|
|
|
main().catch((error) => {
|
|
console.error('Unexpected error:', error.message);
|
|
process.exit(1);
|
|
});
|
|
WRAPPER_EOF
|
|
|
|
# Create the main binary wrapper
|
|
makeWrapper "${nodejs}/bin/node" "$out/bin/freebuff" \
|
|
--set NODE_PATH "$out/lib/node_modules" \
|
|
--add-flags "$out/lib/node_modules/freebuff/wrapper-launcher.js"
|
|
|
|
runHook postInstall
|
|
'';
|
|
|
|
doCheck = false;
|
|
|
|
passthru = {
|
|
category = "AI Coding Agents";
|
|
updateScript = [
|
|
"nix-update"
|
|
"--flake"
|
|
".#freebuff"
|
|
];
|
|
};
|
|
|
|
meta = with lib; {
|
|
description = "The world's strongest free coding agent";
|
|
homepage = "https://codebuff.com";
|
|
license = licenses.mit;
|
|
mainProgram = "freebuff";
|
|
platforms = platforms.linux;
|
|
};
|
|
}
|