Skip to content

Instantly share code, notes, and snippets.

@haitlahcen
Last active January 21, 2019 15:36
Show Gist options
  • Select an option

  • Save haitlahcen/017ce15c0cd653d887de08f6c68d6d0c to your computer and use it in GitHub Desktop.

Select an option

Save haitlahcen/017ce15c0cd653d887de08f6c68d6d0c to your computer and use it in GitHub Desktop.
On the fly postgresql database with postgis extension using Nix
with (import (builtins.fetchTarball {
name = "release-18.09";
url = https://github.com/nixos/nixpkgs/archive/6d4f3311d4b5662e2703cfd3278fd78bd972a56c.tar.gz;
sha256 = "0f3pmk9bf4g8r9kp4v7wksgw2ass9g2p1p2yl07r0nwlij0zpqr6";
}) {});
let
addPostgis = (pg:
buildEnv {
name = "postgresql-and-plugins-${(builtins.parseDrvName pg.name).version}";
paths = [ pg pg.lib (postgis.override { postgresql = pg; }) ];
buildInputs = [ makeWrapper ];
postBuild = ''
mkdir -p $out/bin
rm $out/bin/{pg_config,postgres,pg_ctl}
cp --target-directory=$out/bin ${pg}/bin/{postgres,pg_config,pg_ctl}
wrapProgram $out/bin/postgres --set NIX_PGLIBDIR $out/lib
'';
}
);
in
mkShell {
# Build inputs available in the shell
buildInputs = [ (addPostgis postgresql) glibcLocales ];
# Attributes that are not used by the derivation
# are exported as environment variables
PGDB = "dummy";
PGDATA = "database";
PGPORT = "8888";
# Script that will be run when entering
shellHook = ''
export LANG=en_US.UTF-8;
export LOCALE_ARCHIVE=${glibcLocales}/lib/locale/locale-archive;
mkdir "$PGDATA";
initdb;
pg_ctl start -w;
createdb "$PGDB";
psql "$PGDB" -c "create extension postgis";
finish() {
pg_ctl stop -m fast;
rm -rf "$PGDATA";
}
trap finish EXIT
'';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment