Files
samfelag/modules/ai/pi.nix
T
2026-11-09 11:24:43 +01:00

89 lines
2.9 KiB
Nix

{ config, lib, pkgs, inputs, system, ... }:
let
cfg = config.samfelag.modules.ai.pi;
# pi's bundled extensions don't share a common pi-coding-agent peer range,
# so npm needs --legacy-peer-deps when pi shells out to install them. Wrap
# just `pi` rather than setting this npm-wide.
pi-wrapped = pkgs.symlinkJoin {
name = "pi-coding-agent-wrapped";
paths = [ pkgs.pi-coding-agent ];
nativeBuildInputs = [ pkgs.makeWrapper ];
postBuild = ''
wrapProgram $out/bin/pi --set npm_config_legacy_peer_deps true
'';
};
extensions = [
"pi-claude-bridge"
# "pi-sandbox"
"pi-intercom"
# "pi-observational-memory"
];
desiredSettings = {
packages = map (p: "npm:${p}") extensions;
defaultProvider = "claude-bridge";
defaultModel = "claude-sonnet-5";
};
in
{
options.samfelag.modules.ai.pi = with lib.types; {
enable = lib.mkEnableOption "pi";
};
config = lib.mkIf cfg.enable {
# nixos-26.05's pi-coding-agent (0.75.4) is far behind what current
# extensions expect as their peer version (0.80-1.x); pull the package
# from nixpkgs-unstable instead so the running binary actually matches.
nixpkgs.overlays = [
(final: prev: {
pi-coding-agent = inputs.nixpkgs-unstable.legacyPackages.${system}.pi-coding-agent;
})
];
environment.systemPackages = [
pi-wrapped
# pkgs.ripgrep # required by the pi-sandbox extension
# pkgs.bubblewrap # required by the pi-sandbox extension (bwrap)
pkgs.nodejs # provides npm, used by pi itself to install extensions
];
# Extensions aren't packaged in nixpkgs; pi resolves/fetches them itself
# from npm the first time it runs. Pi also writes to settings.json at
# runtime (model choice, theme, etc.), so merge our declared keys into
# it instead of overwriting it wholesale.
system.userActivationScripts.piExtensions = ''
settings="$HOME/.pi/agent/settings.json"
mkdir -p "$(dirname "$settings")"
[ -f "$settings" ] || echo '{}' > "$settings"
${pkgs.jq}/bin/jq \
--argjson desired ${lib.escapeShellArg (builtins.toJSON desiredSettings)} \
'. * $desired' \
"$settings" > "$settings.tmp"
mv "$settings.tmp" "$settings"
'';
# pi-claude-bridge and models.json aren't written to by pi at runtime
# (unlike settings.json, which accumulates model/theme choices), so these
# are safe to own outright instead of merging.
hm.home.file = {
".pi/agent/claude-bridge.json".text = builtins.toJSON {
provider = {
# The SDK's bundled binary can't run on NixOS; point it at ours instead.
pathToClaudeCodeExecutable = "${pkgs.claude-code}/bin/claude";
};
};
# pi's own schema requires "providers", even empty; it only ever
# ships a bare {} on first run, which fails validation.
".pi/agent/models.json".text = builtins.toJSON {
providers = {
};
};
};
};
}