1let
2
3 generic =
4 # dependencies
5 { stdenv, lib, fetchurl, makeWrapper
6 , glibc, zlib, readline, openssl, icu, systemd, libossp_uuid
7 , pkgconfig, libxml2, tzdata
8
9 # for postgreql.pkgs
10 , this, self, newScope, buildEnv
11
12 # source specification
13 , version, sha256, psqlSchema
14 }:
15 let
16 atLeast = lib.versionAtLeast version;
17 icuEnabled = atLeast "10";
18
19 in stdenv.mkDerivation rec {
20 name = "postgresql-${version}";
21 inherit version;
22
23 src = fetchurl {
24 url = "mirror://postgresql/source/v${version}/${name}.tar.bz2";
25 inherit sha256;
26 };
27
28 outputs = [ "out" "lib" "doc" "man" ];
29 setOutputFlags = false; # $out retains configureFlags :-/
30
31 buildInputs =
32 [ zlib readline openssl libxml2 makeWrapper ]
33 ++ lib.optionals icuEnabled [ icu ]
34 ++ lib.optionals (atLeast "9.6" && !stdenv.isDarwin) [ systemd ]
35 ++ lib.optionals (!stdenv.isDarwin) [ libossp_uuid ];
36
37 nativeBuildInputs = lib.optionals icuEnabled [ pkgconfig ];
38
39 enableParallelBuilding = !stdenv.isDarwin;
40
41 makeFlags = [ "world" ];
42
43 NIX_CFLAGS_COMPILE = [ "-I${libxml2.dev}/include/libxml2" ];
44
45 # Otherwise it retains a reference to compiler and fails; see #44767. TODO: better.
46 preConfigure = "CC=${stdenv.cc.targetPrefix}cc";
47
48 configureFlags = [
49 "--with-openssl"
50 "--with-libxml"
51 "--sysconfdir=/etc"
52 "--libdir=$(lib)/lib"
53 "--with-system-tzdata=${tzdata}/share/zoneinfo"
54 (lib.optionalString (atLeast "9.6" && !stdenv.isDarwin) "--with-systemd")
55 (if stdenv.isDarwin then "--with-uuid=e2fs" else "--with-ossp-uuid")
56 ] ++ lib.optionals icuEnabled [ "--with-icu" ];
57
58 patches =
59 [ (if atLeast "9.4" then ./patches/disable-resolve_symlinks-94.patch else ./patches/disable-resolve_symlinks.patch)
60 (if atLeast "9.6" then ./patches/less-is-more-96.patch else ./patches/less-is-more.patch)
61 (if atLeast "9.6" then ./patches/hardcode-pgxs-path-96.patch else ./patches/hardcode-pgxs-path.patch)
62 ./patches/specify_pkglibdir_at_runtime.patch
63 ];
64
65 installTargets = [ "install-world" ];
66
67 LC_ALL = "C";
68
69 postConfigure =
70 let path = if atLeast "9.6" then "src/common/config_info.c" else "src/bin/pg_config/pg_config.c"; in
71 ''
72 # Hardcode the path to pgxs so pg_config returns the path in $out
73 substituteInPlace "${path}" --replace HARDCODED_PGXS_PATH $out/lib
74 '';
75
76 postInstall =
77 ''
78 moveToOutput "lib/pgxs" "$out" # looks strange, but not deleting it
79 moveToOutput "lib/libpgcommon.a" "$out"
80 moveToOutput "lib/libpgport.a" "$out"
81 moveToOutput "lib/libecpg*" "$out"
82
83 # Prevent a retained dependency on gcc-wrapper.
84 substituteInPlace "$out/lib/pgxs/src/Makefile.global" --replace ${stdenv.cc}/bin/ld ld
85
86 if [ -z "''${dontDisableStatic:-}" ]; then
87 # Remove static libraries in case dynamic are available.
88 for i in $out/lib/*.a $lib/lib/*.a; do
89 name="$(basename "$i")"
90 ext="${stdenv.hostPlatform.extensions.sharedLibrary}"
91 if [ -e "$lib/lib/''${name%.a}$ext" ] || [ -e "''${i%.a}$ext" ]; then
92 rm "$i"
93 fi
94 done
95 fi
96 '';
97
98 postFixup = lib.optionalString (!stdenv.isDarwin && stdenv.hostPlatform.libc == "glibc")
99 ''
100 # initdb needs access to "locale" command from glibc.
101 wrapProgram $out/bin/initdb --prefix PATH ":" ${glibc.bin}/bin
102 '';
103
104 doInstallCheck = false; # needs a running daemon?
105
106 disallowedReferences = [ stdenv.cc ];
107
108 passthru = {
109 inherit readline psqlSchema version;
110
111 pkgs = let
112 scope = { postgresql = this; };
113 newSelf = self // scope;
114 newSuper = { callPackage = newScope (scope // this.pkgs); };
115 in import ./packages.nix newSelf newSuper;
116
117 withPackages = postgresqlWithPackages {
118 inherit makeWrapper buildEnv;
119 postgresql = this;
120 }
121 this.pkgs;
122 };
123
124 meta = with lib; {
125 homepage = https://www.postgresql.org;
126 description = "A powerful, open source object-relational database system";
127 license = licenses.postgresql;
128 maintainers = with maintainers; [ ocharles thoughtpolice danbst ];
129 platforms = platforms.unix;
130 knownVulnerabilities = optional (!atLeast "9.4")
131 "PostgreSQL versions older than 9.4 are not maintained anymore!";
132 };
133 };
134
135 postgresqlWithPackages = { postgresql, makeWrapper, buildEnv }: pkgs: f: buildEnv {
136 name = "postgresql-and-plugins-${postgresql.version}";
137 paths = f pkgs ++ [
138 postgresql
139 postgresql.lib
140 postgresql.man # in case user installs this into environment
141 ];
142 buildInputs = [ makeWrapper ];
143
144 # We include /bin to ensure the $out/bin directory is created, which is
145 # needed because we'll be removing the files from that directory in postBuild
146 # below. See #22653
147 pathsToLink = ["/" "/bin"];
148
149 postBuild = ''
150 mkdir -p $out/bin
151 rm $out/bin/{pg_config,postgres,pg_ctl}
152 cp --target-directory=$out/bin ${postgresql}/bin/{postgres,pg_config,pg_ctl}
153 wrapProgram $out/bin/postgres --set NIX_PGLIBDIR $out/lib
154 '';
155 };
156
157in self: {
158
159 postgresql_9_4 = self.callPackage generic {
160 version = "9.4.24";
161 psqlSchema = "9.4";
162 sha256 = "0acl1wmah3r1a0qjjmpc256glccrjnzq4pkwklx4d9s6vmkks9aj";
163 this = self.postgresql_9_4;
164 inherit self;
165 };
166
167 postgresql_9_5 = self.callPackage generic {
168 version = "9.5.19";
169 psqlSchema = "9.5";
170 sha256 = "1cqvbsyfs9048wbvdv0vhhaksjyjqv2vvh6ij4vqmjibc4kal34n";
171 this = self.postgresql_9_5;
172 inherit self;
173 };
174
175 postgresql_9_6 = self.callPackage generic {
176 version = "9.6.15";
177 psqlSchema = "9.6";
178 sha256 = "02hp69h2p02asfblkaahblzdz2zmawd2r11h6237y5j7yadgxn9w";
179 this = self.postgresql_9_6;
180 inherit self;
181 };
182
183 postgresql_10 = self.callPackage generic {
184 version = "10.10";
185 psqlSchema = "10.0"; # should be 10, but changing it is invasive
186 sha256 = "0lzj46dwd9cw94gnqm36bxd7jlhfdyqjrfzr3c4xd3prfn2rnkxd";
187 this = self.postgresql_10;
188 inherit self;
189 };
190
191 postgresql_11 = self.callPackage generic {
192 version = "11.5";
193 psqlSchema = "11.1"; # should be 11, but changing it is invasive
194 sha256 = "106ikalvrilihlvhq7xj7snq98hgbgq6qsgjrd252wgw1c327pvz";
195 this = self.postgresql_11;
196 inherit self;
197 };
198
199}