Skip to content

Instantly share code, notes, and snippets.

@gusmacaulay
Forked from lightdiscord/shell.nix
Last active January 1, 2024 10:57
Show Gist options
  • Save gusmacaulay/9dc5793439750912458f3c6a8945de7d to your computer and use it in GitHub Desktop.
Save gusmacaulay/9dc5793439750912458f3c6a8945de7d to your computer and use it in GitHub Desktop.
Postgresql/Postgis inside nix-shell with sqitch and default postgres user
with import <nixpkgs> {};
stdenv.mkDerivation {
name = "postgresql-inside-nixshell";
buildInputs = [
glibcLocales
(pkgs.postgresql.withPackages (p: [ p.postgis ]))
sqitchPg
pgcli
];
shellHook = ''
export PGDATA="$PWD/db"
export SOCKET_DIRECTORIES="$PWD/sockets"
mkdir $SOCKET_DIRECTORIES
initdb
echo "unix_socket_directories = '$SOCKET_DIRECTORIES'" >> $PGDATA/postgresql.conf
pg_ctl -l $PGDATA/logfile start
createuser postgres --createdb -h localhost
function end {
echo "Shutting down the database..."
pg_ctl stop
echo "Removing directories..."
rm -rf $PGDATA $SOCKET_DIRECTORIES
}
trap end EXIT
'';
}
@gusmacaulay
Copy link
Author

This shell.nix runs up a postgresql instance in a nix-shell, it solves issue with postgis package working with postgresql, and it solves issue with locale settings which means it can run on other host OS' eg. ubuntu,debian

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment