From 638c4d62abd060cb6aab8adfafb57ed1c30c315f Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Sun, 26 Nov 2023 15:44:53 -0700 Subject: [PATCH] Add postgres spec. --- specs/postgres/runner | 3 +++ specs/postgres/template/.envrc | 3 +++ specs/postgres/template/README.md | 27 +++++++++++++++++++++++ specs/postgres/template/default.nix | 10 +++++++++ specs/postgres/template/flake.nix | 34 +++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+) create mode 100755 specs/postgres/runner create mode 100644 specs/postgres/template/.envrc create mode 100644 specs/postgres/template/README.md create mode 100644 specs/postgres/template/default.nix create mode 100644 specs/postgres/template/flake.nix diff --git a/specs/postgres/runner b/specs/postgres/runner new file mode 100755 index 0000000..e423c2c --- /dev/null +++ b/specs/postgres/runner @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +cp -r template/. "$OUT" diff --git a/specs/postgres/template/.envrc b/specs/postgres/template/.envrc new file mode 100644 index 0000000..b9238c3 --- /dev/null +++ b/specs/postgres/template/.envrc @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +use flake diff --git a/specs/postgres/template/README.md b/specs/postgres/template/README.md new file mode 100644 index 0000000..05fc50b --- /dev/null +++ b/specs/postgres/template/README.md @@ -0,0 +1,27 @@ +# Postgres Flake Template + +This is a template for constructing a working environment for local +[Postgres](https://www.postgresql.org/) (version 15.5) usage. [direnv](https://direnv.net/) +can be used to a launch a dev shell upon entering this directory (refer to +`.envrc`). Otherwise run via: +```bash +$> nix develop +``` + +## Quickstart + +To begin, create a new database: +```bash +pg_ctl initdb +``` +If the flake's default `devShell` is loaded, this will create a database cluster +at `$PWD/data`. To start the database, run the following: +```bash +pg_ctl start -o --unix_socket_directories="$PGDATA" +``` +To shut the database down, run: +```bash +pg_ctl stop +``` +You can also specify a different location for the database cluster using the +`-D` option in each of the above commands. diff --git a/specs/postgres/template/default.nix b/specs/postgres/template/default.nix new file mode 100644 index 0000000..f620865 --- /dev/null +++ b/specs/postgres/template/default.nix @@ -0,0 +1,10 @@ +(import + ( + let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in + fetchTarball { + url = lock.nodes.flake-compat.locked.url or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; + sha256 = lock.nodes.flake-compat.locked.narHash; + } + ) + { src = ./.; } +).defaultNix diff --git a/specs/postgres/template/flake.nix b/specs/postgres/template/flake.nix new file mode 100644 index 0000000..7ed71d3 --- /dev/null +++ b/specs/postgres/template/flake.nix @@ -0,0 +1,34 @@ +{ + description = '' + An opinionated postgres flake. + + To generate a copy of this template elsewhere, run: + $> bootstrap postgres + ''; + + inputs = { + flake-compat.url = "https://flakehub.com/f/edolstra/flake-compat/1.tar.gz"; + flake-utils.url = "github:numtide/flake-utils"; + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + }; + + outputs = { self, nixpkgs, flake-utils, ... }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + in + { + devShells.default = pkgs.mkShell { + packages = with pkgs; [ + postgresql_15 + ]; + shellHook = '' + # The server will try to use the data directory named by this + # environment variable whenever `-D` is not specified (for e.g. + # `postgres` and `pg_ctl`). + # https://www.postgresql.org/docs/15/server-start.html + export PGDATA="$PWD/data" + ''; + }; + }); +}