Bloc notes | Postgresql development database with NixOS container


Posted on June 22, 2018 by Alex comments [labo nixos containers postgresql]

This article is a draft for an experiment as a Docker alternative for a development station.

pg.nix

with import <nixpkgs> {};

{

  boot.tmpOnTmpfs = true;
  networking.firewall.allowedTCPPorts = [ 5432 ];
  services.postgresql = {
    enable = true;
    enableTCPIP = true;
    authentication = pkgs.lib.mkOverride 10 ''
      local all all trust
      host all all 0.0.0.0/0 trust
      host all all ::1/128 trust
    '';
    initialScript = pkgs.writeText "dev-initScript" ''
      CREATE ROLE dev WITH LOGIN PASSWORD 'dev' CREATEDB;
      CREATE DATABASE dev;
      GRANT ALL PRIVILEGES ON DATABASE dev TO dev;
    '';
  };

}
sudo nixos-container create pg-dev --config-file=./nix/pg.nix
sudo nixos-container start pg-dev
nix-shell -p postgresql --run 'psql -h $(nixos-container show-ip pg-dev) -U dev -W dev'