refactor nix build

This commit is contained in:
DSeeLP 2025-04-17 19:51:50 +02:00
parent 6141cefa41
commit bb973121b9
3 changed files with 84 additions and 125 deletions

43
flake.lock generated
View File

@ -1,10 +1,25 @@
{
"nodes": {
"crane": {
"locked": {
"lastModified": 1744386647,
"narHash": "sha256-DXwQEJllxpYeVOiSlBhQuGjfvkoGHTtILLYO2FvcyzQ=",
"owner": "ipetkov",
"repo": "crane",
"rev": "d02c1cdd7ec539699aa44e6ff912e15535969803",
"type": "github"
},
"original": {
"owner": "ipetkov",
"repo": "crane",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 0,
"narHash": "sha256-hnB0V0R/aKASnTBeTthFvW60uydv1xswWD4weqSuSfg=",
"path": "/nix/store/alyms72p6aivxmniyqy01qw9bc3ym1s1-source",
"narHash": "sha256-LWqduOgLHCFxiTNYi3Uj5Lgz0SR+Xhw3kr/3Xd0GPTM=",
"path": "/nix/store/5ds20jm3x2s4z7wn3581r6lc9ybmh45b-source",
"type": "path"
},
"original": {
@ -14,28 +29,8 @@
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
}
},
"rust-overlay": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1740709839,
"narHash": "sha256-4dF++MXIXna/AwlZWDKr7bgUmY4xoEwvkF1GewjNrt0=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "b4270835bf43c6f80285adac6f66a26d83f0f277",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
"crane": "crane",
"nixpkgs": "nixpkgs"
}
}
},

112
flake.nix
View File

@ -3,17 +3,14 @@
inputs = {
nixpkgs.url = "flake:nixpkgs";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
crane.url = "github:ipetkov/crane";
};
outputs =
{
self,
nixpkgs,
rust-overlay,
crane,
}:
let
inherit (nixpkgs) lib;
@ -48,7 +45,6 @@
import nixpkgs {
inherit system;
overlays = [
(import rust-overlay)
(_: final: overlay final)
];
}
@ -57,68 +53,60 @@
overlay =
pkgs:
let
rev = self.shortRev or "dirty";
cargoDeps =
platform:
platform.importCargoLock {
lockFile = ./Cargo.lock;
outputHashes = {
"dbmigrator-0.4.4-alpha" = "sha256-Nwxw74IyZeZ9dODb+aneQmuQe0grO+g45B3zv1XaihE=";
};
};
mkRustPlatform =
pkgs: rustVersion:
pkgs.makeRustPlatform {
cargo = rustVersion;
rustc = rustVersion;
};
buildRustVersion = pkgs.pkgsBuildBuild.rust-bin.stable.latest.minimal;
buildRustPlatform = mkRustPlatform pkgs.pkgsBuildBuild buildRustVersion;
schemas = pkgs.pkgsBuildBuild.stdenv.mkDerivation {
pname = "bankingserver_schemas";
version = "unstable-${rev}";
inherit src;
cargoDeps = cargoDeps buildRustPlatform;
cargoBuildType = "debug";
# "CARGO_TARGET_${stdenv.hostPlatform.rust.cargoEnvVarTarget}_LINKER" =
# "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc";
nativeBuildInputs = [
buildRustPlatform.cargoSetupHook
buildRustVersion
pkgs.pkgsBuildBuild.pkg-config
];
buildInputs = [ pkgs.pkgsBuildBuild.openssl ];
buildCraneLib = crane.mkLib pkgs.buildPackages;
commonArgs = pkgs: {
inherit src;
strictDeps = true;
nativeBuildInputs = [ pkgs.buildPackages.pkg-config ];
env.OPENSSL_NO_VENDOR = true;
buildInputs = [ pkgs.openssl ];
# PKG_CONFIG_ALLOW_CROSS = if pkgs.stdenv.buildPlatform != pkgs.stdenv.hostPlatform then 1 else 0;
buildPhase = ''
touch openapi.json
cargo build --bin generate-schemas
target/debug/generate-schemas
runHook postBuild
'';
installPhase = ''
mkdir -p $out/share/bankserver
cp -r schemas $out/share/bankserver/schemas
'';
doCheck = false;
};
rustVersion = pkgs.pkgsBuildHost.rust-bin.fromRustupToolchain {
channel = "stable";
targets = [
pkgs.pkgsBuildHost.targetPlatform.rust.rustcTarget
pkgs.pkgsBuildHost.buildPlatform.rust.rustcTarget
];
};
rustPlatform = mkRustPlatform pkgs rustVersion;
openapi = buildCraneLib.buildPackage (
let
common = (commonArgs pkgs.buildPackages);
in
common
// {
cargoArtifacts = buildCraneLib.buildDepsOnly common;
pnameSuffix = "-openapi.json";
nativeBuildInputs =
common.nativeBuildInputs
++ (with pkgs.buildPackages; [
yq-go
redocly
]);
cargoExtraArgs = "--bin generate-schemas";
preBuild = ''
touch openapi.json
'';
installPhaseCommand = ''
''${CARGO_TARGET_DIR:-target}/$CARGO_PROFILE/generate-schemas
rm -rf target
yq eval-all -n 'load("openapi-def.yaml") *n load("schemas/schemas.json")' > openapi-temp.yaml
redocly bundle openapi-temp.yaml -o openapi.json
cp openapi.json $out
'';
}
);
bankserver = pkgs.callPackage ./package.nix {
inherit
src
rustPlatform
schemas
rev
;
cargoDeps = cargoDeps rustPlatform;
inherit src openapi;
commonArgs = commonArgs pkgs;
craneLib = crane.mkLib pkgs;
};
in
{

View File

@ -1,44 +1,20 @@
{
rustPlatform,
stdenv,
pkg-config,
openssl,
redocly,
yq-go,
targetPlatform,
craneLib,
commonArgs,
src,
schemas,
cargoDeps,
rev ? "dirty",
openapi,
}:
rustPlatform.buildRustPackage {
pname = "bankingserver";
version = "unstable-${rev}";
inherit src cargoDeps;
craneLib.buildPackage (
commonArgs
// {
inherit src;
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
nativeBuildInputs = [
schemas
redocly
yq-go
pkg-config
];
preBuild = ''
echo hi
cp ${openapi} openapi.json
'';
buildInputs = [ openssl ];
env.OPENSSL_NO_VENDOR = true;
preBuild = ''
cp -r ${schemas}/share/bankserver/schemas schemas
yq eval-all -n 'load("openapi-def.yaml") *n load("schemas/schemas.json")' > openapi-temp.yaml
redocly bundle openapi-temp.yaml -o openapi.json
'';
cargoBuildFlags = "--bin bankserver";
CARGO_BUILD_TARGET = targetPlatform.config;
TARGET_CC = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc";
doCheck = false;
}
cargoExtraArgs = "--bin bankserver";
}
)