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

49 lines
1.2 KiB
Nix
Raw Normal View History

{ stdenv
, pkgs
, lib
}:
2023-01-15 08:36:16 +01:00
{ name
, database
, username
, password ? null
, passwordFile ? null
, dependsOn ? {}
}:
assert lib.assertMsg (
2023-01-15 08:36:16 +01:00
(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
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";
}