feat(build): use crate2nix

This commit is contained in:
Edward Langley
2026-04-02 11:34:22 -07:00
parent edd6431444
commit be277f43c2
2 changed files with 280 additions and 32 deletions

View File

@ -7,6 +7,7 @@
url = "github:oxalica/rust-overlay";
};
flake-utils.url = "github:numtide/flake-utils";
crate2nix.url = "github:nix-community/crate2nix";
};
outputs = {
@ -14,6 +15,7 @@
nixpkgs,
rust-overlay,
flake-utils,
crate2nix,
}:
flake-utils.lib.eachDefaultSystem (system: let
overlays = [(import rust-overlay)];
@ -24,6 +26,25 @@
extensions = ["rust-src" "clippy" "rustfmt"];
targets = pkgs.lib.optionals isLinux ["x86_64-unknown-linux-musl"];
};
buildPkgs =
if isLinux
then pkgs.pkgsMusl
else pkgs;
generatedCargoNix = crate2nix.tools.${system}.generatedCargoNix {
name = "improvise";
src = ./.;
};
cargoNix = import generatedCargoNix {
inherit pkgs;
buildRustCrateForPkgs = pkgs:
buildPkgs.buildRustCrate.override {
rustc = rustToolchain;
cargo = rustToolchain;
};
};
in {
devShells.default = pkgs.mkShell ({
nativeBuildInputs =
@ -31,48 +52,21 @@
rustToolchain
pkgs.pkg-config
pkgs.rust-analyzer
crate2nix.packages.${system}.default
]
++ pkgs.lib.optionals isLinux [
# Provide cc (gcc) for building proc-macro / build-script crates
# that target the host (x86_64-unknown-linux-gnu).
pkgs.gcc
# musl-gcc wrapper for the static musl target.
pkgs.pkgsMusl.stdenv.cc
];
RUST_BACKTRACE = "1";
}
// pkgs.lib.optionalAttrs isLinux {
# Tell Cargo which linker to use for each target so it never
# falls back to rust-lld (which can't find glibc on NixOS).
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER = "${pkgs.gcc}/bin/gcc";
CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER = "${pkgs.pkgsMusl.stdenv.cc}/bin/cc";
# Default build target: static musl binary.
CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl";
});
packages.default =
if isLinux
then
(pkgs.pkgsMusl.makeRustPlatform {
cargo = rustToolchain;
rustc = rustToolchain;
}).buildRustPackage {
pname = "improvise";
version = "0.1.0";
src = ./.;
cargoLock.lockFile = ./Cargo.lock;
}
else
(pkgs.makeRustPlatform {
cargo = rustToolchain;
rustc = rustToolchain;
}).buildRustPackage {
pname = "improvise";
version = "0.1.0";
src = ./.;
cargoLock.lockFile = ./Cargo.lock;
};
packages.default = cargoNix.rootCrate.build;
});
}