Skip to content

Commit 195220e

Browse files
samroseyvan-sraka
authored andcommitted
feat: create an activation script and systemd service
1 parent a92686d commit 195220e

File tree

1 file changed

+90
-2
lines changed

1 file changed

+90
-2
lines changed

flake.nix

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,40 @@
9191
buildInputs = [ pkgs.bash auth-service ];
9292

9393
buildPhase = ''
94-
mkdir -p $out/etc $out/bin
94+
mkdir -p $out/etc $out/bin $out/lib/systemd/system
9595
9696
# Write the auth configuration
9797
cat > $out/etc/auth.env <<EOF
9898
# Auth configuration generated by Nix
9999
${lib.concatStringsSep "\n" (lib.mapAttrsToList (name: value: "${name}=${value}") config.config.auth.settings)}
100100
EOF
101101
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+
102128
# Write a script to manage the auth service
103129
cat > $out/bin/manage-auth <<EOF
104130
#!/bin/sh
@@ -133,14 +159,76 @@
133159
esac
134160
EOF
135161
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
136222
'';
137223

138224
installPhase = "true";
139225
};
140226

141227
in
142228
{
143-
packages.default = authConfigOutput;
229+
packages = {
230+
default = authConfigOutput;
231+
};
144232
devShells.default = pkgs.mkShell {
145233
buildInputs = [
146234
pkgs.bash

0 commit comments

Comments
 (0)