1010#adapted from https://github.com/NixOS/nixpkgs/blob/master/pkgs/servers/sql/postgresql/ext/pgaudit.nix
1111let
1212 pname = "pgaudit" ;
13+
14+ # Load version configuration from external file
15+ allVersions = ( builtins . fromJSON ( builtins . readFile ./versions.json ) ) . ${ pname } ;
16+
17+ # Filter versions compatible with current PostgreSQL version
18+ supportedVersions = lib . filterAttrs (
19+ _ : value : builtins . elem ( lib . versions . major postgresql . version ) value . postgresql
20+ ) allVersions ;
21+
22+ # Derived version information
23+ versions = lib . naturalSort ( lib . attrNames supportedVersions ) ;
24+ latestVersion = lib . last versions ;
25+ numberOfVersions = builtins . length versions ;
26+ packages = builtins . attrValues (
27+ lib . mapAttrs ( name : value : build name value . hash ) supportedVersions
28+ ) ;
29+
30+ # Build function for individual pgaudit versions
1331 build =
1432 version : hash :
1533 stdenv . mkDerivation {
3351 postBuild =
3452 lib . optionalString ( version == "1.7.0" ) ''
3553 mv ${ pname } --1.7.sql ${ pname } --1.7.0.sql
54+ cp ${ pname } --1.7.0.sql ${ pname } --1.6.1--1.7.0.sql
3655 ''
3756 + lib . optionalString ( version == "1.7.1" ) ''
3857 mv ${ pname } --1.7--1.7.1.sql ${ pname } --1.7.0--1.7.1.sql
3958 '' ;
4059
4160 installPhase = ''
61+ runHook preInstall
62+
4263 mkdir -p $out/{lib,share/postgresql/extension}
64+
65+ # Install shared library with version suffix
4366 mv ${ pname } ${ postgresql . dlSuffix } $out/lib/${ pname } -${ version } ${ postgresql . dlSuffix }
4467
45- create_sql_files() {
46- cp *.sql $out/share/postgresql/extension
47- }
48-
49- create_control_files() {
50- sed -e "/^default_version =/d" \
51- -e "s|^module_pathname = .*|module_pathname = '\$libdir/${ pname } '|" \
52- ${ pname } .control > $out/share/postgresql/extension/${ pname } --${ version } .control
53-
54- if [[ "${ version } " == "${ latestVersion } " ]]; then
55- {
56- echo "default_version = '${ latestVersion } '"
57- cat $out/share/postgresql/extension/${ pname } --${ latestVersion } .control
58- } > $out/share/postgresql/extension/${ pname } .control
59- ln -sfn ${ pname } -${ latestVersion } ${ postgresql . dlSuffix } $out/lib/${ pname } ${ postgresql . dlSuffix }
60- fi
61- }
62-
63- create_sql_files
64- create_control_files
68+ # Install SQL files
69+ sed -i '1s/^/DROP EVENT TRIGGER IF EXISTS pgaudit_ddl_command_end; \n/' *.sql
70+ sed -i '1s/^/DROP EVENT TRIGGER IF EXISTS pgaudit_sql_drop; \n/' *.sql
71+ sed -i 's/CREATE FUNCTION/CREATE OR REPLACE FUNCTION/' *.sql
72+ cp *.sql $out/share/postgresql/extension
73+
74+ # Create version-specific control file
75+ sed -e "/^default_version =/d" \
76+ -e "s|^module_pathname = .*|module_pathname = '\$libdir/${ pname } '|" \
77+ ${ pname } .control > $out/share/postgresql/extension/${ pname } --${ version } .control
78+
79+ # For the latest version, create default control file and symlink
80+ if [[ "${ version } " == "${ latestVersion } " ]]; then
81+ {
82+ echo "default_version = '${ latestVersion } '"
83+ cat $out/share/postgresql/extension/${ pname } --${ latestVersion } .control
84+ } > $out/share/postgresql/extension/${ pname } .control
85+ ln -sfn ${ pname } -${ latestVersion } ${ postgresql . dlSuffix } $out/lib/${ pname } ${ postgresql . dlSuffix }
86+ fi
87+
88+ runHook postInstall
6589 '' ;
6690
6791 meta = with lib ; {
7296 inherit ( postgresql . meta ) platforms ;
7397 } ;
7498 } ;
75- allVersions = ( builtins . fromJSON ( builtins . readFile ./versions.json ) ) . ${ pname } ;
76- supportedVersions = lib . filterAttrs (
77- _ : value : builtins . elem ( lib . versions . major postgresql . version ) value . postgresql
78- ) allVersions ;
79- versions = lib . naturalSort ( lib . attrNames supportedVersions ) ;
80- latestVersion = lib . last versions ;
81- numberOfVersions = builtins . length versions ;
82- packages = builtins . attrValues (
83- lib . mapAttrs ( name : value : build name value . hash ) supportedVersions
84- ) ;
8599in
86100pkgs . buildEnv {
87101 name = pname ;
@@ -91,12 +105,16 @@ pkgs.buildEnv {
91105 "/share/postgresql/extension"
92106 ] ;
93107 postBuild = ''
94- # checks
95- (set -x
96- test "$(ls -A $out/lib/${ pname } *${ postgresql . dlSuffix } | wc -l)" = "${
97- toString ( numberOfVersions + 1 )
98- } "
99- )
108+ # Verify all expected library files are present
109+ expectedFiles=${ toString ( numberOfVersions + 1 ) }
110+ actualFiles=$(ls -A $out/lib/${ pname } *${ postgresql . dlSuffix } | wc -l)
111+
112+ if [[ "$actualFiles" != "$expectedFiles" ]]; then
113+ echo "Error: Expected $expectedFiles library files, found $actualFiles"
114+ echo "Files found:"
115+ ls -la $out/lib/${ pname } *${ postgresql . dlSuffix } || true
116+ exit 1
117+ fi
100118 '' ;
101119 passthru = {
102120 inherit versions numberOfVersions ;
0 commit comments