style: ~apply alejandra

This commit is contained in:
zerosummed
2025-04-17 03:57:54 +03:00
parent 3dfe3d61f6
commit 29ff6989e5
7 changed files with 299 additions and 234 deletions

View File

@@ -26,16 +26,15 @@
# 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
{
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.
#
@@ -46,116 +45,117 @@
# 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
{
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 "") == "")
assert lib.asserts.assertMsg ((args.installPhase or "") == "")
"buildPgrxExtensions overwrites the installPhase, so providing one does nothing";
assert lib.asserts.assertMsg ((args.buildPhase or "") == "")
assert lib.asserts.assertMsg ((args.buildPhase or "") == "")
"buildPgrxExtensions overwrites the buildPhase, so providing one does nothing";
assert lib.asserts.assertMsg (useFakeRustfmt -> !rustfmtInNativeBuildInputs)
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
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";
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"
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
# 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
'';
preCheck = preBuildAndTest + args.preCheck or "";
argsForBuildRustPackage = builtins.removeAttrs args ["postgresql" "useFakeRustfmt" "usePgTestCheckFeature"];
installPhase = ''
runHook preInstall
# so we don't accidentally `(rustPlatform.buildRustPackage argsForBuildRustPackage) // { ... }` because
# we forgot parentheses
finalArgs =
argsForBuildRustPackage
// {
buildInputs = (args.buildInputs or []) ++ lib.optionals stdenv.hostPlatform.isDarwin [Security];
echo "Executing buildPgrxExtension install"
nativeBuildInputs =
(args.nativeBuildInputs or [])
++ [
cargo-pgrx
postgresql
pkg-config
rustPlatform.bindgenHook
]
++ lib.optionals useFakeRustfmt [fakeRustfmt];
${maybeEnterBuildAndTestSubdir}
buildPhase = ''
runHook preBuild
cargo-pgrx pgrx stop all
echo "Executing cargo-pgrx buildPhase"
${preBuildAndTest}
${maybeEnterBuildAndTestSubdir}
mv $out/${postgresql}/* $out
rm -rf $out/nix
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}
${maybeLeaveBuildAndTestSubdir}
runHook postInstall
'';
runHook postBuild
'';
PGRX_PG_SYS_SKIP_BINDING_REWRITE = "1";
CARGO_BUILD_INCREMENTAL = "false";
RUST_BACKTRACE = "full";
preCheck = preBuildAndTest + args.preCheck or "";
checkNoDefaultFeatures = true;
checkFeatures = (args.checkFeatures or [ ]) ++ (lib.optionals usePgTestCheckFeature [ "pg_test" ]) ++ [ "pg${pgrxPostgresMajor}" ];
};
in
rustPlatform.buildRustPackage finalArgs
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

257
flake.nix
View File

@@ -60,7 +60,7 @@
system,
pkgs,
}: let
pkgs-unstable = import nixpkgs-unstable { inherit system; };
pkgs-unstable = import nixpkgs-unstable {inherit system;};
in {
packages.${system} = let
rust = {
@@ -111,7 +111,7 @@
version = "0.0.10";
src = pkgs.fetchFromGitHub {
inherit pname version;
inherit pname version;
owner = "nessshon";
repo = "aiogram-newsletter";
rev = "bb8a42e4bcff66a9a606fc92ccc27b1d094b20fc";
@@ -142,23 +142,23 @@
shells = self.devShells.${system};
in {
c = pkgs.mkShell {
buildInputs = (with pkgs; [ inotify-tools gdb gcc ]) ++ (with self.packages.${system}; [ c-hectic nvim-pager watch ]);
buildInputs = (with pkgs; [inotify-tools gdb gcc]) ++ (with self.packages.${system}; [c-hectic nvim-pager watch]);
PAGER = "${self.packages.${system}.nvim-pager}/bin/pager";
};
postgres-c = pkgs.mkShell {
buildInputs = (with pkgs; [ inotify-tools postgresql_15 ]) ++ (with self.packages.${system}; [ nvim-pager ]) ++ (with pkgs-unstable; [ gdb gcc ]);
postgres-c = pkgs.mkShell {
buildInputs = (with pkgs; [inotify-tools postgresql_15]) ++ (with self.packages.${system}; [nvim-pager]) ++ (with pkgs-unstable; [gdb gcc]);
PAGER = "${self.packages.${system}.nvim-pager}/bin/pager";
shellHook = ''
shellHook = ''
export PATH=${pkgs-unstable.gcc}/bin:$PATH
export PAGER="${self.packages.${system}.nvim-pager}/bin/pager"
'';
};
pure-c = pkgs.mkShell {
buildInputs = (with pkgs; [ inotify-tools ]) ++ (with self.packages.${system}; [ nvim-pager ]) ++ (with pkgs-unstable; [ gdb gcc ]);
pure-c = pkgs.mkShell {
buildInputs = (with pkgs; [inotify-tools]) ++ (with self.packages.${system}; [nvim-pager]) ++ (with pkgs-unstable; [gdb gcc]);
PAGER = "${self.packages.${system}.nvim-pager}/bin/pager";
shellHook = ''
shellHook = ''
export PATH=${pkgs-unstable.gcc}/bin:$PATH
export PAGER="${self.packages.${system}.nvim-pager}/bin/pager"
'';
@@ -204,47 +204,63 @@
modules = [
self.nixosModules."preset.default"
self.nixosModules."hardware.hetzner"
({modulesPath, pkgs, ...}: {
({
modulesPath,
pkgs,
...
}: {
imports = [
(modulesPath + "/profiles/qemu-guest.nix")
];
users.users.root.openssh.authorizedKeys.keys = [ ];
users.users.root.openssh.authorizedKeys.keys = [];
environment.systemPackages = with pkgs; [
(pkgs.writers.writeMinCBin "minc-hello-world" ["<stdio.h>"] /*c*/ ''
printf("hello world\n");
'')
(pkgs.writers.writeMinCBin "minc-env" ["<stdio.h>" "<stdlib.h>"] /*c*/ ''
char *env_name;
if (argc > 1) {
env_name = argv[1];
} else {
env_name = "HOME";
}
char *value = getenv(env_name);
if (value) {
printf("%s: %s\n", env_name, value);
} else {
printf("Environment variable %s not found.\n", env_name);
}
'')
(pkgs.writers.writeMinCBin "minc-env-check" ["<stdio.h>" "<stdlib.h>"] /*c*/ ''
char *env_name;
if (argc > 1) {
env_name = argv[1];
} else {
env_name = "HOME";
}
(pkgs.writers.writeMinCBin "minc-hello-world" ["<stdio.h>"]
/*
c
*/
''
printf("hello world\n");
'')
(pkgs.writers.writeMinCBin "minc-env" ["<stdio.h>" "<stdlib.h>"]
/*
c
*/
''
char *env_name;
if (argc > 1) {
env_name = argv[1];
} else {
env_name = "HOME";
}
char *value = getenv(env_name);
if (value) {
printf("%s: %s\n", env_name, value);
} else {
printf("Environment variable %s not found.\n", env_name);
}
'')
(pkgs.writers.writeMinCBin "minc-env-check" ["<stdio.h>" "<stdlib.h>"]
/*
c
*/
''
char *env_name;
if (argc > 1) {
env_name = argv[1];
} else {
env_name = "HOME";
}
char *value = getenv(env_name);
if (value) {
char buffer[128];
sprintf(buffer, "echo $%s\n", env_name);
system(buffer);
} else {
printf("Environment variable %s not found.\n", env_name);
}
'')
char *value = getenv(env_name);
if (value) {
char buffer[128];
sprintf(buffer, "echo $%s\n", env_name);
system(buffer);
} else {
printf("Environment variable %s not found.\n", env_name);
}
'')
];
programs.zsh.shellAliases = {
jc = ''journalctl'';
@@ -257,17 +273,17 @@
vmVariant = {
systemd.services.fix-root-perms = {
description = "Fix root directory permissions";
after = [ "local-fs.target" ];
wantedBy = [ "multi-user.target" ];
after = ["local-fs.target"];
wantedBy = ["multi-user.target"];
serviceConfig = {
Type = "oneshot";
ExecStart = "${pkgs.coreutils}/bin/chmod 755 /";
};
};
virtualisation = {
diskSize = 1024*6;
diskSize = 1024 * 6;
diskImage = null;
forwardPorts = [ ];
forwardPorts = [];
};
};
};
@@ -279,7 +295,10 @@
};
})
];
pkgs = import nixpkgs {inherit system; overlays = [ self.overlays.default ];};
pkgs = import nixpkgs {
inherit system;
overlays = [self.overlays.default];
};
};
})
// {
@@ -369,7 +388,7 @@
overlays.default = final: prev: (
let
version = "1.6.1";
pkgs-unstable = import nixpkgs-unstable { inherit (prev) system; };
pkgs-unstable = import nixpkgs-unstable {inherit (prev) system;};
buildPgrxExtension =
prev.callPackage (import (builtins.path {
@@ -380,11 +399,10 @@
inherit (pkgs-unstable.darwin.apple_sdk.frameworks) Security;
};
buildPostgresqlExtension =
prev.callPackage (import (builtins.path {
name = "extension-builder";
path = ./buildPostgresqlExtension.nix;
}));
buildPostgresqlExtension = prev.callPackage (import (builtins.path {
name = "extension-builder";
path = ./buildPostgresqlExtension.nix;
}));
buildSmtpExt = versionSuffix: let
postgresql = prev."postgresql_${versionSuffix}";
@@ -402,15 +420,16 @@
inherit src postgresql;
buildInputs = with prev; [ openssl ];
buildInputs = with prev; [openssl];
cargoHash = "sha256-AbLT7vcFV89zwZIaTC1ELat9l4UeNP8Bn9QMMOms1Co=";
doCheck = false;
};
buildPlHaskellExt = versionSuffix: let
version = "4.0";
in buildPostgresqlExtension {
version = "4.0";
in
buildPostgresqlExtension {
postgresql = prev."postgresql_${versionSuffix}";
} {
pname = "plhaskell";
@@ -439,36 +458,61 @@
};
in {
hectic = self.packages.${prev.system};
postgresql_17 = prev.postgresql_17 // {pkgs = prev.postgresql_17.pkgs // {
http = buildHttpExt "17";
pg_smtp_client = buildSmtpExt "17";
plhaskell = buildPlHaskellExt "15";
postgreact = prev.callPackage ./package/c/postgreact/default.nix { postgresql = prev.postgresql_17; };
};};
postgresql_16 = prev.postgresql_16 // {pkgs = prev.postgresql_16.pkgs // {
http = buildHttpExt "16";
pg_smtp_client = buildSmtpExt "16";
plhaskell = buildPlHaskellExt "15";
postgreact = prev.callPackage ./package/c/postgreact/default.nix { postgresql = prev.postgresql_16; };
};};
postgresql_15 = prev.postgresql_15 // {pkgs = prev.postgresql_15.pkgs // {
http = buildHttpExt "15";
pg_smtp_client = buildSmtpExt "15";
plhaskell = buildPlHaskellExt "15";
postgreact = prev.callPackage ./package/c/postgreact/default.nix { postgresql = prev.postgresql_15; };
};};
postgresql_14 = prev.postgresql_14 // {pkgs = prev.postgresql_14.pkgs // {
http = buildHttpExt "14";
pg_smtp_client = buildSmtpExt "14";
plhaskell = buildPlHaskellExt "15";
postgreact = prev.callPackage ./package/c/postgreact/default.nix { postgresql = prev.postgresql_14; };
};};
postgresql_17 =
prev.postgresql_17
// {
pkgs =
prev.postgresql_17.pkgs
// {
http = buildHttpExt "17";
pg_smtp_client = buildSmtpExt "17";
plhaskell = buildPlHaskellExt "15";
postgreact = prev.callPackage ./package/c/postgreact/default.nix {postgresql = prev.postgresql_17;};
};
};
postgresql_16 =
prev.postgresql_16
// {
pkgs =
prev.postgresql_16.pkgs
// {
http = buildHttpExt "16";
pg_smtp_client = buildSmtpExt "16";
plhaskell = buildPlHaskellExt "15";
postgreact = prev.callPackage ./package/c/postgreact/default.nix {postgresql = prev.postgresql_16;};
};
};
postgresql_15 =
prev.postgresql_15
// {
pkgs =
prev.postgresql_15.pkgs
// {
http = buildHttpExt "15";
pg_smtp_client = buildSmtpExt "15";
plhaskell = buildPlHaskellExt "15";
postgreact = prev.callPackage ./package/c/postgreact/default.nix {postgresql = prev.postgresql_15;};
};
};
postgresql_14 =
prev.postgresql_14
// {
pkgs =
prev.postgresql_14.pkgs
// {
http = buildHttpExt "14";
pg_smtp_client = buildSmtpExt "14";
plhaskell = buildPlHaskellExt "15";
postgreact = prev.callPackage ./package/c/postgreact/default.nix {postgresql = prev.postgresql_14;};
};
};
writers = let
writeC =
name: argsOrScript:
if lib.isAttrs argsOrScript && !lib.isDerivation argsOrScript then
writeC = name: argsOrScript:
if lib.isAttrs argsOrScript && !lib.isDerivation argsOrScript
then
prev.writers.makeBinWriter (
argsOrScript // {
argsOrScript
// {
compileScript = ''
# Force gcc to treat the input file as C code
${prev.gcc}/bin/gcc -fsyntax-only -xc $contentPath
@@ -479,7 +523,8 @@
${prev.gcc}/bin/gcc -xc -o $out $contentPath
'';
}
) name
)
name
else
prev.writers.makeBinWriter {
compileScript = ''
@@ -491,29 +536,35 @@
fi
${prev.gcc}/bin/gcc -xc -o $out $contentPath
'';
} name argsOrScript;
writeMinC =
name: includes: body:
writeC name ''
${builtins.concatStringsSep "\n" (map (h: "#include " + h) includes)}
}
name
argsOrScript;
writeMinC = name: includes: body:
writeC name ''
${builtins.concatStringsSep "\n" (map (h: "#include " + h) includes)}
int main(int argc, char *argv[]) {
${body}
}
'';
in prev.writers // {
writeCBin = name: writeC "/bin/${name}";
writeC = writeC;
writeMinCBin = name: includes: body: writeMinC "/bin/${name}" includes body;
writeMinC = writeMinC;
};
int main(int argc, char *argv[]) {
${body}
}
'';
in
prev.writers
// {
writeCBin = name: writeC "/bin/${name}";
writeC = writeC;
writeMinCBin = name: includes: body: writeMinC "/bin/${name}" includes body;
writeMinC = writeMinC;
};
}
);
lib = {
# -- For all systems --
inherit dotEnv minorEnvironment parseEnv forAllSystemsWithPkgs forSpecSystemsWithPkgs;
readEnvironment = { envVarsToRead, prefix ? "" }:
readEnvironment = {
envVarsToRead,
prefix ? "",
}:
builtins.listToAttrs
(map (name: {
inherit name;

View File

@@ -1,12 +1,17 @@
{ stdenv, gcc, lib, bash, inotify-tools }:
{
stdenv,
gcc,
lib,
bash,
inotify-tools,
}:
stdenv.mkDerivation {
pname = "hectic";
version = "1.0";
src = ./.;
doCheck = true;
nativeBuildInputs = [ gcc inotify-tools ];
nativeBuildInputs = [gcc inotify-tools];
buildPhase = ''
ls

View File

@@ -4,7 +4,6 @@
postgresql,
...
}:
stdenv.mkDerivation {
pname = "postgreact";
version = "0.1";
@@ -36,6 +35,6 @@ stdenv.mkDerivation {
homepage = "https://github.com/yukkop/util.nix";
license = licenses.mit;
platforms = postgresql.meta.platforms;
maintainers = with maintainers; [ ];
maintainers = with maintainers; [];
};
}

View File

@@ -1,12 +1,17 @@
{ stdenv, gcc, lib, bash, c-hectic }:
{
stdenv,
gcc,
lib,
bash,
c-hectic,
}:
stdenv.mkDerivation {
pname = "prettify";
version = "1.0";
src = ./.;
doCheck = false;
nativeBuildInputs = [ gcc c-hectic ];
nativeBuildInputs = [gcc c-hectic];
buildPhase = ''
ls

View File

@@ -1,12 +1,17 @@
{ stdenv, gcc, lib, bash, gdb }:
{
stdenv,
gcc,
lib,
bash,
gdb,
}:
stdenv.mkDerivation {
pname = "watch";
version = "1.0";
src = ./.;
doCheck = false;
nativeBuildInputs = [ gcc gdb ];
nativeBuildInputs = [gcc gdb];
buildPhase = ''
${bash}/bin/sh ./make.sh build

View File

@@ -16,7 +16,7 @@ in
cargoLock.lockFile = ./Cargo.lock;
buildInputs = [ postgresql_15 ];
buildInputs = [postgresql_15];
doCheck = true;
}