|
91 | 91 | buildInputs = [ pkgs.bash auth-service ];
|
92 | 92 |
|
93 | 93 | buildPhase = ''
|
94 |
| - mkdir -p $out/etc $out/bin |
| 94 | + mkdir -p $out/etc $out/bin $out/lib/systemd/system |
95 | 95 |
|
96 | 96 | # Write the auth configuration
|
97 | 97 | cat > $out/etc/auth.env <<EOF
|
98 | 98 | # Auth configuration generated by Nix
|
99 | 99 | ${lib.concatStringsSep "\n" (lib.mapAttrsToList (name: value: "${name}=${value}") config.config.auth.settings)}
|
100 | 100 | EOF
|
101 | 101 |
|
| 102 | + # Write the systemd unit file |
| 103 | + cat > $out/lib/systemd/system/gotrue.service <<EOF |
| 104 | + [Unit] |
| 105 | + Description=Gotrue |
| 106 | +
|
| 107 | + [Service] |
| 108 | + Type=simple |
| 109 | + WorkingDirectory=/opt/gotrue |
| 110 | + ExecStart=/opt/gotrue/gotrue --config-dir /etc/auth.d |
| 111 | + User=gotrue |
| 112 | + Restart=always |
| 113 | + RestartSec=3 |
| 114 | +
|
| 115 | + MemoryAccounting=true |
| 116 | + MemoryMax=50% |
| 117 | +
|
| 118 | + EnvironmentFile=-/etc/gotrue.generated.env |
| 119 | + EnvironmentFile=/etc/gotrue.env |
| 120 | + EnvironmentFile=-/etc/gotrue.overrides.env |
| 121 | +
|
| 122 | + Slice=services.slice |
| 123 | +
|
| 124 | + [Install] |
| 125 | + WantedBy=multi-user.target |
| 126 | + EOF |
| 127 | +
|
102 | 128 | # Write a script to manage the auth service
|
103 | 129 | cat > $out/bin/manage-auth <<EOF
|
104 | 130 | #!/bin/sh
|
|
133 | 159 | esac
|
134 | 160 | EOF
|
135 | 161 | chmod +x $out/bin/manage-auth
|
| 162 | +
|
| 163 | + # Write the activation script |
| 164 | + cat > $out/bin/activate <<EOF |
| 165 | + #!/bin/sh |
| 166 | + set -e |
| 167 | +
|
| 168 | + # Create necessary directories |
| 169 | + mkdir -p /opt/gotrue |
| 170 | + mkdir -p /etc/auth.d |
| 171 | + mkdir -p /etc/gotrue |
| 172 | +
|
| 173 | + # Create gotrue user if it doesn't exist |
| 174 | + if ! id "gotrue" &>/dev/null; then |
| 175 | + useradd -r -s /bin/false gotrue |
| 176 | + fi |
| 177 | +
|
| 178 | + # Set proper ownership |
| 179 | + chown -R gotrue:gotrue /opt/gotrue |
| 180 | + chown -R gotrue:gotrue /etc/auth.d |
| 181 | + chown -R gotrue:gotrue /etc/gotrue |
| 182 | +
|
| 183 | + # Set proper permissions |
| 184 | + chmod 775 /opt/gotrue |
| 185 | + chmod 775 /etc/auth.d |
| 186 | + chmod 775 /etc/gotrue |
| 187 | +
|
| 188 | + # Copy the binary |
| 189 | + cp ${auth-service}/bin/supabase-auth /opt/gotrue/gotrue |
| 190 | + chown gotrue:gotrue /opt/gotrue/gotrue |
| 191 | + chmod 755 /opt/gotrue/gotrue |
| 192 | +
|
| 193 | + # Copy the systemd unit file |
| 194 | + cp $out/lib/systemd/system/gotrue.service /etc/systemd/system/ |
| 195 | + chmod 644 /etc/systemd/system/gotrue.service |
| 196 | +
|
| 197 | + # Copy the environment file |
| 198 | + cp $out/etc/auth.env /etc/gotrue.generated.env |
| 199 | + chown gotrue:gotrue /etc/gotrue.generated.env |
| 200 | + chmod 600 /etc/gotrue.generated.env |
| 201 | +
|
| 202 | + # Create symlinks for easy access |
| 203 | + ln -sf $out/bin/manage-auth /usr/local/bin/gotrue-manage |
| 204 | + ln -sf $out/share/gotrue/gotrue.service /usr/local/share/gotrue/gotrue.service |
| 205 | + ln -sf $out/bin/activate /usr/local/bin/auth-activate |
| 206 | +
|
| 207 | + # Reload systemd |
| 208 | + systemctl daemon-reload |
| 209 | +
|
| 210 | + # Enable and start the service |
| 211 | + systemctl enable gotrue.service |
| 212 | + systemctl restart gotrue.service |
| 213 | +
|
| 214 | + echo "Gotrue service has been activated and started" |
| 215 | + echo "You can manage the service using: gotrue-manage {start|stop|restart|status}" |
| 216 | + EOF |
| 217 | + chmod +x $out/bin/activate |
| 218 | +
|
| 219 | + # Create symlinks to the systemd unit files for easy access |
| 220 | + mkdir -p $out/share/gotrue |
| 221 | + ln -s $out/lib/systemd/system/gotrue.service $out/share/gotrue/gotrue.service |
136 | 222 | '';
|
137 | 223 |
|
138 | 224 | installPhase = "true";
|
139 | 225 | };
|
140 | 226 |
|
141 | 227 | in
|
142 | 228 | {
|
143 |
| - packages.default = authConfigOutput; |
| 229 | + packages = { |
| 230 | + default = authConfigOutput; |
| 231 | + }; |
144 | 232 | devShells.default = pkgs.mkShell {
|
145 | 233 | buildInputs = [
|
146 | 234 | pkgs.bash
|
|
0 commit comments