refactor: pull out flake packages

This commit is contained in:
2025-07-17 15:28:29 +00:00
parent 23d9db27e6
commit 33422320a5
4 changed files with 276 additions and 278 deletions

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}" ${postgresql.pg_config}/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 ${postgresql.pg_config}/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

View File

@@ -0,0 +1,140 @@
# PostgreSQL's build system for extensions (PGXS) makes the following assumptions:
# - All extensions will be installed in the same prefix as PostgreSQL itself.
# - pg_config is able to return the correct paths for bindir/libdir/datadir etc.
#
# Both of those assumptions break with nix. Since each extension is a separate
# derivation, we need to put all its files into a different folder. At the same
# time, pg_config only points to the PostgreSQL derivation's paths.
#
# When building extensions, the paths provided by pg_config are used for two
# purposes:
# - To find postgres libs and headers and reference those paths via -L and -I flags.
# - To determine the correct install directory.
#
# The PGXS Makefiles also support an environment variable DESTDIR, which is added as
# a prefix to all install locations. This is primarily used for temporary installs
# while running the test suite. Since pg_config returns absolute paths to /nix/store
# for us, using DESTDIR will result in install locations of the form:
# $DESTIDR/nix/store/<postgresql-output>/...
#
# In multiple iterations, the following approaches have been tried to work around all
# of this:
# 1. For a long time, all extensions in nixpkgs just overwrote the installPhase
# and moved the respective files to the correct location manually. This approach
# is not maintainable, because whenever upstream adds a new file, we'd have to
# make sure the file is correctly installed as well. Additionally, it makes adding
# a new extension harder than it should be.
#
# 2. A wrapper around pg_config could just replace the returned paths with paths to
# $out of currently building derivation, i.e. the extension. This works for install-
# ation, but breaks for any of the libs and headers the extension needs from postgres
# itself.
#
# 3. A variation of 2., but make the pg_config wrapper only return the changed paths
# during the installPahse. During configure and build, it would return the regular
# paths to the PostgreSQL derivation. This works better, but not for every case.
# Some extensions try to be smarter and search for the "postgres" binary to deduce
# the necessary paths from that. Those would still need special handling.
#
# 4. Use the fact that DESTDIR is prepended to every installation directory - and only
# there, to run a replacement of all Makefiles in postgres' lib/pgxs/ folder and
# all Makefiles in the extension's source. "$DESTDIR/$bindir" can be replaced with
# "$out/bin" etc. - thus mapping the installPhase directly into the right output.
# This works beautifully - for the majority of cases. But it doesn't work for
# some extensions that use CMake. And it doesn't work for some extensions that use
# custom variables instead of the default "bindir" and friends.
#
# 5. Just set DESTDIR to the extensions's output and then clean up afterward. This will
# result in paths like this:
# /nix/store/<extension-output>/nix/store/<postgresql-output>/...
# Directly after the installPhase, we'll move the files in the right folder.
# This seems to work consistently across all extensions we have in nixpkgs right now.
# Of course, it would break down for any extension that doesn't support DESTDIR -
# but that just means PGXS is not used either, so that's OK.
#
# This last approach is the one we're taking in this file. To make sure the removal of the
# nested nix/store happens immediately after the installPhase, before any other postInstall
# hooks run, this needs to be run in an override of `mkDerivation` and not in a setup hook.
{
lib,
stdenv,
postgresql,
nix-update-script,
}: args: let
buildPostgresqlExtension = finalAttrs: {enableUpdateScript ? true, ...} @ prevAttrs: {
passthru =
prevAttrs.passthru
or {}
// lib.optionalAttrs enableUpdateScript {
updateScript =
prevAttrs.passthru.updateScript
or (nix-update-script (
lib.optionalAttrs (lib.hasInfix "unstable" prevAttrs.version) {
extraArgs = ["--version=branch"];
}
));
};
buildInputs = [postgresql postgresql.pg_config] ++ prevAttrs.buildInputs or [];
installFlags =
[
"DESTDIR=${placeholder "out"}"
]
++ prevAttrs.installFlags or [];
postInstall =
''
# DESTDIR + pg_config install the files into
# /nix/store/<extension>/nix/store/<postgresql>/...
# We'll now remove the /nix/store/<postgresql> part:
if [[ -d "$out${postgresql}" ]]; then
cp -alt "$out" "$out${postgresql}"/*
rm -r "$out${postgresql}"
fi
if [[ -d "$out${postgresql.dev}" ]]; then
mkdir -p "''${dev:-$out}"
cp -alt "''${dev:-$out}" "$out${postgresql.dev}"/*
rm -r "$out${postgresql.dev}"
fi
if [[ -d "$out${postgresql.lib}" ]]; then
mkdir -p "''${lib:-$out}"
cp -alt "''${lib:-$out}" "$out${postgresql.lib}"/*
rm -r "$out${postgresql.lib}"
fi
if [[ -d "$out${postgresql.doc}" ]]; then
mkdir -p "''${doc:-$out}"
cp -alt "''${doc:-$out}" "$out${postgresql.doc}"/*
rm -r "$out${postgresql.doc}"
fi
if [[ -d "$out${postgresql.man}" ]]; then
mkdir -p "''${man:-$out}"
cp -alt "''${man:-$out}" "$out${postgresql.man}"/*
rm -r "$out${postgresql.man}"
fi
# In some cases (postgis) parts of the install script
# actually work "OK", before we add DESTDIR, so some
# files end up in
# /nix/store/<extension>/nix/store/<extension>/...
if [[ -d "$out$out" ]]; then
cp -alt "$out" "$out$out"/*
rm -r "$out$out"
fi
if [[ -d "$out/nix/store" ]]; then
if ! rmdir "$out/nix/store" "$out/nix"; then
find "$out/nix"
nixErrorLog 'Found left-overs in $out/nix/store, make sure to move them into $out properly.'
exit 1
fi
fi
''
+ prevAttrs.postInstall or "";
};
in
stdenv.mkDerivation (lib.extends buildPostgresqlExtension (lib.toFunction args))

275
package/default.nix Normal file
View File

@@ -0,0 +1,275 @@
{ self, system, pkgs }: let
rust = {
nativeBuildInputs = [
pkgs.pkgsBuildHost.rust-bin.stable."1.81.0".default
pkgs.pkg-config
];
commonArgs = {
inherit (self.lib) cargoToml;
inherit (rust) nativeBuildInputs;
};
};
buildPostgresqlExtension =
pkgs: pkgs.callPackage (import (builtins.path {
name = "extension-builder";
path = ./buildPostgresqlExtension.nix;
}));
buildHemarExt = pkgs: versionSuffix: let
postgresql = pkgs."postgresql_${versionSuffix}";
c-hectic = self.packages.${pkgs.system}.c-hectic;
in buildPostgresqlExtension pkgs {
stdenv = pkgs.clangStdenv;
inherit postgresql;
} {
pname = "hemar";
version = "0.1";
src = ./c/hemar;
nativeBuildInputs = (with pkgs; [pkg-config]) ++ [ c-hectic ];
dontShrinkRPath = true;
postFixup = ''
echo ">>> postFixup running..."
${pkgs.patchelf}/bin/patchelf --set-rpath ${c-hectic}/lib $out/lib/hemar.so
'';
preInstall = ''mkdir $out'';
};
buildPgrxExtension = pkgs:
pkgs.callPackage (import (builtins.path {
name = "extension-builder";
path = ./buildPgrxExtension.nix;
})) {
cargo-pgrx = pkgs.cargo-pgrx_0_12_6;
inherit (pkgs.darwin.apple_sdk.frameworks) Security;
};
buildSmtpExt = pkgs: versionSuffix: let
postgresql = pkgs."postgresql_${versionSuffix}";
src = pkgs.fetchFromGitHub {
owner = "brianpursley";
repo = "pg_smtp_client";
rev = "6ff3b71e3705e0d4081a51c21ca0379e869ba5fb";
hash = "sha256-wC/2rAsSDO83UITaFhtaf3do3aaOAko4gnKUOzwURc8=";
};
cargo = self.lib.cargoToml src;
in
buildPgrxExtension pkgs {
pname = cargo.package.name;
version = cargo.package.version;
inherit src postgresql;
buildInputs = with pkgs; [ openssl ];
cargoHash = "sha256-Cg5qY4TKkSJRSAtlFbjIRhea0dXPLEyasi5n09HcYeo=";
doCheck = false;
};
buildPlShExt = pkgs: versionSuffix: let
version = "4.0";
in buildPostgresqlExtension pkgs {
stdenv = pkgs.clangStdenv;
postgresql = pkgs."postgresql_${versionSuffix}";
} {
pname = "plsh";
inherit version;
src = pkgs.fetchFromGitHub {
owner = "petere";
repo = "plsh";
rev = "d88079617309974f71b3f8e4d5f96869dba66835";
hash = "sha256-H9B5L+yIjjVNhnuF+bIZKyCrOqfIvu5W26aqyqL5UdQ=";
};
nativeBuildInputs = with pkgs; [ pkg-config ];
};
buildPlHaskellExt = pkgs: versionSuffix: let
version = "4.0";
in buildPostgresqlExtension pkgs {
stdenv = pkgs.clangStdenv;
postgresql = pkgs."postgresql_${versionSuffix}";
} {
pname = "plhaskell";
inherit version;
src = pkgs.fetchFromGitHub {
owner = "ed-o-saurus";
repo = "PLHaskell";
rev = "d917f0991a455cf0558c2036e360ba1a9b40a8ef";
hash = "sha256-+sJmR/SCMfxxExa7GZuNmWez1dfhvlM9qOdO9gHNf74=";
};
preBuild = ''
last=$(pwd)
cd ${pkgs.haskellPackages.ghc}
include=$(dirname "${pkgs.haskellPackages.ghc}/$(find . -name HsFFI.h)")
ls $include
cd $last
export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -I$include"
'';
nativeBuildInputs = with pkgs; [
pkg-config
curl
ghc
haskellPackages.hsc2hs
haskellPackages.ghc
];
};
buildHttpExt = pkgs: versionSuffix: let
version = "1.6.1";
in buildPostgresqlExtension pkgs {
stdenv = pkgs.clangStdenv;
postgresql = pkgs."postgresql_${versionSuffix}";
} {
pname = "http";
inherit version;
src = pkgs.fetchFromGitHub {
owner = "pramsey";
repo = "pgsql-http";
rev = "v${version}";
hash = "sha256-C8eqi0q1dnshUAZjIsZFwa5FTYc7vmATF3vv2CReWPM=";
};
nativeBuildInputs = with pkgs; [pkg-config curl];
};
in {
py3-datetime = pkgs.python3Packages.buildPythonPackage rec {
pname = "DateTime";
version = "5.5";
src = pkgs.fetchPypi {
inherit pname version;
sha256 = "sha256-IexjMfh6f8tXvXxZ6KaL//5vy/Ws27x7NW1qmgIBkdM=";
};
};
py3-marzban = pkgs.python3Packages.buildPythonPackage rec {
pname = "marzban";
version = "0.4.3";
src = pkgs.fetchPypi {
inherit pname version;
sha256 = "sha256-z71Wl4AuET3oES7/48u+paL9F12SdrkohcEee/tkWVk=";
};
format = "pyproject";
propagatedBuildInputs = with pkgs.python3Packages; [
httpx
paramiko
sshtunnel
];
nativeBuildInputs = (with pkgs.python3Packages; [
setuptools
wheel
setuptools-scm
httpx
pydantic
paramiko
sshtunnel
]) ++ (with self.packages.${system}; [
py3-datetime
]);
doCheck = false;
};
py3-asyncpayments = pkgs.python3Packages.buildPythonPackage rec {
pname = "asyncpayments";
version = "1.4.6";
src = pkgs.fetchPypi {
inherit pname version;
sha256 = "sha256-t7AZiRb7DHZgJHPNQwAEuc0mrTQ14+82d19VomTjs8U=";
};
format = "pyproject";
nativeBuildInputs = with pkgs.python3Packages; [ setuptools wheel setuptools-scm ];
propagatedBuildInputs = with pkgs.python3Packages; [ aiohttp requests ];
doCheck = false;
};
py3-payok = pkgs.python3Packages.buildPythonPackage rec {
pname = "payok";
version = "1.2";
src = pkgs.fetchPypi {
inherit pname version;
sha256 = "sha256-UN+MSNGhrPpw7hZRLAx8XY3jC0ldo+DlbaSJ64wWBHo=";
};
propagatedBuildInputs = with pkgs.python3Packages; [ requests ];
doCheck = false;
};
py3-asyncio = pkgs.python3Packages.buildPythonPackage rec {
pname = "asyncio";
version = "3.4.3";
src = pkgs.python3Packages.fetchPypi {
inherit pname version;
sha256 = "sha256-gzYP+LyXmA5P8lyWTHvTkj0zPRd6pPf7c2sBnybHy0E=";
};
};
py3-cryptomus = pkgs.python3Packages.buildPythonPackage rec {
pname = "cryptomus";
version = "1.1";
src = pkgs.python3Packages.fetchPypi {
inherit pname version;
sha256 = "sha256-f0BBGfemKxMdz+LMvawWqqRfmF+TrCpMwgtJEYt+fgU=";
};
};
py3-modulegraph = pkgs.python3Packages.buildPythonPackage rec {
pname = "modulegraph";
version = "0.19.6";
src = pkgs.python3Packages.fetchPypi {
inherit pname version;
sha256 = "sha256-yRTIyVoOEP6IUF1OnCKEtOPbxwlD4wbMZWfjbMVBv0s=";
};
};
py3-swifter = pkgs.python3Packages.buildPythonPackage rec {
pname = "swifter";
version = "1.4.0";
src = pkgs.python3Packages.fetchPypi {
inherit pname version;
sha256 = "sha256-4bt0R2ohs/B6F6oYyX/cuoWZcmvRfacy8J2rzFDia6A=";
};
};
py3-aiogram-newsletter = pkgs.python3Packages.buildPythonPackage rec {
pname = "aiogram-newsletter";
version = "0.0.10";
src = pkgs.fetchFromGitHub {
inherit pname version;
owner = "nessshon";
repo = "aiogram-newsletter";
rev = "bb8a42e4bcff66a9a606fc92ccc27b1d094b20fc";
sha256 = "sha256-atKhccp8Pr8anJUo+M9hnYkYrcgnB9SxrpmsiVusJZs=";
};
};
nvim-alias = pkgs.callPackage ./nvim-alias.nix {};
bolt-unpack = pkgs.callPackage ./bolt-unpack.nix {};
nvim-pager = pkgs.callPackage ./nvim-pager.nix {};
printobstacle = pkgs.callPackage ./printobstacle.nix {};
printprogress = pkgs.callPackage ./printprogress.nix {};
colorize = pkgs.callPackage ./colorize.nix {};
github-gh-tl = pkgs.callPackage ./github/gh-tl.nix {};
supabase-with-env-collection = pkgs.callPackage ./supabase-with-env-collection.nix {};
migration-name = pkgs.callPackage ./migration-name.nix {};
prettify-log = pkgs.callPackage ./prettify-log/default.nix rust.commonArgs;
pg-from = pkgs.callPackage ./postgres/pg-from/default.nix rust.commonArgs;
pg-schema = pkgs.callPackage ./postgres/pg-schema/default.nix rust.commonArgs;
pg_wdumpall = pkgs.callPackage ./postgres/pg_wdumpall.nix rust.commonArgs;
pg_wdump = pkgs.callPackage ./postgres/pg_wdump.nix rust.commonArgs;
pg-migration = pkgs.callPackage ./postgres/pg-migration/default.nix rust.commonArgs;
pg-17-ext-hemar = buildHemarExt pkgs "17";
pg-17-ext-http = buildHttpExt pkgs "17";
pg-17-ext-smtp-client = buildSmtpExt pkgs "17";
pg-17-ext-plhaskell = buildPlHaskellExt pkgs "17";
pg-17-ext-plsh = buildPlShExt pkgs "17";
pg-16-ext-hemar = buildHemarExt pkgs "16";
pg-16-ext-http = buildHttpExt pkgs "16";
pg-16-ext-smtp-client = buildSmtpExt pkgs "16";
pg-16-ext-plhaskell = buildPlHaskellExt pkgs "16";
pg-16-ext-plsh = buildPlShExt pkgs "16";
pg-15-ext-hemar = buildHemarExt pkgs "15";
pg-15-ext-http = buildHttpExt pkgs "15";
pg-15-ext-smtp-client = buildSmtpExt pkgs "15";
pg-15-ext-plhaskell = buildPlHaskellExt pkgs "15";
pg-15-ext-plsh = buildPlShExt pkgs "15";
slpt = pkgs.callPackage ./slpt.nix {};
c-hectic = pkgs.callPackage ./c/hectic/default.nix {};
watch = pkgs.callPackage ./c/watch/default.nix {};
support-bot = pkgs.callPackage ./support-bot {};
}