feat: postgres extension pg_smtp_client

This commit is contained in:
2025-02-16 10:07:37 +00:00
parent 7cc4c6292b
commit c3cdf9858a
3 changed files with 242 additions and 13 deletions

161
buildPgrxExtension.nix Normal file
View File

@@ -0,0 +1,161 @@
# preBuildAndTest and some small other bits
# taken from https://github.com/tcdi/pgrx/blob/v0.9.4/nix/extension.nix
# (but now heavily modified)
# which uses MIT License with the following license file
#
# MIT License
#
# Portions Copyright 2019-2021 ZomboDB, LLC.
# Portions Copyright 2021-2022 Technology Concepts & Design, Inc. <support@tcdi.com>.
# All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
{ lib
, cargo-pgrx
, pkg-config
, rustPlatform
, stdenv
, Security
, writeShellScriptBin
}:
# The idea behind: Use it mostly like rustPlatform.buildRustPackage and so
# we hand most of the arguments down.
#
# Additional arguments are:
# - `postgresql` postgresql package of the version of postgresql this extension should be build for.
# Needs to be the build platform variant.
# - `useFakeRustfmt` Whether to use a noop fake command as rustfmt. cargo-pgrx tries to call rustfmt.
# If the generated rust bindings aren't needed to use the extension, its a
# unnecessary and heavy dependency. If you set this to true, you also
# have to add `rustfmt` to `nativeBuildInputs`.
{ buildAndTestSubdir ? null
, buildType ? "release"
, buildFeatures ? [ ]
, cargoBuildFlags ? [ ]
, postgresql
# cargo-pgrx calls rustfmt on generated bindings, this is not strictly necessary, so we avoid the
# dependency here. Set to false and provide rustfmt in nativeBuildInputs, if you need it, e.g.
# if you include the generated code in the output via postInstall.
, useFakeRustfmt ? true
, usePgTestCheckFeature ? true
, ...
} @ args:
let
rustfmtInNativeBuildInputs = lib.lists.any (dep: lib.getName dep == "rustfmt") (args.nativeBuildInputs or []);
in
assert lib.asserts.assertMsg ((args.installPhase or "") == "")
"buildPgrxExtensions overwrites the installPhase, so providing one does nothing";
assert lib.asserts.assertMsg ((args.buildPhase or "") == "")
"buildPgrxExtensions overwrites the buildPhase, so providing one does nothing";
assert lib.asserts.assertMsg (useFakeRustfmt -> !rustfmtInNativeBuildInputs)
"The parameter useFakeRustfmt is set to true, but rustfmt is included in nativeBuildInputs. Either set useFakeRustfmt to false or remove rustfmt from nativeBuildInputs.";
assert lib.asserts.assertMsg (!useFakeRustfmt -> rustfmtInNativeBuildInputs)
"The parameter useFakeRustfmt is set to false, but rustfmt is not included in nativeBuildInputs. Either set useFakeRustfmt to true or add rustfmt from nativeBuildInputs.";
let
fakeRustfmt = writeShellScriptBin "rustfmt" ''
exit 0
'';
maybeDebugFlag = lib.optionalString (buildType != "release") "--debug";
maybeEnterBuildAndTestSubdir = lib.optionalString (buildAndTestSubdir != null) ''
export CARGO_TARGET_DIR="$(pwd)/target"
pushd "${buildAndTestSubdir}"
'';
maybeLeaveBuildAndTestSubdir = lib.optionalString (buildAndTestSubdir != null) "popd";
pgrxPostgresMajor = lib.versions.major postgresql.version;
preBuildAndTest = ''
export PGRX_HOME=$(mktemp -d)
export PGDATA="$PGRX_HOME/data-${pgrxPostgresMajor}/"
cargo-pgrx pgrx init "--pg${pgrxPostgresMajor}" ${lib.getDev postgresql}/bin/pg_config
echo "unix_socket_directories = '$(mktemp -d)'" > "$PGDATA/postgresql.conf"
# This is primarily for Mac or other Nix systems that don't use the nixbld user.
export USER="$(whoami)"
pg_ctl start
createuser -h localhost --superuser --createdb "$USER" || true
pg_ctl stop
'';
argsForBuildRustPackage = builtins.removeAttrs args [ "postgresql" "useFakeRustfmt" "usePgTestCheckFeature" ];
# so we don't accidentally `(rustPlatform.buildRustPackage argsForBuildRustPackage) // { ... }` because
# we forgot parentheses
finalArgs = argsForBuildRustPackage // {
buildInputs = (args.buildInputs or [ ]) ++ lib.optionals stdenv.hostPlatform.isDarwin [ Security ];
nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [
cargo-pgrx
postgresql
pkg-config
rustPlatform.bindgenHook
] ++ lib.optionals useFakeRustfmt [ fakeRustfmt ];
buildPhase = ''
runHook preBuild
echo "Executing cargo-pgrx buildPhase"
${preBuildAndTest}
${maybeEnterBuildAndTestSubdir}
PGRX_BUILD_FLAGS="--frozen -j $NIX_BUILD_CORES ${builtins.concatStringsSep " " cargoBuildFlags}" \
${lib.optionalString stdenv.hostPlatform.isDarwin ''RUSTFLAGS="''${RUSTFLAGS:+''${RUSTFLAGS} }-Clink-args=-Wl,-undefined,dynamic_lookup"''} \
cargo pgrx package \
--pg-config ${lib.getDev postgresql}/bin/pg_config \
${maybeDebugFlag} \
--features "${builtins.concatStringsSep " " buildFeatures}" \
--out-dir "$out"
${maybeLeaveBuildAndTestSubdir}
runHook postBuild
'';
preCheck = preBuildAndTest + args.preCheck or "";
installPhase = ''
runHook preInstall
echo "Executing buildPgrxExtension install"
${maybeEnterBuildAndTestSubdir}
cargo-pgrx pgrx stop all
mv $out/${postgresql}/* $out
rm -rf $out/nix
${maybeLeaveBuildAndTestSubdir}
runHook postInstall
'';
PGRX_PG_SYS_SKIP_BINDING_REWRITE = "1";
CARGO_BUILD_INCREMENTAL = "false";
RUST_BACKTRACE = "full";
checkNoDefaultFeatures = true;
checkFeatures = (args.checkFeatures or [ ]) ++ (lib.optionals usePgTestCheckFeature [ "pg_test" ]) ++ [ "pg${pgrxPostgresMajor}" ];
};
in
rustPlatform.buildRustPackage finalArgs

17
flake.lock generated
View File

@@ -16,9 +16,26 @@
"type": "github" "type": "github"
} }
}, },
"nixpkgs-unstable": {
"locked": {
"lastModified": 1739451785,
"narHash": "sha256-3ebRdThRic9bHMuNi2IAA/ek9b32bsy8F5R4SvGTIog=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "1128e89fd5e11bb25aedbfc287733c6502202ea9",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": { "root": {
"inputs": { "inputs": {
"nixpkgs": "nixpkgs", "nixpkgs": "nixpkgs",
"nixpkgs-unstable": "nixpkgs-unstable",
"rust-overlay": "rust-overlay" "rust-overlay": "rust-overlay"
} }
}, },

View File

@@ -2,6 +2,7 @@
description = "yukkop's nix utilities"; description = "yukkop's nix utilities";
inputs = { inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
rust-overlay = { rust-overlay = {
url = "github:oxalica/rust-overlay"; url = "github:oxalica/rust-overlay";
inputs = { inputs = {
@@ -14,8 +15,10 @@
self, self,
nixpkgs, nixpkgs,
rust-overlay, rust-overlay,
nixpkgs-unstable,
}: let }: let
lib = nixpkgs.lib; lib = nixpkgs.lib;
recursiveUpdate = lib.recursiveUpdate; recursiveUpdate = lib.recursiveUpdate;
supportedSystems = ["x86_64-linux" "aarch64-linux" "x86_64-darwin"]; supportedSystems = ["x86_64-linux" "aarch64-linux" "x86_64-darwin"];
@@ -56,7 +59,10 @@
forAllSystemsWithPkgs [(import rust-overlay)] ({ forAllSystemsWithPkgs [(import rust-overlay)] ({
system, system,
pkgs, pkgs,
}: { }:
let
in
{
packages.${system} = let packages.${system} = let
rust = { rust = {
nativeBuildInputs = [ nativeBuildInputs = [
@@ -210,16 +216,49 @@
overlays.default = final: prev: ( overlays.default = final: prev: (
let let
version = "1.6.1"; version = "1.6.1";
buildHttpExt = versionSuffix: let pkgs-unstable = import nixpkgs-unstable { inherit (prev) system; };
buildPostgresqlExtension =
prev.callPackage (import (builtins.path { buildPgrxExtension =
name = "extension-builder"; prev.callPackage (import (builtins.path {
path = ./buildPostgresqlExtension.nix; name = "extension-builder";
})) { path = ./buildPgrxExtension.nix;
postgresql = prev."postgresql_${versionSuffix}"; })) {
}; cargo-pgrx = pkgs-unstable.cargo-pgrx_0_12_6;
inherit (pkgs-unstable.darwin.apple_sdk.frameworks) Security;
};
buildPostgresqlExtension =
prev.callPackage (import (builtins.path {
name = "extension-builder";
path = ./buildPostgresqlExtension.nix;
}));
buildSmtpExt = versionSuffix: let
postgresql = prev."postgresql_${versionSuffix}";
src = prev.fetchFromGitHub {
owner = "brianpursley";
repo = "pg_smtp_client";
rev = "6ff3b71e3705e0d4081a51c21ca0379e869ba5fb";
hash = "sha256-wC/2rAsSDO83UITaFhtaf3do3aaOAko4gnKUOzwURc8=";
};
cargo = self.lib.cargoToml src;
in in
buildPgrxExtension {
pname = cargo.package.name;
version = cargo.package.version;
inherit src postgresql;
buildInputs = with prev; [ openssl ];
cargoHash = "sha256-AbLT7vcFV89zwZIaTC1ELat9l4UeNP8Bn9QMMOms1Co=";
doCheck = false;
};
buildHttpExt = versionSuffix:
buildPostgresqlExtension { buildPostgresqlExtension {
postgresql = prev."postgresql_${versionSuffix}";
} {
pname = "http"; pname = "http";
inherit version; inherit version;
src = prev.fetchFromGitHub { src = prev.fetchFromGitHub {
@@ -232,10 +271,22 @@
}; };
in { in {
hectic = self.packages.${prev.system}; hectic = self.packages.${prev.system};
postgresql_17 = prev.postgresql_17 // {pkgs = prev.postgresql_17.pkgs // {http = buildHttpExt "17";};}; postgresql_17 = prev.postgresql_17 // {pkgs = prev.postgresql_17.pkgs // {
postgresql_16 = prev.postgresql_16 // {pkgs = prev.postgresql_16.pkgs // {http = buildHttpExt "16";};}; http = buildHttpExt "17";
postgresql_15 = prev.postgresql_15 // {pkgs = prev.postgresql_15.pkgs // {http = buildHttpExt "15";};}; pg_smtp_client = buildSmtpExt "17";
postgresql_14 = prev.postgresql_14 // {pkgs = prev.postgresql_14.pkgs // {http = buildHttpExt "14";};}; };};
postgresql_16 = prev.postgresql_16 // {pkgs = prev.postgresql_16.pkgs // {
http = buildHttpExt "16";
pg_smtp_client = buildSmtpExt "16";
};};
postgresql_15 = prev.postgresql_15 // {pkgs = prev.postgresql_15.pkgs // {
http = buildHttpExt "15";
pg_smtp_client = buildSmtpExt "15";
};};
postgresql_14 = prev.postgresql_14 // {pkgs = prev.postgresql_14.pkgs // {
http = buildHttpExt "14";
pg_smtp_client = buildSmtpExt "14";
};};
} }
); );
lib = { lib = {