Commit 034e3e5c authored by Profpatsch's avatar Profpatsch

initial postgres database build with mock data

parent f2bb0978
{ lib, pkgs, config, ... }:
let
cfg = config.org.techcultivation.deploy.database;
postgresConnectionData = {
Host = "";
Port = 0;
DbName = "sangha";
User = "sanghauser";
Password = "sanghapass";
SslMode = "disable";
};
# sanghaSrc = pkgs.fetchgit {
# url = https://gitlab.techcultivation.org/sangha/sangha.git;
# rev = "ac81d99248ed487395a68820ed9ceaf2e73746b8";
# sha256 = "1n1fgsg2afklwk0hkssjgkncwdwrls8dyfl6llv2ngghq74n1i8z";
# };
sanghaSrc = /home/philip/go/src/gitlab.techcultivation.org/sangha/sangha;
sanghaDatabaseInit = with postgresConnectionData;
pkgs.writeText "sangha-create-db.sql" ''
CREATE DATABASE ${DbName};
CREATE USER ${User} WITH PASSWORD '${Password}';
GRANT ALL PRIVILEGES ON DATABASE ${DbName} TO ${User};
'';
postgresInitDatabase = pkgs.runCommand "sangha-db-template" {
nativeBuildInputs = [ pkgs.postgresql pkgs.jq ];
} ''
mkdir $out
export PGDATA=$out
# init the postgres database in $out
initdb --nosync --username=postgres
# start postgres server in build environment
pg_ctl start -w
# init database
psql --file=${sanghaDatabaseInit} --username=postgres
# copy sangha configuration with correct connection data
jq '.Connections.PostgreSQLConnection *= ${
lib.generators.toJSON {} postgresConnectionData
}' \
${sanghaSrc}/config.example.json \
> ./config.json
# init database tables
${sanghaSrc}/sangha database init
# insert mock data
psql --file=${sanghaSrc}/mocks/projects.sql --username=postgres sangha
pg_ctl stop -w
'';
in {
options.org.techcultivation.deploy.database = {
enable = lib.mkEnableOption "database container";
buildDockerImage = lib.mkEnableOption "database container docker image";
# TODO: here? naming?
# buildInitScript = lib.mkOption {
# type = with lib.types; lines;
# example = "
# description = ''
# Init scripts to be run when building the database deploy.
# '';
# TODO: max_connections & shared_buffers
# set by initdb, should be modifiable
# see https://www.postgresql.org/docs/9.1/static/runtime-config-resource.html
# and https://www.postgresql.org/docs/9.1/static/runtime-config-connection.html
};
options.test = lib.mkOption {
type = lib.types.package;
};
config =
let
nixosConfig = {
# services.postgresql = {
# enable = true;
# initialScript = postgresInit;
# };
test = postgresInitDatabase;
};
dockerConfig = {
# TODO
};
in
lib.mkMerge [
(lib.mkIf cfg.enable nixosConfig)
(lib.mkIf cfg.buildDockerImage dockerConfig)
];
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment