selfprivacy-nixos-config/sp-modules/pleroma/module.nix

139 lines
4.1 KiB
Nix
Raw Normal View History

2023-12-02 23:26:29 +00:00
{ config, lib, pkgs, ... }:
let
secrets-filepath = "/etc/selfprivacy/secrets.json";
2024-02-15 09:56:12 +00:00
cfg = config.selfprivacy.modules.pleroma;
2023-12-02 23:26:29 +00:00
inherit (import ./common.nix config) secrets-exs sp;
in
{
options.selfprivacy.modules.pleroma = {
enable = lib.mkOption {
default = false;
2023-12-28 08:54:59 +00:00
type = lib.types.bool;
2023-12-02 23:26:29 +00:00
};
location = lib.mkOption {
2023-12-28 08:54:59 +00:00
type = lib.types.str;
2023-12-02 23:26:29 +00:00
};
2024-02-15 09:56:12 +00:00
subdomain = lib.mkOption {
default = "social";
type = lib.types.strMatching "[A-Za-z0-9][A-Za-z0-9\-]{0,61}[A-Za-z0-9]";
};
2023-12-02 23:26:29 +00:00
};
2024-02-15 09:56:12 +00:00
config = lib.mkIf cfg.enable {
2023-12-02 23:26:29 +00:00
fileSystems = lib.mkIf sp.useBinds {
"/var/lib/pleroma" = {
2024-02-15 09:56:12 +00:00
device = "/volumes/${cfg.location}/pleroma";
options = [
"bind"
"x-systemd.required-by=pleroma-secrets.service"
"x-systemd.required-by=pleroma.service"
"x-systemd.before=pleroma.service"
"x-systemd.before=pleroma-secrets.service"
];
2023-12-02 23:26:29 +00:00
};
"/var/lib/postgresql" = {
2024-02-15 09:56:12 +00:00
device = "/volumes/${cfg.location}/postgresql";
options = [
"bind"
"x-systemd.required-by=pleroma-secrets.service"
"x-systemd.required-by=pleroma.service"
"x-systemd.before=pleroma-secrets.service"
"x-systemd.before=pleroma.service"
];
2023-12-02 23:26:29 +00:00
};
};
services = {
pleroma = {
enable = true;
user = "pleroma";
group = "pleroma";
configs = [
(builtins.replaceStrings
[ "$DOMAIN" "$LUSER" ]
[ sp.domain sp.username ]
(builtins.readFile ./config.exs.in))
];
};
postgresql = {
enable = true;
package = pkgs.postgresql_12;
initialScript = "/etc/setup.psql";
ensureDatabases = [
"pleroma"
];
ensureUsers = [
{
name = "pleroma";
2023-12-18 18:16:40 +00:00
ensureDBOwnership = true;
2023-12-02 23:26:29 +00:00
}
];
};
};
systemd.services.pleroma-secrets = {
before = [ "pleroma.service" ];
requiredBy = [ "pleroma.service" ];
serviceConfig.Type = "oneshot";
path = with pkgs; [ coreutils jq ];
script = ''
set -o nounset
2023-12-03 06:37:37 +00:00
password="$(jq -re '.databasePassword' ${secrets-filepath})"
2023-12-02 23:26:29 +00:00
filecontents=$(cat <<- EOF
import Config
config :pleroma, Pleroma.Repo,
password: "$password"
EOF
)
install -C -m 0700 -o pleroma -g pleroma -d /var/lib/pleroma
install -C -m 0600 -o pleroma -g pleroma -DT \
2023-12-02 23:26:29 +00:00
<(printf "%s" "$filecontents") ${secrets-exs}
'';
};
environment.etc."setup.psql".text = ''
CREATE USER pleroma;
CREATE DATABASE pleroma OWNER pleroma;
\c pleroma;
--Extensions made by ecto.migrate that need superuser access
CREATE EXTENSION IF NOT EXISTS citext;
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
'';
users.users.pleroma = {
extraGroups = [ "postgres" ];
isNormalUser = false;
isSystemUser = true;
group = "pleroma";
};
# seems to be an upstream nixpkgs/nixos bug (missing hexdump)
systemd.services.pleroma.path = [ pkgs.util-linux ];
2024-02-15 09:56:12 +00:00
services.nginx.virtualHosts."${cfg.subdomain}.${sp.domain}" = {
useACMEHost = sp.domain;
root = "/var/www/${cfg.subdomain}.${sp.domain}";
2023-12-18 21:07:05 +00:00
forceSSL = true;
extraConfig = ''
add_header Strict-Transport-Security $hsts_header;
#add_header Content-Security-Policy "script-src 'self'; object-src 'none'; base-uri 'none';" always;
add_header 'Referrer-Policy' 'origin-when-cross-origin';
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
proxy_cookie_path / "/; secure; HttpOnly; SameSite=strict";
'';
locations = {
"/" = {
proxyPass = "http://127.0.0.1:4000";
};
};
};
2024-07-26 23:52:21 +00:00
systemd = {
services = {
pleroma.serviceConfig.Slice = "pleroma.slice";
};
slices.pleroma = {
description = "Pleroma service slice";
};
};
2023-12-02 23:26:29 +00:00
};
}