fix: make nix flake and cargo config portable across Linux and macOS

musl tooling is Linux-only, so guard it behind an isLinux check in
flake.nix and remove the hardcoded musl build target from .cargo/config.toml
(the nix devShell already sets CARGO_BUILD_TARGET on Linux).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Edward Langley
2026-03-30 22:22:11 -07:00
parent a655078997
commit e3078ba61d
2 changed files with 32 additions and 23 deletions

View File

@ -1,8 +1,6 @@
[build] # The nix devShell sets CARGO_BUILD_TARGET=x86_64-unknown-linux-musl
# Always build a fully-static musl binary by default. # and the matching linker env vars, so no [build] target is needed here.
# The devShell sets CARGO_BUILD_TARGET and CARGO_TARGET_*_LINKER, # This keeps the config portable across Linux and macOS.
# so the linker is resolved automatically via the environment.
target = "x86_64-unknown-linux-musl"
[target.x86_64-unknown-linux-gnu] [target.x86_64-unknown-linux-gnu]
# Use gcc (not rust-lld) for host-target build scripts on NixOS. # Use gcc (not rust-lld) for host-target build scripts on NixOS.

View File

@ -15,48 +15,59 @@
let let
overlays = [ (import rust-overlay) ]; overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs { inherit system overlays; }; pkgs = import nixpkgs { inherit system overlays; };
isLinux = pkgs.lib.hasInfix "linux" system;
rustToolchain = pkgs.rust-bin.stable.latest.default.override { rustToolchain = pkgs.rust-bin.stable.latest.default.override {
extensions = [ "rust-src" "clippy" "rustfmt" ]; extensions = [ "rust-src" "clippy" "rustfmt" ];
targets = [ "x86_64-unknown-linux-musl" ]; targets = pkgs.lib.optionals isLinux [ "x86_64-unknown-linux-musl" ];
}; };
muslCC = pkgs.pkgsMusl.stdenv.cc;
in in
{ {
devShells.default = pkgs.mkShell { devShells.default = pkgs.mkShell ({
nativeBuildInputs = [ nativeBuildInputs = [
rustToolchain rustToolchain
pkgs.pkg-config pkgs.pkg-config
] ++ pkgs.lib.optionals isLinux [
# Provide cc (gcc) for building proc-macro / build-script crates # Provide cc (gcc) for building proc-macro / build-script crates
# that target the host (x86_64-unknown-linux-gnu). # that target the host (x86_64-unknown-linux-gnu).
pkgs.gcc pkgs.gcc
# musl-gcc wrapper for the static musl target. # musl-gcc wrapper for the static musl target.
muslCC pkgs.pkgsMusl.stdenv.cc
]; ];
RUST_BACKTRACE = "1";
} // pkgs.lib.optionalAttrs isLinux {
# Tell Cargo which linker to use for each target so it never # Tell Cargo which linker to use for each target so it never
# falls back to rust-lld (which can't find glibc on NixOS). # falls back to rust-lld (which can't find glibc on NixOS).
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER = CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER =
"${pkgs.gcc}/bin/gcc"; "${pkgs.gcc}/bin/gcc";
CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER = CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER =
"${muslCC}/bin/cc"; "${pkgs.pkgsMusl.stdenv.cc}/bin/cc";
# Default build target: static musl binary. # Default build target: static musl binary.
CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl"; CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl";
});
RUST_BACKTRACE = "1";
};
packages.default = packages.default =
(pkgs.pkgsMusl.makeRustPlatform { if isLinux then
cargo = rustToolchain; (pkgs.pkgsMusl.makeRustPlatform {
rustc = rustToolchain; cargo = rustToolchain;
}).buildRustPackage { rustc = rustToolchain;
pname = "improvise"; }).buildRustPackage {
version = "0.1.0"; pname = "improvise";
src = ./.; version = "0.1.0";
cargoLock.lockFile = ./Cargo.lock; src = ./.;
}; cargoLock.lockFile = ./Cargo.lock;
}
else
(pkgs.makeRustPlatform {
cargo = rustToolchain;
rustc = rustToolchain;
}).buildRustPackage {
pname = "improvise";
version = "0.1.0";
src = ./.;
cargoLock.lockFile = ./Cargo.lock;
};
}); });
} }