From 14f70e92e249f56ecf413fa8f262cb6120ea3350 Mon Sep 17 00:00:00 2001 From: pacien Date: Sat, 24 Jul 2021 13:03:57 +0200 Subject: flake: add devel env with isolated postgresql --- .gitignore | 1 + flake.nix | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 flake.nix diff --git a/.gitignore b/.gitignore index 171ad92..177ecee 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +development_database *~ *.swp *.swo diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..e09265f --- /dev/null +++ b/flake.nix @@ -0,0 +1,82 @@ +# UGE / L2 / Intro to relational databases / Python project prototype +# Author: Pacien TRAN-GIRARD +# Licence: EUPL-1.2 + +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-21.05"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, flake-utils }: + flake-utils.lib.eachDefaultSystem (system: + with import nixpkgs { inherit system; }; + let + + develPackagesAndScripts = [ + postgresql_13 # PostgreSQL server with the standard admin tools. + + # More pleasant alternative to psql, with colours and auto-completion. + # Custom configuration to suppress irrelevant warnings and messages. + (writeShellScriptBin "pgcli" '' + ${pgcli}/bin/pgcli --pgclirc "${writeText "pgclirc" '' + [main] + keyring = False + less_chatty = True + ''}" "$@" + '') + + # Script for initialising an independent development database. + # This creates a default empty database name "postgres" and owned by the + # current user. Data are stored in ./development_database/pgdata. + (writeShellScriptBin "dev-initdb" '' + initdb \ + --no-locale \ + --encoding UTF8 \ + --auth-host reject \ + --auth-local peer \ + "$@" + '') + + # Script for starting an independent development posgresql server. + # Accepts connections only through a local UNIX-domain socket. + (writeShellScriptBin "dev-postgres" '' + postgres \ + -h "" \ + -k "$PGHOST" \ + -d 2 \ + "$@" + '') + ]; + + exportEnvVar = k: v: ''export ${k}="${v}"; echo ${k}=\"${v}\"''; + exportDevelEnvVars = lib.mapAttrsToList exportEnvVar develEnvVars; + develEnvVars = rec { + PGDATA = "$PWD/development_database/pgdata"; + PGHOST = "$PWD/development_database"; + PGPORT = "5432"; + PGDATABASE = "app"; + }; + + in { + + devShell = mkShell rec { + buildInputs = develPackagesAndScripts; + + shellHook = '' + echo -e "\nDEVSHELL ENVIRONMENT VARIABLES:" + ${lib.concatStringsSep "\n" exportDevelEnvVars} + + echo -e "\nDEVSHELL COMMANDS:" + ls "${symlinkJoin { name = "env"; paths = buildInputs; }}/bin" + + # Use the default user shell instead of Bash + $(${finger_bsd}/bin/finger $USER \ + | ${gnugrep}/bin/grep -oP 'Shell: \K.*') + + exit $? + ''; + }; + + }); +} -- cgit v1.2.3