simplify pi setup (#56)

remove attach function
This commit is contained in:
Hari 2026-04-02 23:36:09 -04:00 committed by GitHub
parent 1ed089acbc
commit 019bb0619a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 38 deletions

View file

@ -42,7 +42,7 @@
ld = "lumen diff"; ld = "lumen diff";
lg = "lazygit"; lg = "lazygit";
nim = "nvim ."; nim = "nvim .";
pa = "dtach -a /run/pi-agent/pi-agent.sock";
} }
// lib.optionalAttrs hostConfig.isDarwin { // lib.optionalAttrs hostConfig.isDarwin {
tailscale = "/Applications/Tailscale.app/Contents/MacOS/Tailscale"; tailscale = "/Applications/Tailscale.app/Contents/MacOS/Tailscale";

View file

@ -5,60 +5,42 @@
}: }:
let let
piAgentEnvFile = "/var/lib/pi-agent/pi-agent.env"; piAgentEnvFile = "/var/lib/pi-agent/pi-agent.env";
piAgentEnvCheck = pkgs.writeShellScript "pi-agent-env-check" '' npmBin = "/home/${username}/.local/share/npm/bin";
[ -f "${piAgentEnvFile}" ] piBin = "${npmBin}/pi";
'';
piBin = "/home/${username}/.local/share/npm/bin/pi";
# Wrapper that runs pi inside dtach so it gets a PTY (systemd services
# don't have a terminal) and can be attached to later for debugging:
# dtach -a /run/pi-agent/pi-agent.sock
piAgentStart = pkgs.writeShellScript "start-pi-agent" '' piAgentStart = pkgs.writeShellScript "start-pi-agent" ''
if [ ! -x "${piBin}" ]; then [ -x "${piBin}" ] || { echo "pi not found at ${piBin}" >&2; exit 1; }
echo "pi binary not found at ${piBin}" >&2 export PATH="${npmBin}:$PATH"
exit 1
fi
# RPC subprocesses spawned by pi-channels do spawn("pi", ...) and
# need the npm bin directory on PATH.
export PATH="/home/${username}/.local/share/npm/bin:$PATH"
exec ${pkgs.dtach}/bin/dtach -N /run/pi-agent/pi-agent.sock \ exec ${pkgs.dtach}/bin/dtach -N /run/pi-agent/pi-agent.sock \
${piBin} --chat-bridge ${piBin} --chat-bridge
''; '';
in in
{ {
# Ensure state directory and env file have correct permissions.
systemd.tmpfiles.rules = [ systemd.tmpfiles.rules = [
"d /var/lib/pi-agent 0750 ${username} users -" "d /var/lib/pi-agent 0750 ${username} users -"
"z ${piAgentEnvFile} 0600 ${username} users -" "z ${piAgentEnvFile} 0600 ${username} users -"
"d /run/pi-agent 0750 ${username} users -" "d /run/pi-agent 0750 ${username} users -"
]; ];
# Pi agent running 24/7 inside dtach. # Pi coding agent running as a Telegram bridge gateway.
# Extensions (pi-channels, pi-schedule-prompt, pi-subagents) load # The main process hosts extensions (pi-channels, pi-schedule-prompt,
# inside Pi's process and handle Telegram bridging, scheduled tasks, # pi-subagents) and polls Telegram. Actual prompts run in separate
# and background subagent delegation. # pi --mode rpc subprocesses spawned on demand.
# #
# The --chat-bridge flag auto-enables the Telegram bridge on startup. # Config: ~/.pi/agent/settings.json (bot token, bridge settings)
# Telegram bot token lives in ~/.pi/agent/settings.json (see pi-channels docs). # API key: /var/lib/pi-agent/pi-agent.env
# ANTHROPIC_API_KEY comes from the env file.
#
# Attach for debugging: dtach -a /run/pi-agent/pi-agent.sock
# Detach with: Ctrl+\
systemd.services.pi-agent = { systemd.services.pi-agent = {
description = "Pi Coding Agent (24/7)"; description = "Pi Telegram Bridge";
after = [ "network-online.target" ]; after = [ "network-online.target" ];
wants = [ "network-online.target" ]; wants = [ "network-online.target" ];
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
path = [ path = with pkgs; [
pkgs.nodejs_22 nodejs_22
pkgs.git git
pkgs.dtach dtach
pkgs.coreutils coreutils
pkgs.gnutar gnutar
pkgs.gzip gzip
]; ];
environment = { environment = {
HOME = "/home/${username}"; HOME = "/home/${username}";
@ -73,7 +55,9 @@ in
User = username; User = username;
Group = "users"; Group = "users";
WorkingDirectory = "/home/${username}"; WorkingDirectory = "/home/${username}";
ExecCondition = piAgentEnvCheck; ExecCondition = "${pkgs.writeShellScript "pi-env-check" ''
[ -f "${piAgentEnvFile}" ]
''}";
EnvironmentFile = piAgentEnvFile; EnvironmentFile = piAgentEnvFile;
ExecStart = piAgentStart; ExecStart = piAgentStart;
Restart = "on-failure"; Restart = "on-failure";