1
0
Fork 0
selfhostblocks/_disnix/postgresdb/default.nix

48 lines
1.2 KiB
Nix

{ stdenv
, pkgs
, lib
}:
{ name
, database
, username
, password ? null
, passwordFile ? null
, dependsOn ? {}
}:
assert lib.assertMsg (
(password == null && passwordFile != null)
|| (password != null && passwordFile == null)
) "set either postgresPassword or postgresPasswordFile";
# 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.
# TODO: https://stackoverflow.com/a/69480184/1013628
{
inherit name;
inherit database username password passwordFile;
pkg = stdenv.mkDerivation {
name = database;
src = pkgs.writeTextDir "${database}.sql" ''
CREATE USER "${username}" WITH PASSWORD '${password}';
GRANT ALL PRIVILEGES ON DATABASE "${database}" TO "${username}";
'';
buildCommand = ''
mkdir -p $out/postgresql-databases
cp $src/*.sql $out/postgresql-databases
'';
};
inherit dependsOn;
type = "postgresql-database";
}