2022-10-09 08:53:35 +02:00
|
|
|
{ stdenv
|
|
|
|
, pkgs
|
|
|
|
, lib
|
2022-04-19 23:21:55 +02:00
|
|
|
}:
|
2023-01-15 08:36:16 +01:00
|
|
|
{ name
|
|
|
|
|
|
|
|
, database
|
|
|
|
, username
|
|
|
|
, password ? null
|
|
|
|
, passwordFile ? null
|
|
|
|
|
|
|
|
, dependsOn ? {}
|
2022-10-09 08:53:35 +02:00
|
|
|
}:
|
|
|
|
|
|
|
|
assert lib.assertMsg (
|
2023-01-15 08:36:16 +01:00
|
|
|
(password == null && passwordFile != null)
|
|
|
|
|| (password != null && passwordFile == null)
|
2022-10-09 08:53:35 +02:00
|
|
|
) "set either postgresPassword or postgresPasswordFile";
|
2022-04-19 23:21:55 +02:00
|
|
|
|
|
|
|
# From https://github.com/svanderburg/dysnomia/blob/master/dysnomia-modules/postgresql-database.in
|
|
|
|
# and https://github.com/svanderburg/dysnomia/blob/master/tests/deployment/postgresql-database.nix
|
|
|
|
#
|
|
|
|
# On activation, an initial dump can be restored. If the mutable component
|
|
|
|
# contains a sub folder named postgresql-databases/, then the dump files stored
|
|
|
|
# inside get imported.
|
|
|
|
|
2022-12-18 01:45:00 +01:00
|
|
|
# TODO: https://stackoverflow.com/a/69480184/1013628
|
2023-01-15 08:36:16 +01:00
|
|
|
{
|
|
|
|
inherit name;
|
|
|
|
inherit database username password passwordFile;
|
|
|
|
|
|
|
|
pkg = stdenv.mkDerivation {
|
|
|
|
name = database;
|
|
|
|
|
|
|
|
src = pkgs.writeTextDir "${database}.sql" ''
|
|
|
|
CREATE USER "${username}" WITH PASSWORD '${password}';
|
2023-02-20 05:11:20 +01:00
|
|
|
GRANT ALL PRIVILEGES ON DATABASE "${database}" TO "${username}";
|
2023-01-15 08:36:16 +01:00
|
|
|
'';
|
|
|
|
|
|
|
|
buildCommand = ''
|
|
|
|
mkdir -p $out/postgresql-databases
|
|
|
|
cp $src/*.sql $out/postgresql-databases
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
inherit dependsOn;
|
|
|
|
type = "postgresql-database";
|
2022-04-19 23:21:55 +02:00
|
|
|
}
|