2021-09-23 15:27:42 +01:00
|
|
|
{ lib, pkgs, config, ... }:
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services.lemmy;
|
|
|
|
settingsFormat = pkgs.formats.json { };
|
|
|
|
in
|
|
|
|
{
|
|
|
|
meta.maintainers = with maintainers; [ happysalada ];
|
|
|
|
# Don't edit the docbook xml directly, edit the md and generate it:
|
|
|
|
# `pandoc lemmy.md -t docbook --top-level-division=chapter --extract-media=media -f markdown+smart > lemmy.xml`
|
|
|
|
meta.doc = ./lemmy.xml;
|
|
|
|
|
2022-09-12 09:34:55 +01:00
|
|
|
imports = [
|
|
|
|
(mkRemovedOptionModule [ "services" "lemmy" "jwtSecretPath" ] "As of v0.13.0, Lemmy auto-generates the JWT secret.")
|
|
|
|
];
|
|
|
|
|
2021-09-23 15:27:42 +01:00
|
|
|
options.services.lemmy = {
|
|
|
|
|
2022-08-28 20:18:44 +01:00
|
|
|
enable = mkEnableOption (lib.mdDoc "lemmy a federated alternative to reddit in rust");
|
2021-09-23 15:27:42 +01:00
|
|
|
|
|
|
|
ui = {
|
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 1234;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Port where lemmy-ui should listen for incoming requests.";
|
2021-09-23 15:27:42 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-08-28 20:18:44 +01:00
|
|
|
caddy.enable = mkEnableOption (lib.mdDoc "exposing lemmy with the caddy reverse proxy");
|
2021-09-23 15:27:42 +01:00
|
|
|
|
2022-09-19 08:09:38 +01:00
|
|
|
database.createLocally = mkEnableOption (lib.mdDoc "creation of database on the instance");
|
|
|
|
|
2021-09-23 15:27:42 +01:00
|
|
|
settings = mkOption {
|
|
|
|
default = { };
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Lemmy configuration";
|
2021-09-23 15:27:42 +01:00
|
|
|
|
|
|
|
type = types.submodule {
|
|
|
|
freeformType = settingsFormat.type;
|
|
|
|
|
|
|
|
options.hostname = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = null;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "The domain name of your instance (eg 'lemmy.ml').";
|
2021-09-23 15:27:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
options.port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 8536;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Port where lemmy should listen for incoming requests.";
|
2021-09-23 15:27:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
options.federation = {
|
2022-08-28 20:18:44 +01:00
|
|
|
enabled = mkEnableOption (lib.mdDoc "activitypub federation");
|
2021-09-23 15:27:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
options.captcha = {
|
|
|
|
enabled = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Enable Captcha.";
|
2021-09-23 15:27:42 +01:00
|
|
|
};
|
|
|
|
difficulty = mkOption {
|
|
|
|
type = types.enum [ "easy" "medium" "hard" ];
|
|
|
|
default = "medium";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "The difficultly of the captcha to solve.";
|
2021-09-23 15:27:42 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
config =
|
|
|
|
lib.mkIf cfg.enable {
|
|
|
|
services.lemmy.settings = (mapAttrs (name: mkDefault)
|
|
|
|
{
|
|
|
|
bind = "127.0.0.1";
|
|
|
|
tls_enabled = true;
|
|
|
|
pictrs_url = with config.services.pict-rs; "http://${address}:${toString port}";
|
|
|
|
actor_name_max_length = 20;
|
|
|
|
|
|
|
|
rate_limit.message = 180;
|
|
|
|
rate_limit.message_per_second = 60;
|
|
|
|
rate_limit.post = 6;
|
|
|
|
rate_limit.post_per_second = 600;
|
|
|
|
rate_limit.register = 3;
|
|
|
|
rate_limit.register_per_second = 3600;
|
|
|
|
rate_limit.image = 6;
|
|
|
|
rate_limit.image_per_second = 3600;
|
|
|
|
} // {
|
|
|
|
database = mapAttrs (name: mkDefault) {
|
|
|
|
user = "lemmy";
|
|
|
|
host = "/run/postgresql";
|
|
|
|
port = 5432;
|
|
|
|
database = "lemmy";
|
|
|
|
pool_size = 5;
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-09-16 08:28:29 +01:00
|
|
|
services.postgresql = mkIf cfg.database.createLocally {
|
2021-09-23 15:27:42 +01:00
|
|
|
enable = mkDefault true;
|
|
|
|
};
|
|
|
|
|
|
|
|
services.pict-rs.enable = true;
|
|
|
|
|
|
|
|
services.caddy = mkIf cfg.caddy.enable {
|
|
|
|
enable = mkDefault true;
|
|
|
|
virtualHosts."${cfg.settings.hostname}" = {
|
|
|
|
extraConfig = ''
|
|
|
|
handle_path /static/* {
|
|
|
|
root * ${pkgs.lemmy-ui}/dist
|
|
|
|
file_server
|
|
|
|
}
|
|
|
|
@for_backend {
|
2022-09-09 09:18:41 +01:00
|
|
|
path /api/* /pictrs/* /feeds/* /nodeinfo/*
|
2021-09-23 15:27:42 +01:00
|
|
|
}
|
|
|
|
handle @for_backend {
|
|
|
|
reverse_proxy 127.0.0.1:${toString cfg.settings.port}
|
|
|
|
}
|
|
|
|
@post {
|
|
|
|
method POST
|
|
|
|
}
|
|
|
|
handle @post {
|
|
|
|
reverse_proxy 127.0.0.1:${toString cfg.settings.port}
|
|
|
|
}
|
|
|
|
@jsonld {
|
|
|
|
header Accept "application/activity+json"
|
|
|
|
header Accept "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
|
|
|
|
}
|
|
|
|
handle @jsonld {
|
|
|
|
reverse_proxy 127.0.0.1:${toString cfg.settings.port}
|
|
|
|
}
|
|
|
|
handle {
|
|
|
|
reverse_proxy 127.0.0.1:${toString cfg.ui.port}
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
assertions = [{
|
2022-09-16 08:28:29 +01:00
|
|
|
assertion = cfg.database.createLocally -> cfg.settings.database.host == "localhost" || cfg.settings.database.host == "/run/postgresql";
|
2021-09-23 15:27:42 +01:00
|
|
|
message = "if you want to create the database locally, you need to use a local database";
|
|
|
|
}];
|
|
|
|
|
|
|
|
systemd.services.lemmy = {
|
|
|
|
description = "Lemmy server";
|
|
|
|
|
|
|
|
environment = {
|
|
|
|
LEMMY_CONFIG_LOCATION = "/run/lemmy/config.hjson";
|
|
|
|
|
|
|
|
# Verify how this is used, and don't put the password in the nix store
|
|
|
|
LEMMY_DATABASE_URL = with cfg.settings.database;"postgres:///${database}?host=${host}";
|
|
|
|
};
|
|
|
|
|
|
|
|
documentation = [
|
|
|
|
"https://join-lemmy.org/docs/en/administration/from_scratch.html"
|
|
|
|
"https://join-lemmy.org/docs"
|
|
|
|
];
|
|
|
|
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
2022-09-19 08:09:38 +01:00
|
|
|
after = [ "pict-rs.service" ] ++ lib.optionals cfg.database.createLocally [ "lemmy-postgresql.service" ];
|
2021-09-23 15:27:42 +01:00
|
|
|
|
2022-09-19 08:09:38 +01:00
|
|
|
requires = lib.optionals cfg.database.createLocally [ "lemmy-postgresql.service" ];
|
2021-09-23 15:27:42 +01:00
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
DynamicUser = true;
|
|
|
|
RuntimeDirectory = "lemmy";
|
2022-09-12 09:54:27 +01:00
|
|
|
ExecStartPre = "${pkgs.coreutils}/bin/install -m 600 ${settingsFormat.generate "config.hjson" cfg.settings} /run/lemmy/config.hjson";
|
|
|
|
ExecStart = "${pkgs.lemmy-server}/bin/lemmy_server";
|
2021-09-23 15:27:42 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.lemmy-ui = {
|
|
|
|
description = "Lemmy ui";
|
|
|
|
|
|
|
|
environment = {
|
|
|
|
LEMMY_UI_HOST = "127.0.0.1:${toString cfg.ui.port}";
|
|
|
|
LEMMY_INTERNAL_HOST = "127.0.0.1:${toString cfg.settings.port}";
|
|
|
|
LEMMY_EXTERNAL_HOST = cfg.settings.hostname;
|
|
|
|
LEMMY_HTTPS = "false";
|
|
|
|
};
|
|
|
|
|
|
|
|
documentation = [
|
|
|
|
"https://join-lemmy.org/docs/en/administration/from_scratch.html"
|
|
|
|
"https://join-lemmy.org/docs"
|
|
|
|
];
|
|
|
|
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
|
|
|
after = [ "lemmy.service" ];
|
|
|
|
|
|
|
|
requires = [ "lemmy.service" ];
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
DynamicUser = true;
|
|
|
|
WorkingDirectory = "${pkgs.lemmy-ui}";
|
|
|
|
ExecStart = "${pkgs.nodejs}/bin/node ${pkgs.lemmy-ui}/dist/js/server.js";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-09-19 08:09:38 +01:00
|
|
|
systemd.services.lemmy-postgresql = mkIf cfg.database.createLocally {
|
2021-09-23 15:27:42 +01:00
|
|
|
description = "Lemmy postgresql db";
|
|
|
|
after = [ "postgresql.service" ];
|
|
|
|
partOf = [ "lemmy.service" ];
|
|
|
|
script = with cfg.settings.database; ''
|
|
|
|
PSQL() {
|
|
|
|
${config.services.postgresql.package}/bin/psql --port=${toString cfg.settings.database.port} "$@"
|
|
|
|
}
|
|
|
|
# check if the database already exists
|
|
|
|
if ! PSQL -lqt | ${pkgs.coreutils}/bin/cut -d \| -f 1 | ${pkgs.gnugrep}/bin/grep -qw ${database} ; then
|
|
|
|
PSQL -tAc "CREATE ROLE ${user} WITH LOGIN;"
|
|
|
|
PSQL -tAc "CREATE DATABASE ${database} WITH OWNER ${user};"
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
serviceConfig = {
|
|
|
|
User = config.services.postgresql.superUser;
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|