Commit 9372ef33 authored by Profpatsch's avatar Profpatsch

containers/postgres: factor out initdb templating code

parent 44d3d841
let
# evaluate the deployment config
eval = import <nixpkgs/nixos> {
configuration = { ... }: {
configuration = { lib, ... }: {
imports = [ ./postgres.nix ];
config = {
org.techcultivation.deploy.database.enable = true;
......@@ -10,6 +10,11 @@ let
# our own packages
nixpkgs.overlays = [(self: _: {
sangha = self.callPackage ../packages/sangha {};
helpers = lib.mapAttrs (_: pkg: self.callPackage pkg {}) {
postgresTemplate = ./helpers/postgres-template.nix;
genUtf8Localedb = ./helpers/gen-utf8-localedb.nix;
};
})];
};
};
......
# generate a localedb for the given UTF-8 locale string
{ glibcLocales }:
# a locale string formatted like `en_US.UTF-8`
locale:
glibcLocales.override {
locales = [ (locale + "/UTF-8") ];
allLocales = false;
}
{ lib, pkgs }:
{
# template name
name,
# `genUtf8Localedb`
locale,
# script to connect to the database running locally
initScript,
# attrset of postgresql.conf values
postgresqlConf
}:
let
localedb = pkgs.helpers.genUtf8Localedb locale;
config =
let
format = with lib.generators; toKeyValue {
mkKeyValue = mkKeyValueDefault {
mkValueString = v: if builtins.isString v then "'${v}'" else toString v;
} "=";
};
in format postgresqlConf;
in pkgs.runCommand "postgres-template-${name}" {
nativeBuildInputs = [ pkgs.postgresql localedb ];
} ''
mkdir $out
export PGDATA=$out
# init the postgres database in $out
initdb --nosync --username=postgres \
--encoding=UTF8 --locale=${locale}
# overwrite generated config file
mv $PGDATA/postgresql.conf $PGDATA/postgresql.example.conf
cp ${pkgs.writeText "sangha-postgresql.conf" config} \
$PGDATA/postgresql.conf
# start postgres server in build environment
pg_ctl start -w
${initScript}
pg_ctl stop -w
''
......@@ -20,16 +20,9 @@ let
# 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
# generated postgresql.conf string
sanghaPostgresConfig =
let
l = sanghaDatabaseLocale;
format = with lib.generators; toKeyValue {
mkKeyValue = mkKeyValueDefault {
mkValueString = v: if builtins.isString v then "'${v}'" else toString v;
} "=";
};
in format {
let l = sanghaDatabaseLocale;
in {
listen_addresses = "*";
max_connections = 100;
shared_buffers = "128MB";
......@@ -70,6 +63,7 @@ let
${pkgs.sangha.src}/config.example.json \
> $out
'';
# sangha db SQL initialization code
sanghaCreateDb = with connectionData;
pkgs.writeText "sangha-create-db.sql" ''
......@@ -78,43 +72,32 @@ let
GRANT ALL PRIVILEGES ON DATABASE ${DbName} TO ${User};
'';
in pkgs.runCommand "sangha-postgres-template" {
nativeBuildInputs = [ pkgs.postgresql localeFiles ];
# pass through the sangha configfile used
passthru = { inherit sanghaConfig; };
meta.description = ''
Postgres database for sangha, completely initialized
and with mock data.
'';
} ''
mkdir $out
export PGDATA=$out
# init the postgres database in $out
initdb --nosync --username=postgres \
--encoding=UTF8 --locale=${sanghaDatabaseLocale}
# overwrite generated config file
mv $PGDATA/postgresql.conf $PGDATA/postgresql.example.conf
cp ${pkgs.writeText "sangha-postgresql.conf" sanghaPostgresConfig} \
$PGDATA/postgresql.conf
# start postgres server in build environment
pg_ctl start -w
# init database
psql --file=${sanghaCreateDb} --username=postgres
# init database tables
# TODO: use command line flag of sangha (buggy)
cp ${sanghaConfig} ./config.json
${lib.getBin pkgs.sangha}/bin/sangha database init
# insert mock data
psql --file=${pkgs.sangha.src}/mocks/projects.sql --username=postgres sangha
template = pkgs.helpers.postgresTemplate {
name = "sangha";
locale = sanghaDatabaseLocale;
postgresqlConf = sanghaPostgresConfig;
initScript = ''
# init database
psql --file=${sanghaCreateDb} --username=postgres
# init database tables
# TODO: use command line flag of sangha (buggy)
cp ${sanghaConfig} ./config.json
${lib.getBin pkgs.sangha}/bin/sangha database init
# insert mock data
psql --file=${pkgs.sangha.src}/mocks/projects.sql --username=postgres sangha
'';
};
pg_ctl stop -w
'';
in template.overrideAttrs (_: {
# pass through the sangha configfile used
passthru = { inherit sanghaConfig; };
meta.description = ''
Postgres database for sangha, completely initialized
and with mock data.
'';
});
in {
......
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