Mastodon: Difference between revisions

From NixOS Wiki
imported>Erictapen
(Specified that there is only one supported database for Mastodon)
imported>LeSuisse
(Add instructions to setup a minimal Mastodon instance)
Line 1: Line 1:
== Setup your own personal Mastodon instance on a dedicated server ==
The <service>services.mastodon</service> service can be used to setup a Mastodon instance in [https://docs.joinmastodon.org/admin/config/#single_user_mode single user mode].
It will setup all the necessary services (PostgreSQL, Redis, Nginx...) and setup a valid certificate to be used for the HTTPS connection:
<syntaxHighlight lang=nix>
  security.acme = {
    acceptTerms = true;
    defaults.email = "<EMAIL TO USE FOR CORRESPONDENCE WITH Let's Encrypt>";
  };
  services.mastodon = {
    enable = true;
    localDomain = "social.example.com"; # Replace with your own domain
    configureNginx = true;
    smtp.fromAddress = "noreply@social.example.com"; # Email address used by Mastodon to send emails, replace with your own
    extraConfig.SINGLE_USER_MODE = "true";
  };
  networking.firewall.allowedTCPPorts = [ 80 443 ];
</syntaxHighlight>
You can then create your account using <code>tootctl</code>: <code>su - mastodon -s "$(which bash)" -c 'mastodon-env tootctl accounts create USERNAME --email=YOUR_EMAIL --confirmed --role=Owner'</code>
== Automatic backups ==
== Automatic backups ==



Revision as of 20:45, 20 December 2022

Setup your own personal Mastodon instance on a dedicated server

The <service>services.mastodon</service> service can be used to setup a Mastodon instance in single user mode.

It will setup all the necessary services (PostgreSQL, Redis, Nginx...) and setup a valid certificate to be used for the HTTPS connection:

  security.acme = {
    acceptTerms = true;
    defaults.email = "<EMAIL TO USE FOR CORRESPONDENCE WITH Let's Encrypt>";
  };
  services.mastodon = {
    enable = true;
    localDomain = "social.example.com"; # Replace with your own domain
    configureNginx = true;
    smtp.fromAddress = "noreply@social.example.com"; # Email address used by Mastodon to send emails, replace with your own
    extraConfig.SINGLE_USER_MODE = "true";
  };
  networking.firewall.allowedTCPPorts = [ 80 443 ];

You can then create your account using tootctl: su - mastodon -s "$(which bash)" -c 'mastodon-env tootctl accounts create USERNAME --email=YOUR_EMAIL --confirmed --role=Owner'

Automatic backups

Mastodon uses postgreSQL as database. Luckily, Nixpkgs offers a useful service, services.postgresqlBackup.enable.

Example settings, assuming you have the default database settings:

  services.postgresqlBackup = {
    enable = true;
    databases = [ "mastodon" ];
  };

Using Caddy as a server

Use the following template:

services = {
  caddy = {
    enable = true;
    virtualHosts = {
    
      # Don't forget to change the host!
      "<your-server-host>" = {
        extraConfig = ''
          handle_path /system/* {
              file_server * {
                  root /var/lib/mastodon/public-system
              }
          }

          handle /api/v1/streaming/* {
              reverse_proxy  unix//run/mastodon-streaming/streaming.socket
          }

          route * {
              file_server * {
              root ${pkgs.mastodon}/public
              pass_thru
              }
              reverse_proxy * unix//run/mastodon-web/web.socket
          }

          handle_errors {
              root * ${pkgs.mastodon}/public
              rewrite 500.html
              file_server
          }

          encode gzip

          header /* {
              Strict-Transport-Security "max-age=31536000;"
          }
          header /emoji/* Cache-Control "public, max-age=31536000, immutable"
          header /packs/* Cache-Control "public, max-age=31536000, immutable"
          header /system/accounts/avatars/* Cache-Control "public, max-age=31536000, immutable"
          header /system/media_attachments/files/* Cache-Control "public, max-age=31536000, immutable"
        '';
    };
  };
};

# Caddy requires file and socket access
users.users.caddy.extraGroups = [ "mastodon" ];

Hints for running in your local network for testing

If you get a Mastodon::HostValidationError when trying to federate with another ActivityPub instance in your local network you need to allow Mastodon to access local ip addresses in outgoing http (federation) requests. To do this set the following environment variable: ALLOWED_PRIVATE_ADDRESSES to a comma-separated list of allowed ip addresses with the format specified in https://ruby-doc.org/stdlib-2.5.1/libdoc/ipaddr/rdoc/IPAddr.html. This is also documented in the Mastodon admin guide[1].