170 lines
4.5 KiB
Nix
170 lines
4.5 KiB
Nix
{ pkgs }:
|
|
let
|
|
inherit (pkgs) lib;
|
|
runtime = ".compose";
|
|
in
|
|
{
|
|
mkWrapper =
|
|
{
|
|
config,
|
|
name,
|
|
enableTui ? false,
|
|
modules ? [ ],
|
|
}:
|
|
let
|
|
merged_config = lib.foldl' (acc: elem: lib.recursiveUpdate acc elem) config modules;
|
|
# checks = lib.concatStringsSep "\n" (
|
|
# lib.concatMap (
|
|
# attrs:
|
|
# lib.mapAttrsToList (k: v: ''
|
|
# echo "Running ${k}"
|
|
# ${v}/bin/check
|
|
# echo "Passed ${k}"
|
|
# '') attrs.checks
|
|
# ) modules
|
|
# );
|
|
removeNullAndEmptyAttrs =
|
|
attrs:
|
|
let
|
|
f = lib.filterAttrsRecursive (key: value: value != null && value != { });
|
|
in
|
|
# filterAttrsRecursive doesn't delete the *resulting* empty attrs, so we must
|
|
# evaluate it again and to get rid of it.
|
|
lib.pipe attrs [
|
|
f
|
|
f
|
|
];
|
|
toPCJson =
|
|
name: attrs:
|
|
pkgs.writeTextFile {
|
|
name = "process-compose-${name}.json";
|
|
text = builtins.toJSON attrs;
|
|
};
|
|
configFile = toPCJson name (
|
|
removeNullAndEmptyAttrs (
|
|
merged_config
|
|
// {
|
|
is_tui_disabled = !enableTui;
|
|
}
|
|
)
|
|
);
|
|
in
|
|
# {
|
|
# inherit configFile merged_config;
|
|
# };
|
|
pkgs.writeShellApplication {
|
|
inherit name;
|
|
text = ''
|
|
PC_CONFIG_FILES=${configFile} ${pkgs.process-compose}/bin/process-compose "$@"
|
|
|
|
'';
|
|
};
|
|
mkPostgres =
|
|
{
|
|
name,
|
|
default_config ? true,
|
|
config ? { },
|
|
extra_config ? '''',
|
|
package ? pkgs.postgresql,
|
|
port ? 5432,
|
|
listen_addresses ? "localhost",
|
|
...
|
|
}:
|
|
let
|
|
|
|
runtimeDir = "${runtime}/${name}";
|
|
data_dir = "${runtimeDir}/data";
|
|
merged_config =
|
|
(
|
|
if default_config then
|
|
{
|
|
listen_addresses = "localhost";
|
|
port = 5432;
|
|
shared_buffers = "128MB";
|
|
work_mem = "4MB";
|
|
logging_collector = false;
|
|
|
|
}
|
|
else
|
|
{ }
|
|
)
|
|
// config
|
|
// {
|
|
unix_socket_directories = data_dir;
|
|
inherit port listen_addresses;
|
|
};
|
|
toStr =
|
|
value:
|
|
if true == value then
|
|
"yes"
|
|
else if false == value then
|
|
"no"
|
|
else if lib.isString value then
|
|
"'${lib.replaceStrings [ "'" ] [ "''" ] value}'"
|
|
else
|
|
builtins.toString value;
|
|
configFile = pkgs.writeTextFile {
|
|
name = "postgresql.conf";
|
|
text = lib.concatStringsSep "\n" (
|
|
lib.mapAttrsToList (n: v: "${n} = ${toStr v}") (
|
|
lib.filterAttrs (lib.const (x: x != null)) merged_config
|
|
)
|
|
);
|
|
};
|
|
setupScript = pkgs.writeShellScriptBin "setup-postgres" ''
|
|
set -euo pipefail
|
|
export PATH=${package}/bin:${pkgs.coreutils}/bin
|
|
POSTGRES_RUN_INITIAL_SCRIPT="false"
|
|
if [[ ! -d "${data_dir}" ]]; then
|
|
|
|
initdb -D ${data_dir} --no-instructions
|
|
POSTGRES_RUN_INITIAL_SCRIPT="true"
|
|
echo
|
|
echo "PostgreSQL initdb process completed"
|
|
echo
|
|
fi
|
|
cp ${configFile} ${data_dir}/postgresql.conf
|
|
if [[ "$POSTGRES_RUN_INITIAL_SCRIPT" == "true" ]]; then
|
|
echo
|
|
echo "PostgreSQL is setting up the initial database"
|
|
echo
|
|
if [[ -z "$${PGHOST+x}" ]]; then
|
|
OLDPGHOST=""
|
|
else
|
|
OLDPGHOST="$PGHOST"
|
|
fi
|
|
PGHOST=${lib.escapeShellArg runtimeDir}
|
|
pg_ctl -D "${data_dir}" -w start -o "-c unix_socket_directories=${data_dir} -c listen_addresses= -p ${builtins.toString port}"
|
|
pg_ctl -D "${data_dir}" -m fast -w stop
|
|
PGHOST="$OLDPGHOST"
|
|
unset OLDPGHOST
|
|
else
|
|
echo
|
|
echo "Database directory exists. Skipping initialization"
|
|
echo
|
|
fi
|
|
|
|
unset POSTGRES_RUN_INITIAL_SCRIPT
|
|
'';
|
|
|
|
script = pkgs.writeShellApplication {
|
|
name = "run-postgres";
|
|
text = ''
|
|
set -euo pipefail
|
|
mkdir -p ${lib.escapeShellArg runtimeDir}
|
|
${setupScript}/bin/setup-postgres
|
|
exec ${package}/bin/postgres -D${data_dir}
|
|
'';
|
|
};
|
|
in
|
|
# configFileCheck = pkgs.runCommand {} ''
|
|
# ${package}/bin/postgres -D${configFile} -C config_file
|
|
# touch $out
|
|
# '';
|
|
{
|
|
processes."${name}" = {
|
|
command = "${script}/bin/run-postgres";
|
|
};
|
|
};
|
|
}
|