summary refs log tree commit diff
path: root/host/Spacebar-nginx/containers/spacebar-server-dev-nix
diff options
context:
space:
mode:
Diffstat (limited to 'host/Spacebar-nginx/containers/spacebar-server-dev-nix')
-rwxr-xr-xhost/Spacebar-nginx/containers/spacebar-server-dev-nix/container.nix24
-rw-r--r--host/Spacebar-nginx/containers/spacebar-server-dev-nix/import.nix41
-rwxr-xr-xhost/Spacebar-nginx/containers/spacebar-server-dev-nix/root.nix31
-rwxr-xr-xhost/Spacebar-nginx/containers/spacebar-server-dev-nix/services/nginx.nix115
-rw-r--r--host/Spacebar-nginx/containers/spacebar-server-dev-nix/services/postgres.nix30
-rwxr-xr-xhost/Spacebar-nginx/containers/spacebar-server-dev-nix/services/spacebar-server.nix44
6 files changed, 285 insertions, 0 deletions
diff --git a/host/Spacebar-nginx/containers/spacebar-server-dev-nix/container.nix b/host/Spacebar-nginx/containers/spacebar-server-dev-nix/container.nix
new file mode 100755
index 0000000..bed6690
--- /dev/null
+++ b/host/Spacebar-nginx/containers/spacebar-server-dev-nix/container.nix
@@ -0,0 +1,24 @@
+{ pkgs, lib, spacebar-server, ... }:
+
+{
+  privateNetwork = true;
+  autoStart = true;
+  specialArgs = {
+    inherit spacebar-server;
+  };  
+  config = { lib, pkgs, spacebar-server, ... }: {
+    imports = [ ./root.nix ];
+    environment.etc."resolv.conf".text = ''
+      nameserver 8.8.8.8
+      nameserver 8.4.4.8
+      nameserver 1.1.1.1
+      nameserver 1.0.0.1
+      '';
+    networking.firewall = {
+      enable = true;
+      allowedTCPPorts = [ 80 ];
+    };
+  };  
+  hostAddress = "192.168.101.1";
+  localAddress = "192.168.100.1";
+}
\ No newline at end of file
diff --git a/host/Spacebar-nginx/containers/spacebar-server-dev-nix/import.nix b/host/Spacebar-nginx/containers/spacebar-server-dev-nix/import.nix
new file mode 100644
index 0000000..5daacfb
--- /dev/null
+++ b/host/Spacebar-nginx/containers/spacebar-server-dev-nix/import.nix
@@ -0,0 +1,41 @@
+{ config, pkgs, lib, spacebar-server, secrets, ... }:
+
+{
+    containers."spacebarchat-server-dev-nix" = import ./container.nix {
+      inherit pkgs lib spacebar-server;
+    };
+
+    security.acme.certs."dev-nix.server.spacebar.chat" = {
+      domain = "dev-nix.server.spacebar.chat";
+      extraDomainNames = [ "*.dev-nix.server.spacebar.chat" ];
+      group = "nginx";
+      dnsProvider = "cloudflare";
+      credentialsFile = pkgs.writeTextFile {
+        name = "cloudflare-credentials";
+        text = ''
+          # Cloudflare API credentials used by lego
+          # https://go-acme.github.io/lego/dns/cloudflare/
+          CLOUDFLARE_DNS_API_TOKEN=${secrets.secret_keys.cloudflare_dns}
+        '';
+      };
+    };
+
+    services.nginx.virtualHosts."*.dev-nix.server.spacebar.chat" = {
+      serverName = "*.dev-nix.server.spacebar.chat";
+      useACMEHost = "dev-nix.server.spacebar.chat";
+      forceSSL = true;
+      locations."/" = {
+        proxyPass = "http://192.168.100.1";
+      };
+    };
+    services.nginx.virtualHosts."dev-nix.server.spacebar.chat" = {
+      serverName = "dev-nix.server.spacebar.chat";
+      useACMEHost = "dev-nix.server.spacebar.chat";
+      forceSSL = true;
+      locations."/" = {
+        proxyPass = "http://192.168.100.1";
+      };
+    };
+
+  system.stateVersion = "22.11"; # DO NOT EDIT!
+}
\ No newline at end of file
diff --git a/host/Spacebar-nginx/containers/spacebar-server-dev-nix/root.nix b/host/Spacebar-nginx/containers/spacebar-server-dev-nix/root.nix
new file mode 100755
index 0000000..bb005f9
--- /dev/null
+++ b/host/Spacebar-nginx/containers/spacebar-server-dev-nix/root.nix
@@ -0,0 +1,31 @@
+{ config, pkgs, lib, ... }:
+
+{
+  imports =
+    [
+      ./services/nginx.nix
+      ./services/spacebar-server.nix
+    ];
+
+  environment.systemPackages = with pkgs; [
+    wget
+    neofetch
+    lnav
+    zsh
+    git
+    lsd
+    htop
+    btop
+    duf
+    kitty.terminfo
+    neovim
+    tmux
+    jq
+    yq
+    pv
+    dig
+    cloud-utils
+    neovim
+  ];
+  system.stateVersion = "22.11"; # DO NOT EDIT!
+}
\ No newline at end of file
diff --git a/host/Spacebar-nginx/containers/spacebar-server-dev-nix/services/nginx.nix b/host/Spacebar-nginx/containers/spacebar-server-dev-nix/services/nginx.nix
new file mode 100755
index 0000000..d5b8366
--- /dev/null
+++ b/host/Spacebar-nginx/containers/spacebar-server-dev-nix/services/nginx.nix
@@ -0,0 +1,115 @@
+{ config, pkgs, lib, spacebar-server, ... }:
+
+{
+  services = {
+    nginx = {
+      enable = true;
+      package = pkgs.nginxQuic;
+      recommendedProxySettings = true;
+      recommendedZstdSettings = true;
+      recommendedGzipSettings = true;
+      recommendedBrotliSettings = true;
+      recommendedOptimisation = true;
+      appendConfig = ''
+        worker_processes 16;
+        '';
+       eventsConfig = ''
+        #use kqueue;
+        worker_connections 512;
+        '';
+      appendHttpConfig = ''
+        #sendfile on;
+        disable_symlinks off;
+      '';
+      additionalModules = with pkgs.nginxModules; [
+        moreheaders
+      ];
+      virtualHosts = {
+        "dev-nix.server.spacebar.chat" = {
+          locations."= /.well-known/spacebarchat/client".extraConfig = ''
+            more_set_headers 'Content-Type application/json';
+            more_set_headers 'Access-Control-Allow-Origin *';
+            return 200 '${builtins.toJSON {
+              cdn = "cdn.dev-nix.server.spacebar.chat";
+              gateway = "gateway.dev-nix.server.spacebar.chat";
+              api = "api.dev-nix.server.spacebar.chat";
+            }}';
+          '';
+        };
+        "api.dev-nix.server.spacebar.chat" = {
+          locations."/" = {
+            proxyPass = "http://127.0.0.1:3001"; 
+            extraConfig = ''
+              if ($request_method = 'OPTIONS') {
+                more_set_headers 'Access-Control-Allow-Origin: *';
+                more_set_headers 'Access-Control-Allow-Methods: *';
+                #
+                # Custom headers and headers various browsers *should* be OK with but aren't
+                #
+                more_set_headers 'Access-Control-Allow-Headers: *';
+                #
+                # Tell client that this pre-flight info is valid for 20 days
+                #
+                more_set_headers 'Access-Control-Max-Age: 1728000';
+                more_set_headers 'Content-Type: text/plain; charset=utf-8';
+                more_set_headers 'Content-Length: 0';
+                return 204;
+              }
+            '';
+          };
+        };
+        "cdn.dev-nix.server.spacebar.chat" = {
+          locations."/" = {
+            proxyPass = "http://127.0.0.1:3003"; 
+            extraConfig = ''
+              if ($request_method = 'OPTIONS') {
+                more_set_headers 'Access-Control-Allow-Origin: *';
+                more_set_headers 'Access-Control-Allow-Methods: *';
+                #
+                # Custom headers and headers various browsers *should* be OK with but aren't
+                #
+                more_set_headers 'Access-Control-Allow-Headers: *';
+                #
+                # Tell client that this pre-flight info is valid for 20 days
+                #
+                more_set_headers 'Access-Control-Max-Age: 1728000';
+                more_set_headers 'Content-Type: text/plain; charset=utf-8';
+                more_set_headers 'Content-Length: 0';
+                return 204;
+              }
+            '';
+          };
+        };
+        "gateway.dev-nix.server.spacebar.chat" = {
+          locations."/" = {
+            proxyPass = "http://127.0.0.1:3002"; 
+            extraConfig = ''
+              if ($request_method = 'OPTIONS') {
+                more_set_headers 'Access-Control-Allow-Origin: *';
+                more_set_headers 'Access-Control-Allow-Methods: *';
+                #
+                # Custom headers and headers various browsers *should* be OK with but aren't
+                #
+                more_set_headers 'Access-Control-Allow-Headers: *';
+                #
+                # Tell client that this pre-flight info is valid for 20 days
+                #
+                more_set_headers 'Access-Control-Max-Age: 1728000';
+                more_set_headers 'Content-Type: text/plain; charset=utf-8';
+                more_set_headers 'Content-Length: 0';
+                return 204;
+              }
+            '';
+          };
+        };
+      };
+    };
+  };
+  systemd.services.nginx.serviceConfig = {
+    LimitNOFILE=5000000;
+  };
+  security.acme.acceptTerms = true;
+  security.acme.defaults.email = "root@rory.gay";
+
+  system.stateVersion = "22.11"; # DO NOT EDIT!
+}
diff --git a/host/Spacebar-nginx/containers/spacebar-server-dev-nix/services/postgres.nix b/host/Spacebar-nginx/containers/spacebar-server-dev-nix/services/postgres.nix
new file mode 100644
index 0000000..97ea3e6
--- /dev/null
+++ b/host/Spacebar-nginx/containers/spacebar-server-dev-nix/services/postgres.nix
@@ -0,0 +1,30 @@
+{ config, pkgs, lib, ... }:
+
+{
+  #systemd.tmpfiles.rules = [  "d /data/pg 0750 postgres postgres" ];
+
+  services.postgresql = {
+    enable = true;
+    package = pkgs.postgresql_15;
+    enableTCPIP = true;
+    authentication = pkgs.lib.mkOverride 10 ''
+      # TYPE, DATABASE, USER, ADDRESS, METHOD
+      local all all trust
+      host all all 127.0.0.1/32 trust
+      host all all ::1/128 trust
+      host all all 0.0.0.0/0 md5
+    '';
+    initialScript = pkgs.writeText "backend-initScript" ''
+      CREATE ROLE spacebar WITH LOGIN PASSWORD 'spacebar' CREATEDB;
+      CREATE DATABASE spacebar;
+      GRANT ALL PRIVILEGES ON DATABASE spacebar TO spacebar;
+    '';
+    #dataDir = "/data/pg";
+    settings = {
+      "max_connections" = "100";
+      "shared_buffers" = "128MB";
+      "max_wal_size" = "1GB";
+      "min_wal_size" = "80MB";
+    };
+  };
+}
\ No newline at end of file
diff --git a/host/Spacebar-nginx/containers/spacebar-server-dev-nix/services/spacebar-server.nix b/host/Spacebar-nginx/containers/spacebar-server-dev-nix/services/spacebar-server.nix
new file mode 100755
index 0000000..d207687
--- /dev/null
+++ b/host/Spacebar-nginx/containers/spacebar-server-dev-nix/services/spacebar-server.nix
@@ -0,0 +1,44 @@
+{ config, pkgs, lib, spacebar-server, ... }:
+
+{
+  systemd.services = {
+    "spacebar-server-api" = {
+        wantedBy = [ "multi-user.target" ];
+        after = [ "resolvconf.target" ];
+        serviceConfig = {
+            ExecStart = ''
+              ${spacebar-server.packages.${pkgs.system}.default}/bin/start-api
+              '';
+            #Restart = "never";
+            User = "root";
+            WorkingDirectory = "/var/lib/spacebar-server";
+        };
+    };
+    "spacebar-server-gateway" = {
+        wantedBy = [ "multi-user.target" ];
+        after = [ "resolvconf.target" ];
+        serviceConfig = {
+            ExecStart = ''
+              ${spacebar-server.packages.${pkgs.system}.default}/bin/start-gateway
+              '';
+            #Restart = "never";
+            User = "root";
+            WorkingDirectory = "/var/lib/spacebar-server";
+        };
+    };
+    "spacebar-server-cdn" = {
+        wantedBy = [ "multi-user.target" ];
+        after = [ "resolvconf.target" ];
+        serviceConfig = {
+            ExecStart = ''
+              ${spacebar-server.packages.${pkgs.system}.default}/bin/start-cdn
+              '';
+            #Restart = "never";
+            User = "root";
+            WorkingDirectory = "/var/lib/spacebar-server";
+        };
+    };
+  };
+
+  system.stateVersion = "22.11"; # DO NOT EDIT!
+}