lemmy-container/lemmy-container.nix

161 lines
4.6 KiB
Nix
Raw Normal View History

2024-01-20 11:25:09 -08:00
{ config, lib, pkgs, ... }@toplevel:
with lib;
2024-01-24 19:33:59 -08:00
let cfg = config.services.lemmyContainer;
2024-01-20 11:25:09 -08:00
in {
2024-01-20 11:37:14 -08:00
options.services.lemmyContainer = with types; {
2024-01-20 11:25:09 -08:00
enable = mkEnableOption "Enable Lemmy server in a Podman container.";
hostname = mkOption {
type = str;
description = "Host of the Lemmy server.";
};
port = mkOption {
type = port;
description = "Port on which to listen for requests.";
default = 1234;
};
site-name = mkOption {
type = str;
description = "Name of the Lemmy site.";
};
2024-01-26 15:06:19 -08:00
# admin-password-file = mkOption {
# type = str;
# description = "Path to a file containing the administrator password.";
# };
smtp = {
host = mkOption {
type = str;
description = "SMTP server hostname.";
};
port = mkOption {
2024-01-20 11:56:19 -08:00
type = port;
description = "SMTP server port.";
default = 25;
};
};
2024-01-22 15:07:40 -08:00
server-package = mkOption {
type = package;
description = "Package to use for the server.";
default = pkgs.lemmy-server;
};
2024-01-24 19:33:59 -08:00
state-directory = mkOption {
type = str;
description = "Path at which to store server state.";
};
2024-01-20 11:25:09 -08:00
};
config = mkIf cfg.enable {
2024-01-24 19:40:27 -08:00
systemd.tmpfiles.rules = [
2024-01-24 22:14:06 -08:00
"d ${cfg.state-directory}/postgres 0700 root root - -"
"d ${cfg.state-directory}/pictrs 0700 root root - -"
2024-01-24 19:40:27 -08:00
];
2024-01-24 19:33:59 -08:00
containers.lemmy = {
autoStart = true;
privateNetwork = true;
forwardPorts = [{
protocol = "tcp";
hostPort = cfg.port;
containerPort = 80;
}];
2024-01-25 09:57:04 -08:00
ephemeral = true;
2024-01-24 19:33:59 -08:00
bindMounts = {
"/var/lib/postgres/data" = {
hostPath = "${cfg.state-directory}/postgres";
2024-01-25 09:57:04 -08:00
isReadOnly = false;
2024-01-24 19:33:59 -08:00
};
2024-01-25 09:57:04 -08:00
"/var/lib/private" = {
hostPath = "${cfg.state-directory}/pictrs";
isReadOnly = false;
};
2024-01-26 15:06:19 -08:00
# "/run/lemmy-container/admin.passwd" = {
# isReadOnly = true;
# hostPath = cfg.admin-password-file;
# };
2024-01-24 19:33:59 -08:00
};
2024-01-24 23:08:30 -08:00
additionalCapabilities = [ "CAP_SYS_ADMIN" ];
2024-01-24 19:33:59 -08:00
config = {
boot.tmp.useTmpfs = true;
2024-01-24 22:04:18 -08:00
system.nssModules = mkForce [ ];
2024-01-25 22:16:59 -08:00
systemd.services.postgresPasswdGenerator = {
requiredBy = [ "lemmy.service" "postgresql.service" ];
before = [ "lemmy.service" ];
after = [ "postgresql.service" ];
2024-01-25 22:50:22 -08:00
path = with pkgs; [ sudo pwgen config.services.postgresql.package ];
2024-01-25 22:16:59 -08:00
script = ''
PASSWD=$(pwgen 25)
2024-01-25 22:49:28 -08:00
mkdir -p /run/lemmy
2024-01-25 22:16:59 -08:00
echo "postgresql://lemmy:$PASSWD@lemmy&host=/var/run/postgresql" > /run/lemmy/postgresql.passwd
sudo -u postgres psql -c "ALTER USER lemmy ENCRYPTED PASSWORD '$PASSWD';"
'';
};
2024-01-24 19:33:59 -08:00
services = {
nscd.enable = false;
2024-01-25 23:25:17 -08:00
postgresql = {
enable = true;
ensureUsers = [{
name = "lemmy";
ensureDBOwnership = true;
}];
ensureDatabases = [ "lemmy" ];
};
2024-01-24 19:33:59 -08:00
pict-rs.enable = true;
lemmy = {
enable = true;
2024-01-25 22:20:59 -08:00
database.uriFile = "/run/lemmy/postgresql.passwd";
2024-01-26 15:06:19 -08:00
# adminPasswordFile = "/run/lemmy-container/admin.passwd";
2024-01-24 19:33:59 -08:00
nginx.enable = true;
server.package = cfg.server-package;
settings = {
email = {
2024-01-25 11:44:53 -08:00
smtp_server = "${cfg.smtp.host}:${toString cfg.smtp.port}";
2024-01-24 19:33:59 -08:00
smtp_from_address = "noreply@${cfg.hostname}";
2024-01-25 11:59:40 -08:00
tls_type = "starttls";
2024-01-24 19:33:59 -08:00
};
hostname = cfg.hostname;
2024-01-25 14:31:47 -08:00
setup = {
admin_username = "admin";
site_name = cfg.site-name;
};
2024-01-24 19:33:59 -08:00
};
};
nginx = {
recommendedGzipSettings = true;
recommendedOptimisation = true;
recommendedProxySettings = true;
commonHttpConfig = ''
log_format with_response_time '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$request_time" "$upstream_response_time"';
access_log /var/log/nginx/access.log with_response_time;
'';
};
};
};
};
2024-01-20 11:25:09 -08:00
services.nginx = {
enable = true;
virtualHosts."${cfg.hostname}" = {
enableACME = true;
forceSSL = true;
locations."/" = {
proxyPass = "http://127.0.0.1:${toString cfg.port}/";
proxyWebsockets = true;
recommendedProxySettings = true;
};
};
};
};
}