Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
8e52753
feat(nix): add the site profile with supautils
brainrake Oct 8, 2026
3df19f7
Merge branch 'develop' into martonboros/mpg-12-site-env-update
brainrake Oct 8, 2026
6eb13a7
refactor(nix): drop generation pruning and gc from activate
brainrake Oct 8, 2026
c43dedf
docs(nix): describe activate by what it sets up
brainrake Oct 8, 2026
1be6fac
test(nix): run site-nix-gc to the end, restore ami release paths
brainrake Oct 9, 2026
99a9903
feat(nix): prune old profile generations in site-nix-gc
brainrake Oct 9, 2026
78b45e8
Merge branch 'develop' into martonboros/mpg-12-site-env-update
brainrake Oct 9, 2026
6fefb9c
refactor(nix): scope the site profile library path to lib/postgresql
brainrake Oct 10, 2026
33a3d3f
refactor(nix): assume nix is on PATH in site-nix-gc
brainrake Oct 10, 2026
37d8f9f
refactor(nix): run site scripts on the host bash to shrink the closure
brainrake Oct 13, 2026
b4d3087
refactor(nix): drop the per-path deletion log from site-nix-gc
brainrake Oct 13, 2026
d2777fb
Merge branch 'develop' into martonboros/mpg-12-site-env-update
brainrake Oct 14, 2026
13be08f
fix(nix): define the site check on linux only
brainrake Oct 14, 2026
8039363
refactor(nix): move the site test to its own file
brainrake Oct 14, 2026
829282f
feat(ami): pass the site-env store path to ansible
brainrake Oct 14, 2026
5e18459
docs(nix): describe the site profile update and its failure modes
brainrake Oct 14, 2026
dd8cb67
fix(nix): run the site gc to completion and without systemd
brainrake Oct 15, 2026
0c5a273
fix(nix): keep one old site profile generation
brainrake Oct 15, 2026
d662fb8
docs(nix): document site profile rollback and the activate rules
brainrake Oct 16, 2026
d318635
refactor(nix): simplify site-nix-gc and check supautils comes from th…
brainrake Oct 16, 2026
3862ee0
refactor(nix): move site-nix-gc to a follow-up
brainrake Oct 16, 2026
477425c
fix(ci): publish the site-env path to the shared catalog bucket
brainrake Oct 16, 2026
d493f67
refactor(nix): inline the activate script builder
brainrake Oct 18, 2026
9662914
feat(nix): show the configured library settings in activate
brainrake Oct 19, 2026
2b68738
refactor(ci): lay out the site-env catalog by system and name
brainrake Oct 19, 2026
ddc901b
Merge branch 'develop' into martonboros/mpg-12-site-env-update
brainrake Oct 19, 2026
3cb7397
Update .github/workflows/ami-release-nix.yml
brainrake Oct 19, 2026
a01b76b
Merge remote-tracking branch 'origin/develop' into mpg12-squashed
brainrake Oct 19, 2026
3a08642
fix(ci): write the site-env latest pointer in the sha-first layout
brainrake Oct 19, 2026
29e4bef
refactor(ansible): install and activate the site env in one task
brainrake Oct 20, 2026
b24bc8c
docs(nix): trim the site profile docs
brainrake Oct 20, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/actions/build-ami/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,14 @@ runs:
run: |
echo "::group::AMI Build: Stage 2"

system=$([[ ${{ inputs.arch }} == arm64 ]] && echo aarch64-linux || echo x86_64-linux)
site_env_path=$(nix eval --raw "github:supabase/postgres/${{ inputs.packages_git_sha }}#packages.$system.site-env-${{ inputs.postgres_version }}.outPath")

nix run .#build-ami -- stage2 ${{ inputs.arch }} \
-var "ami_name=${{ inputs.ami_name_prefix }}" \
-var "git-head-version=${{ inputs.git_sha }}" \
-var "git_sha=${{ inputs.packages_git_sha }}" \
-var "site_env_path=$site_env_path" \
-var "instance_type=${{ inputs.instance_type }}" \
-var "packer-execution-id=$PACKER_EXECUTION_ID" \
-var "postgres_major_version=${{ inputs.postgres_version }}" \
Expand Down
13 changes: 13 additions & 0 deletions .github/workflows/ami-release-nix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,19 @@ jobs:

echo "Catalog uploaded to ${CATALOG_S3}"

- name: Publish site-env store path

@brainrake brainrake Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This publishes the output paths of the site-env package to public S3 bucket as simple strings in files. It is only used by a scheduled workflow that updates the pins. The pins are the /nix/store path strings themselves so S3 is not part of the update / download.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, so this is why not using a json like the above catalog?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Structured data is not needed here. Plain text is simpler and more robust.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Counterpoint: the structure is stringly typed in the file name.

@brainrake brainrake Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code to publish and consume it does end up smaller and with fewer failure modes.

run: |
GIT_SHA="${{ steps.resolve-git-sha.outputs.sha }}"
SYSTEM=$(nix eval --impure --raw --expr 'builtins.currentSystem')
SITE_ENV_NAME="site-env-${POSTGRES_MAJOR_VERSION}"
SITE_ENV_PATH=$(nix eval --raw ".#${SITE_ENV_NAME}.outPath")

aws s3 cp - "s3://${{ secrets.SHARED_AWS_ARTIFACTS_BUCKET }}/nix-catalog/${GIT_SHA}/${SYSTEM}/${SITE_ENV_NAME}" <<< "$SITE_ENV_PATH"

if [[ "$GITHUB_REF" == refs/heads/release/* ]]; then
aws s3 cp - "s3://${{ secrets.SHARED_AWS_ARTIFACTS_BUCKET }}/nix-catalog/latest/${SYSTEM}/${SITE_ENV_NAME}" <<< "$GIT_SHA"
fi

- name: Create release
uses: softprops/action-gh-release@v3.0.3
with:
Expand Down
2 changes: 1 addition & 1 deletion ansible/files/postgresql_config/postgresql.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ jit_provider = 'llvmjit' # JIT library to use

# - Other Defaults -

#dynamic_library_path = '$libdir'
dynamic_library_path = '/nix/var/nix/profiles/site/lib/postgresql:$libdir'

@brainrake brainrake Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Load supautils from site profile. Safe migration story:

  1. Before any of this lands, supautils is in postgres libdir.
  2. AMI lands nix profile and config change. Loads supautils from site profile. Old AMIs don’t change.
  3. Salt writes it back to just libdir.
  4. Supautils is both in site profile and postgres libdir (written by salt to store). It is loaded from libdir.
  5. Flopping stops on new instances.
  6. Salt bootstrap lands. Old instances get nix profile bootstrapped but no config/package changes.
  7. Verify everyone has site profile.
  8. Salt config change lands. It loads supautils from site profile
  9. Verify config change landed cleanly.
  10. Clean up old salt and compatibility shims.

#gin_fuzzy_search_limit = 0

#------------------------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions ansible/tasks/stage2-setup-postgres.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@
nix-env --set {{ postgres_env_path.stdout }}
"

- name: Install and activate site env from nix binary cache
ansible.builtin.shell: |
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh &&
nix-env --profile /nix/var/nix/profiles/site --set {{ site_env_path }} &&
/nix/var/nix/profiles/site/bin/activate

- name: Set ownership and permissions for file and dirs
ansible.builtin.file:
group: 'postgres'
Expand Down
1 change: 1 addition & 0 deletions ebssurrogate/scripts/nix-provision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function execute_stage2_playbook {
ansible-playbook /tmp/ansible-playbook/ansible/playbook.yml \
--extra-vars '{"stage2":true, "qemu":false}' \
--extra-vars "git_commit_sha=$GIT_SHA" \
--extra-vars "site_env_path=$SITE_ENV_PATH" \
--extra-vars "psql_version=psql_$POSTGRES_MAJOR_VERSION" \
--extra-vars "postgresql_version=postgresql_$POSTGRES_MAJOR_VERSION" \
--extra-vars "postgresql_major_version=$POSTGRES_MAJOR_VERSION" \
Expand Down
1 change: 1 addition & 0 deletions ebssurrogate/scripts/qemu-bootstrap-nix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ function execute_stage2_playbook {
ansible-playbook ./ansible/playbook.yml \
--extra-vars '{"stage2": true, "qemu": true}' \
--extra-vars "git_commit_sha=$GIT_SHA" \
--extra-vars "site_env_path=$SITE_ENV_PATH" \
--extra-vars "postgresql_version=postgresql_$POSTGRES_MAJOR_VERSION" \
--extra-vars "postgresql_major_version=$POSTGRES_MAJOR_VERSION" \
--extra-vars "postgresql_major=$POSTGRES_MAJOR_VERSION" \
Expand Down
1 change: 1 addition & 0 deletions nix/checks.nix
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,7 @@
}
)
// pkgs.lib.optionalAttrs (pkgs.stdenv.isLinux) {
site = import ./tests/site.nix { inherit self pkgs; };
inherit (self'.packages)
postgresql_15_debug
postgresql_15_src
Expand Down
1 change: 1 addition & 0 deletions nix/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ learn how to play with `postgres` in the [build guide](./build-postgres.md).
- **[Update Extensions](./update-extension.md)** - How to update existing extensions
- **[Update Nix Dependecies](./updating-dependencies.md)** - How to update the Nix dependencies
- **[New Major PostgreSQL](./new-major-postgres.md)** - Adding support for new PostgreSQL versions
- **[Site Profile](./site-profile.md)** - The site profile deployed to instances

## Testing

Expand Down
52 changes: 52 additions & 0 deletions nix/docs/site-profile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Site profile

The `site-env-15`, `site-env-17`, and `site-env-orioledb-17` package sets are deployed to `/nix/var/nix/profiles/site`. They hold supautils, and `activate`.

## Publish

`ami-release-nix.yml` uploads each store path as a string to the shared artifacts bucket, in the object `nix-catalog/<sha>/<system>/site-env-<major>`. Release branches also upload the git sha as a string to `nix-catalog/latest/<system>/site-env-<major>`. The AMI bake installs the store path passed in as `site_env_path` and runs `activate`.

## Update

```sh
nix-env --profile /nix/var/nix/profiles/site --set <store path>
/nix/var/nix/profiles/site/bin/activate
```

`--set` downloads the closure from the binary cache if it is missing. It then creates the generation link `site-<N>-link` to the store path and repoints the `site` symlink at that link. If it fails, `nix-env` exits with 1 and the profile is unchanged. This holds when the path can't be fetched, isn't a store path, or the profile directory isn't writable.

`activate` runs after the switch. It prints the library settings configured in `postgresql.conf`, and is idempotent. If it fails, the new generation stays active. Run `activate` again, or roll back.

New sessions load supautils from the new profile. Open sessions keep the copy they loaded until they reconnect. Postgres does not restart. A restart applies the change to every session at once. The same holds after a rollback.

## Roll back

```sh
nix-env --profile /nix/var/nix/profiles/site --rollback
Comment thread
brainrake marked this conversation as resolved.
/nix/var/nix/profiles/site/bin/activate
```

Rollback switches to the previous generation. `activate` then runs from that generation and makes the machine match it. There is no separate rollback script.

To go further back than the previous generation, install an older version by path.

## Install any version by path

```sh
nix-env --profile /nix/var/nix/profiles/site --set <older store path>
/nix/var/nix/profiles/site/bin/activate
```

This is the way to roll back past the one kept generation. It is the same as an update with an older path.

## `activate` safety

`activate` must be idempotent, and the previous generation's `activate` must be able to converge the machine back. Safety shall be ensured by comparing snapshots in tests before and after an update and a rollback.

## Test

```sh
nix build .#checks.<system>.site -L
```

The `site` VM test covers loading supautils from the site profile, the `$libdir` fallthrough, and `activate`.
3 changes: 2 additions & 1 deletion nix/ext/supautils.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ stdenv.mkDerivation rec {
patches = [ ./patches/supautils-strtol-glibc-compat.patch ];

installPhase = ''
mkdir -p $out/lib
mkdir -p $out/lib/postgresql

install -D *${postgresql.dlSuffix} -t $out/lib
ln -s ../supautils${postgresql.dlSuffix} $out/lib/postgresql/supautils${postgresql.dlSuffix}
'';

meta = with lib; {
Expand Down
1 change: 1 addition & 0 deletions nix/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ nav:
- Update Extensions: update-extension.md
- Update Nix Dependencies: updating-dependencies.md
- New Major PostgreSQL: new-major-postgres.md
- Site Profile: site-profile.md
- Testing:
- Adding Tests: adding-tests.md
- Migration Tests: migration-tests.md
Expand Down
3 changes: 3 additions & 0 deletions nix/packages/build-qemu-image/build-qemu-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ install --mode 444 "$code" "$workdir/ovmf_code.fd"
install --mode 644 "$vars" "$workdir/ovmf_vars.fd" # qemu writes to the vars pflash during boot

git_sha=${GIT_SHA:-$(git rev-parse HEAD)}
system=$([[ $arch == arm64 ]] && echo aarch64-linux || echo x86_64-linux)
site_env_path=$(nix eval --raw "github:supabase/postgres/$git_sha#packages.$system.site-env-$postgres_major_version.outPath")
postgres_version=$(yq -er ".postgres_release.postgres$postgres_major_version" ansible/postgres_release.yml)

# Build the cloud-init seed ISO before invoking packer.
Expand All @@ -64,6 +66,7 @@ PACKER_LOG=${PACKER_LOG:-1} packer build \
-var "git_sha=$git_sha" \
-var "machine=$machine" \
-var "postgres-version=$postgres_version" \
-var "site_env_path=$site_env_path" \
-var "postgres_major_version=$postgres_major_version" \
-var "qemu_binary=$qemu" \
-var "workdir=$workdir" \
Expand Down
30 changes: 29 additions & 1 deletion nix/packages/site-env.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,37 @@
let
makeSiteEnv =
version: extraPaths:
let
supautils = self'.legacyPackages."psql_${version}".exts.supautils;
# Site profile activation script. MUST STAY IDEMPOTENT!
activate = pkgs.writeTextFile {
name = "activate";
executable = true;
destination = "/bin/activate";
# Runs on the host bash to keep Nix's bash and glibc out of the closure.
text = ''
#!/usr/bin/env bash
set -euo pipefail
echo "Activating site profile."
echo "Configured in /etc/postgresql/postgresql.conf (runtime could differ):"
for guc in dynamic_library_path session_preload_libraries; do
grep -h "^$guc" /etc/postgresql/postgresql.conf 2>/dev/null || echo "$guc is not set"
done
echo "Site profile activated."
'';
checkPhase = ''
${pkgs.shellcheck}/bin/shellcheck "$target"
'';
};
in
pkgs.buildEnv {
name = "site-env-${version}";
paths = [ self'.legacyPackages."psql_${version}".exts.supautils ] ++ extraPaths;
paths = [
supautils
activate
]
++ extraPaths;
passthru = { inherit activate; };
};

siteEnvs = {
Expand Down
50 changes: 50 additions & 0 deletions nix/tests/site.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{ self, pkgs }:
let
system = pkgs.pkgsLinux.stdenv.hostPlatform.system;
site-env-17 = self.packages.${system}."site-env-17";
psql_17 = self.legacyPackages.${system}."psql_17".bin;
psql_17_slim = self.legacyPackages.${system}."psql_17_slim".bin;
pgConf = pkgs.writeText "postgresql-test.conf" ''
dynamic_library_path = '/nix/var/nix/profiles/site/lib/postgresql:$libdir'
session_preload_libraries = 'supautils'
unix_socket_directories = '/tmp'
'';
in
pkgs.testers.runNixOSTest {
name = "site";
nodes.machine =
{ ... }:
{
environment.systemPackages = [ site-env-17 ];
virtualisation.additionalPaths = [ psql_17 ];
users.users.postgres = {
isSystemUser = true;
group = "postgres";
shell = pkgs.bash;
};
users.groups.postgres = { };
};
testScript = ''
# site profile absent: supautils loads from $libdir
machine.succeed("install -d -o postgres -g postgres /tmp/pgdata-slim")
machine.succeed("su postgres -c '${psql_17_slim}/bin/initdb -D /tmp/pgdata-slim'")
machine.succeed("install -o postgres -g postgres ${pgConf} /tmp/pgdata-slim/postgresql.conf")
machine.succeed("su postgres -c '${psql_17_slim}/bin/pg_ctl -D /tmp/pgdata-slim -l /tmp/pg-slim.log start'")
machine.succeed("su postgres -c '${psql_17_slim}/bin/psql -h /tmp -d postgres -c \"select 1\"'")
machine.succeed("su postgres -c '${psql_17_slim}/bin/pg_ctl -D /tmp/pgdata-slim stop'")

machine.succeed("nix-env --profile /nix/var/nix/profiles/site --set ${site-env-17}")

# without $libdir in the path, supautils can only come from the site profile
machine.succeed("install -d -o postgres -g postgres /tmp/pgdata")
machine.succeed("su postgres -c '${psql_17}/bin/initdb -D /tmp/pgdata'")
machine.succeed("install -o postgres -g postgres ${pgConf} /tmp/pgdata/postgresql.conf")
machine.succeed("sed -i 's/:\\$libdir//' /tmp/pgdata/postgresql.conf")
machine.succeed("grep -qx \"dynamic_library_path = '/nix/var/nix/profiles/site/lib/postgresql'\" /tmp/pgdata/postgresql.conf")
machine.succeed("su postgres -c '${psql_17}/bin/pg_ctl -D /tmp/pgdata -l /tmp/pg.log start'")
machine.succeed("su postgres -c '${psql_17}/bin/psql -h /tmp -d postgres -c \"select 1\"'")
machine.succeed("su postgres -c '${psql_17}/bin/pg_ctl -D /tmp/pgdata stop'")

machine.succeed("${site-env-17}/bin/activate")
'';
}
5 changes: 5 additions & 0 deletions qemu.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ variable "git_sha" {
type = string
}

variable "site_env_path" {
type = string
}

variable "postgres-version" {
type = string
default = ""
Expand Down Expand Up @@ -109,6 +113,7 @@ build {
provisioner "shell" {
environment_vars = [
"GIT_SHA=${var.git_sha}",
"SITE_ENV_PATH=${var.site_env_path}",
"POSTGRES_MAJOR_VERSION=${var.postgres_major_version}"
]
use_env_var_file = true
Expand Down
5 changes: 5 additions & 0 deletions stage2-nix-psql.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ variable "git_sha" {
default = env("GIT_SHA")
}

variable "site_env_path" {
type = string
}

variable "postgres_major_version" {
type = string
default = ""
Expand Down Expand Up @@ -140,6 +144,7 @@ build {
provisioner "shell" {
environment_vars = [
"GIT_SHA=${var.git_sha}",
"SITE_ENV_PATH=${var.site_env_path}",
"POSTGRES_MAJOR_VERSION=${var.postgres_major_version}"
]
use_env_var_file = true
Expand Down
Loading