Compare commits
71 Commits
main
...
cb19db3066
| Author | SHA1 | Date | |
|---|---|---|---|
| cb19db3066 | |||
| 48d14d7595 | |||
| 3d03da0f96 | |||
| 218c2b0360 | |||
| 61ffac84a1 | |||
| f7169da0cf | |||
| d5c171836a | |||
| 2c3bd39a7e | |||
| 2ace2216dd | |||
| 4e2c2d1728 | |||
| 2e13f2ce8d | |||
| 722010341d | |||
| e2e24b0694 | |||
| 3c6a6fc0b5 | |||
| 604660bff4 | |||
| f5ab052719 | |||
| c7d116be87 | |||
| 90db6f67ec | |||
| 9c1e750d77 | |||
| c9623ed8ab | |||
| dbfcda72d8 | |||
| 19350df1d5 | |||
| 0c1c9e9ae9 | |||
| 24fc414926 | |||
| 3b67a7ab1c | |||
| 4947fd35b5 | |||
| e201b01369 | |||
| 405227bf7a | |||
| 23764abbed | |||
| 19b02ca7c1 | |||
| bc8e522b6f | |||
| b1196d03c2 | |||
| 19e6ea2386 | |||
| 721e5b923a | |||
| 224294cf72 | |||
| 883acbe6d4 | |||
| 3db3606686 | |||
| 79bc2bcc72 | |||
| b0f6a95e55 | |||
| c84a3bf6fc | |||
| 4751fa7d96 | |||
| be76322a9e | |||
| 95f264f4b8 | |||
| c271bd731d | |||
| 5fcbcae71a | |||
| f429bb2ab1 | |||
| 886af20792 | |||
| d0e7fcf8fe | |||
| 33fe4bd898 | |||
| a85a7040d0 | |||
| 84ac5314a4 | |||
| d6e8ef7aed | |||
| 2ce75022a7 | |||
| ec566ac330 | |||
| 82930f63f3 | |||
| b53f6f65ef | |||
| 416fe9a8ae | |||
| 03e3438195 | |||
| 3c860225e6 | |||
| a68ee5fe04 | |||
| df17c8b0d5 | |||
| 773319fe0d | |||
| 739830d352 | |||
| fd3726ead2 | |||
| 7dd5d80bc6 | |||
| 6858834b20 | |||
| 9efbbbacdf | |||
| 54775073d0 | |||
| 7e46072451 | |||
| ba4fb29ce5 | |||
| 4602860deb |
@@ -3,9 +3,9 @@
|
||||
|
||||
inputs = { };
|
||||
|
||||
outputs =
|
||||
{ ... }:
|
||||
{
|
||||
outputs = _: {
|
||||
lib = {
|
||||
mkLib = pkgs: import ./lib.nix { inherit pkgs; };
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
9
lib.nix
9
lib.nix
@@ -1,6 +1,7 @@
|
||||
{ pkgs }:
|
||||
let
|
||||
inherit (pkgs) lib;
|
||||
runtime = ".compose";
|
||||
in
|
||||
{
|
||||
mkWrapper =
|
||||
@@ -8,8 +9,10 @@ in
|
||||
config,
|
||||
name,
|
||||
enableTui ? false,
|
||||
modules ? [ ],
|
||||
}:
|
||||
let
|
||||
merged_config = lib.foldl' (acc: elem: lib.recursiveUpdate acc elem) config modules;
|
||||
removeNullAndEmptyAttrs =
|
||||
attrs:
|
||||
let
|
||||
@@ -29,7 +32,7 @@ in
|
||||
};
|
||||
configFile = toPCJson name (
|
||||
removeNullAndEmptyAttrs (
|
||||
config
|
||||
merged_config
|
||||
// {
|
||||
is_tui_disabled = !enableTui;
|
||||
}
|
||||
@@ -39,8 +42,10 @@ in
|
||||
pkgs.writeShellApplication {
|
||||
inherit name;
|
||||
text = ''
|
||||
PC_CONFIG_FILES=${configFile} ${pkgs.process-compose}/bin/process-compose "$@"
|
||||
|
||||
PC_CONFIG_FILES=${configFile} RUNTIME_PATH=$(pwd)/${runtime} ${pkgs.process-compose}/bin/process-compose "$@"
|
||||
'';
|
||||
};
|
||||
mkPostgres = import ./services/postgres.nix { inherit pkgs; };
|
||||
mkRedis = import ./services/redis.nix { inherit pkgs; };
|
||||
}
|
||||
|
||||
152
services/postgres.nix
Normal file
152
services/postgres.nix
Normal file
@@ -0,0 +1,152 @@
|
||||
{
|
||||
pkgs,
|
||||
}:
|
||||
let
|
||||
inherit (pkgs) lib;
|
||||
in
|
||||
{
|
||||
name,
|
||||
default_config ? true,
|
||||
config ? { },
|
||||
extra_config ? '''',
|
||||
package ? pkgs.postgresql,
|
||||
port ? 5432,
|
||||
bind ? "localhost",
|
||||
initialDatabases ? [ ],
|
||||
...
|
||||
}:
|
||||
let
|
||||
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 = ".";
|
||||
listen_addresses = bind;
|
||||
inherit port;
|
||||
};
|
||||
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
|
||||
)
|
||||
)
|
||||
+ extra_config;
|
||||
};
|
||||
setupInitialDatabases =
|
||||
if initialDatabases != [ ] then
|
||||
(lib.concatMapStrings (db: ''
|
||||
echo "Checking presence of database ${db.name}"
|
||||
dbAlreadyExists="$(echo "SELECT 1 AS result FROM pg_database WHERE datname='${db.name}';" | psql --dbname postgres | ${pkgs.gnugrep}/bin/grep -c 'exists = "1"' || true)"
|
||||
echo "$dbAlreadyExists"
|
||||
if [ 1 -ne "$dbAlreadyExists" ]; then
|
||||
echo "Creating database ${db.name}"
|
||||
echo 'CREATE DATABASE "${db.name}";' | psql --dbname postgres
|
||||
${lib.optionalString (db.user != null && db.password != null) ''
|
||||
echo "Creating user ${db.user}"
|
||||
psql --dbname postgres <<'EOF'
|
||||
DO $$
|
||||
BEGIN
|
||||
CREATE ROLE "${db.user}" WITH LOGIN PASSWORD '${db.password}';
|
||||
EXCEPTION WHEN duplicate_object THEN RAISE NOTICE '%, skipping', SQLERRM USING ERRCODE = SQLSTATE;
|
||||
END
|
||||
$$;
|
||||
GRANT ALL PRIVILEGES ON DATABASE "${db.name}" TO "${db.user}";
|
||||
\c ${db.name}
|
||||
GRANT ALL PRIVILEGES ON SCHEMA public TO "${db.user}";
|
||||
EOF
|
||||
''}
|
||||
else
|
||||
echo "Database ${db.name} already exists"
|
||||
fi
|
||||
'') initialDatabases)
|
||||
else
|
||||
"";
|
||||
setupScript = pkgs.writeShellApplication {
|
||||
name = "setup-postgres";
|
||||
runtimeInputs = [
|
||||
package
|
||||
pkgs.coreutils
|
||||
];
|
||||
text = ''
|
||||
POSTGRES_RUN_INITIAL_SCRIPT="false"
|
||||
if [[ ! -d "$PGDATA" ]]; then
|
||||
initdb --no-instructions
|
||||
POSTGRES_RUN_INITIAL_SCRIPT="true"
|
||||
echo
|
||||
echo "PostgreSQL initdb process completed"
|
||||
echo
|
||||
fi
|
||||
cp ${configFile} "$PGDATA"/postgresql.conf
|
||||
if [[ "$POSTGRES_RUN_INITIAL_SCRIPT" == "true" ]]; then
|
||||
echo
|
||||
echo "PostgreSQL is setting up the initial database"
|
||||
echo
|
||||
pg_ctl -w start -o "-c listen_addresses= -p ${builtins.toString port}"
|
||||
${setupInitialDatabases}
|
||||
pg_ctl -m fast -w stop
|
||||
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
|
||||
PGDATA=$RUNTIME_PATH/${name}
|
||||
PGHOST=$RUNTIME_PATH/${name}
|
||||
PGPORT=${builtins.toString port}
|
||||
echo "Starting postgres with PGDATA=''${PGDATA} PGHOST=''${PGHOST} PGPORT=''${PGPORT}"
|
||||
export PGDATA PGHOST PGPORT
|
||||
${setupScript}/bin/setup-postgres
|
||||
${package}/bin/postgres
|
||||
'';
|
||||
};
|
||||
in
|
||||
# configFileCheck = pkgs.runCommand {} ''
|
||||
# ${package}/bin/postgres -D${configFile} -C config_file
|
||||
# touch $out
|
||||
# '';
|
||||
{
|
||||
processes."${name}" = {
|
||||
command = "${script}/bin/run-postgres";
|
||||
shutdown.signal = 2;
|
||||
readiness_probe = {
|
||||
exec.command = "${package}/bin/pg_isready -d template1";
|
||||
initial_delay_seconds = 2;
|
||||
period_seconds = 2;
|
||||
timeout_seconds = 2;
|
||||
success_threshold = 1;
|
||||
failure_threshold = 5;
|
||||
};
|
||||
availability.restart = "on_failure";
|
||||
};
|
||||
}
|
||||
73
services/redis.nix
Normal file
73
services/redis.nix
Normal file
@@ -0,0 +1,73 @@
|
||||
{
|
||||
pkgs,
|
||||
}:
|
||||
let
|
||||
inherit (pkgs) lib;
|
||||
in
|
||||
{
|
||||
name,
|
||||
default_config ? true,
|
||||
extra_config ? '''',
|
||||
package ? pkgs.redis,
|
||||
port ? 6379,
|
||||
bind ? "localhost",
|
||||
...
|
||||
}:
|
||||
|
||||
let
|
||||
|
||||
generateRedisConfig = ''
|
||||
cat > $RUNTIME_PATH/${name}.conf <<EOF
|
||||
${lib.optionalString default_config ''
|
||||
port ${builtins.toString port}
|
||||
${lib.optionalString (bind != null) "bind ${bind}"}
|
||||
${lib.optionalString (port == 0) "unixsocket $RUNTIME_PATH/${name}.sock"}
|
||||
${lib.optionalString (port == 0) "unixsocketperm 700"}
|
||||
''}
|
||||
${extra_config}
|
||||
EOF
|
||||
'';
|
||||
|
||||
script = pkgs.writeShellApplication {
|
||||
name = "run-redis";
|
||||
text = ''
|
||||
set -euo pipefail
|
||||
REDISDATA=$RUNTIME_PATH/${name}/
|
||||
${
|
||||
if (port == 0) then
|
||||
''
|
||||
REDIS_UNIX_SOCKET = $RUNTIME_PATH/${name}.sock
|
||||
export REDIS_UNIX_SOCKET
|
||||
echo "Starting redis on unix socket ''${REDIS_UNIX_SOCKET}"
|
||||
''
|
||||
else
|
||||
''
|
||||
echo "Starting redis on port ${builtins.toString port}"
|
||||
''
|
||||
}
|
||||
export REDISDATA
|
||||
if [ ! -d $REDISDATA ]; then
|
||||
mkdir -p $REDISDATA
|
||||
fi
|
||||
${generateRedisConfig}
|
||||
${package}/bin/redis-server $RUNTIME_PATH/${name}.conf --daemonize no --dir "$REDISDATA"
|
||||
'';
|
||||
};
|
||||
tcpPing = "${package}/bin/redis-cli -p ${builtins.toString port} ping";
|
||||
unixPing = "${package}/bin/redis-cli -s $RUNTIME_PATH/${name}.sock ping";
|
||||
in
|
||||
{
|
||||
processes."${name}" = {
|
||||
command = "${script}/bin/run-redis";
|
||||
readiness_probe = {
|
||||
exec.command = if port == 0 then unixPing else tcpPing;
|
||||
initial_delay_seconds = 2;
|
||||
period_seconds = 10;
|
||||
timeout_seconds = 4;
|
||||
success_threshold = 1;
|
||||
failure_threshold = 3;
|
||||
};
|
||||
availability.restart = "on_failure";
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user