Skip to content

Commit

Permalink
feat(services): added postgres service
Browse files Browse the repository at this point in the history
services-flake is used to provide services the application needs to run.
For now, this includes postgres which can be used for data storage going
forward. Use `nix run .#services` to launch process-compose which will
spin up the services.

The http server also depends on a lockpad server being present but this
is not ran through the services at this time. Instead lockpad has its
own `services` package which functions the same way.

Environment variables have been added that are used to configure
dependent services:
- `PRJ_DATA_HOME` - where to persist service data (recommend: set to the
 absolute path to the `.data` subdirectory of this repo)
- `ANNAPURNA_POSTGRES_PORT` - determines the port to use (note, this is
  disabled via commenting `listen_port`, but the name is reserved)
  • Loading branch information
justinrubek committed Mar 4, 2024
1 parent 7d2faeb commit 42dd383
Show file tree
Hide file tree
Showing 6 changed files with 673 additions and 81 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ result*
target

.static
.data
14 changes: 14 additions & 0 deletions flake-parts/postgres.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{...} @ part-inputs: {
imports = [];

perSystem = {
pkgs,
inputs',
self',
...
}: {
packages = {
postgresql = inputs'.nix-postgres.packages."psql_15/bin";
};
};
}
45 changes: 45 additions & 0 deletions flake-parts/services.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{inputs, ...} @ part-inputs: {
imports = [];

perSystem = {
pkgs,
inputs',
self',
...
}: let
ports = {
annapurna-postgres = "\${ANNAPURNA_POSTGRES_PORT}";
};

global-imports = [
({name, ...}: {
dataDirEnv = "\${PRJ_DATA_HOME}/${name}";
socketDirEnv = "\${PRJ_DATA_HOME}/${name}/sockets";
})
];
in rec {
process-compose = {
services = {
imports = [
inputs.services-flake.processComposeModules.default
];

services.postgres."annapurna-postgres" = {
imports = global-imports;

enable = true;
package = self'.packages.postgresql;

# by not including a listen address, we only listen on the unix socket
# listen_addresses = "127.0.0.1";
port = ports.annapurna-postgres;
initialDatabases = [
{
name = "annapurna-local";
}
];
};
};
};
};
}
4 changes: 3 additions & 1 deletion flake-parts/shells.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
lib,
...
}: let
inherit (self'.packages) rust-toolchain;
inherit (self'.packages) rust-toolchain postgresql;
inherit (self'.legacyPackages) cargoExtraPackages ciPackages;

devTools = [
Expand All @@ -21,6 +21,8 @@
pkgs.wasm-bindgen-cli
# formatting
self'.packages.treefmt

postgresql
];
in {
devShells = {
Expand Down
Loading

0 comments on commit 42dd383

Please sign in to comment.