This repository has no description
3.3 kB
86 lines
1{ self, inputs, ... }:
2{
3 perSystem =
4 { pkgs, system, ... }:
5 let
6 piPkg = self.legacyPackages.${system}.llm.pi;
7
8 # Pre-install pi plugins into a fake npm global prefix
9 pluginPrefixRaw =
10 pkgs.runCommand "pi-plugins-raw"
11 {
12 nativeBuildInputs = [
13 pkgs.nodejs
14 pkgs.cacert
15 ];
16 outputHashMode = "recursive";
17 outputHashAlgo = "sha256";
18 outputHash = "sha256-QZSVCJ0XirRz56v6ogxaB37c0bI8+OGEjrnqCFr/YI8=";
19 impureEnvVars = [
20 "http_proxy"
21 "https_proxy"
22 ];
23 SSL_CERT_FILE = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt";
24 }
25 ''
26 export HOME=$TMPDIR
27 export npm_config_prefix=$out
28 npm install -g pi-hooks shitty-extensions
29 '';
30
31 # Patch shitty-extensions:
32 # - Remove the resistance extension (annoying terminator quote widget)
33 # - Rebind ultrathink ctrl+u -> ctrl+shift+u (conflicts with deleteToLineStart)
34 # - Rebind speedreading ctrl+r -> ctrl+shift+r (conflicts with renameSession)
35 # Patch pi-hooks:
36 # - Replace terminal bell with pw-play peon sounds in permission extension
37 pluginPrefix = pkgs.runCommand "pi-plugins" { } ''
38 cp -a ${pluginPrefixRaw} $out
39 chmod -R u+w $out
40 pkg=$out/lib/node_modules/shitty-extensions/package.json
41 ${pkgs.jq}/bin/jq '.pi.extensions |= map(select(contains("resistance") | not))' "$pkg" > "$pkg.tmp"
42 mv "$pkg.tmp" "$pkg"
43
44 # Fix keybinding conflicts in extension source
45 ${pkgs.gnused}/bin/sed -i 's/"ctrl+u"/"ctrl+shift+u"/' $out/lib/node_modules/shitty-extensions/extensions/ultrathink.ts
46 ${pkgs.gnused}/bin/sed -i 's/"ctrl+r"/"ctrl+shift+r"/' $out/lib/node_modules/shitty-extensions/extensions/speedreading.ts
47
48 # Patch permission extension to use pw-play for peon sounds on Linux
49 ${pkgs.python3}/bin/python3 ${./patch-permission-sound.py} \
50 $out/lib/node_modules/pi-hooks/permission/permission.ts \
51 ${pkgs.pipewire}/bin/pw-play
52 '';
53 in
54 {
55 packages.pi = inputs.wrappers.lib.wrapPackage {
56 pkgs = pkgs;
57 package = piPkg;
58 runtimeInputs = [ pkgs.nodejs ];
59 wrapper =
60 { exePath, ... }:
61 ''
62 set -efu
63 export npm_config_prefix="${pluginPrefix}"
64
65 # Ensure settings.json has our plugins listed
66 SETTINGS_DIR="''${PI_CODING_AGENT_DIR:-$HOME/.pi/agent}"
67 SETTINGS_FILE="$SETTINGS_DIR/settings.json"
68 mkdir -p "$SETTINGS_DIR"
69
70 # Add packages to settings if not already present
71 if [ ! -f "$SETTINGS_FILE" ]; then
72 echo '{"packages":["npm:pi-hooks","npm:shitty-extensions"]}' > "$SETTINGS_FILE"
73 else
74 for pkg in "npm:pi-hooks" "npm:shitty-extensions"; do
75 if ! grep -q "$pkg" "$SETTINGS_FILE"; then
76 ${pkgs.jq}/bin/jq --arg p "$pkg" '.packages = ((.packages // []) + [$p] | unique)' "$SETTINGS_FILE" > "$SETTINGS_FILE.tmp"
77 mv "$SETTINGS_FILE.tmp" "$SETTINGS_FILE"
78 fi
79 done
80 fi
81
82 exec ${exePath} "$@"
83 '';
84 };
85 };
86}