lol

cargo-pgx: add buildPgxExtension

authored by

Eric Wolf and committed by
Eric Wolf
c845a281 be0b3484

+164
+161
pkgs/development/tools/rust/cargo-pgx/buildPgxExtension.nix
··· 1 + # preBuildAndTest and some small other bits 2 + # taken from https://github.com/tcdi/pgx/blob/v0.4.5/nix/extension.nix 3 + # (but now heavily modified) 4 + # which uses MIT License with the following license file 5 + # 6 + # MIT License 7 + # 8 + # Portions Copyright 2019-2021 ZomboDB, LLC. 9 + # Portions Copyright 2021-2022 Technology Concepts & Design, Inc. <support@tcdi.com>. 10 + # All rights reserved. 11 + # 12 + # Permission is hereby granted, free of charge, to any person obtaining a copy 13 + # of this software and associated documentation files (the "Software"), to deal 14 + # in the Software without restriction, including without limitation the rights 15 + # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 + # copies of the Software, and to permit persons to whom the Software is 17 + # furnished to do so, subject to the following conditions: 18 + # 19 + # The above copyright notice and this permission notice shall be included in all 20 + # copies or substantial portions of the Software. 21 + # 22 + # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 + # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 + # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 + # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 + # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 + # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 28 + # SOFTWARE. 29 + 30 + { lib 31 + , cargo-pgx 32 + , pkg-config 33 + , rustPlatform 34 + , stdenv 35 + , Security 36 + , writeShellScriptBin 37 + }: 38 + 39 + # The idea behind: Use it mostly like rustPlatform.buildRustPackage and so 40 + # we hand most of the arguments down. 41 + # 42 + # Additional arguments are: 43 + # - `postgresql` postgresql package of the version of postgresql this extension should be build for. 44 + # Needs to be the build platform variant. 45 + # - `useFakeRustfmt` Wether to use a noop fake command as rustfmt. cargo-pgx tries to call rustfmt. 46 + # If the generated rust bindings aren't needed to use the extension, its a 47 + # unnecessary and heavy dependency. If you set this to true, you also 48 + # have to add `rustfmt` to `nativeBuildInputs`. 49 + 50 + { buildAndTestSubdir ? null 51 + , buildType ? "release" 52 + , buildFeatures ? [ ] 53 + , cargoBuildFlags ? [ ] 54 + , postgresql 55 + # cargo-pgx calls rustfmt on generated bindings, this is not strictly necessary, so we avoid the 56 + # dependency here. Set to false and provide rustfmt in nativeBuildInputs, if you need it, e.g. 57 + # if you include the generated code in the output via postInstall. 58 + , useFakeRustfmt ? true 59 + , ... 60 + } @ args: 61 + let 62 + rustfmtInNativeBuildInputs = lib.lists.any (dep: lib.getName dep == "rustfmt") (args.nativeBuildInputs or []); 63 + in 64 + 65 + assert lib.asserts.assertMsg ((args.installPhase or "") == "") 66 + "buildPgxExtensions overwrites the installPhase, so providing one does nothing"; 67 + assert lib.asserts.assertMsg ((args.buildPhase or "") == "") 68 + "buildPgxExtensions overwrites the buildPhase, so providing one does nothing"; 69 + assert lib.asserts.assertMsg (useFakeRustfmt -> !rustfmtInNativeBuildInputs) 70 + "The parameter useFakeRustfmt is set to true, but rustfmt is included in nativeBuildInputs. Either set useFakeRustfmt to false or remove rustfmt from nativeBuildInputs."; 71 + assert lib.asserts.assertMsg (!useFakeRustfmt -> rustfmtInNativeBuildInputs) 72 + "The parameter useFakeRustfmt is set to false, but rustfmt is not included in nativeBuildInputs. Either set useFakeRustfmt to true or add rustfmt from nativeBuildInputs."; 73 + 74 + let 75 + fakeRustfmt = writeShellScriptBin "rustfmt" '' 76 + exit 0 77 + ''; 78 + maybeDebugFlag = lib.optionalString (buildType != "release") "--debug"; 79 + maybeEnterBuildAndTestSubdir = lib.optionalString (buildAndTestSubdir != null) '' 80 + export CARGO_TARGET_DIR="$(pwd)/target" 81 + pushd "${buildAndTestSubdir}" 82 + ''; 83 + maybeLeaveBuildAndTestSubdir = lib.optionalString (buildAndTestSubdir != null) "popd"; 84 + 85 + pgxPostgresMajor = lib.versions.major postgresql.version; 86 + preBuildAndTest = '' 87 + export PGX_HOME=$(mktemp -d) 88 + export PGDATA="$PGX_HOME/data-${pgxPostgresMajor}/" 89 + cargo-pgx pgx init "--pg${pgxPostgresMajor}" ${postgresql}/bin/pg_config 90 + echo "unix_socket_directories = '$(mktemp -d)'" > "$PGDATA/postgresql.conf" 91 + 92 + # This is primarily for Mac or other Nix systems that don't use the nixbld user. 93 + export USER="$(whoami)" 94 + pg_ctl start 95 + createuser -h localhost --superuser --createdb "$USER" || true 96 + pg_ctl stop 97 + ''; 98 + 99 + argsForBuildRustPackage = builtins.removeAttrs args [ "postgresql" "useFakeRustfmt" ]; 100 + 101 + # so we don't accidentally `(rustPlatform.buildRustPackage argsForBuildRustPackage) // { ... }` because 102 + # we forgot parentheses 103 + finalArgs = argsForBuildRustPackage // { 104 + buildInputs = (args.buildInputs or [ ]) ++ lib.optionals stdenv.isDarwin [ Security ]; 105 + 106 + nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [ 107 + cargo-pgx 108 + postgresql 109 + rustPlatform.rust.rustc 110 + pkg-config 111 + rustPlatform.bindgenHook 112 + ] ++ lib.optionals useFakeRustfmt [ fakeRustfmt ]; 113 + 114 + buildPhase = '' 115 + runHook preBuild 116 + 117 + echo "Executing cargo-pgx buildPhase" 118 + ${preBuildAndTest} 119 + ${maybeEnterBuildAndTestSubdir} 120 + 121 + NIX_PGLIBDIR="${postgresql}/lib" \ 122 + PGX_BUILD_FLAGS="--frozen -j $NIX_BUILD_CORES ${builtins.concatStringsSep " " cargoBuildFlags}" \ 123 + cargo-pgx pgx package \ 124 + --pg-config ${postgresql}/bin/pg_config \ 125 + ${maybeDebugFlag} \ 126 + --features "${builtins.concatStringsSep " " buildFeatures}" \ 127 + --out-dir "$out" 128 + 129 + ${maybeLeaveBuildAndTestSubdir} 130 + 131 + runHook postBuild 132 + ''; 133 + 134 + preCheck = preBuildAndTest + args.preCheck or ""; 135 + 136 + installPhase = '' 137 + runHook preInstall 138 + 139 + echo "Executing buildPgxExtension install" 140 + 141 + ${maybeEnterBuildAndTestSubdir} 142 + 143 + cargo-pgx pgx stop all 144 + 145 + mv $out/${postgresql}/* $out 146 + rm -rf $out/nix 147 + 148 + ${maybeLeaveBuildAndTestSubdir} 149 + 150 + runHook postInstall 151 + ''; 152 + 153 + PGX_PG_SYS_SKIP_BINDING_REWRITE = "1"; 154 + CARGO_BUILD_INCREMENTAL = "false"; 155 + RUST_BACKTRACE = "full"; 156 + 157 + checkNoDefaultFeatures = true; 158 + checkFeatures = (args.checkFeatures or [ ]) ++ [ "pg_test pg${pgxPostgresMajor}" ]; 159 + }; 160 + in 161 + rustPlatform.buildRustPackage finalArgs
+3
pkgs/top-level/all-packages.nix
··· 15751 15751 cargo-pgx = callPackage ../development/tools/rust/cargo-pgx { 15752 15752 inherit (darwin.apple_sdk.frameworks) Security; 15753 15753 }; 15754 + buildPgxExtension = callPackage ../development/tools/rust/cargo-pgx/buildPgxExtension.nix { 15755 + inherit (darwin.apple_sdk.frameworks) Security; 15756 + }; 15754 15757 cargo-release = callPackage ../development/tools/rust/cargo-release { }; 15755 15758 cargo-rr = callPackage ../development/tools/rust/cargo-rr { }; 15756 15759 cargo-tarpaulin = callPackage ../development/tools/analysis/cargo-tarpaulin {