From 19aabcf6f512b62363a3291f63c9890759ad4d2e Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 12 Apr 2022 17:01:11 +0200 Subject: [PATCH] home: direnv: add 'postgres' library file --- home/direnv/default.nix | 23 +++++++++++++++++++---- home/direnv/lib/postgres.sh | 22 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 home/direnv/lib/postgres.sh diff --git a/home/direnv/default.nix b/home/direnv/default.nix index 86409f0..666f572 100644 --- a/home/direnv/default.nix +++ b/home/direnv/default.nix @@ -7,11 +7,26 @@ in enable = mkDisableOption "direnv configuration"; }; - config.programs.direnv = lib.mkIf cfg.enable { - enable = true; - nix-direnv = { - # A better `use_nix` + config = lib.mkIf cfg.enable { + programs.direnv = { enable = true; + nix-direnv = { + # A better `use_nix` + enable = true; + }; }; + + xdg.configFile = + let + libDir = ./lib; + contents = builtins.readDir libDir; + names = lib.attrNames contents; + files = lib.filter (name: contents.${name} == "regular") names; + linkLibFile = name: + lib.nameValuePair + "direnv/lib/${name}" + { source = libDir + "/${name}"; }; + in + lib.my.genAttrs' files linkLibFile; }; } diff --git a/home/direnv/lib/postgres.sh b/home/direnv/lib/postgres.sh new file mode 100644 index 0000000..c2e6a8f --- /dev/null +++ b/home/direnv/lib/postgres.sh @@ -0,0 +1,22 @@ +#shellcheck shell=bash + +layout_postgres() { + if ! has postgres || ! has initdb; then + # shellcheck disable=2016 + log_error 'layout_postgres: `postgres` and `initdb` are not in PATH' + return 1 + fi + + # shellcheck disable=2155 + export PGDATA="$(direnv_layout_dir)/postgres" + export PGHOST="$PGDATA" + + if [[ ! -d "$PGDATA" ]]; then + initdb + cat >> "$PGDATA/postgresql.conf" << EOF +listen_addresses = '' +unix_socket_directories = '$PGHOST' +EOF + echo "CREATE DATABASE $USER;" | postgres --single -E postgres + fi +}