How to configure PostgreSQL declaratively (NixOS and non-NixOS)?

there are two reasons why.

  1. Kinda isolation. Which is great for reproducibility
  2. It allows NixOS-style declarative config

Note, that there is another project to extend nix-shell into NixOS config: nixos-shell by Chris. It was nice, but it requires now some love to work on Ubuntu.

It would be really nice to combine extra-container and nixos-shell tools together (ideally embed into stock nixos-container).

Is it because nix-shell kind of like chroot ?

nix-shell is nowhere like chroot. nix-shell --pure is closer.

but I assume the proper way to move forward in production is using containers.

You can run without both containers and systemd if you want, There exists a thing called “runner”:

$ cat runner.nix
with import <nixpkgs/nixos> { configuration = {
  imports = [ ./configuration-test.nix ];
  boot.isContainer = true;
}; };
config.systemd.services.postgresql.runner

$ cat configuration-test.nix
{ config, pkgs, ... }: {
   services.postgresql.enable = true;
   services.postgresql.dataDir = "/tmp/datadir";
   services.postgresql.port = 5431;
   system.stateVersion = "18.09";
}

$ sudo -u postgres $(nix-build runner.nix --no-out-link)
these derivations will be built:
  /nix/store/jpgy6m37c9lpzj8pq234410sl8nl3q8n-unit-script-postgresql-post-start.drv
  /nix/store/y2md2wq9gw49v74cxh1510lyins4wlxc-postgresql.conf.drv
  /nix/store/k3qja76c9jq5wjf2j7vdbiv3qa97ipfk-unit-script-postgresql-start.drv
  /nix/store/gvshan2phnxh6p74iigvy50crvdx6dpk-postgresql-runner.drv
building '/nix/store/jpgy6m37c9lpzj8pq234410sl8nl3q8n-unit-script-postgresql-post-start.drv'...
building '/nix/store/y2md2wq9gw49v74cxh1510lyins4wlxc-postgresql.conf.drv'...
building '/nix/store/k3qja76c9jq5wjf2j7vdbiv3qa97ipfk-unit-script-postgresql-start.drv'...
building '/nix/store/gvshan2phnxh6p74iigvy50crvdx6dpk-postgresql-runner.drv'...
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = (unset),
        LC_ALL = "en_US.UTF-8",
        LC_CTYPE = "en_US.UTF-8",
        LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
running ExecStartPre: /nix/store/8jr8k2nnhal3rsi75pws0nyjlvafzijb-unit-script-postgresql-pre-start
running ExecStart: /nix/store/n44a8135hq4kp6fvp5dxl67i8y29z6zr-unit-script-postgresql-start
running ExecStartPost: /nix/store/blpjf150p5j98fvm989gk0kp8qk11c2b-unit-script-postgresql-post-start
LOG:  database system was shut down at 2019-09-18 19:08:03 GMT
LOG:  MultiXact member wraparound protections are now enabled
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started

I don’t use it myself.

PS. In general, NixOS (not Nix, NixOS!) experience on Ubuntu is bad. I’ve recorded my current setup in NixOS-like experience on Ubuntu in AWS. So if you find containers difficult to use on Ubuntu, then don’t bother with that and use what is easiest for you.

1 Like