# OpenClaw Podman Module # Provides: AI Agent with Discord integration running in an isolated container # # Usage: # myModules.openclaw-podman = { # enable = true; # port = 18789; # domain = "openclaw.example.com"; # }; { config, lib, pkgs, inputs, ... }: let cfg = config.myModules.openclaw-podman; in { options.myModules.openclaw-podman = { enable = lib.mkEnableOption "OpenClaw AI Agent (Podman)"; superpowers = { enable = lib.mkEnableOption "openclaw-superpowers extension"; src = lib.mkOption { type = lib.types.path; default = inputs.openclaw-superpowers; description = "Path to openclaw-superpowers source"; }; }; port = lib.mkOption { type = lib.types.port; default = 18789; description = "Gateway port for OpenClaw"; }; domain = lib.mkOption { type = lib.types.str; example = "openclaw.example.com"; description = "Public domain for OpenClaw"; }; }; config = lib.mkIf cfg.enable { # Enable podman myModules.podman.enable = true; # Create directory for OpenClaw data systemd.tmpfiles.rules = [ "d /var/lib/openclaw 0755 1000 1000 -" # Assuming node user is uid 1000 ]; # OpenClaw container (bridge network — isolated from host services) virtualisation.oci-containers.containers."openclaw" = { image = "ghcr.io/openclaw/openclaw:latest"; ports = [ "127.0.0.1:${toString cfg.port}:8080" ]; environmentFiles = [ config.sops.templates."openclaw.env".path ]; volumes = [ "/var/lib/openclaw:/home/node/.openclaw" ] ++ lib.optionals cfg.superpowers.enable [ "${cfg.superpowers.src}:/home/node/superpowers-src:ro" ]; }; # Copy the declarative config before starting the container # This allows OpenClaw to safely write/rename the file at runtime without EBUSY errors systemd.services."podman-openclaw".preStart = lib.mkBefore '' mkdir -p /var/lib/openclaw cp -f ${./openclaw-config.json} /var/lib/openclaw/openclaw.json chown -R 1000:1000 /var/lib/openclaw chmod -R u+rwX /var/lib/openclaw ${lib.optionalString cfg.superpowers.enable '' # Setup openclaw-superpowers mkdir -p /var/lib/openclaw/extensions mkdir -p /var/lib/openclaw/skill-state ln -sfT /home/node/superpowers-src/skills /var/lib/openclaw/extensions/superpowers # Replicate install.sh stateful skill registration REPO_SRC="${cfg.superpowers.src}" for skill_file in $REPO_SRC/skills/openclaw-native/*/SKILL.md; do [ -f "$skill_file" ] || continue skill_name=$(basename $(dirname "$skill_file")) # Check if stateful: true in frontmatter if sed -n '2,/^---$/p' "$skill_file" | grep -q '^stateful: *true'; then mkdir -p "/var/lib/openclaw/skill-state/$skill_name" if [ ! -f "/var/lib/openclaw/skill-state/$skill_name/state.yaml" ]; then echo "# Runtime state for $skill_name — managed by openclaw-superpowers" > "/var/lib/openclaw/skill-state/$skill_name/state.yaml" fi fi done chown -R 1000:1000 /var/lib/openclaw/extensions /var/lib/openclaw/skill-state ''} ''; # Optional: Install PyYAML inside the container on startup # We do this as a postStart or a simple background loop if needed, # but a better way is to ensure the image has it. # Since we can't easily change the image here, we'll try to run a one-time pip install. systemd.services."openclaw-superpowers-setup" = lib.mkIf cfg.superpowers.enable { description = "One-time setup for OpenClaw superpowers (PyYAML and Cron)"; after = [ "podman-openclaw.service" ]; wantedBy = [ "multi-user.target" ]; serviceConfig = { Type = "oneshot"; ExecStart = "${pkgs.podman}/bin/podman exec -u node openclaw bash -c '\ python3 -m pip install --user PyYAML && \ for skill_file in /home/node/superpowers-src/skills/openclaw-native/*/SKILL.md; do \ [ -f \"$skill_file\" ] || continue; \ skill_name=$(basename $(dirname \"$skill_file\")); \ fm_cron=$(sed -n \"2,/^---$/p\" \"$skill_file\" | grep \"^cron:\" | sed \"s/^cron: *//\" | tr -d \"'\\\"\"); \ if [ -n \"$fm_cron\" ]; then \ openclaw cron add \"$skill_name\" \"$fm_cron\" || echo \"Cron add failed for $skill_name\"; \ fi; \ done'"; RemainAfterExit = true; }; }; }; }