Skip to content

Commit a92686d

Browse files
samroseyvan-sraka
authored andcommitted
feat: a nix package, config and devShell for supabase-auth
1 parent 746c937 commit a92686d

File tree

5 files changed

+309
-0
lines changed

5 files changed

+309
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ www/.DS_Store
1717
www/node_modules
1818
npm-debug.log
1919
.data
20+
result

flake.lock

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
{
2+
description = "Supabase Auth Service with Nix modules and steps";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
6+
flake-utils.url = "github:numtide/flake-utils";
7+
};
8+
9+
outputs = { self, nixpkgs, flake-utils }:
10+
let
11+
systems = [
12+
"x86_64-linux"
13+
"aarch64-linux"
14+
"x86_64-darwin"
15+
"aarch64-darwin"
16+
];
17+
18+
forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f system);
19+
20+
mkAuthConfig = system:
21+
let
22+
pkgs = nixpkgs.legacyPackages.${system};
23+
lib = pkgs.lib;
24+
25+
# Go package
26+
auth-service = pkgs.buildGoModule {
27+
pname = "supabase-auth";
28+
version = "0.1.0";
29+
src = ./.;
30+
31+
vendorHash = "sha256-QBQUUFWT3H3L7ajFV8cgi0QREXnm0ReIisD+4ACfLZQ=";
32+
33+
buildFlags = [ "-tags" "netgo" ];
34+
doCheck = false;
35+
36+
# Specify the main package
37+
subPackages = [ "." ];
38+
39+
# Specify the output binary name
40+
postInstall = ''
41+
mv $out/bin/auth $out/bin/supabase-auth
42+
'';
43+
};
44+
45+
# Evaluate both the auth and steps modules
46+
config = lib.evalModules {
47+
modules = [
48+
./nix/auth-module.nix
49+
./nix/steps-module.nix
50+
{
51+
_module.args.pkgs = pkgs;
52+
auth = {
53+
enable = true;
54+
package = auth-service;
55+
port = 9999;
56+
settings = {
57+
GOTRUE_DB_DRIVER = "postgres";
58+
GOTRUE_SITE_URL = "http://localhost:3000";
59+
SITE_URL = "http://localhost:3000";
60+
GOTRUE_API_EXTERNAL_URL = "http://localhost:9999";
61+
API_EXTERNAL_URL = "http://localhost:9999";
62+
GOTRUE_DB_HOST = "localhost";
63+
GOTRUE_DB_PORT = "5432";
64+
GOTRUE_DB_NAME = "postgres";
65+
GOTRUE_DB_USER = "postgres";
66+
GOTRUE_DB_PASSWORD = "postgres";
67+
DATABASE_URL = "postgres://postgres:postgres@localhost:5432/postgres";
68+
GOTRUE_JWT_SECRET = "your-super-secret-jwt-token-with-at-least-32-characters-long";
69+
GOTRUE_JWT_EXP = "3600";
70+
GOTRUE_JWT_DEFAULT_GROUP_NAME = "authenticated";
71+
GOTRUE_DISABLE_SIGNUP = "false";
72+
GOTRUE_MAILER_AUTOCONFIRM = "true";
73+
GOTRUE_SMTP_ADMIN_EMAIL = "[email protected]";
74+
GOTRUE_SMTP_HOST = "localhost";
75+
GOTRUE_SMTP_PORT = "2500";
76+
GOTRUE_SMTP_USER = "";
77+
GOTRUE_SMTP_PASS = "";
78+
GOTRUE_SMTP_SENDER_NAME = "Supabase";
79+
};
80+
};
81+
steps = {
82+
enable = true;
83+
};
84+
}
85+
];
86+
};
87+
88+
authConfigOutput = pkgs.stdenv.mkDerivation {
89+
name = "auth-config";
90+
src = ./.;
91+
buildInputs = [ pkgs.bash auth-service ];
92+
93+
buildPhase = ''
94+
mkdir -p $out/etc $out/bin
95+
96+
# Write the auth configuration
97+
cat > $out/etc/auth.env <<EOF
98+
# Auth configuration generated by Nix
99+
${lib.concatStringsSep "\n" (lib.mapAttrsToList (name: value: "${name}=${value}") config.config.auth.settings)}
100+
EOF
101+
102+
# Write a script to manage the auth service
103+
cat > $out/bin/manage-auth <<EOF
104+
#!/bin/sh
105+
106+
case "\$1" in
107+
start)
108+
echo "Starting auth service..."
109+
${auth-service}/bin/supabase-auth -c $out/etc/auth.env
110+
# Execute steps if enabled
111+
${lib.optionalString config.config.steps.enable (lib.concatStringsSep "\n" config.config.steps.commands)}
112+
;;
113+
stop)
114+
echo "Stopping auth service..."
115+
pkill -f "supabase-auth"
116+
;;
117+
restart)
118+
echo "Restarting auth service..."
119+
pkill -f "supabase-auth"
120+
${auth-service}/bin/supabase-auth -c $out/etc/auth.env
121+
;;
122+
status)
123+
if pgrep -f "supabase-auth" > /dev/null; then
124+
echo "Auth service is running"
125+
else
126+
echo "Auth service is not running"
127+
fi
128+
;;
129+
*)
130+
echo "Usage: \$0 {start|stop|restart|status}"
131+
exit 1
132+
;;
133+
esac
134+
EOF
135+
chmod +x $out/bin/manage-auth
136+
'';
137+
138+
installPhase = "true";
139+
};
140+
141+
in
142+
{
143+
packages.default = authConfigOutput;
144+
devShells.default = pkgs.mkShell {
145+
buildInputs = [
146+
pkgs.bash
147+
auth-service
148+
pkgs.go
149+
pkgs.gopls
150+
pkgs.gotools
151+
pkgs.go-outline
152+
pkgs.gocode
153+
pkgs.gopkgs
154+
pkgs.godef
155+
pkgs.golint
156+
pkgs.delve
157+
];
158+
shellHook = ''
159+
echo "Build with: nix build ."
160+
echo "Result will be in ./result"
161+
echo "Auth service version: ${auth-service.version}"
162+
'';
163+
};
164+
};
165+
in
166+
{
167+
packages = forAllSystems (system: (mkAuthConfig system).packages);
168+
devShells = forAllSystems (system: (mkAuthConfig system).devShells);
169+
};
170+
}

nix/auth-module.nix

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{ config, lib, pkgs, ... }:
2+
3+
with lib;
4+
5+
let
6+
cfg = config.auth;
7+
in {
8+
options.auth = {
9+
enable = mkEnableOption "Supabase Auth Service";
10+
11+
package = mkOption {
12+
type = types.package;
13+
description = "The Supabase Auth package to use.";
14+
};
15+
16+
port = mkOption {
17+
type = types.port;
18+
default = 9999;
19+
description = "Port to run the auth service on.";
20+
};
21+
22+
settings = mkOption {
23+
type = types.attrs;
24+
default = {
25+
SITE_URL = "http://localhost:3000";
26+
API_EXTERNAL_URL = "http://localhost:9999";
27+
DB_HOST = "localhost";
28+
DB_PORT = "5432";
29+
DB_NAME = "postgres";
30+
DB_USER = "postgres";
31+
DB_PASSWORD = "postgres";
32+
JWT_SECRET = "your-super-secret-jwt-token-with-at-least-32-characters-long";
33+
JWT_EXP = "3600";
34+
JWT_DEFAULT_GROUP_NAME = "authenticated";
35+
DISABLE_SIGNUP = "false";
36+
MAILER_AUTOCONFIRM = "true";
37+
SMTP_ADMIN_EMAIL = "[email protected]";
38+
SMTP_HOST = "localhost";
39+
SMTP_PORT = "2500";
40+
SMTP_USER = "";
41+
SMTP_PASS = "";
42+
SMTP_SENDER_NAME = "Supabase";
43+
};
44+
description = "Configuration settings for the auth service.";
45+
};
46+
};
47+
48+
config = mkIf cfg.enable {
49+
# No NixOS-specific options here
50+
};
51+
}

nix/steps-module.nix

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{ config, lib, pkgs, ... }:
2+
3+
with lib;
4+
5+
let
6+
cfg = config.steps;
7+
in {
8+
options.steps = {
9+
enable = mkEnableOption "Auth service initialization steps";
10+
11+
commands = mkOption {
12+
type = types.listOf types.str;
13+
default = [];
14+
description = "Commands to run during service initialization.";
15+
};
16+
};
17+
18+
config = mkIf cfg.enable {
19+
steps.commands = [
20+
# Wait for database to be ready
21+
#"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"
22+
# Run migrations if they exist
23+
#"if [ -d migrations ]; then go run main.go migrate up; fi"
24+
];
25+
};
26+
}

0 commit comments

Comments
 (0)