Skip to content

Commit 1834a72

Browse files
brainrakeclaude
andcommitted
feat(nix): add the site profile with supautils
Refs: MPG-12 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 893b3d3 commit 1834a72

9 files changed

Lines changed: 172 additions & 4 deletions

File tree

‎.github/workflows/ami-release-nix.yml‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ on:
1010
- ansible/vars.yml
1111
- flake.lock
1212
- flake.nix
13-
- nix/packages/build-ami.nix
13+
- nix/**
1414
workflow_dispatch:
1515

1616
permissions:
@@ -203,6 +203,25 @@ jobs:
203203
204204
echo "Catalog uploaded to ${CATALOG_S3}"
205205
206+
- name: configure aws credentials - public artifacts
207+
uses: aws-actions/configure-aws-credentials@7474bc4690e29a8392af63c5b98e7449536d5c3a # v4.3.1
208+
with:
209+
role-to-assume: ${{ secrets.DEV_AWS_ROLE }}
210+
aws-region: ${{ env.AWS_REGION }}
211+
212+
- name: Publish site-env store path
213+
run: |
214+
GIT_SHA="${{ steps.resolve-git-sha.outputs.sha }}"
215+
SYSTEM=$(nix eval --impure --raw --expr 'builtins.currentSystem')
216+
SITE_ENV_NAME="site-env-${POSTGRES_MAJOR_VERSION}"
217+
SITE_ENV_PATH=$(nix eval --raw ".#${SITE_ENV_NAME}.outPath")
218+
219+
aws s3 cp - "s3://supabase-public-artifacts-bucket/nix-catalog/${GIT_SHA}-${SITE_ENV_NAME}-${SYSTEM}" <<< "$SITE_ENV_PATH"
220+
221+
if [[ "$GITHUB_REF" == refs/heads/release/* ]]; then
222+
aws s3 cp - "s3://supabase-public-artifacts-bucket/nix-catalog/latest-${SITE_ENV_NAME}-${SYSTEM}.sha" <<< "$GIT_SHA"
223+
fi
224+
206225
- name: Create release
207226
uses: softprops/action-gh-release@a06a81a03ee405af7f2048a818ed3f03bbf83c7b # v2.5.0
208227
with:

‎ansible/files/postgresql_config/postgresql.conf.j2‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ jit_provider = 'llvmjit' # JIT library to use
695695

696696
# - Other Defaults -
697697

698-
#dynamic_library_path = '$libdir'
698+
dynamic_library_path = '/nix/var/nix/profiles/site/pg-extensions:$libdir'
699699
#gin_fuzzy_search_limit = 0
700700

701701
#------------------------------------------------------------------------------

‎ansible/tasks/stage2-setup-postgres.yml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,20 @@
7777
nix-env --set {{ postgres_env_path.stdout }}
7878
"
7979
80+
- name: Resolve site env store path
81+
ansible.builtin.shell: |
82+
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh &&
83+
nix build --no-link --print-out-paths github:supabase/postgres/{{ git_commit_sha }}#site-env-{{ postgresql_major_version }}
84+
register: site_env_path
85+
86+
- name: Install site env from nix binary cache
87+
ansible.builtin.shell: |
88+
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh &&
89+
nix-env --profile /nix/var/nix/profiles/site --set {{ site_env_path.stdout }}
90+
91+
- name: Activate site env
92+
ansible.builtin.command: /nix/var/nix/profiles/site/bin/activate
93+
8094
- name: Install supascan for baseline validation
8195
ansible.builtin.shell: |
8296
sudo -u ubuntu bash -c ". /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && nix profile install github:supabase/postgres/{{ git_commit_sha }}#supascan"

‎nix/checks.nix‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,58 @@
909909
wal-g-3
910910
;
911911
devShell = self'.devShells.default;
912+
site =
913+
let
914+
system = pkgs.pkgsLinux.stdenv.hostPlatform.system;
915+
site-env-17 = self.packages.${system}."site-env-17";
916+
psql_17 = self.legacyPackages.${system}."psql_17".bin;
917+
psql_17_slim = self.legacyPackages.${system}."psql_17_slim".bin;
918+
pgConf = pkgs.writeText "postgresql-test.conf" ''
919+
dynamic_library_path = '/nix/var/nix/profiles/site/pg-extensions:$libdir'
920+
session_preload_libraries = 'supautils'
921+
unix_socket_directories = '/tmp'
922+
'';
923+
in
924+
pkgs.testers.runNixOSTest {
925+
name = "site";
926+
nodes.machine =
927+
{ ... }:
928+
{
929+
environment.systemPackages = [ site-env-17 ];
930+
users.users.postgres = {
931+
isSystemUser = true;
932+
group = "postgres";
933+
shell = pkgs.bash;
934+
};
935+
users.groups.postgres = { };
936+
};
937+
testScript = ''
938+
# site profile absent: supautils loads from $libdir
939+
machine.succeed("install -d -o postgres -g postgres /tmp/pgdata-slim")
940+
machine.succeed("su postgres -c '${psql_17_slim}/bin/initdb -D /tmp/pgdata-slim'")
941+
machine.succeed("install -o postgres -g postgres ${pgConf} /tmp/pgdata-slim/postgresql.conf")
942+
machine.succeed("su postgres -c '${psql_17_slim}/bin/pg_ctl -D /tmp/pgdata-slim -l /tmp/pg-slim.log start'")
943+
machine.succeed("su postgres -c '${psql_17_slim}/bin/psql -h /tmp -d postgres -c \"select 1\"'")
944+
machine.succeed("su postgres -c '${psql_17_slim}/bin/pg_ctl -D /tmp/pgdata-slim stop'")
945+
946+
machine.succeed("nix-env --profile /nix/var/nix/profiles/site --set ${site-env-17}")
947+
948+
# postgres can load supautils via the site profile's dynamic_library_path
949+
machine.succeed("install -d -o postgres -g postgres /tmp/pgdata")
950+
machine.succeed("su postgres -c '${psql_17}/bin/initdb -D /tmp/pgdata'")
951+
machine.succeed("install -o postgres -g postgres ${pgConf} /tmp/pgdata/postgresql.conf")
952+
machine.succeed("su postgres -c '${psql_17}/bin/pg_ctl -D /tmp/pgdata -l /tmp/pg.log start'")
953+
machine.succeed("su postgres -c '${psql_17}/bin/psql -h /tmp -d postgres -c \"select 1\"'")
954+
machine.succeed("su postgres -c '${psql_17}/bin/pg_ctl -D /tmp/pgdata stop'")
955+
956+
# activate starts the gc
957+
machine.succeed("mkdir -p /nix/var/nix/profiles/default")
958+
machine.succeed("ln -sfn /run/current-system/sw/bin /nix/var/nix/profiles/default/bin")
959+
machine.succeed("${site-env-17}/bin/activate")
960+
machine.succeed("systemctl is-active site-nix-gc")
961+
machine.succeed("systemctl stop site-nix-gc")
962+
'';
963+
};
912964
}
913965
// (import ./ext/tests {
914966
inherit self;

‎nix/docs/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ learn how to play with `postgres` in the [build guide](./build-postgres.md).
3333
- **[Update Nix Dependecies](./updating-dependencies.md)** - How to update the Nix dependencies
3434
- **[New Major PostgreSQL](./new-major-postgres.md)** - Adding support for new PostgreSQL versions
3535
- **[Nix Overlays](./nix-overlays.md)** - Understanding and using Nix overlays
36+
- **[Site Profile](./site-profile.md)** - The site profile deployed to instances
3637

3738
## Testing
3839

‎nix/docs/site-profile.md‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Site profile
2+
3+
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, `activate`, and `site-nix-gc`.
4+
5+
`dynamic_library_path` in `postgresql.conf` includes `/nix/var/nix/profiles/site/pg-extensions` first, then `$libdir`. supautils also remains in `$libdir`, pending cleanup.
6+
7+
## Publish
8+
9+
`ami-release-nix.yml` uploads each store path as a string to the public artifacts bucket, in the object `nix-catalog/<sha>-site-env-<major>-<system>`. Release branches also upload the git sha as a string to `latest-site-env-<major>-<system>.sha`. The AMI bake installs the env and runs `activate`.
10+
11+
## Update
12+
13+
```sh
14+
nix-env --profile /nix/var/nix/profiles/site --set <store path>
15+
/nix/var/nix/profiles/site/bin/activate
16+
```
17+
18+
`--set` downloads the path if it is missing. `activate` prunes the profile to its last two generations and starts a throttled `nix-store --gc` through systemd. The GC is skipped without systemd. `activate` must stay idempotent.
19+
20+
## Roll back
21+
22+
```sh
23+
nix-env --profile /nix/var/nix/profiles/site --rollback
24+
```
25+
26+
`activate` keeps two generations, so one rollback step is always available.
27+
28+
## Test
29+
30+
```sh
31+
nix build .#checks.<system>.site -L
32+
```
33+
34+
The `site` VM test covers loading supautils from the site profile, the `$libdir` fallthrough, and `activate`.

‎nix/ext/supautils.nix‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ stdenv.mkDerivation rec {
2727
patches = [ ./patches/supautils-strtol-glibc-compat.patch ];
2828

2929
installPhase = ''
30-
mkdir -p $out/lib
30+
mkdir -p $out/lib $out/pg-extensions
3131
3232
install -D *${postgresql.dlSuffix} -t $out/lib
33+
ln -s ../lib/supautils${postgresql.dlSuffix} $out/pg-extensions/supautils${postgresql.dlSuffix}
3334
'';
3435

3536
meta = with lib; {

‎nix/mkdocs.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ nav:
3232
- Update Nix Dependencies: updating-dependencies.md
3333
- New Major PostgreSQL: new-major-postgres.md
3434
- Nix Overlays: nix-overlays.md
35+
- Site Profile: site-profile.md
3536
- Testing:
3637
- Adding Tests: adding-tests.md
3738
- Migration Tests: migration-tests.md

‎nix/packages/site-env.nix‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,57 @@
99
...
1010
}:
1111
let
12+
# Runs nix-store --gc in a throttled transient systemd unit.
13+
site-nix-gc = pkgs.writeShellApplication {
14+
name = "site-nix-gc";
15+
text = ''
16+
if [[ ! -d /run/systemd/system ]]; then
17+
echo "Systemd not found. Skipping nix-store --gc."
18+
exit 0
19+
fi
20+
if systemctl is-active --quiet site-nix-gc; then
21+
echo "Garbage collection already running. Skipping nix-store --gc."
22+
exit 0
23+
fi
24+
systemd-run \
25+
--unit=site-nix-gc --collect --no-block --setenv=NIX_REMOTE=local \
26+
-p Nice=19 -p CPUSchedulingPolicy=idle -p CPUQuota=20% \
27+
-p IOSchedulingClass=idle -p IOWeight=1 -p IOWriteBandwidthMax="/nix 20M" \
28+
-p MemoryHigh=10% -p MemoryMax=15% -p OOMScoreAdjust=1000 \
29+
-p RuntimeMaxSec=2h \
30+
/nix/var/nix/profiles/default/bin/nix-store --gc --max-freed 2G \
31+
|| echo "systemd-run failed. Skipping nix-store --gc."
32+
'';
33+
};
34+
1235
makeSiteEnv =
1336
version: extraPaths:
37+
let
38+
supautils = self'.legacyPackages."psql_${version}".exts.supautils;
39+
# Site profile activation script. MUST STAY IDEMPOTENT!
40+
activate = pkgs.writeShellApplication {
41+
name = "activate";
42+
runtimeInputs = [ site-nix-gc ];
43+
text = ''
44+
echo "Activating site profile."
45+
echo "supautils at /nix/var/nix/profiles/site/pg-extensions/supautils.so: picked up next session via session_preload_libraries, if dynamic_library_path includes /nix/var/nix/profiles/site/pg-extensions."
46+
echo "Unlinking old site profile generations."
47+
nix-env --profile /nix/var/nix/profiles/site --delete-generations +2
48+
echo "Scheduling background low priority nix garbage collection."
49+
site-nix-gc
50+
echo "Site profile activated."
51+
'';
52+
};
53+
in
1454
pkgs.buildEnv {
1555
name = "site-env-${version}";
16-
paths = [ self'.legacyPackages."psql_${version}".exts.supautils ] ++ extraPaths;
56+
paths = [
57+
supautils
58+
activate
59+
site-nix-gc
60+
]
61+
++ extraPaths;
62+
passthru = { inherit activate site-nix-gc; };
1763
};
1864

1965
siteEnvs = {

0 commit comments

Comments
 (0)