nixos/mastodon: update database configuration

This commit is contained in:
Izorkin 2022-03-22 22:21:47 +03:00
parent e2cebf2134
commit 8e14bf10c2
No known key found for this signature in database
GPG key ID: 1436C1B3F3679F09
3 changed files with 54 additions and 17 deletions

View file

@ -144,6 +144,17 @@
updated manually.
</para>
</listitem>
<listitem>
<para>
In <literal>mastodon</literal> it is now necessary to specify
location of file with <literal>PostgreSQL</literal> database
password. In
<literal>services.mastodon.database.passwordFile</literal>
parameter default value
<literal>/var/lib/mastodon/secrets/db-password</literal> has
been changed to <literal>null</literal>.
</para>
</listitem>
<listitem>
<para>
The <literal>nix.readOnlyStore</literal> option has been
@ -208,6 +219,12 @@
<literal>nixos/modules/profiles/minimal.nix</literal> profile.
</para>
</listitem>
<listitem>
<para>
<literal>mastodon</literal> now supports connection to a
remote <literal>PostgreSQL</literal> database.
</para>
</listitem>
<listitem>
<para>
A new <literal>virtualisation.rosetta</literal> module was

View file

@ -43,6 +43,8 @@ In addition to numerous new and upgraded packages, this release has the followin
- Qt 5.12 and 5.14 have been removed, as the corresponding branches have been EOL upstream for a long time. This affected under 10 packages in nixpkgs, largely unmaintained upstream as well, however, out-of-tree package expressions may need to be updated manually.
- In `mastodon` it is now necessary to specify location of file with `PostgreSQL` database password. In `services.mastodon.database.passwordFile` parameter default value `/var/lib/mastodon/secrets/db-password` has been changed to `null`.
- The `nix.readOnlyStore` option has been renamed to `boot.readOnlyNixStore` to clarify that it configures the NixOS boot process, not the Nix daemon.
## Other Notable Changes {#sec-release-23.05-notable-changes}
@ -64,6 +66,8 @@ In addition to numerous new and upgraded packages, this release has the followin
- The minimal ISO image now uses the `nixos/modules/profiles/minimal.nix` profile.
- `mastodon` now supports connection to a remote `PostgreSQL` database.
- A new `virtualisation.rosetta` module was added to allow running `x86_64` binaries through [Rosetta](https://developer.apple.com/documentation/apple-silicon/about-the-rosetta-translation-environment) inside virtualised NixOS guests on Apple silicon. This feature works by default with the [UTM](https://docs.getutm.app/) virtualisation [package](https://search.nixos.org/packages?channel=unstable&show=utm&from=0&size=1&sort=relevance&type=packages&query=utm).
- Resilio sync secret keys can now be provided using a secrets file at runtime, preventing these secrets from ending up in the Nix store.

View file

@ -1,7 +1,9 @@
{ config, lib, pkgs, ... }:
{ lib, pkgs, config, options, ... }:
let
cfg = config.services.mastodon;
opt = options.services.mastodon;
# We only want to create a database if we're actually going to connect to it.
databaseActuallyCreateLocally = cfg.database.createLocally && cfg.database.host == "/run/postgresql";
@ -23,7 +25,6 @@ let
REDIS_HOST = cfg.redis.host;
REDIS_PORT = toString(cfg.redis.port);
DB_HOST = cfg.database.host;
DB_PORT = toString(cfg.database.port);
DB_NAME = cfg.database.name;
LOCAL_DOMAIN = cfg.localDomain;
SMTP_SERVER = cfg.smtp.host;
@ -37,7 +38,8 @@ let
TRUSTED_PROXY_IP = cfg.trustedProxy;
}
// (if cfg.smtp.authenticate then { SMTP_LOGIN = cfg.smtp.user; } else {})
// lib.optionalAttrs (cfg.database.host != "/run/postgresql" && cfg.database.port != null) { DB_PORT = toString cfg.database.port; }
// lib.optionalAttrs cfg.smtp.authenticate { SMTP_LOGIN = cfg.smtp.user; }
// cfg.extraConfig;
systemCallsList = [ "@cpu-emulation" "@debug" "@keyring" "@ipc" "@mount" "@obsolete" "@privileged" "@setuid" ];
@ -314,8 +316,13 @@ in {
};
port = lib.mkOption {
type = lib.types.port;
default = 5432;
type = lib.types.nullOr lib.types.port;
default = if cfg.database.createLocally then null else 5432;
defaultText = lib.literalExpression ''
if config.${opt.database.createLocally}
then null
else 5432
'';
description = lib.mdDoc "Database host port.";
};
@ -333,8 +340,8 @@ in {
passwordFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = "/var/lib/mastodon/secrets/db-password";
example = "/run/keys/mastodon-db-password";
default = null;
example = "/var/lib/mastodon/secrets/db-password";
description = lib.mdDoc ''
A file containing the password corresponding to
{option}`database.user`.
@ -468,7 +475,18 @@ in {
assertions = [
{
assertion = databaseActuallyCreateLocally -> (cfg.user == cfg.database.user);
message = ''For local automatic database provisioning (services.mastodon.database.createLocally == true) with peer authentication (services.mastodon.database.host == "/run/postgresql") to work services.mastodon.user and services.mastodon.database.user must be identical.'';
message = ''
For local automatic database provisioning (services.mastodon.database.createLocally == true) with peer
authentication (services.mastodon.database.host == "/run/postgresql") to work services.mastodon.user
and services.mastodon.database.user must be identical.
'';
}
{
assertion = !databaseActuallyCreateLocally -> (cfg.database.host != "/run/postgresql");
message = ''
<option>services.mastodon.database.host</option> needs to be set if
<option>services.mastodon.database.createLocally</option> is not enabled.
'';
}
{
assertion = cfg.smtp.authenticate -> (cfg.smtp.user != null);
@ -512,10 +530,11 @@ in {
OTP_SECRET="$(cat ${cfg.otpSecretFile})"
VAPID_PRIVATE_KEY="$(cat ${cfg.vapidPrivateKeyFile})"
VAPID_PUBLIC_KEY="$(cat ${cfg.vapidPublicKeyFile})"
'' + lib.optionalString (cfg.database.passwordFile != null) ''
DB_PASS="$(cat ${cfg.database.passwordFile})"
'' + (if cfg.smtp.authenticate then ''
'' + lib.optionalString cfg.smtp.authenticate ''
SMTP_PASSWORD="$(cat ${cfg.smtp.passwordFile})"
'' else "") + ''
'' + ''
EOF
'';
environment = env;
@ -555,13 +574,10 @@ in {
unset PGPASSFILE
'';
path = [ cfg.package pkgs.postgresql ];
environment = env // (if (!databaseActuallyCreateLocally)
then {
PGHOST = cfg.database.host;
PGUSER = cfg.database.user;
}
else {}
);
environment = env // lib.optionalAttrs (!databaseActuallyCreateLocally) {
PGHOST = cfg.database.host;
PGUSER = cfg.database.user;
};
serviceConfig = {
Type = "oneshot";
EnvironmentFile = [ "/var/lib/mastodon/.secrets_env" ];