summaryrefslogtreecommitdiff
path: root/modules/postgresql.nix
diff options
context:
space:
mode:
authorAzat Bahawi <azat@bahawi.net>2024-04-21 02:15:42 +0300
committerAzat Bahawi <azat@bahawi.net>2024-04-21 02:15:42 +0300
commite6ed60548397627bf10f561f9438201dbba0a36e (patch)
treef9a84c5957d2cc4fcd148065ee9365a0c851ae1c /modules/postgresql.nix
parent9ac64328603d44bd272175942d3ea3eaadcabd04 (diff)
2024-04-21
Diffstat (limited to 'modules/postgresql.nix')
-rw-r--r--modules/postgresql.nix98
1 files changed, 98 insertions, 0 deletions
diff --git a/modules/postgresql.nix b/modules/postgresql.nix
new file mode 100644
index 0000000..5081340
--- /dev/null
+++ b/modules/postgresql.nix
@@ -0,0 +1,98 @@
+{
+ config,
+ lib,
+ pkgs,
+ this,
+ ...
+}:
+with lib;
+let
+ cfg = config.nixfiles.modules.postgresql;
+in
+{
+ options.nixfiles.modules.postgresql = {
+ enable = mkEnableOption "PostgreSQL";
+
+ package = mkOption {
+ type = types.package;
+ default = pkgs.postgresql_15;
+ description = "PostgreSQL package to use.";
+ };
+
+ extraPostStart = mkOption {
+ type = with types; listOf str;
+ default = [ ];
+ description = ''
+ Additional post-startup commands.
+
+ This could be used to provide a crude interface to grant permissions and
+ such.
+ '';
+ };
+ };
+
+ config = mkIf cfg.enable {
+ assertions = [
+ {
+ assertion = any (x: x == "en_GB.UTF-8/UTF-8") config.i18n.supportedLocales;
+ message = "The locale must be available";
+ }
+ ];
+
+ ark.directories = [ config.services.postgresql.dataDir ];
+
+ services = {
+ postgresql = {
+ enable = true;
+
+ inherit (cfg) package;
+
+ # In hindsight, it was a poor choice to use ICU as a locale provider.
+ # Now each time ICU version is bumped, I need to carefully upgrade each
+ # database to match the version.
+ initdbArgs = [
+ "--encoding=UTF8"
+ "--locale-provider=icu"
+ "--icu-locale=en_GB@collation=posix"
+ "--locale=en_GB.UTF-8"
+ "--lc-collate=C"
+ "--lc-ctype=C"
+ ];
+
+ # This crutch is here because some services cannot work via a UNIX
+ # socket connection and I can't be bothered to configure proper
+ # authentication.
+ authentication = ''
+ local all all trust
+ '';
+ };
+
+ prometheus.exporters.postgres = {
+ enable = true;
+ listenAddress = mkDefault this.wireguard.ipv4.address;
+ port = mkDefault 9187;
+ };
+ };
+
+ systemd.services.postgresql.postStart = optionalString (
+ cfg.extraPostStart != [ ]
+ ) concatLines cfg.extraPostStart;
+
+ environment.sessionVariables.PSQLRC = toString (
+ pkgs.writeText "psqlrc" ''
+ \set QUIET 1
+
+ \timing
+ \x auto
+ \pset null '[NULL]'
+ \set PROMPT1 '%[%033[1m%]%M %n@%/%R%[%033[0m%]% λ '
+ \set PROMPT2 ' … > '
+ \set VERBOSITY verbose
+ \set HISTCONTROL ignoredups
+ \set HISTFILE /dev/null
+
+ \unset QUIET
+ ''
+ );
+ };
+}