summary refs log tree commit diff
path: root/host/Spacebar-nginx/containers/spacebar-server/services
diff options
context:
space:
mode:
Diffstat (limited to 'host/Spacebar-nginx/containers/spacebar-server/services')
-rwxr-xr-xhost/Spacebar-nginx/containers/spacebar-server/services/spacebar-server.nix169
1 files changed, 105 insertions, 64 deletions
diff --git a/host/Spacebar-nginx/containers/spacebar-server/services/spacebar-server.nix b/host/Spacebar-nginx/containers/spacebar-server/services/spacebar-server.nix
index aaa1396..9a6cdd6 100755
--- a/host/Spacebar-nginx/containers/spacebar-server/services/spacebar-server.nix
+++ b/host/Spacebar-nginx/containers/spacebar-server/services/spacebar-server.nix
@@ -1,74 +1,115 @@
 { config, pkgs, lib, spacebar-server, ... }:
-
+let
+  services = [
+    { name = "api"; description = "Spacebar Server API"; }
+    { name = "gateway"; description = "Spacebar Server Gateway"; }
+    { name = "cdn"; description = "Spacebar Server CDN"; }
+  ];
+in
 {
   services.rabbitmq.enable = true;
 
-  systemd.tmpfiles.rules = [  "d /var/lib/spacebar-server 0750 spacebar spacebar" ];
-  users.users.spacebar = {
-    isSystemUser = true;
-    group = "spacebar";
-    home = "/var/lib/spacebar-server";
-    createHome = true;
-    shell = "/bin/false";
-  };
+  #systemd.tmpfiles.rules = [  "d /var/lib/spacebar-server 0750 spacebar spacebar" ];
+  #users.users.spacebar = {
+  #  isSystemUser = true;
+  #  group = "spacebar";
+  #  home = "/var/lib/spacebar-server";
+  #  createHome = true;
+  #  shell = "/bin/false";
+  #};
 
-  users.groups.spacebar = {
-    #isSystemGroup = true;
-  };
+  #users.groups.spacebar = {
+  #  #isSystemGroup = true;
+  #};
 
-  systemd.services = {
-    "spacebar-server-api" = {
-        wantedBy = [ "multi-user.target" ];
-        after = [ "resolvconf.target" "postgresql.service" "rabbitmq.service" ];
-        serviceConfig = {
-          ExecStart = ''
-            ${spacebar-server.packages.${pkgs.system}.default}/bin/start-api
-            '';
-          #Restart = "never";
-          User = "spacebar";
-          WorkingDirectory = "/var/lib/spacebar-server";
-          Environment = [
-            "DATABASE=postgres://spacebar:spacebar@127.0.0.1/spacebar"
-            "LOG_REQUESTS='-'"
-            #"DB_LOGGING='true'"
-          ];
-      };
-    };
-    "spacebar-server-gateway" = {
-        wantedBy = [ "multi-user.target" ];
-        after = [ "resolvconf.target" "postgresql.service" "rabbitmq.service" "spacebar-server-api.service" ];
-        serviceConfig = {
-          ExecStart = ''
-            ${spacebar-server.packages.${pkgs.system}.default}/bin/start-gateway
-            '';
-          #Restart = "never";
-          User = "spacebar";
-          WorkingDirectory = "/var/lib/spacebar-server";
-          Environment = [
-            "DATABASE=postgres://spacebar:spacebar@127.0.0.1/spacebar"
-            "LOG_REQUESTS='-'"
-            #"DB_LOGGING='true'"
-          ];
-      };
-    };
-    "spacebar-server-cdn" = {
-        wantedBy = [ "multi-user.target" ];
-        after = [ "resolvconf.target" "postgresql.service" "rabbitmq.service" "spacebar-server-api.service" ];
-        serviceConfig = {
-          ExecStart = ''
-            ${spacebar-server.packages.${pkgs.system}.default}/bin/start-cdn
-            '';
-          #Restart = "never";
-          User = "spacebar";
-          WorkingDirectory = "/var/lib/spacebar-server";
-          Environment = [
-            "DATABASE=postgres://spacebar:spacebar@127.0.0.1/spacebar"
-            "LOG_REQUESTS='-'"
-            #"DB_LOGGING='true'"
-          ];
+  #systemd.services = {
+  #  "spacebar-server-api" = {
+  #    description = "Spacebar Server API";
+  #    wantedBy = [ "multi-user.target" ];
+  #    requires = [ "resolvconf.target" "postgresql.service" "rabbitmq.service" ];
+  #    serviceConfig = {
+  #      ExecStart = "${spacebar-server.packages.${pkgs.system}.default}/bin/start-api";
+  #        
+  #      WorkingDirectory = "/var/lib/spacebar-server";
+  #      StateDirectory = "spacebar";
+  #      StateDirectoryMode = "0700";
+  #      ProtectSystem = "strict";
+  #      ProtectHome = true;
+  #      PrivateTmp = true;
+  #      NoNewPrivileges = true;
+  #      PrivateDevices = true;
+  #      Restart = "on-failure";
+  #      Environment = [
+  #        "DATABASE=postgres://spacebar:spacebar@127.0.0.1/spacebar"
+  #        "LOG_REQUESTS='-'"
+  #        #"DB_LOGGING='true'"
+  #      ];
+  #  };
+  #  };
+  #  "spacebar-server-gateway" = {
+  #    wantedBy = [ "multi-user.target" ];
+  #    requires = [ "resolvconf.target" "postgresql.service" "rabbitmq.service" "spacebar-server-api.service" ];
+  #    serviceConfig = {
+  #      ExecStart = ''
+  #        ${spacebar-server.packages.${pkgs.system}.default}/bin/start-gateway
+  #        '';
+  #      #Restart = "never";
+  #      User = "spacebar";
+  #      WorkingDirectory = "/var/lib/spacebar-server";
+  #      Environment = [
+  #        "DATABASE=postgres://spacebar:spacebar@127.0.0.1/spacebar"
+  #        "LOG_REQUESTS='-'"
+  #        #"DB_LOGGING='true'"
+  #      ];
+  #    };
+  #  };
+  #  "spacebar-server-cdn" = {
+  #    wantedBy = [ "multi-user.target" ];
+  #    requires = [ "resolvconf.target" "postgresql.service" "rabbitmq.service" "spacebar-server-api.service" ];
+  #    serviceConfig = {
+  #      ExecStart = ''
+  #        ${spacebar-server.packages.${pkgs.system}.default}/bin/start-cdn
+  #        '';
+  #      #Restart = "never";
+  #      User = "spacebar";
+  #      WorkingDirectory = "/var/lib/spacebar-server";
+  #      Environment = [
+  #        "DATABASE=postgres://spacebar:spacebar@127.0.0.1/spacebar"
+  #        "LOG_REQUESTS='-'"
+  #        #"DB_LOGGING='true'"
+  #      ];
+  #    };
+  #  };
+  #};
+
+  # use some kind of foreach loop to generate the services, with proper casing for descriptions
+  # and service names
+
+  systemd.services = lib.attrsets.genAttrs 
+    (map (service: "spacebar-server-${service.name}") services) 
+    (serviceName: {
+      description = serviceName;
+      wantedBy = [ "multi-user.target" ];
+      requires = [ "postgresql.service" "rabbitmq.service" ] 
+        ++ (if serviceName != "spacebar-server-api" then [ "spacebar-server-api.service" ] else []);
+      serviceConfig = {
+        ExecStart = "${spacebar-server.packages.${pkgs.system}.default}/bin/start-${builtins.substring 16 30 serviceName}";
+        WorkingDirectory = "/var/lib/spacebar-server";
+        StateDirectory = "spacebar";
+        StateDirectoryMode = "0700";
+        ProtectSystem = "strict";
+        ProtectHome = true;
+        PrivateTmp = true;
+        NoNewPrivileges = true;
+        PrivateDevices = true;
+        Restart = "on-failure";
+        Environment = [
+          "DATABASE=postgres://spacebar:spacebar@127.0.0.1/spacebar"
+          "LOG_REQUESTS='-'"
+          #"DB_LOGGING='true'"
+        ];
       };
-    };
-  };
+    });
 
   system.stateVersion = "22.11"; # DO NOT EDIT!
 }