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": { "nodes": {
"crane": {
"locked": {
"lastModified": 1744386647,
"narHash": "sha256-DXwQEJllxpYeVOiSlBhQuGjfvkoGHTtILLYO2FvcyzQ=",
"owner": "ipetkov",
"repo": "crane",
"rev": "d02c1cdd7ec539699aa44e6ff912e15535969803",
"type": "github"
},
"original": {
"owner": "ipetkov",
"repo": "crane",
"type": "github"
}
},
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 0, "lastModified": 0,
"narHash": "sha256-hnB0V0R/aKASnTBeTthFvW60uydv1xswWD4weqSuSfg=", "narHash": "sha256-LWqduOgLHCFxiTNYi3Uj5Lgz0SR+Xhw3kr/3Xd0GPTM=",
"path": "/nix/store/alyms72p6aivxmniyqy01qw9bc3ym1s1-source", "path": "/nix/store/5ds20jm3x2s4z7wn3581r6lc9ybmh45b-source",
"type": "path" "type": "path"
}, },
"original": { "original": {
@ -14,28 +29,8 @@
}, },
"root": { "root": {
"inputs": { "inputs": {
"nixpkgs": "nixpkgs", "crane": "crane",
"rust-overlay": "rust-overlay" "nixpkgs": "nixpkgs"
}
},
"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"
} }
} }
}, },

112
flake.nix
View File

@ -3,17 +3,14 @@
inputs = { inputs = {
nixpkgs.url = "flake:nixpkgs"; nixpkgs.url = "flake:nixpkgs";
rust-overlay = { crane.url = "github:ipetkov/crane";
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
}; };
outputs = outputs =
{ {
self, self,
nixpkgs, nixpkgs,
rust-overlay, crane,
}: }:
let let
inherit (nixpkgs) lib; inherit (nixpkgs) lib;
@ -48,7 +45,6 @@
import nixpkgs { import nixpkgs {
inherit system; inherit system;
overlays = [ overlays = [
(import rust-overlay)
(_: final: overlay final) (_: final: overlay final)
]; ];
} }
@ -57,68 +53,60 @@
overlay = overlay =
pkgs: pkgs:
let let
rev = self.shortRev or "dirty"; buildCraneLib = crane.mkLib pkgs.buildPackages;
cargoDeps =
platform: commonArgs = pkgs: {
platform.importCargoLock { inherit src;
lockFile = ./Cargo.lock; strictDeps = true;
outputHashes = {
"dbmigrator-0.4.4-alpha" = "sha256-Nwxw74IyZeZ9dODb+aneQmuQe0grO+g45B3zv1XaihE="; nativeBuildInputs = [ pkgs.buildPackages.pkg-config ];
};
}; env.OPENSSL_NO_VENDOR = true;
mkRustPlatform =
pkgs: rustVersion: buildInputs = [ pkgs.openssl ];
pkgs.makeRustPlatform {
cargo = rustVersion; # PKG_CONFIG_ALLOW_CROSS = if pkgs.stdenv.buildPlatform != pkgs.stdenv.hostPlatform then 1 else 0;
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 ];
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; doCheck = false;
}; };
rustVersion = pkgs.pkgsBuildHost.rust-bin.fromRustupToolchain { openapi = buildCraneLib.buildPackage (
channel = "stable"; let
targets = [ common = (commonArgs pkgs.buildPackages);
pkgs.pkgsBuildHost.targetPlatform.rust.rustcTarget in
pkgs.pkgsBuildHost.buildPlatform.rust.rustcTarget common
]; // {
}; cargoArtifacts = buildCraneLib.buildDepsOnly common;
rustPlatform = mkRustPlatform pkgs rustVersion;
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 { bankserver = pkgs.callPackage ./package.nix {
inherit inherit src openapi;
src commonArgs = commonArgs pkgs;
rustPlatform craneLib = crane.mkLib pkgs;
schemas
rev
;
cargoDeps = cargoDeps rustPlatform;
}; };
in in
{ {

View File

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