Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 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
44ba5fa
feat(nix): add site-nix-gc to the site profile
brainrake Oct 18, 2026
9662914
feat(nix): show the configured library settings in activate
brainrake Oct 19, 2026
b3e62ec
Merge commit '9662914542b41302023c2e71b243277b219720ad' into martonbo…
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
5ad6b53
Merge remote-tracking branch 'origin/martonboros/mpg-12-site-env-upda…
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
87f84b9
docs(nix): note the site-env catalog bucket is private
brainrake Oct 20, 2026
34e018a
Merge remote-tracking branch 'refs/remotes/origin/martonboros/mpg-12-…
brainrake Oct 20, 2026
fe0ffb2
Merge remote-tracking branch 'refs/remotes/origin/develop' into marto…
brainrake Oct 21, 2026
8e0f967
Merge branch 'develop' into martonboros/mpg-1384-add-site-nix-gc-to-t…
brainrake Oct 21, 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
6 changes: 4 additions & 2 deletions nix/docs/site-profile.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 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`.
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`.

## Publish

Expand All @@ -19,6 +19,8 @@ nix-env --profile /nix/var/nix/profiles/site --set <store path>

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.

`site-nix-gc` keeps the current generation and one older one for the site, postgres, and default profiles. It then runs `nix-store --gc` to completion. With systemd it runs in a throttled transient unit. Without systemd it runs under `nice` and `ionice`.

## Roll back

```sh
Expand All @@ -28,7 +30,7 @@ nix-env --profile /nix/var/nix/profiles/site --rollback

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.
`site-nix-gc` keeps the current generation and one older one. To go further back, install an older version by path.

## Install any version by path

Expand Down
79 changes: 59 additions & 20 deletions nix/packages/site-env.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,78 @@
...
}:
let
# Scripts run on the host bash to keep Nix's bash and glibc out of the closure.
writeHostBash =
name: text:
pkgs.writeTextFile {
inherit name;
executable = true;
destination = "/bin/${name}";
text = ''
#!/usr/bin/env bash
set -euo pipefail
${text}
'';
checkPhase = ''
${pkgs.shellcheck}/bin/shellcheck "$target"
'';
};

# Prunes old generations, then runs nix-store --gc. Throttled by a transient systemd unit, or by nice and ionice without systemd.
site-nix-gc = writeHostBash "site-nix-gc" ''
prune() { nix-env --profile "$1" --delete-generations +2; }
prune /nix/var/nix/profiles/site
prune /nix/var/nix/profiles/default
if [[ -L /var/lib/postgresql/.nix-profile ]]; then
prune "$(readlink /var/lib/postgresql/.nix-profile)"
fi

export NIX_REMOTE=local
gc=("$(command -v nix-store)" --gc --quiet)

if [[ -d /run/systemd/system ]]; then
if systemctl is-active --quiet site-nix-gc; then
echo "Garbage collection already running."
exit 0
fi
systemd-run \
--unit=site-nix-gc --collect --no-block --setenv=NIX_REMOTE=local \
-p Nice=19 -p CPUSchedulingPolicy=idle -p CPUQuota=20% \
-p IOSchedulingClass=idle -p IOWeight=1 -p IOWriteBandwidthMax="/nix 20M" \
-p MemoryHigh=10% -p MemoryMax=15% -p OOMScoreAdjust=1000 \
-p RuntimeMaxSec=2h \
"''${gc[@]}" && exit 0
echo "systemd-run failed. Running in the foreground."
fi

throttle=(nice -n 19)
command -v ionice >/dev/null && throttle+=(ionice -c 3)
exec "''${throttle[@]}" "''${gc[@]}"
'';

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"
'';
};
activate = writeHostBash "activate" ''
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."
'';
in
pkgs.buildEnv {
name = "site-env-${version}";
paths = [
supautils
activate
site-nix-gc
]
++ extraPaths;
passthru = { inherit activate; };
passthru = { inherit activate site-nix-gc; };
};

siteEnvs = {
Expand Down
11 changes: 11 additions & 0 deletions nix/tests/site.nix
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ pkgs.testers.runNixOSTest {
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'")

# site-nix-gc runs to the end and reports what it freed
machine.succeed("nix-store --gc")
machine.succeed("echo garbage > /tmp/garbage && nix-store --add /tmp/garbage")
machine.succeed("nix-env --profile /nix/var/nix/profiles/default --set $(dirname $(dirname $(readlink -f $(command -v nix-env))))")
machine.succeed("${site-env-17}/bin/activate")
machine.succeed("${site-env-17}/bin/site-nix-gc")
machine.wait_until_fails("systemctl is-active site-nix-gc", timeout=1800)
print(machine.succeed("journalctl -u site-nix-gc --no-pager | grep freed"))

# without systemd the gc runs in the foreground
machine.succeed("mount -t tmpfs none /run/systemd")
machine.succeed("${site-env-17}/bin/site-nix-gc 2>&1 | grep freed")
'';
}
Loading