From 8ae17f0ef08a6213b3894663458854e57d51b045 Mon Sep 17 00:00:00 2001 From: Paul-Henri Froidmont Date: Tue, 13 Jan 2026 13:55:05 +0100 Subject: [PATCH] Add mia --- modules/ai/opencode.nix | 48 +++- modules/ai/prompts/basic-rules.txt | 13 ++ modules/services/work-proxy.nix | 2 +- overlay.nix | 5 +- packages/mia/bundle.ts | 38 ++++ packages/mia/package.nix | 241 +++++++++++++++++++++ packages/mia/relax-bun-version-check.patch | 28 +++ 7 files changed, 370 insertions(+), 5 deletions(-) create mode 100644 modules/ai/prompts/basic-rules.txt create mode 100644 packages/mia/bundle.ts create mode 100644 packages/mia/package.nix create mode 100644 packages/mia/relax-bun-version-check.patch diff --git a/modules/ai/opencode.nix b/modules/ai/opencode.nix index 0b39e56..0c67683 100644 --- a/modules/ai/opencode.nix +++ b/modules/ai/opencode.nix @@ -21,25 +21,60 @@ in enable = true; package = inputs.llm-agents.packages.${pkgs.system}.opencode; settings = { - model = "glm_4_5_air"; + model = "minimax_m2_1"; permission = { - bash = "ask"; + bash = { + "*" = "ask"; + + pwd = "allow"; + whoami = "allow"; + id = "allow"; + "uname*" = "allow"; + "date*" = "allow"; + + "ls*" = "allow"; + + "git status*" = "allow"; + "git diff*" = "allow"; + "git log*" = "allow"; + "git branch*" = "allow"; + "git rev-parse*" = "allow"; + "git remote -v" = "allow"; + + "node -v" = "allow"; + "npm -v" = "allow"; + "python --version" = "allow"; + "pip --version" = "allow"; + }; + edit = "ask"; + + skill = { + "*" = "allow"; + }; }; provider = { vllm = { npm = "@ai-sdk/openai-compatible"; name = "vLLM"; + options = { baseURL = "http://model1.lefoyer.lu:8030/v1"; apiKey = "dummy"; }; + models = { glm_4_5_air = { name = "GLM 4.5 Air (local)"; temperature = true; default = true; }; + + minimax_m2_1 = { + name = "MiniMax M2.1 (local)"; + temperature = true; + default = true; + }; }; }; openai = { @@ -65,12 +100,19 @@ in agent = { build = { mode = "primary"; - temperature = 0.4; + temperature = 0.1; + prompt = "{file:${./prompts/basic-rules.txt}}"; }; plan = { mode = "primary"; temperature = 0.4; }; + debug = { + disable = false; + }; + review = { + disable = false; + }; }; lsp = { metals = { diff --git a/modules/ai/prompts/basic-rules.txt b/modules/ai/prompts/basic-rules.txt new file mode 100644 index 0000000..87799ef --- /dev/null +++ b/modules/ai/prompts/basic-rules.txt @@ -0,0 +1,13 @@ +- No artifacts +- Less code is better than more code +- No fallback mechanisms — they hide real failures +- Rewrite existing components over adding new ones +- Flag obsolete files to keep the codebase lightweight +- Avoid race conditions at all costs +- Take your time to ultrathink when on extended thinking mode — thinking is cheaper than fixing bugs +- Add comments only when necessary — the code should speak for itself +- Always add meaningful logs — but only where it brings value +- Always do production ready code +- Code in a modular way to promote collaboration between agents - Adding features must not break the rest of the system + +These rules aim to maintain a clean, modular and maintainable codebase while promoting effective collaboration between different agents and developers. Don't write/change any code until you're very confident (95% or more) in what needs to be done. If unclear, ask for more info. diff --git a/modules/services/work-proxy.nix b/modules/services/work-proxy.nix index 4cdf8a1..900c206 100644 --- a/modules/services/work-proxy.nix +++ b/modules/services/work-proxy.nix @@ -27,7 +27,6 @@ in }; security.pki.certificateFiles = [ - "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt" ./certs/Foyer-Group-Root-CA.crt ./certs/Foyer-Sub-CA.crt ]; @@ -55,6 +54,7 @@ in (sbt.override { jre = jdk17; }) mongodb-compass chisel + mia ]; }; } diff --git a/overlay.nix b/overlay.nix index 2b8d37a..e6f3340 100644 --- a/overlay.nix +++ b/overlay.nix @@ -1 +1,4 @@ -final: prev: { activitywatch-bin = prev.callPackage ./packages/activitywatch.nix { }; } +final: prev: { + activitywatch-bin = prev.callPackage ./packages/activitywatch.nix { }; + mia = prev.callPackage ./packages/mia/package.nix { }; +} diff --git a/packages/mia/bundle.ts b/packages/mia/bundle.ts new file mode 100644 index 0000000..768e3e2 --- /dev/null +++ b/packages/mia/bundle.ts @@ -0,0 +1,38 @@ +#!/usr/bin/env bun + +import solidPlugin from "./node_modules/@opentui/solid/scripts/solid-plugin" +import fs from "fs" + +const version = process.env.OPENCODE_VERSION! +const channel = process.env.OPENCODE_CHANNEL! + +const result = await Bun.build({ + target: "bun", + outdir: "./dist", + entrypoints: [ + "./src/index.ts", + "./src/cli/cmd/tui/worker.ts" + ], + plugins: [solidPlugin], + naming: { + entry: "[dir]/[name].js" + }, + define: { + OPENCODE_VERSION: JSON.stringify(version), + OPENCODE_CHANNEL: JSON.stringify(channel), + }, + external: [ + "@opentui/core-*", + ], +}) + +if (!result.success) { + console.error("Bundle failed:", result.logs) + process.exit(1) +} + +// Move worker file to worker.ts at the dist root so the code can find it +if (fs.existsSync("./dist/cli/cmd/tui/worker.js")) { + fs.renameSync("./dist/cli/cmd/tui/worker.js", "./dist/worker.ts") + fs.rmdirSync("./dist/cli/cmd/tui", { recursive: true }) +} diff --git a/packages/mia/package.nix b/packages/mia/package.nix new file mode 100644 index 0000000..d2950c3 --- /dev/null +++ b/packages/mia/package.nix @@ -0,0 +1,241 @@ +{ + pkgs, + lib, + stdenvNoCC, + bun, + fetchFromGitHub, + fzf, + makeBinaryWrapper, + models-dev, + nix-update-script, + ripgrep, + testers, + writableTmpDirAsHomeHook, +}: +let + customCaBundle = pkgs.runCommand "ca-bundle-with-foyer.crt" { } '' + cat ${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt \ + ${../../modules/services/certs/Foyer-Group-Root-CA.crt} \ + ${../../modules/services/certs/Foyer-Sub-CA.crt} > $out + ''; + + pname = "mia"; + version = "1.1.2.1"; + src = fetchFromGitHub { + githubBase = "github.foyer.lu"; + owner = "rdo"; + repo = "mia"; + rev = "6e6193ceb325a0077f7a49051276cbdd6be60736"; + # tag = "v${version}-mia"; + hash = "sha256-qrYbdhII6jOPdztk7JxNlOz8vxef7G8WmMGOQr2vb5A="; + curlOptsList = [ + "--location" + "--cacert" + "${customCaBundle}" + ]; + }; + + node_modules = stdenvNoCC.mkDerivation { + pname = "${pname}-node_modules"; + inherit version src; + + impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [ + "GIT_PROXY_COMMAND" + "SOCKS_SERVER" + ]; + + nativeBuildInputs = [ + bun + writableTmpDirAsHomeHook + ]; + + dontConfigure = true; + + buildPhase = '' + runHook preBuild + + export BUN_INSTALL_CACHE_DIR=$(mktemp -d) + + bun install \ + --cpu="*" \ + --filter=./packages/opencode \ + --force \ + --frozen-lockfile \ + --ignore-scripts \ + --no-progress \ + --os="*" \ + --production + + bun run ./nix/scripts/canonicalize-node-modules.ts + bun run ./nix/scripts/normalize-bun-binaries.ts + + runHook postBuild + ''; + + installPhase = '' + runHook preInstall + + mkdir -p $out + while IFS= read -r dir; do + rel="''${dir#./}" + dest="$out/$rel" + mkdir -p "$(dirname "$dest")" + cp -R "$dir" "$dest" + done < <(find . -type d -name node_modules -prune | sort) + + runHook postInstall + ''; + + # NOTE: Required else we get errors that our fixed-output derivation references store paths + dontFixup = true; + + outputHash = "sha256-pOGHE02eoZzSkQwfUmG2EVaN80mBxKhIW+BFEufTh3w="; + outputHashAlgo = "sha256"; + outputHashMode = "recursive"; + }; +in +stdenvNoCC.mkDerivation (finalAttrs: { + inherit + pname + version + src + node_modules + ; + + nativeBuildInputs = [ + bun + makeBinaryWrapper + models-dev + ]; + + patches = [ + # NOTE: Relax Bun version check to be a warning instead of an error + ./relax-bun-version-check.patch + ]; + + configurePhase = '' + runHook preConfigure + + cp -R ${node_modules}/. . + + runHook postConfigure + ''; + + env.MODELS_DEV_API_JSON = "${models-dev}/dist/_api.json"; + env.OPENCODE_VERSION = finalAttrs.version; + env.OPENCODE_CHANNEL = "stable"; + + preBuild = '' + chmod -R u+w ./packages/opencode/node_modules + pushd ./packages/opencode/node_modules/@parcel/ + for pkg in ../../../../node_modules/.bun/@parcel+watcher-*; do + linkName=$(basename "$pkg" | sed 's/@.*+\(.*\)@.*/\1/') + ln -sf "$pkg/node_modules/@parcel/$linkName" "$linkName" + done + popd + + pushd ./packages/opencode/node_modules/@opentui/ + for pkg in ../../../../node_modules/.bun/@opentui+core-*; do + linkName=$(basename "$pkg" | sed 's/@.*+\(.*\)@.*/\1/') + ln -sf "$pkg/node_modules/@opentui/$linkName" "$linkName" + done + popd + ''; + + buildPhase = '' + runHook preBuild + + + cd ./packages/opencode + cp ${./bundle.ts} ./bundle.ts + bun run ./bundle.ts + + runHook postBuild + ''; + + dontStrip = true; + + installPhase = '' + runHook preInstall + + mkdir -p $out/lib/opencode + # Copy the bundled dist directory + cp -r dist $out/lib/opencode/ + + # Fix WASM paths in worker.ts - use absolute paths to the installed location + # Main wasm is tree-sitter-.wasm, language wasms are tree-sitter--.wasm + main_wasm=$(find "$out/lib/opencode/dist" -maxdepth 1 -name 'tree-sitter-[a-z0-9]*.wasm' -print -quit) + + substituteInPlace $out/lib/opencode/dist/worker.ts \ + --replace-fail 'module2.exports = "../../../tree-sitter-' 'module2.exports = "'"$out"'/lib/opencode/dist/tree-sitter-' \ + --replace-fail 'new URL("tree-sitter.wasm", import.meta.url).href' "\"$main_wasm\"" + + # Copy only the native modules we need (marked as external in bundle.ts) + mkdir -p $out/lib/opencode/node_modules/.bun + mkdir -p $out/lib/opencode/node_modules/@opentui + + # Copy @opentui/core platform-specific packages + for pkg in ../../node_modules/.bun/@opentui+core-*; do + if [ -d "$pkg" ]; then + cp -r "$pkg" $out/lib/opencode/node_modules/.bun/$(basename "$pkg") + fi + done + + mkdir -p $out/bin + makeWrapper ${lib.getExe bun} $out/bin/mia \ + --add-flags "run" \ + --add-flags "$out/lib/opencode/dist/index.js" \ + --prefix PATH : ${ + lib.makeBinPath [ + fzf + ripgrep + ] + } \ + --argv0 opencode + + runHook postInstall + ''; + + postInstall = '' + # Add symlinks for platform-specific native modules + for pkg in $out/lib/opencode/node_modules/.bun/@opentui+core-*; do + if [ -d "$pkg" ]; then + pkgName=$(basename "$pkg" | sed 's/@opentui+\(core-[^@]*\)@.*/\1/') + ln -sf ../.bun/$(basename "$pkg")/node_modules/@opentui/$pkgName \ + $out/lib/opencode/node_modules/@opentui/$pkgName + fi + done + ''; + + passthru = { + tests.version = testers.testVersion { + package = finalAttrs.finalPackage; + command = "HOME=$(mktemp -d) opencode --version"; + inherit (finalAttrs) version; + }; + updateScript = nix-update-script { + extraArgs = [ + "--subpackage" + "node_modules" + ]; + }; + }; + + meta = { + description = "AI coding agent built for the terminal"; + longDescription = '' + OpenCode is a terminal-based agent that can build anything. + It combines a TypeScript/JavaScript core with a Go-based TUI + to provide an interactive AI coding experience. + ''; + homepage = "https://github.com/sst/opencode"; + license = lib.licenses.mit; + platforms = [ + "aarch64-linux" + "x86_64-linux" + "aarch64-darwin" + "x86_64-darwin" + ]; + mainProgram = "opencode"; + }; +}) diff --git a/packages/mia/relax-bun-version-check.patch b/packages/mia/relax-bun-version-check.patch new file mode 100644 index 0000000..5d14f16 --- /dev/null +++ b/packages/mia/relax-bun-version-check.patch @@ -0,0 +1,28 @@ +From 0e07ea8225f5667e39c6aa59eea726266f0afab0 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= +Date: Thu, 13 Nov 2025 10:16:31 +0100 +Subject: [PATCH] Change Bun version check from error to warning +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +Signed-off-by: Jörg Thalheim +--- + packages/script/src/index.ts | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/packages/script/src/index.ts b/packages/script/src/index.ts +index 141d2b75..de06d0dc 100644 +--- a/packages/script/src/index.ts ++++ b/packages/script/src/index.ts +@@ -10,7 +10,7 @@ if (!expectedBunVersion) { + } + + if (process.versions.bun !== expectedBunVersion) { +- throw new Error(`This script requires bun@${expectedBunVersion}, but you are using bun@${process.versions.bun}`) ++ console.warn(`Warning: This script expects bun@${expectedBunVersion}, but you are using bun@${process.versions.bun}`) + } + + const CHANNEL = process.env["OPENCODE_CHANNEL"] ?? (await $`git branch --show-current`.text().then((x) => x.trim())) +-- +2.51.0