Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reintroduce nix flake #1683

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ cmake*/
.direnv
pgrx-examples/*/target
/bin
result
4 changes: 4 additions & 0 deletions ci/nix-update-rust-toolchain.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh

echo "---- update nix rust dependency ----"
nix flake update fenix --commit-lock-file
144 changes: 144 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

147 changes: 147 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
{
inputs = {
crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
};
nixpkgs.url = "nixpkgs/nixos-unstable";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-parts.url = "github:hercules-ci/flake-parts";
gitignore = {
url = "github:hercules-ci/gitignore";
inputs.nixpkgs.follows = "nixpkgs";
};
};

outputs = inputs:
inputs.flake-parts.lib.mkFlake {inherit inputs;} {
systems = ["x86_64-linux" "aarch64-linux" "aarch64-darwin"];
perSystem = {
lib,
pkgs,
inputs',
system,
self',
...
}: let
rustToolchain = inputs'.fenix.packages.stable.toolchain;
craneLib = inputs.crane.lib.${system}.overrideToolchain rustToolchain;
in {
packages = {
cargo-pgrx = craneLib.buildPackage {
src = inputs.gitignore.lib.gitignoreSource ./.;
inherit (craneLib.crateNameFromCargoToml {cargoToml = ./cargo-pgrx/Cargo.toml;}) pname version;
cargoExtraArgs = "--package cargo-pgrx";
nativeBuildInputs = [pkgs.pkg-config];
buildInputs = [pkgs.openssl] ++ lib.optionals pkgs.stdenv.isDarwin [pkgs.darwin.apple_sdk.frameworks.Security pkgs.libiconv];
# fixes to enable running pgrx tests
preCheck = ''
export PGRX_HOME=$(mktemp -d)
'';
# skip tests that require pgrx to be initialized using `cargo pgrx init`
cargoTestExtraArgs = "-- --skip=command::schema::tests::test_parse_managed_postmasters";
};
};
devShells.default = with pkgs; mkShell {
inputsFrom = [
self'.packages.cargo-pgrx
];
nativeBuildInputs = with pkgs; [
rustToolchain
pkg-config
];
buildInputs = with pkgs; [
openssl
] ++ lib.optionals stdenv.isDarwin [
darwin.apple_sdk.frameworks.Security
libiconv
];

shellHook = ''
export PGRX_HOME=$(mktemp -d)
'';
};
};
flake.lib.buildPgrxExtension = {
rustToolchain,
system,
src,
postgresql,
additionalFeatures ? [],
}: let
cargo-pgrx = inputs.self.packages.${system}.cargo-pgrx;
pkgs = inputs.nixpkgs.legacyPackages.${system};
craneLib = inputs.crane.lib.${system}.overrideToolchain rustToolchain;

postgresMajor = inputs.nixpkgs.lib.versions.major postgresql.version;
cargoToml = builtins.fromTOML (builtins.readFile "${src}/Cargo.toml");
name = cargoToml.package.name;
pgrxFeatures = builtins.toString additionalFeatures;

preBuildAndTest = ''
export PGRX_HOME=$(mktemp -d)
mkdir -p $PGRX_HOME/${postgresMajor}

cp -r -L ${postgresql}/. $PGRX_HOME/${postgresMajor}/
chmod -R ugo+w $PGRX_HOME/${postgresMajor}
cp -r -L ${postgresql.lib}/lib/. $PGRX_HOME/${postgresMajor}/lib/

${cargo-pgrx}/bin/cargo-pgrx pgrx init \
--pg${postgresMajor} $PGRX_HOME/${postgresMajor}/bin/pg_config \
'';

craneCommonBuildArgs = {
inherit src;
pname = "${name}-pg${postgresMajor}";
nativeBuildInputs = [
pkgs.pkg-config
pkgs.rustPlatform.bindgenHook
Copy link
Author

@justinrubek justinrubek Apr 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bindgenHook is a handy package I found that sets BINDGEN_EXTRA_CLANG_ARGS, which was previously being done manually in the build step. See https://github.com/symphorien/nixpkgs/blob/master/pkgs/build-support/rust/hooks/rust-bindgen-hook.sh

postgresql.lib
postgresql
];
cargoExtraArgs = "--no-default-features --features \"pg${postgresMajor} ${pgrxFeatures}\"";
postPatch = "patchShebangs .";
preBuild = preBuildAndTest;
preCheck = preBuildAndTest;
postBuild = ''
if [ -f "${name}.control" ]; then
export NIX_PGLIBDIR=${postgresql.out}/share/postgresql/extension/
${cargo-pgrx}/bin/cargo-pgrx pgrx package --pg-config ${postgresql}/bin/pg_config --features "${pgrxFeatures}" --out-dir $out
export NIX_PGLIBDIR=$PGRX_HOME/${postgresMajor}/lib
fi
'';

PGRX_PG_SYS_SKIP_BINDING_REWRITE = "1";
CARGO = "${rustToolchain}/bin/cargo";
CARGO_BUILD_INCREMENTAL = "false";
RUST_BACKTRACE = "full";
Comment on lines +117 to +120
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These get set as environment variables for the build

};

cargoArtifacts = craneLib.buildDepsOnly craneCommonBuildArgs;
in
craneLib.mkCargoDerivation ({
inherit cargoArtifacts;
buildPhaseCargoCommand = ''
${cargo-pgrx}/bin/cargo-pgrx pgrx package --pg-config ${postgresql}/bin/pg_config --features "${pgrxFeatures}" --out-dir $out
'';
doCheck = false;
preFixup = ''
if [ -f "${name}.control" ]; then
${cargo-pgrx}/bin/cargo-pgrx pgrx stop all
rm -rfv $out/target*
fi
'';

postInstall = ''
mkdir -p $out/lib
cp target/release/lib${name}.so $out/lib/${name}.so
mv -v $out/${postgresql.out}/* $out
rm -rfv $out/nix
'';
}
// craneCommonBuildArgs);
};
}