Files
millerson-overlay.nix/packages/freebuff/package.nix
Alexander Miroshnichenko bb3c53b40b
Some checks failed
CI / check (push) Has been cancelled
refactor(freebuff): run engine directly from nix-store
Remove unnecessary copy to ~/.config/manicode/. The patched ELF
binary and tree-sitter.wasm stay in the nix store alongside a minimal
JS launcher that spawns the engine with terminal cleanup handling.
2026-05-12 11:48:46 +03:00

151 lines
4.4 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=";
};
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"
patchelf \
--set-interpreter "${glibc}/lib/ld-linux-x86-64.so.2" \
--set-rpath "${glibc}/lib:" \
"$out/bin/freebuff-engine"
if [ -f /tmp/fb-engine/tree-sitter.wasm ]; then
cp /tmp/fb-engine/tree-sitter.wasm "$out/bin/"
fi
rm -rf /tmp/fb-engine
# Extract npm dependencies from pre-fetched tarballs
extractNpmPkg() {
local src="$1" target="$2"
mkdir -p "$target"
tar xzf "$src" -C "$target" --strip-components=1
}
mkdir -p "$out/lib/node_modules/tar"
mkdir -p "$out/lib/node_modules/chownr"
mkdir -p "$out/lib/node_modules/minipass"
mkdir -p "$out/lib/node_modules/minizlib"
mkdir -p "$out/lib/node_modules/yallist"
mkdir -p "$out/lib/node_modules/@isaacs/fs-minipass"
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"
extractNpmPkg "${pkgFsMinipass}" "$out/lib/node_modules/@isaacs/fs-minipass"
# Launcher: run the patched engine directly from nix-store
cat > "$out/bin/launcher.js" << LAUNCHER_EOF
#!/usr/bin/env node
const { spawn } = require('child_process');
const TERMINAL_RESET = '\x1b[?1049l\x1b[?1000l\x1b[?1002l\x1b[?1003l\x1b[?1006l\x1b[?2004l\x1b[?25h';
const engine = '${placeholder "out"}/bin/freebuff-engine';
function reset() {
try { if (process.stdin.isTTY && process.stdin.setRawMode) process.stdin.setRawMode(false); } catch(e){}
try { if (process.stdout.isTTY) process.stdout.write(TERMINAL_RESET); } catch(e){}
}
const child = spawn(engine, process.argv.slice(2), { stdio: 'inherit' });
child.on('exit', (code, signal) => { reset(); process.exit(signal ? 1 : code || 0); });
child.on('error', (e) => { console.error('Failed to start freebuff:', e.message); process.exit(1); });
LAUNCHER_EOF
makeWrapper "${nodejs}/bin/node" "$out/bin/freebuff" \
--set NODE_PATH "$out/lib/node_modules" \
--add-flags "$out/bin/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;
};
}