Files
process-compose-wrapper/lib.nix
2025-03-16 00:38:09 +04:00

116 lines
2.8 KiB
Nix

{ pkgs }:
let
inherit (pkgs) lib;
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,
data_dir ? ".data/",
...
}:
let
merged_config =
(
if default_config then
{
listen_addresses = "localhost";
port = 5432;
shared_buffers = "128MB";
work_mem = "4MB";
logging_collector = false;
}
else
{ }
)
// config;
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
)
);
};
in
# configFileCheck = pkgs.runCommand {} ''
# ${package}/bin/postgres -D${configFile} -C config_file
# touch $out
# '';
{
processes."${name}" = {
command = "${package}/bin/postgres -D ${data_dir} --config-file ${configFile}";
};
};
}