Skip to content

Instantly share code, notes, and snippets.

@phanirithvij
Last active January 29, 2025 16:06
Show Gist options
  • Save phanirithvij/bedc6bf9a5cf15f05daadb67925d7912 to your computer and use it in GitHub Desktop.
Save phanirithvij/bedc6bf9a5cf15f05daadb67925d7912 to your computer and use it in GitHub Desktop.
debug_colab, basic tmux server for google colab, alternative to embedded colab-xterm
  • It relies on free plan of cloudflare tunnels. You can ofcourse adapt it to use ngrok, frp etc.
  • It depends on nix, because nix is simply the best option to install software on linux.
  • Not any better than xterm, and because it opens in a new tab, might even kill the colab

So overall use at your own risk.

Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/phanirithvij/bedc6bf9a5cf15f05daadb67925d7912/debug_colab.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "MaCMynN-Ykce",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "cdfbdf8b-9cf9-4c3b-a2af-b5db139d57af"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\u001b[1minfo:\u001b[0m downloading installer \u001b[4mhttps://install.determinate.systems/nix/tag/v0.34.0/nix-installer-x86_64-linux\u001b[0m\n",
"\u001b[32m INFO\u001b[0m nix-installer v0.34.0\n",
"\u001b[32m INFO\u001b[0m Step: Create directory `/nix`\n",
"\u001b[32m INFO\u001b[0m Step: Provision Nix\n",
"\u001b[32m INFO\u001b[0m Step: Create build users (UID 30001-30032) and group (GID 30000)\n",
"\u001b[32m INFO\u001b[0m Step: Configure Nix\n",
"\u001b[32m INFO\u001b[0m Step: Create directory `/etc/tmpfiles.d`\n",
"\u001b[32m INFO\u001b[0m Step: Configure upstream Nix daemon service\n",
"\u001b[32m INFO\u001b[0m Step: Remove directory `/nix/temp-install-dir`\n",
"\u001b[1m\u001b[32mNix was installed successfully!\u001b[39m\u001b[0m\n",
"To get started using Nix, open a new shell or run `\u001b[1m. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh\u001b[0m`\n",
"\n"
]
}
],
"source": [
"# Install nix\n",
"!curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix -OJ\n",
"!chmod +x ./nix; ./nix install --no-confirm;\n",
"\n",
"# Add nix to PATH\n",
"# https://github.com/googlecolab/colabtools/issues/2659#issuecomment-1061296670\n",
"import os\n",
"os.environ['PATH'] += ':/root/.nix-profile/bin'"
]
},
{
"cell_type": "code",
"source": [
"!nix profile install nixpkgs#{tmux,cloudflared,ttyd,nettools}"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "FzyzqVyxb7Il",
"outputId": "7dd3cdaf-9b12-4d71-ae78-614afd05e2b2"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\u001b[K"
]
}
]
},
{
"cell_type": "code",
"source": [
"%%bash\n",
"kill -9 $(ps aux | grep 'tmux new-session' | head -n1 | awk '{printf $2}')\n",
"killall ttyd\n",
"killall cloudflared\n",
"env TERM=\"screen-256color\" tmux new-session -d\n",
"tmux send-keys -t 0 'ttyd -c \"admin:admin\" -i 0.0.0.0 -p 8391 -a -W tmux' C-m\n",
"cloudflared tunnel --url http://localhost:8391 > mylog.txt 2>&1 &"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Lv4s61GGS0_Y",
"outputId": "596dacb4-1ead-40b1-e597-385372ebebd4"
},
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"bash: line 1: kill: (1899) - No such process\n",
"ttyd: no process found\n",
"cloudflared: no process found\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"%%bash\n",
"sleep 6\n",
"url=$(head -n 10 mylog.txt | grep -o 'https://.*' | tail -n 1 | cut -d' ' -f1)\n",
"echo \"https://admin:admin@\"$(echo $url | cut -d'/' -f3)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "yyFUtnvzS6GI",
"outputId": "b1127222-7163-4038-8cf8-523b4ce72164"
},
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"https://admin:[email protected]\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# TO DEBUG\n",
"!netstat -tuplen |grep 8391\n",
"!ps aux | grep tmux | head -n1\n",
"!ps aux | grep 'tmux new-session' | head -n1 | awk '{printf $2}'"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "kgVdoFYmYvGU",
"outputId": "f981791b-7dd6-4fcc-fff6-69bdf530d538"
},
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"tcp 0 0 0.0.0.0:8391 0.0.0.0:* LISTEN 0 52037 1926/ttyd \n",
"root 1906 0.0 0.0 8728 3744 ? Ss 14:22 0:00 tmux new-session -d\n",
"1906"
]
}
]
},
{
"cell_type": "code",
"source": [
"!nix profile install nixpkgs#{lf,lazygit,ripgrep,duf,gdu,btop,neovim,viddy,fzf}"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "kKku1sKMbFQF",
"outputId": "c747a0c6-a728-4c63-e216-0e5ee02d73bc"
},
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\u001b[K"
]
}
]
}
],
"metadata": {
"colab": {
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment