]> git.immae.eu Git - perso/Immae/Config/Nix.git/blobdiff - modules/webapps/mastodon.nix
Configuration adjustments for shaarli and mastodon
[perso/Immae/Config/Nix.git] / modules / webapps / mastodon.nix
index 6255de91a708be894751ce298c4e67391abbaac5..df2dbb23dac2a2eec289a037de296790071f3c1d 100644 (file)
@@ -27,7 +27,7 @@ in
       '';
     };
     socketsPrefix = lib.mkOption {
-      type = lib.types.string;
+      type = lib.types.str;
       default = "live";
       description = ''
         The prefix to use for Mastodon sockets.
@@ -96,18 +96,24 @@ in
   };
 
   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";
@@ -135,6 +141,7 @@ in
       '';
 
       serviceConfig = {
+        Slice = "mastodon.slice";
         User = cfg.user;
         EnvironmentFile = cfg.configFile;
         PrivateTmp = true;
@@ -160,7 +167,7 @@ in
       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
@@ -171,7 +178,11 @@ in
         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;
@@ -187,6 +198,36 @@ in
       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;
+    };
+
     systemd.services.mastodon-sidekiq = {
       description = "Mastodon Sidekiq";
       wantedBy = [ "multi-user.target" ];
@@ -204,6 +245,7 @@ in
       '';
 
       serviceConfig = {
+        Slice = "mastodon.slice";
         User = cfg.user;
         EnvironmentFile = cfg.configFile;
         PrivateTmp = true;