diff --git a/nixos/modules/profiles/base.nix b/nixos/modules/profiles/base.nix index d6b9ef9..ca22bec 100644 --- a/nixos/modules/profiles/base.nix +++ b/nixos/modules/profiles/base.nix @@ -4,6 +4,10 @@ let inputs = config.dadada.inputs; in { + imports = [ + ./upgrade-pg-cluster.nix + ]; + i18n.defaultLocale = mkDefault "en_US.UTF-8"; console = mkDefault { font = "Lat2-Terminus16"; diff --git a/nixos/modules/profiles/upgrade-pg-cluster.nix b/nixos/modules/profiles/upgrade-pg-cluster.nix new file mode 100644 index 0000000..3042265 --- /dev/null +++ b/nixos/modules/profiles/upgrade-pg-cluster.nix @@ -0,0 +1,32 @@ +{ config, pkgs, lib, ... }: +{ + environment.systemPackages = lib.mkIf config.services.postgresql.enable [ + ( + let + # Do not forget to list the extensions you need. + newPostgres = pkgs.postgresql_15.withPackages (pp: [ ]); + in + pkgs.writeScriptBin "upgrade-pg-cluster" '' + set -eux + # XXX it's perhaps advisable to stop all services that depend on postgresql + systemctl stop postgresql + + export NEWDATA="/var/lib/postgresql/${newPostgres.psqlSchema}" + + export NEWBIN="${newPostgres}/bin" + + export OLDDATA="${config.services.postgresql.dataDir}" + export OLDBIN="${config.services.postgresql.package}/bin" + + install -d -m 0700 -o postgres -g postgres "$NEWDATA" + cd "$NEWDATA" + sudo -u postgres $NEWBIN/initdb -D "$NEWDATA" + + sudo -u postgres $NEWBIN/pg_upgrade \ + --old-datadir "$OLDDATA" --new-datadir "$NEWDATA" \ + --old-bindir $OLDBIN --new-bindir $NEWBIN \ + "$@" + '' + ) + ]; +}