Skip to content

Commit 739087f

Browse files
jfrocheyvan-sraka
authored andcommitted
Add basic nixos test
Only test that the service starts for now.
1 parent 1e1a1fd commit 739087f

File tree

5 files changed

+191
-123
lines changed

5 files changed

+191
-123
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ www/.DS_Store
1717
www/node_modules
1818
npm-debug.log
1919
.data
20+
2021
result
22+
.nixos-test-history

nix/checks/nixos.nix

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
pkgs,
3+
flake,
4+
perSystem,
5+
...
6+
}:
7+
flake.inputs.nixpkgs.lib.nixos.runTest {
8+
name = "auth";
9+
hostPkgs = pkgs;
10+
node.specialArgs = { inherit flake perSystem; };
11+
nodes.server =
12+
{ config, ... }:
13+
{
14+
imports = [
15+
(import flake.nixosModules.auth)
16+
];
17+
18+
virtualisation = {
19+
forwardPorts = [
20+
{
21+
from = "host";
22+
host.port = 13022;
23+
guest.port = 22;
24+
}
25+
];
26+
};
27+
services.openssh = {
28+
enable = true;
29+
};
30+
31+
services.auth.enable = true;
32+
33+
services.postgresql = {
34+
enable = true;
35+
enableTCPIP = true;
36+
initialScript = pkgs.writeText "init-postgres-with-password" ''
37+
CREATE USER supabase_admin LOGIN CREATEROLE CREATEDB REPLICATION BYPASSRLS;
38+
39+
-- Supabase super admin
40+
CREATE USER supabase_auth_admin NOINHERIT CREATEROLE LOGIN NOREPLICATION PASSWORD 'secret';
41+
CREATE SCHEMA IF NOT EXISTS auth AUTHORIZATION supabase_auth_admin;
42+
GRANT CREATE ON DATABASE postgres TO supabase_auth_admin;
43+
ALTER USER supabase_auth_admin SET search_path = 'auth';
44+
'';
45+
authentication = ''
46+
host supabase_auth_admin postgres samenet scram-sha-256
47+
'';
48+
};
49+
};
50+
testScript =
51+
{ nodes, ... }:
52+
''
53+
start_all()
54+
55+
server.wait_for_unit("multi-user.target")
56+
server.wait_for_unit("postgresql.service")
57+
58+
server.wait_for_unit("gotrue.service")
59+
'';
60+
}

nix/modules/nixos/auth.nix

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
{
2+
pkgs,
3+
lib,
4+
config,
5+
perSystem,
6+
...
7+
}:
8+
let
9+
cfg = config.services.auth;
10+
gotrue = perSystem.self.default;
11+
default_settings = rec {
12+
API_EXTERNAL_URL = "http://localhost:9999";
13+
DB_HOST = "localhost";
14+
DB_NAME = "postgres";
15+
DB_PASSWORD = "secret";
16+
DB_PORT = "5432";
17+
DB_USER = "supabase_auth_admin";
18+
DISABLE_SIGNUP = "false";
19+
DATABASE_URL = "postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}";
20+
GOTRUE_API_EXTERNAL_URL = "http://localhost:9999";
21+
GOTRUE_DB_DRIVER = "postgres";
22+
GOTRUE_DB_HOST = DB_HOST;
23+
GOTRUE_DB_NAME = DB_NAME;
24+
GOTRUE_DB_PASSWORD = DB_PASSWORD;
25+
GOTRUE_DB_PORT = DB_PORT;
26+
GOTRUE_DB_USER = DB_USER;
27+
GOTRUE_DISABLE_SIGNUP = "false";
28+
GOTRUE_JWT_DEFAULT_GROUP_NAME = "authenticated";
29+
GOTRUE_JWT_EXP = "3600";
30+
GOTRUE_JWT_SECRET = "your-super-secret-jwt-token-with-at-least-32-characters-long";
31+
GOTRUE_MAILER_AUTOCONFIRM = "true";
32+
GOTRUE_SITE_URL = "http://localhost:3000";
33+
GOTRUE_SMTP_ADMIN_EMAIL = "[email protected]";
34+
GOTRUE_SMTP_HOST = "localhost";
35+
GOTRUE_SMTP_PASS = "";
36+
GOTRUE_SMTP_PORT = "2500";
37+
GOTRUE_SMTP_SENDER_NAME = "Supabase";
38+
GOTRUE_SMTP_USER = "";
39+
JWT_DEFAULT_GROUP_NAME = "authenticated";
40+
JWT_EXP = "3600";
41+
JWT_SECRET = "your-super-secret-jwt-token-with-at-least-32-characters-long";
42+
MAILER_AUTOCONFIRM = "true";
43+
SITE_URL = "http://localhost:3000";
44+
SMTP_ADMIN_EMAIL = "[email protected]";
45+
SMTP_HOST = "localhost";
46+
SMTP_PASS = "";
47+
SMTP_PORT = "2500";
48+
SMTP_SENDER_NAME = "Supabase";
49+
SMTP_USER = "";
50+
};
51+
auth_env = pkgs.writeText "auth.env" (
52+
lib.concatStringsSep "\n" (
53+
(lib.mapAttrsToList (name: value: "${name}=${value}") (default_settings // cfg.settings))
54+
)
55+
);
56+
in
57+
{
58+
options.services.auth = {
59+
enable = lib.mkEnableOption "Supabase Auth Service";
60+
61+
package = lib.mkOption {
62+
type = lib.types.package;
63+
default = pkgs.callPackage ../../package.nix { };
64+
description = "The Supabase Auth package to use.";
65+
};
66+
67+
port = lib.mkOption {
68+
type = lib.types.port;
69+
default = 9999;
70+
description = "Port to run the auth service on.";
71+
};
72+
73+
settings = lib.mkOption {
74+
type = lib.types.attrs;
75+
default = { };
76+
description = "Configuration settings for the auth service.";
77+
};
78+
};
79+
80+
config = lib.mkIf cfg.enable {
81+
networking.firewall.allowedTCPPorts = [ 9122 ];
82+
83+
users.users.gotrue = {
84+
isSystemUser = true;
85+
description = "gotrue service user";
86+
group = "gotrue";
87+
};
88+
users.groups.gotrue = { };
89+
90+
systemd.services.gotrue = {
91+
description = "gotrue (auth)";
92+
wantedBy = [ "multi-user.target" ];
93+
serviceConfig = {
94+
Type = "simple";
95+
WorkingDirectory = "/opt/gotrue";
96+
ExecStart = "${gotrue}/bin/gotrue --config-dir /etc/auth.d";
97+
User = "gotrue";
98+
Restart = "always";
99+
RestartSec = 3;
100+
MemoryAccounting = true;
101+
MemoryMax = "50%";
102+
Slice = "services.slice";
103+
EnvironmentFile = [
104+
"/etc/gotrue/auth.env"
105+
"-/etc/gotrue.generated.env"
106+
"-/etc/gotrue.overrides.env"
107+
];
108+
# preStart = ''
109+
# pg_isready -h ${config.auth.settings.DB_HOST} -p ${config.auth.settings.DB_PORT} -U ${config.auth.settings.DB_USER}; do sleep 1; done
110+
# '';
111+
};
112+
};
113+
114+
systemd.tmpfiles.rules = [
115+
"d /etc/auth.d 0755 gotrue gotrue -"
116+
"d /opt/gotrue 0755 gotrue gotrue -"
117+
"C /etc/gotrue/auth.env 0440 gotrue gotrue - ${auth_env}"
118+
];
119+
};
120+
}
121+
122+
# TODO: initialization steps as activation script?
123+
# - Wait for database to be ready:
124+
# until pg_isready -h ${config.auth.settings.DB_HOST} -p ${config.auth.settings.DB_PORT} -U ${config.auth.settings.DB_USER}; do sleep 1; done
125+
# - Run migrations if they exist:
126+
# if [ -d migrations ]; then go run main.go migrate up; fi

nix/modules/nixosModules/auth.nix

Lines changed: 0 additions & 122 deletions
This file was deleted.

nix/package.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{ pkgs, ... }:
22
pkgs.buildGoModule {
33
pname = "supabase-auth";
4-
version = "0.1.0";
4+
version = "2.180.0";
55
src = ./..;
66

77
vendorHash = "sha256-knYvNkEVffWisvb4Dhm5qqtqQ4co9MGoNt6yH6dUll8=";
@@ -10,6 +10,8 @@ pkgs.buildGoModule {
1010
"-tags"
1111
"netgo"
1212
];
13+
14+
# we cannot run test in the sandbox as tests rely on postgresql tcp connection
1315
doCheck = false;
1416

1517
subPackages = [ "." ];

0 commit comments

Comments
 (0)