pirate-backup/flake.nix
2023-03-01 19:30:14 +01:00

100 lines
2.8 KiB
Nix

{
description = "Pirate PostgreSQL backup script";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-22.11";
flake-utils.url = "github:numtide/flake-utils";
devshell = {
url = "github:numtide/devshell";
inputs = {
flake-utils.follows = "flake-utils";
nixpkgs.follows = "nixpkgs";
};
};
};
outputs = { self, nixpkgs, flake-utils, devshell }:
flake-utils.lib.eachDefaultSystem (system: {
devShell =
let
pkgs = import nixpkgs {
inherit system;
overlays = [ devshell.overlay ];
};
in
pkgs.devshell.mkShell {
name = "Pirate Backup development shell";
packages = with pkgs; [
isort
nixpkgs-fmt
postgresql_14
python3
];
env = [
{
name = "PGDATA";
eval = "$PRJ_ROOT/tmp/postgres";
}
{
name = "DATABASE_HOST";
eval = "$PGDATA";
}
];
commands = [
{
name = "pg:setup";
category = "database";
help = "Set up PostgreSQL in project folder";
command = ''
initdb --encoding=UTF8 --no-locale -U postgres
echo "listen_addresses = ${"'"}${"'"}" >> $PGDATA/postgresql.conf
echo "unix_socket_directories = '$PGDATA'" >> $PGDATA/postgresql.conf
'';
}
{
name = "pg:start";
category = "database";
help = "Start PostgreSQL instance";
command = ''
[ ! -d $PGDATA ] && pg:setup
pg_ctl -D $PGDATA -U postgres start -l $PRJ_ROOT/log/postgres.log
'';
}
{
name = "pg:stop";
category = "database";
help = "Stop PostgreSQL instance";
command = ''
pg_ctl -D $PGDATA -U postgres stop
'';
}
{
name = "pg:import";
category = "database";
help = "Import sample data";
command = ''
psql -h $PGDATA -U postgres < $PRJ_ROOT/tmp/sample.sql
'';
}
{
name = "pg:console";
category = "database";
help = "Open database console";
command = ''
psql --host $PGDATA -U postgres
'';
}
{
name = "maintenance:lint";
category = "Maintenance";
help = "Lint all the files in project";
command = ''
nixpkgs-fmt *.nix
'';
}
];
};
});
}