2019-05-02 15:51:12 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.mastodon;
|
|
|
|
# We only want to create a database if we're actually going to connect to it.
|
|
|
|
databaseActuallyCreateLocally = cfg.database.createLocally && cfg.database.host == "/run/postgresql";
|
|
|
|
|
|
|
|
env = {
|
|
|
|
RAILS_ENV = "production";
|
|
|
|
NODE_ENV = "production";
|
|
|
|
|
2022-03-26 16:35:32 +00:00
|
|
|
LD_PRELOAD = "${pkgs.jemalloc}/lib/libjemalloc.so";
|
|
|
|
|
2021-09-04 10:07:04 +01:00
|
|
|
# mastodon-web concurrency.
|
|
|
|
WEB_CONCURRENCY = toString cfg.webProcesses;
|
|
|
|
MAX_THREADS = toString cfg.webThreads;
|
|
|
|
|
|
|
|
# mastodon-streaming concurrency.
|
|
|
|
STREAMING_CLUSTER_NUM = toString cfg.streamingProcesses;
|
|
|
|
|
2019-05-02 15:51:12 +01:00
|
|
|
DB_USER = cfg.database.user;
|
|
|
|
|
|
|
|
REDIS_HOST = cfg.redis.host;
|
|
|
|
REDIS_PORT = toString(cfg.redis.port);
|
|
|
|
DB_HOST = cfg.database.host;
|
|
|
|
DB_PORT = toString(cfg.database.port);
|
|
|
|
DB_NAME = cfg.database.name;
|
|
|
|
LOCAL_DOMAIN = cfg.localDomain;
|
|
|
|
SMTP_SERVER = cfg.smtp.host;
|
|
|
|
SMTP_PORT = toString(cfg.smtp.port);
|
|
|
|
SMTP_FROM_ADDRESS = cfg.smtp.fromAddress;
|
|
|
|
PAPERCLIP_ROOT_PATH = "/var/lib/mastodon/public-system";
|
|
|
|
PAPERCLIP_ROOT_URL = "/system";
|
|
|
|
ES_ENABLED = if (cfg.elasticsearch.host != null) then "true" else "false";
|
|
|
|
ES_HOST = cfg.elasticsearch.host;
|
|
|
|
ES_PORT = toString(cfg.elasticsearch.port);
|
2021-02-13 15:37:26 +00:00
|
|
|
|
|
|
|
TRUSTED_PROXY_IP = cfg.trustedProxy;
|
2019-05-02 15:51:12 +01:00
|
|
|
}
|
|
|
|
// (if cfg.smtp.authenticate then { SMTP_LOGIN = cfg.smtp.user; } else {})
|
|
|
|
// cfg.extraConfig;
|
|
|
|
|
2021-05-12 09:34:26 +01:00
|
|
|
systemCallsList = [ "@cpu-emulation" "@debug" "@keyring" "@ipc" "@mount" "@obsolete" "@privileged" "@setuid" ];
|
2021-04-24 13:43:26 +01:00
|
|
|
|
2021-02-13 18:47:41 +00:00
|
|
|
cfgService = {
|
|
|
|
# User and group
|
|
|
|
User = cfg.user;
|
|
|
|
Group = cfg.group;
|
|
|
|
# State directory and mode
|
|
|
|
StateDirectory = "mastodon";
|
|
|
|
StateDirectoryMode = "0750";
|
|
|
|
# Logs directory and mode
|
|
|
|
LogsDirectory = "mastodon";
|
|
|
|
LogsDirectoryMode = "0750";
|
2021-05-12 09:22:44 +01:00
|
|
|
# Proc filesystem
|
|
|
|
ProcSubset = "pid";
|
|
|
|
ProtectProc = "invisible";
|
2021-02-13 18:47:41 +00:00
|
|
|
# Access write directories
|
|
|
|
UMask = "0027";
|
2021-02-14 19:07:40 +00:00
|
|
|
# Capabilities
|
|
|
|
CapabilityBoundingSet = "";
|
|
|
|
# Security
|
|
|
|
NoNewPrivileges = true;
|
2021-02-13 18:47:41 +00:00
|
|
|
# Sandboxing
|
2021-02-14 19:07:40 +00:00
|
|
|
ProtectSystem = "strict";
|
|
|
|
ProtectHome = true;
|
2021-02-13 18:47:41 +00:00
|
|
|
PrivateTmp = true;
|
2021-02-14 19:07:40 +00:00
|
|
|
PrivateDevices = true;
|
|
|
|
PrivateUsers = true;
|
|
|
|
ProtectClock = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" "AF_NETLINK" ];
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
LockPersonality = true;
|
|
|
|
MemoryDenyWriteExecute = false;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictSUIDSGID = true;
|
2021-05-12 09:22:44 +01:00
|
|
|
RemoveIPC = true;
|
2021-02-14 19:07:40 +00:00
|
|
|
PrivateMounts = true;
|
|
|
|
# System Call Filtering
|
|
|
|
SystemCallArchitectures = "native";
|
2021-02-13 18:47:41 +00:00
|
|
|
};
|
|
|
|
|
2019-05-02 15:51:12 +01:00
|
|
|
envFile = pkgs.writeText "mastodon.env" (lib.concatMapStrings (s: s + "\n") (
|
|
|
|
(lib.concatLists (lib.mapAttrsToList (name: value:
|
|
|
|
if value != null then [
|
|
|
|
"${name}=\"${toString value}\""
|
|
|
|
] else []
|
|
|
|
) env))));
|
|
|
|
|
|
|
|
mastodonEnv = pkgs.writeShellScriptBin "mastodon-env" ''
|
|
|
|
set -a
|
2022-02-19 15:29:51 +00:00
|
|
|
export RAILS_ROOT="${cfg.package}"
|
2019-05-02 15:51:12 +01:00
|
|
|
source "${envFile}"
|
|
|
|
source /var/lib/mastodon/.secrets_env
|
|
|
|
eval -- "\$@"
|
|
|
|
'';
|
|
|
|
|
|
|
|
in {
|
|
|
|
|
|
|
|
options = {
|
|
|
|
services.mastodon = {
|
2022-08-28 20:18:44 +01:00
|
|
|
enable = lib.mkEnableOption (lib.mdDoc "Mastodon, a federated social network server");
|
2019-05-02 15:51:12 +01:00
|
|
|
|
|
|
|
configureNginx = lib.mkOption {
|
2022-08-30 13:18:54 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-05-02 15:51:12 +01:00
|
|
|
Configure nginx as a reverse proxy for mastodon.
|
|
|
|
Note that this makes some assumptions on your setup, and sets settings that will
|
|
|
|
affect other virtualHosts running on your nginx instance, if any.
|
|
|
|
Alternatively you can configure a reverse-proxy of your choice to serve these paths:
|
|
|
|
|
2022-08-30 13:18:54 +01:00
|
|
|
`/ -> $(nix-instantiate --eval '<nixpkgs>' -A mastodon.outPath)/public`
|
2019-05-02 15:51:12 +01:00
|
|
|
|
2022-08-30 13:18:54 +01:00
|
|
|
`/ -> 127.0.0.1:{{ webPort }} `(If there was no file in the directory above.)
|
2019-05-02 15:51:12 +01:00
|
|
|
|
2022-08-30 13:18:54 +01:00
|
|
|
`/system/ -> /var/lib/mastodon/public-system/`
|
2019-05-02 15:51:12 +01:00
|
|
|
|
2022-08-30 13:18:54 +01:00
|
|
|
`/api/v1/streaming/ -> 127.0.0.1:{{ streamingPort }}`
|
2019-05-02 15:51:12 +01:00
|
|
|
|
|
|
|
Make sure that websockets are forwarded properly. You might want to set up caching
|
|
|
|
of some requests. Take a look at mastodon's provided nginx configuration at
|
2022-08-30 13:18:54 +01:00
|
|
|
`https://github.com/mastodon/mastodon/blob/master/dist/nginx.conf`.
|
2019-05-02 15:51:12 +01:00
|
|
|
'';
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
user = lib.mkOption {
|
2022-08-15 06:16:25 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-05-02 15:51:12 +01:00
|
|
|
User under which mastodon runs. If it is set to "mastodon",
|
|
|
|
that user will be created, otherwise it should be set to the
|
|
|
|
name of a user created elsewhere. In both cases,
|
2022-08-15 06:16:25 +01:00
|
|
|
`mastodon` and a package containing only
|
|
|
|
the shell script `mastodon-env` will be added to
|
2019-05-02 15:51:12 +01:00
|
|
|
the user's package set. To run a command from
|
2022-08-15 06:16:25 +01:00
|
|
|
`mastodon` such as `tootctl`
|
2019-05-02 15:51:12 +01:00
|
|
|
with the environment configured by this module use
|
2022-08-15 06:16:25 +01:00
|
|
|
`mastodon-env`, as in:
|
2019-05-02 15:51:12 +01:00
|
|
|
|
2022-08-15 06:16:25 +01:00
|
|
|
`mastodon-env tootctl accounts create newuser --email newuser@example.com`
|
2019-05-02 15:51:12 +01:00
|
|
|
'';
|
|
|
|
type = lib.types.str;
|
|
|
|
default = "mastodon";
|
|
|
|
};
|
|
|
|
|
|
|
|
group = lib.mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-05-02 15:51:12 +01:00
|
|
|
Group under which mastodon runs.
|
|
|
|
'';
|
|
|
|
type = lib.types.str;
|
|
|
|
default = "mastodon";
|
|
|
|
};
|
|
|
|
|
|
|
|
streamingPort = lib.mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "TCP port used by the mastodon-streaming service.";
|
2019-05-02 15:51:12 +01:00
|
|
|
type = lib.types.port;
|
|
|
|
default = 55000;
|
|
|
|
};
|
2021-09-04 10:07:04 +01:00
|
|
|
streamingProcesses = lib.mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-09-04 10:07:04 +01:00
|
|
|
Processes used by the mastodon-streaming service.
|
|
|
|
Defaults to the number of CPU cores minus one.
|
|
|
|
'';
|
|
|
|
type = lib.types.nullOr lib.types.int;
|
|
|
|
default = null;
|
|
|
|
};
|
2019-05-02 15:51:12 +01:00
|
|
|
|
|
|
|
webPort = lib.mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "TCP port used by the mastodon-web service.";
|
2019-05-02 15:51:12 +01:00
|
|
|
type = lib.types.port;
|
|
|
|
default = 55001;
|
|
|
|
};
|
2021-09-04 10:07:04 +01:00
|
|
|
webProcesses = lib.mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Processes used by the mastodon-web service.";
|
2021-09-04 10:07:04 +01:00
|
|
|
type = lib.types.int;
|
|
|
|
default = 2;
|
|
|
|
};
|
|
|
|
webThreads = lib.mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Threads per process used by the mastodon-web service.";
|
2021-09-04 10:07:04 +01:00
|
|
|
type = lib.types.int;
|
|
|
|
default = 5;
|
|
|
|
};
|
2019-05-02 15:51:12 +01:00
|
|
|
|
|
|
|
sidekiqPort = lib.mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "TCP port used by the mastodon-sidekiq service.";
|
2019-05-02 15:51:12 +01:00
|
|
|
type = lib.types.port;
|
|
|
|
default = 55002;
|
|
|
|
};
|
2021-09-04 09:53:09 +01:00
|
|
|
sidekiqThreads = lib.mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Worker threads used by the mastodon-sidekiq service.";
|
2021-09-04 09:53:09 +01:00
|
|
|
type = lib.types.int;
|
|
|
|
default = 25;
|
|
|
|
};
|
2019-05-02 15:51:12 +01:00
|
|
|
|
|
|
|
vapidPublicKeyFile = lib.mkOption {
|
2022-08-03 21:46:41 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-05-02 15:51:12 +01:00
|
|
|
Path to file containing the public key used for Web Push
|
|
|
|
Voluntary Application Server Identification. A new keypair can
|
|
|
|
be generated by running:
|
|
|
|
|
2022-08-03 21:46:41 +01:00
|
|
|
`nix build -f '<nixpkgs>' mastodon; cd result; bin/rake webpush:generate_keys`
|
2019-05-02 15:51:12 +01:00
|
|
|
|
2022-08-03 21:46:41 +01:00
|
|
|
If {option}`mastodon.vapidPrivateKeyFile`does not
|
2019-05-02 15:51:12 +01:00
|
|
|
exist, it and this file will be created with a new keypair.
|
|
|
|
'';
|
|
|
|
default = "/var/lib/mastodon/secrets/vapid-public-key";
|
|
|
|
type = lib.types.str;
|
|
|
|
};
|
|
|
|
|
|
|
|
localDomain = lib.mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "The domain serving your Mastodon instance.";
|
2019-05-02 15:51:12 +01:00
|
|
|
example = "social.example.org";
|
|
|
|
type = lib.types.str;
|
|
|
|
};
|
|
|
|
|
|
|
|
secretKeyBaseFile = lib.mkOption {
|
2022-08-03 21:46:41 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-05-02 15:51:12 +01:00
|
|
|
Path to file containing the secret key base.
|
|
|
|
A new secret key base can be generated by running:
|
|
|
|
|
2022-08-03 21:46:41 +01:00
|
|
|
`nix build -f '<nixpkgs>' mastodon; cd result; bin/rake secret`
|
2019-05-02 15:51:12 +01:00
|
|
|
|
|
|
|
If this file does not exist, it will be created with a new secret key base.
|
|
|
|
'';
|
|
|
|
default = "/var/lib/mastodon/secrets/secret-key-base";
|
|
|
|
type = lib.types.str;
|
|
|
|
};
|
|
|
|
|
|
|
|
otpSecretFile = lib.mkOption {
|
2022-08-03 21:46:41 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-05-02 15:51:12 +01:00
|
|
|
Path to file containing the OTP secret.
|
|
|
|
A new OTP secret can be generated by running:
|
|
|
|
|
2022-08-03 21:46:41 +01:00
|
|
|
`nix build -f '<nixpkgs>' mastodon; cd result; bin/rake secret`
|
2019-05-02 15:51:12 +01:00
|
|
|
|
|
|
|
If this file does not exist, it will be created with a new OTP secret.
|
|
|
|
'';
|
|
|
|
default = "/var/lib/mastodon/secrets/otp-secret";
|
|
|
|
type = lib.types.str;
|
|
|
|
};
|
|
|
|
|
|
|
|
vapidPrivateKeyFile = lib.mkOption {
|
2022-08-03 21:46:41 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-05-02 15:51:12 +01:00
|
|
|
Path to file containing the private key used for Web Push
|
|
|
|
Voluntary Application Server Identification. A new keypair can
|
|
|
|
be generated by running:
|
|
|
|
|
2022-08-03 21:46:41 +01:00
|
|
|
`nix build -f '<nixpkgs>' mastodon; cd result; bin/rake webpush:generate_keys`
|
2019-05-02 15:51:12 +01:00
|
|
|
|
|
|
|
If this file does not exist, it will be created with a new
|
|
|
|
private key.
|
|
|
|
'';
|
|
|
|
default = "/var/lib/mastodon/secrets/vapid-private-key";
|
|
|
|
type = lib.types.str;
|
|
|
|
};
|
|
|
|
|
2021-02-13 15:37:26 +00:00
|
|
|
trustedProxy = lib.mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-02-13 15:37:26 +00:00
|
|
|
You need to set it to the IP from which your reverse proxy sends requests to Mastodon's web process,
|
|
|
|
otherwise Mastodon will record the reverse proxy's own IP as the IP of all requests, which would be
|
|
|
|
bad because IP addresses are used for important rate limits and security functions.
|
|
|
|
'';
|
|
|
|
type = lib.types.str;
|
|
|
|
default = "127.0.0.1";
|
|
|
|
};
|
|
|
|
|
2021-02-13 17:47:14 +00:00
|
|
|
enableUnixSocket = lib.mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-02-13 17:47:14 +00:00
|
|
|
Instead of binding to an IP address like 127.0.0.1, you may bind to a Unix socket. This variable
|
|
|
|
is process-specific, e.g. you need different values for every process, and it works for both web (Puma)
|
|
|
|
processes and streaming API (Node.js) processes.
|
|
|
|
'';
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = true;
|
|
|
|
};
|
|
|
|
|
2019-05-02 15:51:12 +01:00
|
|
|
redis = {
|
|
|
|
createLocally = lib.mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Configure local Redis server for Mastodon.";
|
2019-05-02 15:51:12 +01:00
|
|
|
type = lib.types.bool;
|
|
|
|
default = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
host = lib.mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Redis host.";
|
2019-05-02 15:51:12 +01:00
|
|
|
type = lib.types.str;
|
|
|
|
default = "127.0.0.1";
|
|
|
|
};
|
|
|
|
|
|
|
|
port = lib.mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Redis port.";
|
2019-05-02 15:51:12 +01:00
|
|
|
type = lib.types.port;
|
2022-02-19 16:02:48 +00:00
|
|
|
default = 31637;
|
2019-05-02 15:51:12 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
database = {
|
|
|
|
createLocally = lib.mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Configure local PostgreSQL database server for Mastodon.";
|
2019-05-02 15:51:12 +01:00
|
|
|
type = lib.types.bool;
|
|
|
|
default = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
host = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
default = "/run/postgresql";
|
|
|
|
example = "192.168.23.42";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Database host address or unix socket.";
|
2019-05-02 15:51:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
port = lib.mkOption {
|
|
|
|
type = lib.types.int;
|
|
|
|
default = 5432;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Database host port.";
|
2019-05-02 15:51:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
name = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
default = "mastodon";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Database name.";
|
2019-05-02 15:51:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
user = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
default = "mastodon";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Database user.";
|
2019-05-02 15:51:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
passwordFile = lib.mkOption {
|
|
|
|
type = lib.types.nullOr lib.types.path;
|
|
|
|
default = "/var/lib/mastodon/secrets/db-password";
|
|
|
|
example = "/run/keys/mastodon-db-password";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-05-02 15:51:12 +01:00
|
|
|
A file containing the password corresponding to
|
2022-07-28 22:19:15 +01:00
|
|
|
{option}`database.user`.
|
2019-05-02 15:51:12 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
smtp = {
|
|
|
|
createLocally = lib.mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Configure local Postfix SMTP server for Mastodon.";
|
2019-05-02 15:51:12 +01:00
|
|
|
type = lib.types.bool;
|
|
|
|
default = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
authenticate = lib.mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Authenticate with the SMTP server using username and password.";
|
2019-05-02 15:51:12 +01:00
|
|
|
type = lib.types.bool;
|
2021-02-13 14:49:13 +00:00
|
|
|
default = false;
|
2019-05-02 15:51:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
host = lib.mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "SMTP host used when sending emails to users.";
|
2019-05-02 15:51:12 +01:00
|
|
|
type = lib.types.str;
|
|
|
|
default = "127.0.0.1";
|
|
|
|
};
|
|
|
|
|
|
|
|
port = lib.mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "SMTP port used when sending emails to users.";
|
2019-05-02 15:51:12 +01:00
|
|
|
type = lib.types.port;
|
|
|
|
default = 25;
|
|
|
|
};
|
|
|
|
|
|
|
|
fromAddress = lib.mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''"From" address used when sending Emails to users.'';
|
2019-05-02 15:51:12 +01:00
|
|
|
type = lib.types.str;
|
|
|
|
};
|
|
|
|
|
|
|
|
user = lib.mkOption {
|
2022-05-14 16:12:55 +01:00
|
|
|
type = lib.types.nullOr lib.types.str;
|
|
|
|
default = null;
|
|
|
|
example = "mastodon@example.com";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "SMTP login name.";
|
2019-05-02 15:51:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
passwordFile = lib.mkOption {
|
2022-05-14 16:12:55 +01:00
|
|
|
type = lib.types.nullOr lib.types.path;
|
|
|
|
default = null;
|
|
|
|
example = "/var/lib/mastodon/secrets/smtp-password";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-05-02 15:51:12 +01:00
|
|
|
Path to file containing the SMTP password.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
elasticsearch = {
|
|
|
|
host = lib.mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-05-02 15:51:12 +01:00
|
|
|
Elasticsearch host.
|
|
|
|
If it is not null, Elasticsearch full text search will be enabled.
|
|
|
|
'';
|
|
|
|
type = lib.types.nullOr lib.types.str;
|
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
port = lib.mkOption {
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Elasticsearch port.";
|
2019-05-02 15:51:12 +01:00
|
|
|
type = lib.types.port;
|
|
|
|
default = 9200;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
package = lib.mkOption {
|
|
|
|
type = lib.types.package;
|
|
|
|
default = pkgs.mastodon;
|
2021-10-03 17:06:03 +01:00
|
|
|
defaultText = lib.literalExpression "pkgs.mastodon";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Mastodon package to use.";
|
2019-05-02 15:51:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig = lib.mkOption {
|
|
|
|
type = lib.types.attrs;
|
|
|
|
default = {};
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-05-02 15:51:12 +01:00
|
|
|
Extra environment variables to pass to all mastodon services.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
automaticMigrations = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = true;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2019-05-02 15:51:12 +01:00
|
|
|
Do automatic database migrations.
|
|
|
|
'';
|
|
|
|
};
|
2022-11-01 20:30:20 +00:00
|
|
|
|
|
|
|
mediaAutoRemove = {
|
|
|
|
enable = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = true;
|
|
|
|
example = false;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Automatically remove remote media attachments and preview cards older than the configured amount of days.
|
|
|
|
|
|
|
|
Recommended in https://docs.joinmastodon.org/admin/setup/.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
startAt = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
default = "daily";
|
|
|
|
example = "hourly";
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
How often to remove remote media.
|
|
|
|
|
|
|
|
The format is described in {manpage}`systemd.time(7)`.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
olderThanDays = lib.mkOption {
|
|
|
|
type = lib.types.int;
|
|
|
|
default = 30;
|
|
|
|
example = 14;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
How old remote media needs to be in order to be removed.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2019-05-02 15:51:12 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = databaseActuallyCreateLocally -> (cfg.user == cfg.database.user);
|
|
|
|
message = ''For local automatic database provisioning (services.mastodon.database.createLocally == true) with peer authentication (services.mastodon.database.host == "/run/postgresql") to work services.mastodon.user and services.mastodon.database.user must be identical.'';
|
|
|
|
}
|
2022-05-14 16:12:55 +01:00
|
|
|
{
|
|
|
|
assertion = cfg.smtp.authenticate -> (cfg.smtp.user != null);
|
|
|
|
message = ''
|
|
|
|
<option>services.mastodon.smtp.user</option> needs to be set if
|
|
|
|
<option>services.mastodon.smtp.authenticate</option> is enabled.
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assertion = cfg.smtp.authenticate -> (cfg.smtp.passwordFile != null);
|
|
|
|
message = ''
|
|
|
|
<option>services.mastodon.smtp.passwordFile</option> needs to be set if
|
|
|
|
<option>services.mastodon.smtp.authenticate</option> is enabled.
|
|
|
|
'';
|
|
|
|
}
|
2019-05-02 15:51:12 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
systemd.services.mastodon-init-dirs = {
|
|
|
|
script = ''
|
|
|
|
umask 077
|
|
|
|
|
|
|
|
if ! test -f ${cfg.secretKeyBaseFile}; then
|
|
|
|
mkdir -p $(dirname ${cfg.secretKeyBaseFile})
|
|
|
|
bin/rake secret > ${cfg.secretKeyBaseFile}
|
|
|
|
fi
|
|
|
|
if ! test -f ${cfg.otpSecretFile}; then
|
|
|
|
mkdir -p $(dirname ${cfg.otpSecretFile})
|
|
|
|
bin/rake secret > ${cfg.otpSecretFile}
|
|
|
|
fi
|
|
|
|
if ! test -f ${cfg.vapidPrivateKeyFile}; then
|
|
|
|
mkdir -p $(dirname ${cfg.vapidPrivateKeyFile}) $(dirname ${cfg.vapidPublicKeyFile})
|
|
|
|
keypair=$(bin/rake webpush:generate_keys)
|
|
|
|
echo $keypair | grep --only-matching "Private -> [^ ]\+" | sed 's/^Private -> //' > ${cfg.vapidPrivateKeyFile}
|
|
|
|
echo $keypair | grep --only-matching "Public -> [^ ]\+" | sed 's/^Public -> //' > ${cfg.vapidPublicKeyFile}
|
|
|
|
fi
|
|
|
|
|
|
|
|
cat > /var/lib/mastodon/.secrets_env <<EOF
|
|
|
|
SECRET_KEY_BASE="$(cat ${cfg.secretKeyBaseFile})"
|
|
|
|
OTP_SECRET="$(cat ${cfg.otpSecretFile})"
|
|
|
|
VAPID_PRIVATE_KEY="$(cat ${cfg.vapidPrivateKeyFile})"
|
|
|
|
VAPID_PUBLIC_KEY="$(cat ${cfg.vapidPublicKeyFile})"
|
|
|
|
DB_PASS="$(cat ${cfg.database.passwordFile})"
|
|
|
|
'' + (if cfg.smtp.authenticate then ''
|
|
|
|
SMTP_PASSWORD="$(cat ${cfg.smtp.passwordFile})"
|
|
|
|
'' else "") + ''
|
|
|
|
EOF
|
|
|
|
'';
|
2021-02-12 19:31:44 +00:00
|
|
|
environment = env;
|
2019-05-02 15:51:12 +01:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
WorkingDirectory = cfg.package;
|
2021-04-24 13:43:26 +01:00
|
|
|
# System Call Filtering
|
2021-05-12 09:34:26 +01:00
|
|
|
SystemCallFilter = [ ("~" + lib.concatStringsSep " " (systemCallsList ++ [ "@resources" ])) "@chown" "pipe" "pipe2" ];
|
2021-02-13 18:47:41 +00:00
|
|
|
} // cfgService;
|
|
|
|
|
2019-05-02 15:51:12 +01:00
|
|
|
after = [ "network.target" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.mastodon-init-db = lib.mkIf cfg.automaticMigrations {
|
|
|
|
script = ''
|
2021-02-14 18:10:54 +00:00
|
|
|
if [ `psql ${cfg.database.name} -c \
|
2019-05-02 15:51:12 +01:00
|
|
|
"select count(*) from pg_class c \
|
|
|
|
join pg_namespace s on s.oid = c.relnamespace \
|
|
|
|
where s.nspname not in ('pg_catalog', 'pg_toast', 'information_schema') \
|
|
|
|
and s.nspname not like 'pg_temp%';" | sed -n 3p` -eq 0 ]; then
|
2021-05-17 20:03:40 +01:00
|
|
|
SAFETY_ASSURED=1 rails db:schema:load
|
|
|
|
rails db:seed
|
2019-05-02 15:51:12 +01:00
|
|
|
else
|
2021-05-17 20:03:40 +01:00
|
|
|
rails db:migrate
|
2019-05-02 15:51:12 +01:00
|
|
|
fi
|
|
|
|
'';
|
|
|
|
path = [ cfg.package pkgs.postgresql ];
|
|
|
|
environment = env;
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
EnvironmentFile = "/var/lib/mastodon/.secrets_env";
|
|
|
|
WorkingDirectory = cfg.package;
|
2021-04-24 13:43:26 +01:00
|
|
|
# System Call Filtering
|
2021-05-12 09:34:26 +01:00
|
|
|
SystemCallFilter = [ ("~" + lib.concatStringsSep " " (systemCallsList ++ [ "@resources" ])) "@chown" "pipe" "pipe2" ];
|
2021-02-13 18:47:41 +00:00
|
|
|
} // cfgService;
|
2022-10-26 12:30:18 +01:00
|
|
|
after = [ "network.target" "mastodon-init-dirs.service" ]
|
|
|
|
++ lib.optional databaseActuallyCreateLocally "postgresql.service";
|
|
|
|
requires = [ "mastodon-init-dirs.service" ]
|
|
|
|
++ lib.optional databaseActuallyCreateLocally "postgresql.service";
|
2019-05-02 15:51:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.mastodon-streaming = {
|
2022-10-26 12:30:18 +01:00
|
|
|
after = [ "network.target" "mastodon-init-dirs.service" ]
|
|
|
|
++ lib.optional databaseActuallyCreateLocally "postgresql.service"
|
|
|
|
++ lib.optional cfg.automaticMigrations "mastodon-init-db.service";
|
|
|
|
requires = [ "mastodon-init-dirs.service" ]
|
|
|
|
++ lib.optional databaseActuallyCreateLocally "postgresql.service"
|
|
|
|
++ lib.optional cfg.automaticMigrations "mastodon-init-db.service";
|
2019-05-02 15:51:12 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2022-10-26 12:30:18 +01:00
|
|
|
description = "Mastodon streaming";
|
2021-02-13 17:47:14 +00:00
|
|
|
environment = env // (if cfg.enableUnixSocket
|
|
|
|
then { SOCKET = "/run/mastodon-streaming/streaming.socket"; }
|
|
|
|
else { PORT = toString(cfg.streamingPort); }
|
|
|
|
);
|
2019-05-02 15:51:12 +01:00
|
|
|
serviceConfig = {
|
2021-02-16 07:09:14 +00:00
|
|
|
ExecStart = "${cfg.package}/run-streaming.sh";
|
2019-05-02 15:51:12 +01:00
|
|
|
Restart = "always";
|
|
|
|
RestartSec = 20;
|
|
|
|
EnvironmentFile = "/var/lib/mastodon/.secrets_env";
|
2021-02-13 18:47:41 +00:00
|
|
|
WorkingDirectory = cfg.package;
|
2021-02-13 17:47:14 +00:00
|
|
|
# Runtime directory and mode
|
|
|
|
RuntimeDirectory = "mastodon-streaming";
|
|
|
|
RuntimeDirectoryMode = "0750";
|
2021-04-24 13:43:26 +01:00
|
|
|
# System Call Filtering
|
2021-05-12 09:41:11 +01:00
|
|
|
SystemCallFilter = [ ("~" + lib.concatStringsSep " " (systemCallsList ++ [ "@memlock" "@resources" ])) "pipe" "pipe2" ];
|
2021-02-13 18:47:41 +00:00
|
|
|
} // cfgService;
|
2019-05-02 15:51:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.mastodon-web = {
|
2022-10-26 12:30:18 +01:00
|
|
|
after = [ "network.target" "mastodon-init-dirs.service" ]
|
|
|
|
++ lib.optional databaseActuallyCreateLocally "postgresql.service"
|
|
|
|
++ lib.optional cfg.automaticMigrations "mastodon-init-db.service";
|
|
|
|
requires = [ "mastodon-init-dirs.service" ]
|
|
|
|
++ lib.optional databaseActuallyCreateLocally "postgresql.service"
|
|
|
|
++ lib.optional cfg.automaticMigrations "mastodon-init-db.service";
|
2019-05-02 15:51:12 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2022-10-26 12:30:18 +01:00
|
|
|
description = "Mastodon web";
|
2021-02-13 17:47:14 +00:00
|
|
|
environment = env // (if cfg.enableUnixSocket
|
|
|
|
then { SOCKET = "/run/mastodon-web/web.socket"; }
|
|
|
|
else { PORT = toString(cfg.webPort); }
|
|
|
|
);
|
2019-05-02 15:51:12 +01:00
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "${cfg.package}/bin/puma -C config/puma.rb";
|
|
|
|
Restart = "always";
|
|
|
|
RestartSec = 20;
|
|
|
|
EnvironmentFile = "/var/lib/mastodon/.secrets_env";
|
2021-02-13 18:47:41 +00:00
|
|
|
WorkingDirectory = cfg.package;
|
2021-02-13 17:47:14 +00:00
|
|
|
# Runtime directory and mode
|
|
|
|
RuntimeDirectory = "mastodon-web";
|
|
|
|
RuntimeDirectoryMode = "0750";
|
2021-04-24 13:43:26 +01:00
|
|
|
# System Call Filtering
|
2021-11-06 18:39:27 +00:00
|
|
|
SystemCallFilter = [ ("~" + lib.concatStringsSep " " systemCallsList) "@chown" "pipe" "pipe2" ];
|
2021-02-13 18:47:41 +00:00
|
|
|
} // cfgService;
|
2019-05-02 15:51:12 +01:00
|
|
|
path = with pkgs; [ file imagemagick ffmpeg ];
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.mastodon-sidekiq = {
|
2022-10-26 12:30:18 +01:00
|
|
|
after = [ "network.target" "mastodon-init-dirs.service" ]
|
|
|
|
++ lib.optional databaseActuallyCreateLocally "postgresql.service"
|
|
|
|
++ lib.optional cfg.automaticMigrations "mastodon-init-db.service";
|
|
|
|
requires = [ "mastodon-init-dirs.service" ]
|
|
|
|
++ lib.optional databaseActuallyCreateLocally "postgresql.service"
|
|
|
|
++ lib.optional cfg.automaticMigrations "mastodon-init-db.service";
|
2019-05-02 15:51:12 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2022-10-26 12:30:18 +01:00
|
|
|
description = "Mastodon sidekiq";
|
2019-05-02 15:51:12 +01:00
|
|
|
environment = env // {
|
|
|
|
PORT = toString(cfg.sidekiqPort);
|
2021-09-04 09:53:09 +01:00
|
|
|
DB_POOL = toString cfg.sidekiqThreads;
|
2019-05-02 15:51:12 +01:00
|
|
|
};
|
|
|
|
serviceConfig = {
|
2021-09-04 09:53:09 +01:00
|
|
|
ExecStart = "${cfg.package}/bin/sidekiq -c ${toString cfg.sidekiqThreads} -r ${cfg.package}";
|
2019-05-02 15:51:12 +01:00
|
|
|
Restart = "always";
|
|
|
|
RestartSec = 20;
|
|
|
|
EnvironmentFile = "/var/lib/mastodon/.secrets_env";
|
2021-02-13 18:47:41 +00:00
|
|
|
WorkingDirectory = cfg.package;
|
2021-04-24 13:43:26 +01:00
|
|
|
# System Call Filtering
|
2021-05-12 09:34:26 +01:00
|
|
|
SystemCallFilter = [ ("~" + lib.concatStringsSep " " systemCallsList) "@chown" "pipe" "pipe2" ];
|
2021-02-13 18:47:41 +00:00
|
|
|
} // cfgService;
|
2019-05-02 15:51:12 +01:00
|
|
|
path = with pkgs; [ file imagemagick ffmpeg ];
|
|
|
|
};
|
|
|
|
|
2022-11-01 20:30:20 +00:00
|
|
|
systemd.services.mastodon-media-auto-remove = lib.mkIf cfg.mediaAutoRemove.enable {
|
|
|
|
description = "Mastodon media auto remove";
|
|
|
|
environment = env;
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
EnvironmentFile = "/var/lib/mastodon/.secrets_env";
|
|
|
|
} // cfgService;
|
2022-11-11 22:05:10 +00:00
|
|
|
script = let
|
|
|
|
olderThanDays = toString cfg.mediaAutoRemove.olderThanDays;
|
|
|
|
in ''
|
|
|
|
${cfg.package}/bin/tootctl media remove --days=${olderThanDays}
|
|
|
|
${cfg.package}/bin/tootctl preview_cards remove --days=${olderThanDays}
|
|
|
|
'';
|
|
|
|
startAt = cfg.mediaAutoRemove.startAt;
|
2022-11-01 20:30:20 +00:00
|
|
|
};
|
|
|
|
|
2019-05-02 15:51:12 +01:00
|
|
|
services.nginx = lib.mkIf cfg.configureNginx {
|
|
|
|
enable = true;
|
|
|
|
recommendedProxySettings = true; # required for redirections to work
|
|
|
|
virtualHosts."${cfg.localDomain}" = {
|
|
|
|
root = "${cfg.package}/public/";
|
|
|
|
forceSSL = true; # mastodon only supports https
|
|
|
|
enableACME = true;
|
|
|
|
|
|
|
|
locations."/system/".alias = "/var/lib/mastodon/public-system/";
|
|
|
|
|
|
|
|
locations."/" = {
|
|
|
|
tryFiles = "$uri @proxy";
|
|
|
|
};
|
|
|
|
|
|
|
|
locations."@proxy" = {
|
2021-02-13 17:47:14 +00:00
|
|
|
proxyPass = (if cfg.enableUnixSocket then "http://unix:/run/mastodon-web/web.socket" else "http://127.0.0.1:${toString(cfg.webPort)}");
|
2019-05-02 15:51:12 +01:00
|
|
|
proxyWebsockets = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
locations."/api/v1/streaming/" = {
|
2021-02-13 17:47:14 +00:00
|
|
|
proxyPass = (if cfg.enableUnixSocket then "http://unix:/run/mastodon-streaming/streaming.socket" else "http://127.0.0.1:${toString(cfg.streamingPort)}/");
|
2019-05-02 15:51:12 +01:00
|
|
|
proxyWebsockets = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
services.postfix = lib.mkIf (cfg.smtp.createLocally && cfg.smtp.host == "127.0.0.1") {
|
|
|
|
enable = true;
|
2021-02-13 14:49:13 +00:00
|
|
|
hostname = lib.mkDefault "${cfg.localDomain}";
|
2019-05-02 15:51:12 +01:00
|
|
|
};
|
2022-02-19 16:02:48 +00:00
|
|
|
services.redis.servers.mastodon = lib.mkIf (cfg.redis.createLocally && cfg.redis.host == "127.0.0.1") {
|
2019-05-02 15:51:12 +01:00
|
|
|
enable = true;
|
2022-02-19 16:02:48 +00:00
|
|
|
port = cfg.redis.port;
|
|
|
|
bind = "127.0.0.1";
|
2019-05-02 15:51:12 +01:00
|
|
|
};
|
|
|
|
services.postgresql = lib.mkIf databaseActuallyCreateLocally {
|
|
|
|
enable = true;
|
|
|
|
ensureUsers = [
|
|
|
|
{
|
|
|
|
name = cfg.database.user;
|
|
|
|
ensurePermissions."DATABASE ${cfg.database.name}" = "ALL PRIVILEGES";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
ensureDatabases = [ cfg.database.name ];
|
|
|
|
};
|
|
|
|
|
|
|
|
users.users = lib.mkMerge [
|
|
|
|
(lib.mkIf (cfg.user == "mastodon") {
|
|
|
|
mastodon = {
|
|
|
|
isSystemUser = true;
|
|
|
|
home = cfg.package;
|
|
|
|
inherit (cfg) group;
|
|
|
|
};
|
|
|
|
})
|
2022-11-15 18:45:22 +00:00
|
|
|
(lib.attrsets.setAttrByPath [ cfg.user "packages" ] [ cfg.package mastodonEnv pkgs.imagemagick ])
|
2019-05-02 15:51:12 +01:00
|
|
|
];
|
|
|
|
|
2021-02-17 01:07:01 +00:00
|
|
|
users.groups.${cfg.group}.members = lib.optional cfg.configureNginx config.services.nginx.user;
|
2019-05-02 15:51:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
meta.maintainers = with lib.maintainers; [ happy-river erictapen ];
|
|
|
|
|
|
|
|
}
|