Why?

我一直没有使用社交媒体的习惯,像是Twitter, Weibo之类。但是经常看到大家在聊天里分享链接,难免会想尝试一下。 我很喜欢Misskey的UI,好看。但是移动客户端很重要,如果没有的话就基本等于不会习惯性的访问。 简单的尝试之后 Mastodon 满足了大多数我的要求。

NixOS configuration

在NixOS上部署很简单,只需要将DNS解析到相应的机器并填好设置,没有其他的步骤。 需要做的域名只有 WEB_DOMAIN

services.mastodon = {
  enable = true;
  streamingProcesses = 1; # 推荐处理器核心-1
  localDomain = "ocfox.me";
  smtp.fromAddress = "server@mastodon.ocfox.me";
  extraConfig = {
    WEB_DOMAIN = "mastodon.ocfox.me";
    SINGLE_USER_MODE = "true";
  };
};

在最好的情况下,localDomain应该和WEB_DOMAIN保持一致,也就是并不需要单独设置WEB_DOMAIN。 但上面已经跑了别的东西,比如 homepage 之类的——mastodon 允许这么做。 只需要在 localDomain 上提供 webfinger 就好。

接下来是 Caddy 的配置。

  # 保证caddy具有mastodon web socket的读写权限
  systemd.services.caddy.serviceConfig.SupplementaryGroups = [ "mastodon" ];

  services.caddy = {
    enable = true;
    email = "<email for acme>";
    virtualHosts = {
      "<WEB_DOMAIN>" = {
        extraConfig = ''
          handle_path /system/* {
              file_server * {
                  root /var/lib/mastodon/public-system
              }
          }

          handle /api/v1/streaming/* {
              reverse_proxy  unix//run/mastodon-streaming/streaming-1.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;"
          }
        '';
    };
  };

nixos-rebuild之后,就可以通过域名访问 mastodon server 了。 接下来需要为自己创建一个用户。

mastodon-tootctl accounts create USERNAME --email=YOUR_EMAIL --confirmed --role=Owner

生成的密码会打印在下面。

Something needed

如果使用了不同的localDomain,那么还需要在 localDomain 上提供webfinger。(只是单用户或者几个用户的话可以这么做), 不然请参考 manual Using a different domain name for Mastodon and the users it serves

https://<WEB_DOMAIN>/.well-known/webfinger?resource=acct:<USERNAME>@<localDomain>

提供的json 复制到下面的路径就好。

https://<localDomain>/.well-known/webfinger

如果是github pages并且部署后无法访问这个路径,可以在根目录添加空白文件.nojekyll解决。

如果之前在其他 mastodon 兼容的服务器注册过账户,也可以简单地迁移到新账户。 在设置里找到账户->从其它账号迁入,接下来按照说明做就好,迁移完成后旧账户就不能正常使用了(大概)。