|
1 | 1 | {
|
2 | 2 | lib,
|
3 | 3 | stdenv,
|
| 4 | + buildEnv, |
4 | 5 | fetchFromGitHub,
|
5 | 6 | libkrb5,
|
6 | 7 | openssl,
|
7 | 8 | postgresql,
|
8 | 9 | }:
|
9 | 10 | #adapted from https://github.com/NixOS/nixpkgs/blob/master/pkgs/servers/sql/postgresql/ext/pgaudit.nix
|
10 | 11 | let
|
11 |
| - source = |
12 |
| - { |
13 |
| - "17" = { |
14 |
| - version = "17.0"; |
15 |
| - hash = "sha256-3ksq09wiudQPuBQI3dhEQi8IkXKLVIsPFgBnwLiicro="; |
16 |
| - }; |
17 |
| - "16" = { |
18 |
| - version = "16.0"; |
19 |
| - hash = "sha256-8+tGOl1U5y9Zgu+9O5UDDE4bec4B0JC/BQ6GLhHzQzc="; |
20 |
| - }; |
21 |
| - "15" = { |
22 |
| - version = "1.7.0"; |
23 |
| - hash = "sha256-8pShPr4HJaJQPjW1iPJIpj3CutTx8Tgr+rOqoXtgCcw="; |
24 |
| - }; |
25 |
| - } |
26 |
| - .${lib.versions.major postgresql.version} |
27 |
| - or (throw "Source for pgaudit is not available for ${postgresql.version}"); |
28 |
| -in |
29 |
| -stdenv.mkDerivation { |
30 | 12 | pname = "pgaudit";
|
31 |
| - inherit (source) version; |
| 13 | + # Load version configuration from external file |
| 14 | + allVersions = (builtins.fromJSON (builtins.readFile ./versions.json)).${pname}; |
32 | 15 |
|
33 |
| - src = fetchFromGitHub { |
34 |
| - owner = "pgaudit"; |
35 |
| - repo = "pgaudit"; |
36 |
| - rev = source.version; |
37 |
| - hash = source.hash; |
38 |
| - }; |
| 16 | + # Filter versions compatible with current PostgreSQL version |
| 17 | + supportedVersions = lib.filterAttrs ( |
| 18 | + _: value: builtins.elem (lib.versions.major postgresql.version) value.postgresql |
| 19 | + ) allVersions; |
| 20 | + |
| 21 | + # Derived version information |
| 22 | + versions = lib.naturalSort (lib.attrNames supportedVersions); |
| 23 | + latestVersion = lib.last versions; |
| 24 | + numberOfVersions = builtins.length versions; |
| 25 | + packages = builtins.attrValues ( |
| 26 | + lib.mapAttrs (name: value: build name value.hash) supportedVersions |
| 27 | + ); |
| 28 | + |
| 29 | + # Build function for individual pgaudit versions |
| 30 | + build = |
| 31 | + version: hash: |
| 32 | + stdenv.mkDerivation { |
| 33 | + inherit pname version; |
| 34 | + |
| 35 | + src = fetchFromGitHub { |
| 36 | + owner = "pgaudit"; |
| 37 | + repo = "pgaudit"; |
| 38 | + rev = version; |
| 39 | + inherit hash; |
| 40 | + }; |
| 41 | + |
| 42 | + buildInputs = [ |
| 43 | + libkrb5 |
| 44 | + openssl |
| 45 | + postgresql |
| 46 | + ]; |
| 47 | + |
| 48 | + makeFlags = [ "USE_PGXS=1" ]; |
39 | 49 |
|
40 |
| - buildInputs = [ |
41 |
| - libkrb5 |
42 |
| - openssl |
43 |
| - postgresql |
| 50 | + postBuild = |
| 51 | + lib.optionalString (version == "1.7.0") '' |
| 52 | + mv ${pname}--1.7.sql ${pname}--1.7.0.sql |
| 53 | + cp ${pname}--1.7.0.sql ${pname}--1.6.1--1.7.0.sql |
| 54 | + '' |
| 55 | + + lib.optionalString (version == "1.7.1") '' |
| 56 | + mv ${pname}--1.7--1.7.1.sql ${pname}--1.7.0--1.7.1.sql |
| 57 | + ''; |
| 58 | + |
| 59 | + installPhase = '' |
| 60 | + runHook preInstall |
| 61 | +
|
| 62 | + mkdir -p $out/{lib,share/postgresql/extension} |
| 63 | +
|
| 64 | + # Install shared library with version suffix |
| 65 | + mv ${pname}${postgresql.dlSuffix} $out/lib/${pname}-${version}${postgresql.dlSuffix} |
| 66 | +
|
| 67 | + # Install SQL files |
| 68 | + sed -i '1s/^/DROP EVENT TRIGGER IF EXISTS pgaudit_ddl_command_end; \n/' *.sql |
| 69 | + sed -i '1s/^/DROP EVENT TRIGGER IF EXISTS pgaudit_sql_drop; \n/' *.sql |
| 70 | + sed -i 's/CREATE FUNCTION/CREATE OR REPLACE FUNCTION/' *.sql |
| 71 | + cp *.sql $out/share/postgresql/extension |
| 72 | +
|
| 73 | + # Create version-specific control file |
| 74 | + sed -e "/^default_version =/d" \ |
| 75 | + -e "s|^module_pathname = .*|module_pathname = '\$libdir/${pname}'|" \ |
| 76 | + ${pname}.control > $out/share/postgresql/extension/${pname}--${version}.control |
| 77 | +
|
| 78 | + # For the latest version, create default control file and symlink |
| 79 | + if [[ "${version}" == "${latestVersion}" ]]; then |
| 80 | + { |
| 81 | + echo "default_version = '${latestVersion}'" |
| 82 | + cat $out/share/postgresql/extension/${pname}--${latestVersion}.control |
| 83 | + } > $out/share/postgresql/extension/${pname}.control |
| 84 | + ln -sfn ${pname}-${latestVersion}${postgresql.dlSuffix} $out/lib/${pname}${postgresql.dlSuffix} |
| 85 | + fi |
| 86 | +
|
| 87 | + runHook postInstall |
| 88 | + ''; |
| 89 | + |
| 90 | + meta = with lib; { |
| 91 | + description = "Open Source PostgreSQL Audit Logging"; |
| 92 | + homepage = "https://github.com/pgaudit/pgaudit"; |
| 93 | + changelog = "https://github.com/pgaudit/pgaudit/releases/tag/${source.version}"; |
| 94 | + license = licenses.postgresql; |
| 95 | + inherit (postgresql.meta) platforms; |
| 96 | + }; |
| 97 | + }; |
| 98 | +in |
| 99 | +buildEnv { |
| 100 | + name = pname; |
| 101 | + paths = packages; |
| 102 | + pathsToLink = [ |
| 103 | + "/lib" |
| 104 | + "/share/postgresql/extension" |
44 | 105 | ];
|
| 106 | + postBuild = '' |
| 107 | + # checks |
| 108 | + (set -x |
| 109 | + test "$(ls -A $out/lib/${pname}*${postgresql.dlSuffix} | wc -l)" = "${ |
| 110 | + toString (numberOfVersions + 1) |
| 111 | + }" |
| 112 | + ) |
45 | 113 |
|
46 |
| - makeFlags = [ "USE_PGXS=1" ]; |
| 114 | + # Verify all expected library files are present |
| 115 | + expectedFiles=${toString (numberOfVersions + 1)} |
| 116 | + actualFiles=$(ls -A $out/lib/${pname}*${postgresql.dlSuffix} | wc -l) |
47 | 117 |
|
48 |
| - installPhase = '' |
49 |
| - install -D -t $out/lib pgaudit${postgresql.dlSuffix} |
50 |
| - install -D -t $out/share/postgresql/extension *.sql |
51 |
| - install -D -t $out/share/postgresql/extension *.control |
| 118 | + if [[ "$actualFiles" != "$expectedFiles" ]]; then |
| 119 | + echo "Error: Expected $expectedFiles library files, found $actualFiles" |
| 120 | + echo "Files found:" |
| 121 | + ls -la $out/lib/${pname}*${postgresql.dlSuffix} || true |
| 122 | + exit 1 |
| 123 | + fi |
52 | 124 | '';
|
53 | 125 |
|
54 |
| - meta = with lib; { |
55 |
| - description = "Open Source PostgreSQL Audit Logging"; |
56 |
| - homepage = "https://github.com/pgaudit/pgaudit"; |
57 |
| - changelog = "https://github.com/pgaudit/pgaudit/releases/tag/${source.version}"; |
58 |
| - platforms = postgresql.meta.platforms; |
59 |
| - license = licenses.postgresql; |
| 126 | + passthru = { |
| 127 | + inherit versions numberOfVersions; |
| 128 | + pname = "${pname}-all"; |
| 129 | + version = |
| 130 | + "multi-" + lib.concatStringsSep "-" (map (v: lib.replaceStrings [ "." ] [ "-" ] v) versions); |
| 131 | + defaultSettings = { |
| 132 | + "shared_preload_libraries" = pname; |
| 133 | + }; |
60 | 134 | };
|
61 | 135 | }
|
0 commit comments