diff --git a/mastodon/data/db/.gitkeep b/mastodon/data/db/.gitkeep new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/mastodon/data/db/.gitkeep @@ -0,0 +1 @@ + diff --git a/mastodon/data/redis/.gitkeep b/mastodon/data/redis/.gitkeep new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/mastodon/data/redis/.gitkeep @@ -0,0 +1 @@ + diff --git a/mastodon/data/system/.gitkeep b/mastodon/data/system/.gitkeep new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/mastodon/data/system/.gitkeep @@ -0,0 +1 @@ + diff --git a/mastodon/docker-compose.yml b/mastodon/docker-compose.yml new file mode 100644 index 000000000..1933e8947 --- /dev/null +++ b/mastodon/docker-compose.yml @@ -0,0 +1,129 @@ +x-mastodon-image: &mastodon_image ghcr.io/mastodon/mastodon:v4.7.6@sha256:8d46d6d8ac86f16f00a8944e3ba8cd8d1e3de23b33a59354c751e071b58e18a9 +x-mastodon-streaming-image: &mastodon_streaming_image ghcr.io/mastodon/mastodon-streaming:v4.7.6@sha256:5b95d22c22f1d101fd2eb5dac62ad0ca2a93a5b60e410dc071ae645e98cc73e6 + +x-mastodon-environment: &mastodon_environment + DB_HOST: db + DB_PASS: ${APP_MASTODON_DB_PASSWORD} + REDIS_HOST: redis + REDIS_PASSWORD: ${APP_MASTODON_REDIS_PASSWORD} + SECRET_KEY_BASE: ${APP_MASTODON_SECRET_KEY_BASE} + ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY: ${APP_MASTODON_AR_DETERMINISTIC_KEY} + ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT: ${APP_MASTODON_AR_KEY_DERIVATION_SALT} + ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY: ${APP_MASTODON_AR_PRIMARY_KEY} + +# Web push keys and the default sender address, written by hooks/pre-start +x-mastodon-env-file: &mastodon_env_file + - path: ${APP_DATA_DIR}/mastodon.env + required: false + +services: + app_proxy: + environment: + APP_HOST: mastodon_proxy_1 + APP_PORT: 80 + # Federation, public profiles and Mastodon's own apps need the server to be public + PROXY_AUTH_ADD: "false" + + proxy: + image: nginx:1.30.3-alpine3.23@sha256:0d3b80406a13a767339fbe2f41406d6c7da727ab89cf8fae399e81f780f814d1 + restart: on-failure + volumes: + - ${APP_DATA_DIR}/nginx.conf:/etc/nginx/conf.d/default.conf:ro + depends_on: + web: + condition: service_started + streaming: + condition: service_started + + web: + image: *mastodon_image + user: "991:991" + restart: on-failure + stop_grace_period: 1m + command: ["bundle", "exec", "puma", "-C", "config/puma.rb"] + env_file: *mastodon_env_file + environment: + <<: *mastodon_environment + volumes: + - ${APP_DATA_DIR}/data/system:/mastodon/public/system + depends_on: + migrate: + condition: service_completed_successfully + + streaming: + image: *mastodon_streaming_image + user: "991:991" + restart: on-failure + init: true + stop_grace_period: 1m + env_file: *mastodon_env_file + environment: + <<: *mastodon_environment + depends_on: + db: + condition: service_healthy + redis: + condition: service_healthy + + sidekiq: + image: *mastodon_image + user: "991:991" + restart: on-failure + stop_grace_period: 1m + command: ["bundle", "exec", "sidekiq"] + env_file: *mastodon_env_file + environment: + <<: *mastodon_environment + volumes: + - ${APP_DATA_DIR}/data/system:/mastodon/public/system + depends_on: + migrate: + condition: service_completed_successfully + + # Creates the database and the owner account on first start, and runs + # Mastodon's database migrations after updates + migrate: + image: *mastodon_image + user: "991:991" + restart: on-failure + command: ["bundle", "exec", "rails", "db:prepare"] + env_file: *mastodon_env_file + environment: + <<: *mastodon_environment + UMBREL_OWNER_PASSWORD: ${APP_PASSWORD} + volumes: + - ${APP_DATA_DIR}/umbrel-owner.rb:/mastodon/db/seeds/99_umbrel_owner.rb:ro + depends_on: + db: + condition: service_healthy + redis: + condition: service_healthy + + db: + image: postgres:17.12-alpine@sha256:f9cf3e1f0066f0eb0c48707d864f0ac5941e9ff454a072077d97a4d278d07d73 + user: "1000:1000" + restart: on-failure + environment: + POSTGRES_USER: mastodon + POSTGRES_PASSWORD: ${APP_MASTODON_DB_PASSWORD} + POSTGRES_DB: mastodon_production + volumes: + - ${APP_DATA_DIR}/data/db:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U mastodon -d mastodon_production"] + interval: 10s + timeout: 5s + retries: 10 + + redis: + image: redis:8.4.8-alpine@sha256:ff72f74e4f471df79ed920aa8bbc35efe72ff8a1f8acdc50aad5dbb9b14a4034 + user: "1000:1000" + restart: on-failure + command: ["redis-server", "--save", "60", "1", "--loglevel", "warning", "--requirepass", "${APP_MASTODON_REDIS_PASSWORD}"] + volumes: + - ${APP_DATA_DIR}/data/redis:/data + healthcheck: + test: ["CMD-SHELL", "redis-cli --no-auth-warning -a ${APP_MASTODON_REDIS_PASSWORD} ping | grep PONG"] + interval: 10s + timeout: 5s + retries: 10 diff --git a/mastodon/exports.sh b/mastodon/exports.sh new file mode 100644 index 000000000..145709d56 --- /dev/null +++ b/mastodon/exports.sh @@ -0,0 +1,6 @@ +export APP_MASTODON_DB_PASSWORD=$(derive_entropy "env-${app_entropy_identifier}-DB_PASSWORD") +export APP_MASTODON_REDIS_PASSWORD=$(derive_entropy "env-${app_entropy_identifier}-REDIS_PASSWORD") +export APP_MASTODON_SECRET_KEY_BASE=$(derive_entropy "env-${app_entropy_identifier}-SECRET_KEY_BASE") +export APP_MASTODON_AR_DETERMINISTIC_KEY=$(derive_entropy "env-${app_entropy_identifier}-AR_DETERMINISTIC_KEY") +export APP_MASTODON_AR_KEY_DERIVATION_SALT=$(derive_entropy "env-${app_entropy_identifier}-AR_KEY_DERIVATION_SALT") +export APP_MASTODON_AR_PRIMARY_KEY=$(derive_entropy "env-${app_entropy_identifier}-AR_PRIMARY_KEY") diff --git a/mastodon/hooks/pre-start b/mastodon/hooks/pre-start new file mode 100755 index 000000000..b43897a43 --- /dev/null +++ b/mastodon/hooks/pre-start @@ -0,0 +1,100 @@ +#!/usr/bin/env bash +set -euo pipefail + +APP_DIR="$(readlink -f "$(dirname "${BASH_SOURCE[0]}")/..")" +ENV_FILE="${APP_DIR}/mastodon.env" +NGINX_CONFIG_FILE="${APP_DIR}/nginx.conf" +MASTODON_IMAGE="ghcr.io/mastodon/mastodon:v4.7.6@sha256:8d46d6d8ac86f16f00a8944e3ba8cd8d1e3de23b33a59354c751e071b58e18a9" + +# Mastodon runs as user 991 and saves uploads here +chown 991:991 "${APP_DIR}/data/system" + +# LOCAL_DOMAIN and WEB_DOMAIN are app settings, which only reach the containers +compose_config="$("${UMBREL_ROOT}/scripts/app" compose "${APP_ID}" config --format json)" +local_domain="$(jq -r '.services.web.environment.LOCAL_DOMAIN // empty' <<< "${compose_config}")" +web_domain="$(jq -r '.services.web.environment.WEB_DOMAIN // empty' <<< "${compose_config}")" +local_domain="${local_domain,,}" +web_domain="${web_domain,,}" +web_domain="${web_domain:-${local_domain}}" + +# Web push keys are a key pair, so unlike the secrets in exports.sh they +# can't be derived. Generate them once and keep them. +vapid_keys="$(grep '^VAPID_' "${ENV_FILE}" 2>/dev/null || true)" +if [[ "$(grep -c . <<< "${vapid_keys}")" != 2 ]]; then + vapid_keys="$(docker run --rm --entrypoint bundle "${MASTODON_IMAGE}" exec ruby -rwebpush \ + -e 'k = Webpush.generate_key; puts "VAPID_PRIVATE_KEY=#{k.private_key}", "VAPID_PUBLIC_KEY=#{k.public_key}"')" || vapid_keys="" +fi + +# Mastodon's default sender is notifications@localhost. The app setting overrides this. +smtp_from="" +if [[ -n "${local_domain}" ]]; then + smtp_from="SMTP_FROM_ADDRESS=notifications@${local_domain}" +fi + +tmp_file="$(mktemp "${ENV_FILE}.XXXXXX")" +printf '%s\n' "${vapid_keys}" "${smtp_from}" > "${tmp_file}" +chmod 600 "${tmp_file}" +mv "${tmp_file}" "${ENV_FILE}" + +# Until a public domain is set, show how to set one instead of Mastodon +if [[ ! "${local_domain}" =~ ^[a-z0-9-]+(\.[a-z0-9-]+)+$ || ! "${web_domain}" =~ ^[a-z0-9-]+(\.[a-z0-9-]+)+$ || "${local_domain}" == *.local ]]; then + cat > "${NGINX_CONFIG_FILE}" <<'NGINX' +server { + listen 80 default_server; + + location / { + default_type text/html; + add_header Cache-Control "no-store" always; + return 200 'Set up Mastodon

Mastodon needs a public domain

  1. Make a domain, such as social.example.com, reach this app over HTTPS, for example with the Cloudflare Tunnel app.
  2. In Umbrel, open the settings for Mastodon and set LOCAL_DOMAIN to that domain, without https://.
  3. Mastodon restarts, which can take a few minutes. Then open your domain and sign in with the email and password shown in Umbrel.

The domain becomes part of every username on your server and cannot be changed later.

'; + } +} +NGINX + exit 0 +fi + +server_names="${local_domain}" +if [[ "${web_domain}" != "${local_domain}" ]]; then + server_names="${local_domain} ${web_domain}" +fi + +cat > "${NGINX_CONFIG_FILE}" <- + Required. Your server's public domain without https://, such as social.example.com. It must reach this app over HTTPS (for example with Cloudflare Tunnel) and can't be changed later. + - name: WEB_DOMAIN + services: + - web + - streaming + - sidekiq + - migrate + note: >- + Optional. Only needed if your Mastodon website runs on a different domain than your usernames, such as social.example.com for usernames ending in @example.com. Leave empty to use the domain above. Like the domain above, it can't be changed later. + - name: SINGLE_USER_MODE + services: + - web + - sidekiq + default: "false" + options: + - "false" + - "true" + note: >- + Turn on for a personal server just for you. Sign-ups are closed and visitors land on your profile. + - name: SMTP_SERVER + services: + - web + - sidekiq + note: >- + Optional. SMTP server host name, such as smtp.example.com. Needed for account confirmation, password reset and notification emails. + - name: SMTP_PORT + services: + - web + - sidekiq + default: "587" + note: >- + SMTP server port. Use 587 for most providers. For providers that require an encrypted connection from the start, use 465 and turn on SMTP_TLS. + - name: SMTP_TLS + services: + - web + - sidekiq + default: "false" + options: + - "false" + - "true" + note: >- + Turn on only when using port 465. + - name: SMTP_LOGIN + services: + - web + - sidekiq + note: >- + SMTP username. + - name: SMTP_PASSWORD + services: + - web + - sidekiq + note: >- + SMTP password or app password. + - name: SMTP_FROM_ADDRESS + services: + - web + - sidekiq + note: >- + Sender address, such as notifications@social.example.com. Use an address your SMTP provider lets you send from. Defaults to notifications@ followed by your domain. +category: social +name: Mastodon +version: "4.7.6" +tagline: Self-hosted social networking +description: >- + ⚠️ Mastodon is a public social server, so it needs a domain of your own that reaches your Umbrel over HTTPS. It's open to the internet and doesn't use your Umbrel login. + + + To set it up: + + + 1. Point a domain, such as social.example.com, at Mastodon over HTTPS. The Cloudflare Tunnel app is the easiest way to do this. + + 2. Right-click Mastodon on your home screen, open Settings, and under Advanced > Environment variables set LOCAL_DOMAIN to your domain. Choose it carefully: it becomes part of every username on your server and can't be changed later. + + 3. Mastodon restarts and sets itself up, which takes a few minutes. Then open your domain and sign in with the email and password shown in Umbrel. + + + To send sign-up and notification emails, add your email provider's SMTP details in the same settings. + + + Mastodon is a decentralized social network built on the open ActivityPub protocol. Host your own community, publish posts, and follow people on Mastodon and other compatible servers across the fediverse, all while keeping control of your social data. + + + This Umbrel package runs the official Mastodon server with PostgreSQL, Redis, Sidekiq and the streaming service. +releaseNotes: "" +developer: Mastodon gGmbH +website: https://joinmastodon.org +dependencies: [] +repo: https://github.com/mastodon/mastodon +support: https://github.com/mastodon/mastodon/discussions +port: 8388 +gallery: + - 1.webp + - 2.webp + - 3.webp + - 4.webp +path: "" +defaultUsername: "umbrel@umbrel.local" +deterministicPassword: true +submitter: caydyan +submission: https://github.com/getumbrel/umbrel-apps/pull/5734 diff --git a/mastodon/umbrel-owner.rb b/mastodon/umbrel-owner.rb new file mode 100644 index 000000000..dc5e862df --- /dev/null +++ b/mastodon/umbrel-owner.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +# Mounted into db/seeds, so Mastodon runs it once when it creates the +# database. Creates the owner account shown in Umbrel. +password = ENV.fetch('UMBREL_OWNER_PASSWORD', '') + +if password.present? + account = Account.where(username: 'umbrel', domain: nil).first_or_initialize(username: 'umbrel') + account.save(validate: false) + + # Validation is skipped because Mastodon can't look up mail servers for umbrel.local + user = User.where(email: 'umbrel@umbrel.local').first_or_initialize( + email: 'umbrel@umbrel.local', + password: password, + password_confirmation: password, + confirmed_at: Time.now.utc, + role: UserRole.find_by(name: 'Owner'), + account: account, + agreement: true, + approved: true, + bypass_registration_checks: true + ) + user.save!(validate: false) + # Mastodon resets approved on create when sign-ups are closed + user.approve! +end