Skip to content

Commit 02edd1a

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 02edd1a

6 files changed

Lines changed: 138 additions & 16 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("${site-env-17}/bin/update-profile /nix/var/nix/profiles/site ${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/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/packages/site-env.nix‎

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,64 @@
55
{
66
self',
77
pkgs,
8-
lib,
98
...
109
}:
1110
let
11+
# Downloads a store path and sets a profile to it.
12+
update-profile = pkgs.writeShellApplication {
13+
name = "update-profile";
14+
text = ''
15+
profile="''${1:?Usage: update-profile <profile> <store path>}"
16+
path="''${2:?Usage: update-profile <profile> <store path>}"
17+
nix-env --profile "$profile" --set "$path"
18+
'';
19+
};
20+
21+
# Runs nix-store --gc in a throttled transient systemd unit.
22+
site-nix-gc = pkgs.writeShellApplication {
23+
name = "site-nix-gc";
24+
text = ''
25+
[ -d /run/systemd/system ] || { echo "Systemd not found. Skipping nix-store --gc."; exit 0; }
26+
systemctl is-active --quiet site-nix-gc || systemd-run --unit=site-nix-gc --collect --no-block --setenv=NIX_REMOTE=local \
27+
-p Nice=19 -p CPUSchedulingPolicy=idle -p IOSchedulingClass=idle \
28+
-p CPUQuota=20% -p MemoryHigh=10% -p MemoryMax=15% -p OOMScoreAdjust=1000 \
29+
-p IOWeight=1 -p IOWriteBandwidthMax="/nix 20M" -p RuntimeMaxSec=2h \
30+
/nix/var/nix/profiles/default/bin/nix-store --gc --max-freed 2G
31+
'';
32+
};
33+
1234
makeSiteEnv =
13-
version: extraPaths:
35+
version:
36+
let
37+
supautils = self'.legacyPackages."psql_${version}".exts.supautils;
38+
activate = pkgs.writeShellApplication {
39+
name = "activate";
40+
runtimeInputs = [ site-nix-gc ];
41+
text = ''
42+
echo "Activating site profile."
43+
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 pg-extensions."
44+
echo "Unlinking old site profile generations."
45+
nix-env --profile /nix/var/nix/profiles/site --delete-generations +2
46+
echo "Scheduling background low priority garbage collection."
47+
site-nix-gc
48+
echo "Site profile activated."
49+
'';
50+
};
51+
in
1452
pkgs.buildEnv {
1553
name = "site-env-${version}";
16-
paths = [ self'.legacyPackages."psql_${version}".exts.supautils ] ++ extraPaths;
54+
paths = [
55+
supautils
56+
activate
57+
site-nix-gc
58+
update-profile
59+
];
1760
};
1861

1962
siteEnvs = {
20-
21-
"site-env-15" = makeSiteEnv "15" [ ];
22-
23-
# gatekeeper is only available for pg 17+ on linux
24-
25-
"site-env-17" = makeSiteEnv "17" (lib.optionals pkgs.stdenv.isLinux [ self'.packages.gatekeeper ]);
26-
27-
"site-env-orioledb-17" = makeSiteEnv "orioledb-17" (
28-
lib.optionals pkgs.stdenv.isLinux [ self'.packages.gatekeeper ]
29-
);
63+
"site-env-15" = makeSiteEnv "15";
64+
"site-env-17" = makeSiteEnv "17";
65+
"site-env-orioledb-17" = makeSiteEnv "orioledb-17";
3066
};
3167
in
3268
{

0 commit comments

Comments
 (0)