'';
};
socketsPrefix = lib.mkOption {
- type = lib.types.string;
+ type = lib.types.str;
default = "live";
description = ''
The prefix to use for Mastodon sockets.
'';
readOnly = true;
};
+ systemdStateDirectory = lib.mkOption {
+ type = lib.types.str;
+ # Use ReadWritePaths= instead if varDir is outside of /var/lib
+ default = assert lib.strings.hasPrefix "/var/lib/" cfg.dataDir;
+ lib.strings.removePrefix "/var/lib/" cfg.dataDir;
+ description = ''
+ Adjusted Mastodon data directory for systemd
+ '';
+ readOnly = true;
+ };
+ systemdRuntimeDirectory = lib.mkOption {
+ type = lib.types.str;
+ # Use ReadWritePaths= instead if socketsDir is outside of /run
+ default = assert lib.strings.hasPrefix "/run/" cfg.socketsDir;
+ lib.strings.removePrefix "/run/" cfg.socketsDir;
+ description = ''
+ Adjusted Mastodon sockets directory for systemd
+ '';
+ readOnly = true;
+ };
sockets = lib.mkOption {
type = lib.types.attrsOf lib.types.path;
default = {
};
config = lib.mkIf cfg.enable {
- users.users = lib.optionalAttrs (cfg.user == name) (lib.singleton {
- inherit name;
- inherit uid;
- group = cfg.group;
- description = "Mastodon user";
- home = cfg.dataDir;
- useDefaultShell = true;
- });
- users.groups = lib.optionalAttrs (cfg.group == name) (lib.singleton {
- inherit name;
- inherit gid;
- });
+ users.users = lib.optionalAttrs (cfg.user == name) {
+ "${name}" = {
+ inherit uid;
+ group = cfg.group;
+ description = "Mastodon user";
+ home = cfg.dataDir;
+ useDefaultShell = true;
+ };
+ };
+ users.groups = lib.optionalAttrs (cfg.group == name) {
+ "${name}" = {
+ inherit gid;
+ };
+ };
+
+ systemd.slices.mastodon = {
+ description = "Mastodon slice";
+ };
systemd.services.mastodon-streaming = {
description = "Mastodon Streaming";
'';
serviceConfig = {
+ Slice = "mastodon.slice";
User = cfg.user;
EnvironmentFile = cfg.configFile;
PrivateTmp = true;
TimeoutSec = 15;
Type = "simple";
WorkingDirectory = cfg.workdir;
+ StateDirectory = cfg.systemdStateDirectory;
+ RuntimeDirectory = cfg.systemdRuntimeDirectory;
+ RuntimeDirectoryPreserve = "yes";
};
unitConfig.RequiresMountsFor = cfg.dataDir;
environment.BUNDLE_GEMFILE = "${cfg.workdir.gems.confFiles}/Gemfile";
environment.SOCKET = cfg.sockets.rails;
- path = [ cfg.workdir.gems cfg.workdir.gems.ruby pkgs.file ];
+ path = [ cfg.workdir.gems cfg.workdir.gems.ruby pkgs.file pkgs.imagemagick ];
preStart = ''
+ install -m 0755 -d ${cfg.dataDir}/tmp/cache
./bin/bundle exec rails db:migrate
'';
exec ./bin/bundle exec puma -C config/puma.rb
'';
+ postStart = ''
+ exec ./bin/tootctl cache clear
+ '';
serviceConfig = {
+ Slice = "mastodon.slice";
User = cfg.user;
EnvironmentFile = cfg.configFile;
PrivateTmp = true;
TimeoutSec = 60;
Type = "simple";
WorkingDirectory = cfg.workdir;
+ StateDirectory = cfg.systemdStateDirectory;
+ RuntimeDirectory = cfg.systemdRuntimeDirectory;
+ RuntimeDirectoryPreserve = "yes";
+ };
+
+ unitConfig.RequiresMountsFor = cfg.dataDir;
+ };
+
+ systemd.services.mastodon-cleanup = {
+ description = "Cleanup mastodon";
+ startAt = "daily";
+ restartIfChanged = false;
+
+ environment.RAILS_ENV = "production";
+ environment.BUNDLE_PATH = "${cfg.workdir.gems}/${cfg.workdir.gems.ruby.gemPath}";
+ environment.BUNDLE_GEMFILE = "${cfg.workdir.gems.confFiles}/Gemfile";
+ environment.SOCKET = cfg.sockets.rails;
+
+ path = [ cfg.workdir.gems cfg.workdir.gems.ruby pkgs.file ];
+
+ script = ''
+ exec ./bin/tootctl media remove --days 30
+ '';
+
+ serviceConfig = {
+ User = cfg.user;
+ EnvironmentFile = cfg.configFile;
+ PrivateTmp = true;
+ Type = "oneshot";
+ WorkingDirectory = cfg.workdir;
+ StateDirectory = cfg.systemdStateDirectory;
+ RuntimeDirectory = cfg.systemdRuntimeDirectory;
+ RuntimeDirectoryPreserve = "yes";
};
unitConfig.RequiresMountsFor = cfg.dataDir;
'';
serviceConfig = {
+ Slice = "mastodon.slice";
User = cfg.user;
EnvironmentFile = cfg.configFile;
PrivateTmp = true;
TimeoutSec = 15;
Type = "simple";
WorkingDirectory = cfg.workdir;
+ StateDirectory = cfg.systemdStateDirectory;
+ RuntimeDirectory = cfg.systemdRuntimeDirectory;
+ RuntimeDirectoryPreserve = "yes";
};
unitConfig.RequiresMountsFor = cfg.dataDir;
};
- system.activationScripts.mastodon = {
- deps = [ "users" ];
- text = ''
- install -m 0755 -o ${cfg.user} -g ${cfg.group} -d ${cfg.socketsDir}
- install -m 0755 -o ${cfg.user} -g ${cfg.group} -d ${cfg.dataDir} ${cfg.dataDir}/tmp/cache
- '';
- };
-
};
}