aboutsummaryrefslogtreecommitdiff
path: root/nixops/modules/websites/tools/mastodon/default.nix
blob: 3740a49c60c4644e5fdcad618152f25147d440c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
{ lib, pkgs, config, myconfig, mylibs, ... }:
let
  mastodon = pkgs.callPackage ./mastodon.nix {
    inherit (mylibs) fetchedGithub;
    env = myconfig.env.tools.mastodon;
  };

  cfg = config.services.myWebsites.tools.mastodon;
in {
  options.services.myWebsites.tools.mastodon = {
    enable = lib.mkEnableOption "enable mastodon's website";
  };

  config = lib.mkIf cfg.enable {
    ids.uids.mastodon = myconfig.env.tools.mastodon.user.uid;
    ids.gids.mastodon = myconfig.env.tools.mastodon.user.gid;

    users.users.mastodon = {
      name = "mastodon";
      uid = config.ids.uids.mastodon;
      group = "mastodon";
      description = "Mastodon user";
      home = mastodon.railsRoot;
      useDefaultShell = true;
    };

    users.groups.mastodon.gid = config.ids.gids.mastodon;

    systemd.services.mastodon-streaming = {
      description = "Mastodon Streaming";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" "mastodon-web.service" ];

      environment.NODE_ENV = "production";
      environment.SOCKET = mastodon.nodeSocket;

      path = [ pkgs.nodejs pkgs.bashInteractive ];

      script = ''
        exec npm run start
      '';

      postStart = ''
        while [ ! -S $SOCKET ]; do
          sleep 0.5
        done
        chmod a+w $SOCKET
      '';

      postStop = ''
        rm $SOCKET
      '';

      serviceConfig = {
        User = "mastodon";
        EnvironmentFile = mastodon.config;
        PrivateTmp = true;
        Restart = "always";
        TimeoutSec = 15;
        Type = "simple";
        WorkingDirectory = mastodon.railsRoot;
      };

      unitConfig.RequiresMountsFor = mastodon.varDir;
    };

    systemd.services.mastodon-web = {
      description = "Mastodon Web app";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];

      environment.RAILS_ENV = "production";
      environment.BUNDLE_PATH = "${mastodon.gems}/lib/ruby/gems/2.5.0";
      environment.BUNDLE_GEMFILE = "${mastodon.gems.confFiles}/Gemfile";
      environment.SOCKET = mastodon.railsSocket;

      path = [ mastodon.gems mastodon.gems.ruby pkgs.file ];

      preStart = ''
        ./bin/bundle exec rails db:migrate
      '';

      script = ''
        exec ./bin/bundle exec puma -C config/puma.rb
      '';

      serviceConfig = {
        User = "mastodon";
        EnvironmentFile = mastodon.config;
        PrivateTmp = true;
        Restart = "always";
        TimeoutSec = 60;
        Type = "simple";
        WorkingDirectory = mastodon.railsRoot;
      };

      unitConfig.RequiresMountsFor = mastodon.varDir;
    };

    systemd.services.mastodon-sidekiq = {
      description = "Mastodon Sidekiq";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" "mastodon-web.service" ];

      environment.RAILS_ENV="production";
      environment.BUNDLE_PATH = "${mastodon.gems}/${mastodon.gems.ruby.gemPath}";
      environment.BUNDLE_GEMFILE = "${mastodon.gems.confFiles}/Gemfile";
      environment.DB_POOL="5";

      path = [ mastodon.gems mastodon.gems.ruby pkgs.imagemagick pkgs.ffmpeg pkgs.file ];

      script = ''
        exec ./bin/bundle exec sidekiq -c 5 -q default -q mailers -q pull -q push
      '';

      serviceConfig = {
        User = "mastodon";
        EnvironmentFile = mastodon.config;
        PrivateTmp = true;
        Restart = "always";
        TimeoutSec = 15;
        Type = "simple";
        WorkingDirectory = mastodon.railsRoot;
      };

      unitConfig.RequiresMountsFor = mastodon.varDir;
    };

    system.activationScripts.mastodon = {
      deps = [ "users" ];
      text = ''
      install -m 0755 -o mastodon -g mastodon -d ${mastodon.socketsDir}
      install -m 0755 -o mastodon -g mastodon -d ${mastodon.varDir} ${mastodon.varDir}/tmp/cache
      '';
    };

    services.myWebsites.tools.modules = [
      "headers" "proxy" "proxy_wstunnel" "proxy_http" "proxy_balancer"
      "lbmethod_byrequests" "lbmethod_bytraffic" "lbmethod_bybusyness" "lbmethod_heartbeat"
    ];
    security.acme.certs."eldiron".extraDomains."mastodon.immae.eu" = null;
    services.myWebsites.tools.vhostConfs.mastodon = {
      certName    = "eldiron";
      hosts       = ["mastodon.immae.eu" ];
      root        = "${mastodon.railsRoot}/public/";
      extraConfig = [ ''
        Header always set Referrer-Policy "strict-origin-when-cross-origin"
        Header always set Strict-Transport-Security "max-age=31536000"

        <LocationMatch "^/(assets|avatars|emoji|headers|packs|sounds|system)>
          Header always set Cache-Control "public, max-age=31536000, immutable"
          Require all granted
        </LocationMatch>

        ProxyPreserveHost On
        RequestHeader set X-Forwarded-Proto "https"

        RewriteEngine On

        ProxyPass /500.html !
        ProxyPass /sw.js !
        ProxyPass /embed.js !
        ProxyPass /robots.txt !
        ProxyPass /manifest.json !
        ProxyPass /browserconfig.xml !
        ProxyPass /mask-icon.svg !
        ProxyPassMatch ^(/.*\.(png|ico|gif)$) !
        ProxyPassMatch ^/(assets|avatars|emoji|headers|packs|sounds|system|.well-known/acme-challenge) !

        ProxyPassMatch /api/v1/streaming/(.+)$ balancer://node_servers_http/api/v1/streaming/$1
        ProxyPass /api/v1/streaming/ balancer://node_servers/
        ProxyPassReverse /api/v1/streaming/ balancer://node_servers/
        ProxyPass / balancer://puma_servers/
        ProxyPassReverse / balancer://puma_servers/

        <Proxy balancer://puma_servers>
            BalancerMember unix://${mastodon.railsSocket}|http://
        </Proxy>

        <Proxy balancer://node_servers>
            BalancerMember unix://${mastodon.nodeSocket}|ws://localhost
        </Proxy>

        <Proxy balancer://node_servers_http>
            BalancerMember unix://${mastodon.nodeSocket}|http://localhost
        </Proxy>

        Alias /system ${mastodon.varDir}

        <Directory ${mastodon.varDir}>
          Require all granted
          Options -MultiViews
        </Directory>

        <Directory ${mastodon.railsRoot}/public/>
          Require all granted
          Options -MultiViews +FollowSymlinks
        </Directory>

        ErrorDocument 500 /500.html
        ErrorDocument 501 /500.html
        ErrorDocument 502 /500.html
        ErrorDocument 503 /500.html
        ErrorDocument 504 /500.html
      '' ];
    };
  };
}