templates/rust/flake.nix

165 lines
4.6 KiB
Nix
Raw Normal View History

2024-05-06 20:00:10 +00:00
{
inputs = {
flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
nixpkgs.url = "github:NixOs/nixpkgs/nixos-unstable";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
{
self,
nixpkgs,
fenix,
...
}@inputs:
let
cargoMeta = builtins.fromTOML (builtins.readFile ./Cargo.toml);
packageName = cargoMeta.package.name;
forSystems =
function:
nixpkgs.lib.genAttrs [ "x86_64-linux" ] (
system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ (final: prev: { ${packageName} = self.packages.${system}.${packageName}; }) ];
};
2024-05-16 16:01:19 +00:00
fenixPkgs = fenix.packages.${pkgs.system};
fenixChannel = fenixPkgs.toolchainOf {
channel = "nightly";
date =
builtins.replaceStrings [ "nightly-" ] [ "" ]
(builtins.fromTOML (builtins.readFile ./rust-toolchain.toml)).toolchain.channel;
sha256 = "sha256-SzEeSoO54GiBQ2kfANPhWrt0EDRxqEvhIbTt2uJt/TQ=";
};
toolchainFor =
pkgs:
2024-05-16 16:01:19 +00:00
with fenixPkgs;
combine [
minimal.cargo
minimal.rustc
targets.${pkgs.rust.lib.toRustTarget pkgs.stdenv.targetPlatform}.latest.rust-std
];
2024-05-06 20:00:10 +00:00
rustPlatformFor =
pkgs:
2024-05-06 20:00:10 +00:00
let
toolchain = toolchainFor pkgs;
in
pkgs.makeRustPlatform {
cargo = toolchain;
rustc = toolchain;
};
crossPackageFor =
pkgs:
let
rustPlatform = rustPlatformFor pkgs;
2024-05-06 20:00:10 +00:00
in
{
"${packageName}-cross-${pkgs.stdenv.hostPlatform.config}${
if pkgs.stdenv.hostPlatform.isStatic then "-static" else ""
}" = pkgs.callPackage (./. + "/nix/packages/${packageName}.nix") {
inherit cargoMeta rustPlatform;
flake-self = self;
};
2024-05-06 20:00:10 +00:00
};
in
function {
inherit
system
pkgs
2024-05-16 16:01:19 +00:00
fenixPkgs
fenixChannel
toolchainFor
rustPlatformFor
crossPackageFor
2024-05-06 20:00:10 +00:00
;
}
);
in
{
formatter = forSystems ({ pkgs, ... }: pkgs.alejandra);
2024-05-16 16:19:41 +00:00
checks = forSystems (
{ pkgs, fenixChannel, ... }:
{
rustfmt =
pkgs.runCommand "check-rustfmt"
{
nativeBuildInputs =
let
fenixRustToolchain = fenixChannel.withComponents [
"cargo"
"rustfmt-preview"
];
in
[ fenixRustToolchain ];
}
''
cd ${./.}
cargo fmt -- --check
touch $out
'';
}
);
2024-05-06 20:00:10 +00:00
packages = forSystems (
{
pkgs,
2024-05-16 16:01:19 +00:00
fenixChannel,
2024-05-06 20:00:10 +00:00
system,
crossPackageFor,
2024-05-06 20:00:10 +00:00
...
}:
{
${packageName} = pkgs.callPackage (./. + "/nix/packages/${packageName}.nix") {
inherit cargoMeta;
flake-self = self;
rustPlatform = pkgs.makeRustPlatform {
2024-05-16 16:01:19 +00:00
cargo = fenixChannel.toolchain;
rustc = fenixChannel.toolchain;
2024-05-06 20:00:10 +00:00
};
};
default = self.packages.${system}.${packageName};
}
// crossPackageFor pkgs.pkgsCross.musl64.pkgsStatic
// crossPackageFor pkgs.pkgsCross.musl32.pkgsStatic
// crossPackageFor pkgs.pkgsCross.aarch64-multiplatform-musl.pkgsStatic
// crossPackageFor pkgs.pkgsCross.armv7l-hf-multiplatform.pkgsStatic
// crossPackageFor pkgs.pkgsCross.mingwW64.pkgsStatic
2024-05-06 20:00:10 +00:00
);
devShells = forSystems (
2024-05-16 16:01:19 +00:00
{ pkgs, fenixChannel, ... }:
2024-05-06 20:00:10 +00:00
let
2024-05-16 16:01:19 +00:00
fenixRustToolchain = fenixChannel.withComponents [
2024-05-06 20:00:10 +00:00
"cargo"
"clippy-preview"
"rust-src"
"rustc"
"rustfmt-preview"
];
in
{
default = pkgs.callPackage (./. + "/nix/dev-shells/${packageName}.nix") {
inherit fenixRustToolchain cargoMeta;
};
ci = pkgs.callPackage (./nix/dev-shells/ci.nix) { inherit fenixRustToolchain cargoMeta; };
}
);
};
}