Ghostty Shell Integration in Distrobox/Unshare

Problem

Ghostty’s shell integration breaks inside distrobox containers and unshare environments. This is because the GHOSTTY_RESOURCES_DIR path points to the host filesystem, which isn’t accessible from within the isolated environment. (Yes, I want isolation without isolation…)

Solution

Cache the entire Ghostty resources directory locally and use the cached version for shell integration. The code creates ~/.cache/ghostty/resources-copy and only updates it when the source directory is newer, using a timestamp file to track the last sync.

# Ghostty shell integration for Bash with full resources caching
GHOSTTY_CACHE_DIR="$HOME/.cache/ghostty"
GHOSTTY_RESOURCES_CACHE="$GHOSTTY_CACHE_DIR/resources-copy"

if [ -n "$GHOSTTY_RESOURCES_DIR" ]; then
    # Create cache directory if it doesn't exist
    if [ ! -d "$GHOSTTY_CACHE_DIR" ]; then
        mkdir -p "$GHOSTTY_CACHE_DIR"
    fi
    
    # Copy entire resources directory if source is newer or cache doesn't exist
    if [ -d "$GHOSTTY_RESOURCES_DIR" ]; then
        GHOSTTY_SYNC_TIMESTAMP="$GHOSTTY_CACHE_DIR/last-resource-sync"
        NEEDS_SYNC=false
        
        # Check if cache doesn't exist or if source is newer than our last sync
        if [ ! -d "$GHOSTTY_RESOURCES_CACHE" ] || [ ! -f "$GHOSTTY_SYNC_TIMESTAMP" ]; then
            NEEDS_SYNC=true
        elif [ -n "$(find "$GHOSTTY_RESOURCES_DIR" -newer "$GHOSTTY_SYNC_TIMESTAMP" -print -quit 2>/dev/null)" ]; then
            NEEDS_SYNC=true
        fi
        
        if [ "$NEEDS_SYNC" = true ]; then
            # Remove old cache and copy fresh
            rm -rf "$GHOSTTY_RESOURCES_CACHE"
            cp -r "$GHOSTTY_RESOURCES_DIR" "$GHOSTTY_RESOURCES_CACHE"
            # Update sync timestamp
            touch "$GHOSTTY_SYNC_TIMESTAMP"
        fi
    fi
    
    # Clean up variables
    unset GHOSTTY_SYNC_TIMESTAMP NEEDS_SYNC
fi

# Source the cached integration script if it exists, using cached resources
if [ -f "$GHOSTTY_RESOURCES_CACHE/shell-integration/bash/ghostty.bash" ]; then
    export GHOSTTY_RESOURCES_DIR="$GHOSTTY_RESOURCES_CACHE"
    builtin source "$GHOSTTY_RESOURCES_CACHE/shell-integration/bash/ghostty.bash"
fi

unset GHOSTTY_CACHE_DIR GHOSTTY_RESOURCES_CACHE

The key insight is redirecting GHOSTTY_RESOURCES_DIR to point to the cached copy before sourcing the integration script. This ensures all resource lookups use the local cache instead of the inaccessible host paths.

Why Copy Everything?

The Ghostty resources directory is small (~2 or 3 MB), so copying the entire directory ensures all dependencies are available and eliminates potential path issues. This approach is more reliable than trying to selectively copy files. Because why make things complicated when you can just copy everything and call it a day?

Usage

Add this code to your .bashrc or shell configuration file. Place it high up in the file, as per normal Ghostty instructions. It will automatically maintain the cache and provide working Ghostty integration in any environment where the original resources aren’t accessible.

Of course, for this to work, you should make sure that where you cache it, it is accessible in your environment. Otherwise you’re just moving the problem around, which I am not even sure we can call progress.

Created: 2025-08-09 -- Updated: 2025-08-09