{ fenix }:
{
config,
flake-parts-lib,
lib,
...
}:
let
cfg = config.polyphony.rust;
in
{
options =
let
inherit (lib) literalExpression mkEnableOption mkOption;
inherit (lib.types)
attrs
functionTo
nullOr
package
path
str
;
in
{
polyphony.rust = {
enable = (mkEnableOption "workflows and tools for building Rust projects") // {
default = true;
example = false;
};
toolchainFile = mkOption {
type = path;
example = literalExpression "./rust-toolchain.toml";
description = ''
Path to a rust-toolchain.toml file defining the toolchain to use.
'';
};
hash = mkOption {
type = str;
example = "sha256-...";
description = ''
The hash for the configured Rust toolchain's manifest.
You'll probably want to set this to an empty string initially, then copy the hash returned in the error to
this setting.
'';
};
};
perSystem = flake-parts-lib.mkPerSystemOption (
{ system, ... }:
{
options.polyphony.rust = {
toolchain = mkOption {
type = package;
readOnly = true;
description = ''
Package containing a Rust toolchain, as specified by the configuration.
'';
};
package = mkOption {
type = nullOr (functionTo package);
default = null;
example = literalExpression "pkgs: pkgs.callPackage ./rust-build.nix { }";
description = ''
A function that produces the Rust build derivation for this project.
Accepts a Nixpkgs set. The function should return a derivation that builds the Rust project using the
supplied Nixpkgs set. The derivation will be overridden to use a `rustPlatform` containing the
configured Rust toolchain. The derivation will be accessible in the flake's outputs under
`packages.<system>.rustBuild`.
See https://nixos.org/manual/nixpkgs/unstable/#using-rust-nightly-in-a-derivation-with-buildrustpackage
for information about using Rust platforms.
'';
};
crossTargets = mkOption {
type = nullOr attrs;
default = null;
example = literalExpression ''
{
aarch64-unknown-linux-gnu = pkgs.pkgsCross.aarch64-multiplatform;
}'';
description = ''
A mapping from target names to cross-Nixpkgs sets to use for building that target.
For each target specified, a derivation will be added to `packages.<system>.rustBuildCross-<target>`,
based on the function defined for `polyphony.rust.package`. See
https://nixos.org/manual/nixpkgs/unstable/#cross-compilation for details on how the Nixpkgs set
determines the target used.
Make sure all the desired cross targets are specified in your rust-toolchain.toml!
'';
};
};
config = lib.mkIf cfg.enable {
polyphony.rust.toolchain = fenix.packages.${system}.fromToolchainFile {
file = cfg.toolchainFile;
sha256 = config.polyphony.rust.hash;
};
};
}
);
};
config = lib.mkIf cfg.enable {
perSystem =
{
config,
lib,
pkgs,
...
}:
let
inherit (config.polyphony.rust) crossTargets package toolchain;
rustPlatformFor =
pkgs':
pkgs'.makeRustPlatform {
cargo = toolchain;
rustc = toolchain;
};
evalPackage =
pkgs':
(package pkgs').override {
rustPlatform = (rustPlatformFor pkgs');
};
in
lib.mkMerge [
(lib.mkIf (package != null) {
packages.rustBuild = evalPackage pkgs;
})
(lib.mkIf (package != null && crossTargets != null) {
packages = lib.mapAttrs' (
target: pkgs': lib.nameValuePair "rustBuildCross-${target}" (evalPackage pkgs')
) crossTargets;
})
];
};
}