Compare commits

81 Commits
main ... dev

Author SHA1 Message Date
948180a09c add valkey 2025-05-13 18:01:28 +04:00
c0f01d143e disable http server 2025-04-02 21:34:48 +04:00
f15818f6b6 fix pg ready cmd 2025-03-17 17:48:19 +04:00
10443ca529 fix pg ready cmd 2025-03-17 17:46:50 +04:00
ba5e284961 fix pg ready cmd 2025-03-17 17:44:25 +04:00
d7beae23cd fix pg ready cmd 2025-03-17 17:42:54 +04:00
1f2c768dd1 fix pg ready cmd 2025-03-17 17:41:01 +04:00
a00b0457b5 fix pg ready cmd 2025-03-17 17:40:13 +04:00
97d5236d0f fix error 2025-03-17 17:17:04 +04:00
a15b6f3591 fix error 2025-03-17 17:15:26 +04:00
cb19db3066 fix type error 2025-03-17 17:12:56 +04:00
48d14d7595 fix type error 2025-03-17 17:11:56 +04:00
3d03da0f96 fix redis port 2025-03-17 17:10:09 +04:00
218c2b0360 restucture and add redis 2025-03-17 17:09:49 +04:00
61ffac84a1 add ability to create default user 2025-03-17 16:19:47 +04:00
f7169da0cf fix nix code 2025-03-17 16:03:58 +04:00
d5c171836a fix nix code 2025-03-17 16:01:05 +04:00
2c3bd39a7e fix nix code 2025-03-17 16:00:24 +04:00
2ace2216dd add init 2025-03-17 14:01:53 +04:00
4e2c2d1728 remove unix socket from pg 2025-03-17 13:53:12 +04:00
2e13f2ce8d remove unix socket from pg 2025-03-17 13:51:51 +04:00
722010341d remove unix socket from pg 2025-03-17 13:49:28 +04:00
e2e24b0694 remove unix socket from pg 2025-03-17 13:48:51 +04:00
3c6a6fc0b5 remove unix socket from pg 2025-03-17 13:47:00 +04:00
604660bff4 fix script 2025-03-17 13:38:56 +04:00
f5ab052719 fix script 2025-03-17 13:38:21 +04:00
c7d116be87 fix script 2025-03-17 13:36:32 +04:00
90db6f67ec fix script 2025-03-17 13:30:30 +04:00
9c1e750d77 fix script 2025-03-17 13:29:30 +04:00
c9623ed8ab fix script 2025-03-17 13:28:39 +04:00
dbfcda72d8 fix script 2025-03-17 13:27:31 +04:00
19350df1d5 fix script 2025-03-17 13:27:03 +04:00
0c1c9e9ae9 fix script 2025-03-17 13:24:40 +04:00
24fc414926 fix script 2025-03-17 13:18:45 +04:00
3b67a7ab1c fix script 2025-03-17 13:17:50 +04:00
4947fd35b5 fix script 2025-03-17 13:09:31 +04:00
e201b01369 fix script 2025-03-17 13:09:28 +04:00
405227bf7a try fix script 2025-03-17 12:57:01 +04:00
23764abbed try fix script 2025-03-17 12:56:07 +04:00
19b02ca7c1 try fix script 2025-03-17 12:55:23 +04:00
bc8e522b6f try fix script 2025-03-17 12:53:53 +04:00
b1196d03c2 try fix script 2025-03-17 12:52:52 +04:00
19e6ea2386 try fix script 2025-03-17 12:51:46 +04:00
721e5b923a try fix script 2025-03-17 12:50:06 +04:00
224294cf72 try fix script 2025-03-17 12:49:30 +04:00
883acbe6d4 try fix script 2025-03-17 12:48:50 +04:00
3db3606686 try fix script 2025-03-17 12:41:43 +04:00
79bc2bcc72 try fix script 2025-03-17 12:40:36 +04:00
b0f6a95e55 try fix script 2025-03-17 12:38:28 +04:00
c84a3bf6fc try fix script 2025-03-17 12:36:19 +04:00
4751fa7d96 try fix script 2025-03-17 12:35:03 +04:00
be76322a9e try fix script 2025-03-17 12:33:27 +04:00
95f264f4b8 try fix script 2025-03-17 12:31:04 +04:00
c271bd731d try fix script 2025-03-17 12:30:07 +04:00
5fcbcae71a fix script 2025-03-17 12:26:56 +04:00
f429bb2ab1 fix script 2025-03-17 12:25:01 +04:00
886af20792 fix expression 2025-03-17 12:19:05 +04:00
d0e7fcf8fe fix expression 2025-03-17 12:17:20 +04:00
33fe4bd898 fix expression 2025-03-17 12:11:38 +04:00
a85a7040d0 fix expression 2025-03-17 12:06:36 +04:00
84ac5314a4 fix var 2025-03-17 12:03:12 +04:00
d6e8ef7aed rewrite parts of the script 2025-03-17 11:31:35 +04:00
2ce75022a7 fix setup script 2025-03-17 11:23:51 +04:00
ec566ac330 fix script 2025-03-17 11:21:21 +04:00
82930f63f3 fix postgres init script 2025-03-17 11:19:48 +04:00
b53f6f65ef fix postgres cmd and initdb cmd 2025-03-16 00:53:02 +04:00
416fe9a8ae fix postgres cmd 2025-03-16 00:51:56 +04:00
03e3438195 fix initdb 2025-03-16 00:51:11 +04:00
3c860225e6 add initdb command 2025-03-16 00:49:03 +04:00
a68ee5fe04 add initdb command 2025-03-16 00:47:41 +04:00
df17c8b0d5 fix postgres name 2025-03-16 00:38:09 +04:00
773319fe0d fix script 2025-03-16 00:36:46 +04:00
739830d352 fix postgres config 2025-03-16 00:35:35 +04:00
fd3726ead2 fix postgres config 2025-03-16 00:16:13 +04:00
7dd5d80bc6 fix postgres config 2025-03-16 00:15:36 +04:00
6858834b20 update check 2025-03-16 00:14:07 +04:00
9efbbbacdf update check 2025-03-15 23:59:47 +04:00
54775073d0 update check 2025-03-15 23:57:42 +04:00
7e46072451 test process-comose postgres + check functionality 2025-03-15 23:53:20 +04:00
ba4fb29ce5 start adding postgres wrapper 2025-03-15 22:59:52 +04:00
4602860deb try new style 2025-03-15 19:35:18 +04:00
5 changed files with 310 additions and 5 deletions

View File

@@ -3,9 +3,9 @@
inputs = { };
outputs =
{ ... }:
{
outputs = _: {
lib = {
mkLib = pkgs: import ./lib.nix { inherit pkgs; };
};
};
}

11
lib.nix
View File

@@ -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,12 @@ in
pkgs.writeShellApplication {
inherit name;
text = ''
PC_CONFIG_FILES=${configFile} ${pkgs.process-compose}/bin/process-compose "$@"
PC_NO_SERVER=true 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; };
mkValkey = import ./services/valkey.nix { inherit pkgs; };
}

152
services/postgres.nix Normal file
View 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 -h ${bind} -p ${builtins.toString port} -d postgres";
initial_delay_seconds = 2;
period_seconds = 10;
timeout_seconds = 4;
success_threshold = 1;
failure_threshold = 5;
};
availability.restart = "on_failure";
};
}

73
services/redis.nix Normal file
View 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";
};
}

73
services/valkey.nix Normal file
View File

@@ -0,0 +1,73 @@
{
pkgs,
}:
let
inherit (pkgs) lib;
in
{
name,
default_config ? true,
extra_config ? '''',
package ? pkgs.valkey,
port ? 6379,
bind ? "localhost",
...
}:
let
generateValkeyConfig = ''
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-valkey";
text = ''
set -euo pipefail
VALKEYDATA="$RUNTIME_PATH"/${name}/
${
if (port == 0) then
''
VALKEY_UNIX_SOCKET = "$RUNTIME_PATH"/${name}.sock
exportVALKEY_UNIX_SOCKET
echo "Starting valkey on unix socket ''${VALKEY_UNIX_SOCKET}"
''
else
''
echo "Starting valkey on port ${builtins.toString port}"
''
}
export VALKEYDATA
if [ ! -d "$VALKEYDATA" ]; then
mkdir -p "$VALKEYDATA"
fi
${generateValkeyConfig}
${package}/bin/valkey-server "$RUNTIME_PATH"/${name}.conf --daemonize no --dir "$VALKEYDATA"
'';
};
tcpPing = "${package}/bin/valkey-cli -p ${builtins.toString port} ping";
unixPing = "${package}/bin/valkey-cli -s \"$RUNTIME_PATH\"/${name}.sock ping";
in
{
processes."${name}" = {
command = "${script}/bin/run-valkey";
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";
};
}