1 { lib, pkgs, config, ... }:
4 cfg = config.services.lemmy-dev;
5 settingsFormat = pkgs.formats.json { };
8 (mkRemovedOptionModule [ "services" "lemmy-dev" "jwtSecretPath" ]
9 "As of v0.13.0, Lemmy auto-generates the JWT secret.")
12 options.services.lemmy-dev = {
14 enable = mkEnableOption
15 (lib.mdDoc "lemmy a federated alternative to reddit in rust");
17 server = { package = mkPackageOptionMD pkgs "lemmy-server" { }; };
20 package = mkPackageOptionMD pkgs "lemmy-ui" { };
26 lib.mdDoc "Port where lemmy-ui should listen for incoming requests.";
31 mkEnableOption (lib.mdDoc "exposing lemmy with the caddy reverse proxy");
33 mkEnableOption (lib.mdDoc "exposing lemmy with the nginx reverse proxy");
37 mkEnableOption (lib.mdDoc "creation of database on the instance");
40 type = with types; nullOr str;
42 description = lib.mdDoc
43 "The connection URI to use. Takes priority over the configuration file if set.";
49 description = lib.mdDoc "Lemmy configuration";
51 type = types.submodule {
52 freeformType = settingsFormat.type;
54 options.hostname = mkOption {
58 lib.mdDoc "The domain name of your instance (eg 'lemmy.ml').";
61 options.port = mkOption {
65 lib.mdDoc "Port where lemmy should listen for incoming requests.";
72 description = lib.mdDoc "Enable Captcha.";
74 difficulty = mkOption {
75 type = types.enum [ "easy" "medium" "hard" ];
77 description = lib.mdDoc "The difficultly of the captcha to solve.";
85 config = lib.mkIf cfg.enable {
86 services.lemmy-dev.settings = (mapAttrs (name: mkDefault) {
89 pictrs_url = with config.services.pict-rs;
90 "http://${address}:${toString port}";
91 actor_name_max_length = 20;
93 rate_limit.message = 180;
94 rate_limit.message_per_second = 60;
96 rate_limit.post_per_second = 600;
97 rate_limit.register = 3;
98 rate_limit.register_per_second = 3600;
100 rate_limit.image_per_second = 3600;
102 database = mapAttrs (name: mkDefault) {
104 host = "/run/postgresql";
111 services.postgresql = mkIf cfg.database.createLocally {
113 ensureDatabases = [ cfg.settings.database.database ];
115 name = cfg.settings.database.user;
116 ensurePermissions."DATABASE ${cfg.settings.database.database}" =
121 services.pict-rs.enable = true;
123 services.caddy = mkIf cfg.caddy.enable {
124 enable = mkDefault true;
125 virtualHosts."${cfg.settings.hostname}" = {
127 handle_path /static/* {
128 root * ${cfg.ui.package}/dist
132 path /api/* /pictrs/* /feeds/* /nodeinfo/*
134 handle @for_backend {
135 reverse_proxy 127.0.0.1:${toString cfg.settings.port}
141 reverse_proxy 127.0.0.1:${toString cfg.settings.port}
144 header Accept "application/activity+json"
145 header Accept "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
148 reverse_proxy 127.0.0.1:${toString cfg.settings.port}
151 reverse_proxy 127.0.0.1:${toString cfg.ui.port}
157 services.nginx = mkIf cfg.nginx.enable {
158 enable = mkDefault true;
159 virtualHosts."${cfg.settings.hostname}".locations = let
160 ui = "http://127.0.0.1:${toString cfg.ui.port}";
161 backend = "http://127.0.0.1:${toString cfg.settings.port}";
163 "~ ^/(api|pictrs|feeds|nodeinfo|.well-known)" = {
166 proxyWebsockets = true;
167 recommendedProxySettings = true;
170 # mixed frontend and backend requests, based on the request headers
171 proxyPass = "$proxpass";
172 recommendedProxySettings = true;
174 set $proxpass "${ui}";
175 if ($http_accept = "application/activity+json") {
176 set $proxpass "${backend}";
178 if ($http_accept = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"") {
179 set $proxpass "${backend}";
181 if ($request_method = POST) {
182 set $proxpass "${backend}";
185 # Cuts off the trailing slash on URLs to make them valid
186 rewrite ^(.+)/+$ $1 permanent;
194 assertion = cfg.database.createLocally -> cfg.settings.database.host
195 == "localhost" || cfg.settings.database.host == "/run/postgresql";
197 "if you want to create the database locally, you need to use a local database";
200 assertion = (!(hasAttrByPath [ "federation" ] cfg.settings))
201 && (!(hasAttrByPath [ "federation" "enabled" ] cfg.settings));
203 "`services.lemmy.settings.federation` was removed in 0.17.0 and no longer has any effect";
207 systemd.services.lemmy-dev = {
208 description = "Lemmy server (dev)";
211 LEMMY_CONFIG_LOCATION =
212 "${settingsFormat.generate "config.hjson" cfg.settings}";
213 LEMMY_DATABASE_URL = mkIf (cfg.database.uri != null) cfg.database.uri;
217 "https://join-lemmy.org/docs/en/admins/from_scratch.html"
218 "https://join-lemmy.org/docs/en/"
221 wantedBy = [ "multi-user.target" ];
223 after = [ "pict-rs.service" ]
224 ++ lib.optionals cfg.database.createLocally [ "postgresql.service" ];
227 lib.optionals cfg.database.createLocally [ "postgresql.service" ];
231 RuntimeDirectory = "lemmy";
232 ExecStart = "${cfg.server.package}/bin/lemmy_server";
236 systemd.services.lemmy-ui-dev = {
237 description = "Lemmy UI (dev)";
240 LEMMY_UI_HOST = "127.0.0.1:${toString cfg.ui.port}";
241 LEMMY_UI_LEMMY_INTERNAL_HOST = "127.0.0.1:${toString cfg.settings.port}";
242 LEMMY_UI_LEMMY_EXTERNAL_HOST = cfg.settings.hostname;
243 LEMMY_UI_HTTPS = "false";
247 "https://join-lemmy.org/docs/en/admins/from_scratch.html"
248 "https://join-lemmy.org/docs/en/"
251 wantedBy = [ "multi-user.target" ];
253 after = [ "lemmy-dev.service" ];
255 requires = [ "lemmy-dev.service" ];
259 WorkingDirectory = "${cfg.ui.package}";
261 "${pkgs.nodejs}/bin/node ${cfg.ui.package}/dist/js/server.js";