From bb973121b973b69c88791df5485bbb9b308bbac8 Mon Sep 17 00:00:00 2001 From: DSeeLP <46624152+DSeeLP@users.noreply.github.com> Date: Thu, 17 Apr 2025 19:51:50 +0200 Subject: [PATCH] refactor nix build --- flake.lock | 43 +++++++++----------- flake.nix | 112 +++++++++++++++++++++++----------------------------- package.nix | 54 +++++++------------------ 3 files changed, 84 insertions(+), 125 deletions(-) diff --git a/flake.lock b/flake.lock index 1bda546..4991c89 100644 --- a/flake.lock +++ b/flake.lock @@ -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" } } }, diff --git a/flake.nix b/flake.nix index 3524cfe..a5ead7c 100644 --- a/flake.nix +++ b/flake.nix @@ -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 { diff --git a/package.nix b/package.nix index b9fce4f..a9c0922 100644 --- a/package.nix +++ b/package.nix @@ -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"; + } +)