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