Skip to content

Instantly share code, notes, and snippets.

@hitalin
Created November 19, 2024 22:49
Show Gist options
  • Save hitalin/2db19f4add97dc649858ecfa99e5802c to your computer and use it in GitHub Desktop.
Save hitalin/2db19f4add97dc649858ecfa99e5802c to your computer and use it in GitHub Desktop.
set misskey development environment by nix flake
{
description = "Misskey development environment";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
misskeyConfig = builtins.toFile "default.yml" ''
url: "http://localhost:3000/"
port: 3000
db:
host: "localhost"
port: 5433
user: "taka"
pass: ""
redis:
host: "localhost"
port: 6379
id: "aid"
vite:
port: 5173
embedPort: 5174
'';
# Redis config remains the same...
redisConfig = builtins.toFile "redis.conf" ''
bind 0.0.0.0
port 6379
daemonize yes
dir ./data/redis
# RDBスナップショットを完全に無効化
save ""
appendonly no
stop-writes-on-bgsave-error no
# メモリ設定
maxmemory 512mb
maxmemory-policy allkeys-lru
# その他の設定
databases 16
tcp-keepalive 300
supervised no
loglevel notice
logfile ""
always-show-logo no
'';
misskeyEnv = pkgs.writeShellScriptBin "misskey" ''
# ... existing environment variables ...
export NODE_ENV="development"
export VITE_PORT="5173"
export EMBED_VITE_PORT="5174"
export PORT="3000"
# 引数が無い場合はヘルプを表示
show_help() {
echo "Misskey Development Environment"
echo
echo "Usage: misskey <command>"
echo
echo "Commands:"
echo " setup - Initial setup"
echo " start - Start development server"
echo " stop - Stop all services"
echo " clean - Clean environment"
echo " reset - Reset and setup again"
echo " psql - Connect to database"
echo " status - Check services status"
echo " logs - View logs (postgres|all)"
exit 1
}
export PGDATA="$(pwd)/data/postgres"
export PGHOST="localhost"
export PGUSER="taka"
export PGDATABASE="misskey"
export PGPORT="5433"
export LC_ALL="C"
export LANG="C"
export PATH="${pkgs.nodejs_20}/bin:${pkgs.postgresql_15}/bin:${pkgs.redis}/bin:$PATH"
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'
log() { echo -e "''${BLUE}[Misskey]''${NC} $1"; }
success() { echo -e "''${GREEN}✓''${NC} $1"; }
error() { echo -e "''${RED}✗''${NC} $1"; return 1; }
setup_postgres() {
log "Initializing PostgreSQL..."
pg_ctl -D "$PGDATA" stop 2>/dev/null || true
sleep 2
rm -rf "$PGDATA"
mkdir -p "$PGDATA"
initdb -D "$PGDATA" --auth=trust --no-locale --encoding=UTF8 || error "Failed to initialize PostgreSQL"
cat > "$PGDATA/postgresql.conf" << EOC
listen_addresses = 'localhost'
port = 5433
unix_socket_directories = '$PGDATA'
max_connections = 100
shared_buffers = 128MB
dynamic_shared_memory_type = posix
max_wal_size = 1GB
min_wal_size = 80MB
log_destination = 'stderr'
logging_collector = on
log_directory = 'log'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
log_rotation_age = 1d
log_rotation_size = 10MB
log_line_prefix = '%m [%p] '
log_timezone = 'UTC'
datestyle = 'iso, mdy'
timezone = 'UTC'
lc_messages = 'C'
lc_monetary = 'C'
lc_numeric = 'C'
lc_time = 'C'
default_text_search_config = 'pg_catalog.english'
EOC
cat > "$PGDATA/pg_hba.conf" << EOC
local all all trust
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
EOC
mkdir -p "$PGDATA/log"
pg_ctl -D "$PGDATA" -l "$PGDATA/log/postgresql.log" start || error "Failed to start PostgreSQL"
sleep 3
pg_isready -p 5433 || error "PostgreSQL is not responding"
createdb -p 5433 misskey || error "Failed to create database"
success "PostgreSQL initialized"
}
setup_redis() {
log "Initializing Redis..."
redis-cli shutdown 2>/dev/null || true
sleep 1
# データディレクトリのクリーンアップと作成
rm -rf "$(pwd)/data/redis"
mkdir -p "$(pwd)/data/redis"
chmod 777 "$(pwd)/data/redis"
# 設定ファイルをコピーして権限設定
cp ${redisConfig} "$(pwd)/data/redis/redis.conf"
chmod 644 "$(pwd)/data/redis/redis.conf"
# Redisを起動
redis-server "$(pwd)/data/redis/redis.conf" || error "Failed to start Redis"
sleep 2
# Redisへの接続を確認
if ! redis-cli ping > /dev/null 2>&1; then
error "Redis is not responding"
return 1
fi
# 永続化設定を確認
redis-cli config set save "" > /dev/null 2>&1
redis-cli config set appendonly no > /dev/null 2>&1
redis-cli config set stop-writes-on-bgsave-error no > /dev/null 2>&1
success "Redis initialized"
}
setup_config() {
log "Creating Misskey configuration..."
mkdir -p .config
cp ${misskeyConfig} .config/default.yml
success "Configuration created"
}
setup() {
log "Starting setup process..."
setup_postgres
setup_redis
setup_config
log "Installing dependencies..."
git submodule update --init || error "Failed to update git submodules"
pnpm install --frozen-lockfile || error "Failed to install dependencies"
log "Building Misskey..."
pnpm build || error "Build failed"
log "Running migrations..."
pnpm migrate || error "Migration failed"
success "Setup completed successfully"
}
start() {
log "Starting development server..."
if ! pg_isready -p 5433 >/dev/null 2>&1; then
pg_ctl -D "$PGDATA" -l "$PGDATA/log/postgresql.log" start
fi
if ! redis-cli ping >/dev/null 2>&1; then
redis-server --daemonize yes --dir "$(pwd)/data/redis"
fi
mkdir -p data/logs
# Run frontend and backend servers
log "Starting Vite development servers..."
(cd packages/frontend && pnpm dev) &
(cd packages/frontend-embed && pnpm dev) &
(cd packages/backend && pnpm dev)
}
# ... rest of the script remains exactly the same ...
stop() {
log "Stopping services..."
pg_ctl -D "$PGDATA" stop 2>/dev/null || true
redis-cli shutdown 2>/dev/null || true
success "Services stopped"
}
clean() {
log "Cleaning environment..."
stop
rm -rf data/postgres data/redis node_modules .config/default.yml
success "Environment cleaned"
}
# メインのコマンド処理
if [ $# -eq 0 ]; then
show_help
fi
case "''${1:-}" in
setup) setup ;;
start) start ;;
stop) stop ;;
clean) clean ;;
reset) clean && setup ;;
psql) psql -p 5433 misskey ;;
status)
pg_isready -p 5433 && echo "PostgreSQL is running" || echo "PostgreSQL is not running"
redis-cli ping >/dev/null 2>&1 && echo "Redis is running" || echo "Redis is not running"
;;
logs)
case "''${2:-all}" in
postgres) tail -f "$PGDATA/log/postgresql.log" ;;
*) tail -f data/logs/* ;;
esac
;;
*) show_help ;;
esac
'';
in
{
devShells.default = pkgs.mkShell {
packages = with pkgs; [
nodejs_20
pnpm
postgresql_15
redis
ffmpeg
python311
gcc
meilisearch
git
gnumake
misskeyEnv
];
shellHook = ''
echo "🚀 Welcome to Misskey development environment"
echo
echo "Type 'misskey' to see available commands"
echo
if [ ! -f .config/default.yml ]; then
echo "🔧 First time? Run 'misskey setup' to initialize the environment"
fi
trap 'misskey stop' EXIT
# Development environment
export NODE_ENV="development"
export VITE_PORT="5173"
export EMBED_VITE_PORT="5174"
export PORT="3000"
# Additional helpful vars
export DEBUG="*"
export NODE_OPTIONS="--max-old-space-size=4096"
export MISSKEY_TRACE="1"
'';
};
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment