|
9 | 9 | ... |
10 | 10 | }: |
11 | 11 | let |
| 12 | + # Scripts run on the host bash to keep Nix's bash and glibc out of the closure. |
| 13 | + writeHostBash = |
| 14 | + name: text: |
| 15 | + pkgs.writeTextFile { |
| 16 | + inherit name; |
| 17 | + executable = true; |
| 18 | + destination = "/bin/${name}"; |
| 19 | + text = '' |
| 20 | + #!/usr/bin/env bash |
| 21 | + set -euo pipefail |
| 22 | + ${text} |
| 23 | + ''; |
| 24 | + checkPhase = '' |
| 25 | + ${pkgs.shellcheck}/bin/shellcheck "$target" |
| 26 | + ''; |
| 27 | + }; |
| 28 | + |
| 29 | + # Prunes old generations, then runs nix-store --gc. Throttled by a transient systemd unit, or by nice and ionice without systemd. |
| 30 | + site-nix-gc = writeHostBash "site-nix-gc" '' |
| 31 | + prune() { nix-env --profile "$1" --delete-generations +2; } |
| 32 | + prune /nix/var/nix/profiles/site |
| 33 | + prune /nix/var/nix/profiles/default |
| 34 | + if [[ -L /var/lib/postgresql/.nix-profile ]]; then |
| 35 | + prune "$(readlink /var/lib/postgresql/.nix-profile)" |
| 36 | + fi |
| 37 | +
|
| 38 | + export NIX_REMOTE=local |
| 39 | + gc=("$(command -v nix-store)" --gc --quiet) |
| 40 | +
|
| 41 | + if [[ -d /run/systemd/system ]]; then |
| 42 | + if systemctl is-active --quiet site-nix-gc; then |
| 43 | + echo "Garbage collection already running." |
| 44 | + exit 0 |
| 45 | + fi |
| 46 | + systemd-run \ |
| 47 | + --unit=site-nix-gc --collect --no-block --setenv=NIX_REMOTE=local \ |
| 48 | + -p Nice=19 -p CPUSchedulingPolicy=idle -p CPUQuota=20% \ |
| 49 | + -p IOSchedulingClass=idle -p IOWeight=1 -p IOWriteBandwidthMax="/nix 20M" \ |
| 50 | + -p MemoryHigh=10% -p MemoryMax=15% -p OOMScoreAdjust=1000 \ |
| 51 | + -p RuntimeMaxSec=2h \ |
| 52 | + "''${gc[@]}" && exit 0 |
| 53 | + echo "systemd-run failed. Running in the foreground." |
| 54 | + fi |
| 55 | +
|
| 56 | + throttle=(nice -n 19) |
| 57 | + command -v ionice >/dev/null && throttle+=(ionice -c 3) |
| 58 | + exec "''${throttle[@]}" "''${gc[@]}" |
| 59 | + ''; |
| 60 | + |
12 | 61 | makeSiteEnv = |
13 | 62 | version: extraPaths: |
14 | 63 | let |
15 | 64 | supautils = self'.legacyPackages."psql_${version}".exts.supautils; |
16 | 65 | # Site profile activation script. MUST STAY IDEMPOTENT! |
17 | | - activate = pkgs.writeTextFile { |
18 | | - name = "activate"; |
19 | | - executable = true; |
20 | | - destination = "/bin/activate"; |
21 | | - # Runs on the host bash to keep Nix's bash and glibc out of the closure. |
22 | | - text = '' |
23 | | - #!/usr/bin/env bash |
24 | | - set -euo pipefail |
25 | | - echo "Activating site profile." |
26 | | - echo "supautils at /nix/var/nix/profiles/site/lib/postgresql/supautils.so: picked up next session via session_preload_libraries, if dynamic_library_path includes /nix/var/nix/profiles/site/lib/postgresql." |
27 | | - echo "Site profile activated." |
28 | | - ''; |
29 | | - checkPhase = '' |
30 | | - ${pkgs.shellcheck}/bin/shellcheck "$target" |
31 | | - ''; |
32 | | - }; |
| 66 | + activate = writeHostBash "activate" '' |
| 67 | + echo "Activating site profile." |
| 68 | + echo "supautils at /nix/var/nix/profiles/site/lib/postgresql/supautils.so: picked up next session via session_preload_libraries, if dynamic_library_path includes /nix/var/nix/profiles/site/lib/postgresql." |
| 69 | + echo "Site profile activated." |
| 70 | + ''; |
33 | 71 | in |
34 | 72 | pkgs.buildEnv { |
35 | 73 | name = "site-env-${version}"; |
36 | 74 | paths = [ |
37 | 75 | supautils |
38 | 76 | activate |
| 77 | + site-nix-gc |
39 | 78 | ] |
40 | 79 | ++ extraPaths; |
41 | | - passthru = { inherit activate; }; |
| 80 | + passthru = { inherit activate site-nix-gc; }; |
42 | 81 | }; |
43 | 82 |
|
44 | 83 | siteEnvs = { |
|
0 commit comments