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
+ }
0 commit comments