{ lib , autoPatchelfHook , buildNpmPackage , fetchurl , gcc-unwrapped , importNpmLock , makeWrapper , nodejs_22 , stdenv , codexSupport ? true, codex , opencodeSupport ? false, opencode , cursorSupport ? false, cursor-cli , claudeSupport ? false, claude-code , grokSupport ? false, grok-build , githubSupport ? false, gh , gitlabSupport ? true, glab , azureSupport ? false, azure-cli , bitbucketSupport ? false, bitbucket-cli }: let packageJson = lib.importJSON ./npm/package.json; packageJsonForNpm = builtins.removeAttrs packageJson [ "overrides" ]; packageLockJson = lib.importJSON ./npm/package-lock.json; binPath = let rawBinPath = if lib.isAttrs packageJson.bin then packageJson.bin.t3 else packageJson.bin; in lib.removePrefix "./" rawBinPath; binCjsPath = if lib.hasSuffix ".mjs" binPath then "${lib.removeSuffix ".mjs" binPath}.cjs" else null; in buildNpmPackage rec { pname = "t3-cli"; version = "0.1.12"; nodejs = nodejs_22; src = fetchurl { url = "https://registry.npmjs.org/t3/-/t3-${version}.tgz"; hash = "sha512-qVERRybGKGgFNbuFyYy51VrvAXTJouUG1akOexjFOntGlTNLZ9b8nlSnx+0V4j7ywBXY4SIycRNcsOjjFjPhIQ=="; }; sourceRoot = "package"; npmDeps = importNpmLock { package = packageJsonForNpm; packageLock = packageLockJson; fetcherOpts = { "node_modules/@effect/platform-node" = { name = "platform-node.tgz"; }; "node_modules/@effect/platform-node-shared" = { name = "platform-node-shared.tgz"; }; "node_modules/@effect/sql-sqlite-bun" = { name = "sql-sqlite-bun.tgz"; }; "node_modules/effect" = { name = "effect.tgz"; }; }; }; buildInputs = lib.optional stdenv.hostPlatform.isLinux gcc-unwrapped; npmConfigHook = importNpmLock.npmConfigHook; # The published lockfile mixes Effect beta packages with an rc peer. npmFlags = [ "--legacy-peer-deps" ]; nativeBuildInputs = [ makeWrapper ] ++ lib.optional stdenv.hostPlatform.isLinux autoPatchelfHook; dontNpmBuild = true; postPatch = '' cp ${./npm/package.json} package.json cp ${./npm/package-lock.json} package-lock.json ${lib.optionalString (packageJson ? overrides) '' node -e ' const fs = require("fs"); const pkg = JSON.parse(fs.readFileSync("package.json", "utf8")); delete pkg.overrides; fs.writeFileSync("package.json", JSON.stringify(pkg, null, 2) + "\n"); ' ''} bin_path=${lib.escapeShellArg binPath} if [ ! -f "$bin_path" ]; then echo "missing CLI entrypoint: $bin_path" >&2 exit 1 fi sed -i "s/var version = \\\".*\\\";/var version = \\\"${version}\\\";/" "$bin_path" ${lib.optionalString (binCjsPath != null) '' bin_cjs_path=${lib.escapeShellArg binCjsPath} if [ -f "$bin_cjs_path" ]; then sed -i "s/var version = \\\".*\\\";/var version = \\\"${version}\\\";/" "$bin_cjs_path" fi ''} ''; installPhase = '' runHook preInstall mkdir -p $out/lib/node_modules/t3 $out/bin cp -r . $out/lib/node_modules/t3 makeWrapper ${nodejs_22}/bin/node $out/bin/t3 \ --add-flags "$out/lib/node_modules/t3/${binPath}" \ ${lib.concatStringsSep " \\ " (lib.filter (s: s != "") [ (lib.optionalString codexSupport ''--prefix PATH : "${lib.makeBinPath [ codex ]}"'') (lib.optionalString opencodeSupport ''--prefix PATH : "${lib.makeBinPath [ opencode ]}"'') (lib.optionalString cursorSupport ''--prefix PATH : "${lib.makeBinPath [ cursor-cli ]}"'') (lib.optionalString claudeSupport ''--prefix PATH : "${lib.makeBinPath [ claude-code ]}"'') (lib.optionalString grokSupport ''--prefix PATH : "${lib.makeBinPath [ grok-build ]}"'') (lib.optionalString githubSupport ''--prefix PATH : "${lib.makeBinPath [ gh ]}"'') (lib.optionalString gitlabSupport ''--prefix PATH : "${lib.makeBinPath [ glab ]}"'') (lib.optionalString azureSupport ''--prefix PATH : "${lib.makeBinPath [ azure-cli ]}"'') (lib.optionalString bitbucketSupport ''--prefix PATH : "${lib.makeBinPath [ bitbucket-cli ]}"'') ])} runHook postInstall ''; meta = with lib; { description = "T3 Code CLI packaged from the upstream npm artifact"; homepage = "https://github.com/pingdotgg/t3code"; license = licenses.mit; mainProgram = "t3"; platforms = platforms.unix; }; }