From 6efb74062bbcc68b28f7aad6bf861613899316ef Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Sun, 31 Oct 2021 16:33:27 -0400 Subject: [PATCH] Set up PostgreSQL --- modules/postgresql/default.nix | 42 +++++++++++++++++++++++++++++++ nki-personal-do/configuration.nix | 2 ++ 2 files changed, 44 insertions(+) create mode 100644 modules/postgresql/default.nix diff --git a/modules/postgresql/default.nix b/modules/postgresql/default.nix new file mode 100644 index 0000000..cb13f45 --- /dev/null +++ b/modules/postgresql/default.nix @@ -0,0 +1,42 @@ +{ pkgs, config, lib, ... } : + +with lib; +let + cfg = config.cloud.postgresql; + + # From a database name, create an "ensureUser" + # entry with the same name and assign all permissions + # to that database. + userFromDatabase = databaseName : { + name = databaseName; + ensurePermissions = { + "DATABASE ${databaseName}" = "ALL PRIVILEGES"; + }; + }; +in +{ + options.cloud.postgresql.databases = mkOption { + type = types.listOf types.str; + default = []; + description = '' + The list of databases to be created. + An user with the same name + and full access to the database will be created. + ''; + }; + + # PostgreSQL settings. + config.services.postgresql = { + enable = true; + package = pkgs.postgresql_13; + + ensureDatabases = cfg.databases; + + ensureUsers = map userFromDatabase cfg.databases; + }; + + # Backup settings + config.services.postgresqlBackup = { + enable = true; + }; +} diff --git a/nki-personal-do/configuration.nix b/nki-personal-do/configuration.nix index 981b562..4d8912c 100644 --- a/nki-personal-do/configuration.nix +++ b/nki-personal-do/configuration.nix @@ -3,6 +3,8 @@ ./hardware-configuration.nix ./networking.nix # generated at runtime by nixos-infect + # Set up postgresql + ../modules/postgresql ]; boot.cleanTmpDir = true;