-
-
Notifications
You must be signed in to change notification settings - Fork 249
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
justinrubek
wants to merge
5
commits into
pgcentralfoundation:develop
Choose a base branch
from
justinrubek:reintroduce-nix
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c495891
build(nix): add nix flake with `cargo-pgrx` package
justinrubek 242a588
ci(nix): add rust-toolchain update script
justinrubek 13cf708
feat(nix): provide `buildPgrxExtension` function
justinrubek 38ffc9e
fix(nix): aarch64-darwin builds
samrose d8da280
feat: add a devShell
samrose File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ cmake*/ | |
.direnv | ||
pgrx-examples/*/target | ||
/bin | ||
result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 setsBINDGEN_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