summaryrefslogtreecommitdiff
path: root/pkgs/servers/sql/postgresql/libpq.nix
blob: 5ea0c511322566e1aba4821f9695d0f04ad63401 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
{
  # utils
  stdenv,
  fetchFromGitHub,
  lib,

  # runtime dependencies
  openssl,
  tzdata,
  zlib,

  # build dependencies
  bison,
  flex,
  makeWrapper,
  perl,
  pkg-config,

  # passthru / meta
  postgresql,
  buildPackages,

  # Curl
  curlSupport ?
    lib.meta.availableOn stdenv.hostPlatform curl
    # Building statically fails with:
    # configure: error: library 'curl' does not provide curl_multi_init
    # https://www.postgresql.org/message-id/487dacec-6d8d-46c0-a36f-d5b8c81a56f1%40technowledgy.de
    && !stdenv.hostPlatform.isStatic,
  curl,

  # GSSAPI
  gssSupport ? with stdenv.hostPlatform; !isWindows && !isStatic,
  libkrb5,

  # NLS
  nlsSupport ? false,
  gettext,
}:

stdenv.mkDerivation (finalAttrs: {
  pname = "libpq";
  version = "18.4";

  src = fetchFromGitHub {
    owner = "postgres";
    repo = "postgres";
    # rev, not tag, on purpose: see generic.nix.
    rev = "refs/tags/REL_18_4";
    hash = "sha256-Ac/Dqcj8vjcW3my5vsnKaMiQqTq/HPtUzckJ3SMyrfA=";
  };

  __structuredAttrs = true;

  outputs = [
    "out"
    "dev"
  ];
  outputChecks.out = {
    disallowedReferences = [ "dev" ];
    disallowedRequisites = [
      stdenv.cc
    ]
    ++ (map lib.getDev (builtins.filter (drv: drv ? "dev") finalAttrs.buildInputs));
  };

  buildInputs = [
    zlib
    openssl
  ]
  ++ lib.optionals curlSupport [ curl ]
  ++ lib.optionals gssSupport [ libkrb5 ]
  ++ lib.optionals nlsSupport [ gettext ];

  nativeBuildInputs = [
    bison
    flex
    makeWrapper
    perl
    pkg-config
  ];

  # causes random build failures
  enableParallelBuilding = false;

  separateDebugInfo = true;

  buildFlags = [
    "submake-libpgport"
    "submake-libpq"
  ];

  # libpgcommon.a and libpgport.a contain all paths normally returned by pg_config and are
  # linked into all shared libraries. However, almost no binaries actually use those paths.
  # The following flags will remove unused sections from all shared libraries - including
  # those paths. This avoids a lot of circular dependency problems with different outputs,
  # and allows splitting them cleanly.
  env.CFLAGS =
    "-fdata-sections -ffunction-sections"
    + (if stdenv.cc.isClang then " -flto" else " -fmerge-constants -Wl,--gc-sections");

  # This flag was introduced upstream in:
  # https://github.com/postgres/postgres/commit/b6c7cfac88c47a9194d76f3d074129da3c46545a
  # It causes errors when linking against libpq.a in pkgsStatic:
  #   undefined reference to `pg_encoding_to_char'
  # Unsetting the flag fixes it. The upstream reasoning to introduce it is about the risk
  # to have initdb load a libpq.so from a different major version and how to avoid that.
  # This doesn't apply to us with Nix.
  env.NIX_CFLAGS_COMPILE = "-UUSE_PRIVATE_ENCODING_FUNCS";

  configureFlags = [
    "--enable-debug"
    "--sysconfdir=/etc"
    "--with-openssl"
    "--with-system-tzdata=${tzdata}/share/zoneinfo"
    "--without-icu"
    "--without-perl"
    "--without-readline"
  ]
  ++ lib.optionals curlSupport [ "--with-libcurl" ]
  ++ lib.optionals gssSupport [ "--with-gssapi" ]
  ++ lib.optionals nlsSupport [ "--enable-nls" ];

  patches = lib.optionals stdenv.hostPlatform.isLinux [
    ./patches/socketdir-in-run-13+.patch
  ];

  postPatch = ''
    cat ${./pg_config.env.mk} >> src/common/Makefile
  ''
  # Explicitly disable building the shared libs, because that would fail with pkgsStatic.
  + lib.optionalString stdenv.hostPlatform.isStatic ''
    substituteInPlace src/interfaces/libpq/Makefile \
      --replace-fail "all: all-lib libpq-refs-stamp" "all: all-lib"
    substituteInPlace src/Makefile.shlib \
      --replace-fail "all-lib: all-shared-lib" "all-lib: all-static-lib" \
      --replace-fail "install-lib: install-lib-shared" "install-lib: install-lib-static"
  '';

  installPhase = ''
    runHook preInstall

    make -C src/common install pg_config.env
    make -C src/include install
    make -C src/interfaces/libpq install
  ''
  + lib.optionalString curlSupport ''
    make -C src/interfaces/libpq-oauth install
  ''
  + ''
    make -C src/port install

    substituteInPlace src/common/pg_config.env \
      --replace-fail "$out" "@out@"

    install -D src/common/pg_config.env "$dev/nix-support/pg_config.env"
    moveToOutput "lib/*.a" "$dev"

    rm -rfv $out/share
    rm -rfv $dev/lib/*_shlib.a

    runHook postInstall
  '';

  # PostgreSQL always builds both shared and static libs, so we delete those we don't want.
  # Honour the `dontDisableStatic` convention (see `generic.nix`) so consumers can keep
  # the static archives in `$dev/lib` alongside the shared libraries in `$out/lib`.
  postInstall =
    if stdenv.hostPlatform.isStatic then
      "touch $out/empty"
    else
      lib.optionalString (!(finalAttrs.dontDisableStatic or false)) "rm -rfv $dev/lib/*.a";

  doCheck = false;

  passthru.pg_config = buildPackages.callPackage ./pg_config.nix {
    inherit (finalAttrs) finalPackage;
    outputs = {
      out = lib.getOutput "out" finalAttrs.finalPackage;
    };
  };

  meta = {
    inherit (postgresql.meta)
      homepage
      license
      teams
      platforms
      ;
    description = "C application programmer's interface to PostgreSQL";
    changelog = "https://www.postgresql.org/docs/release/${finalAttrs.version}/";
    pkgConfigModules = [ "libpq" ];
  };
})