Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rust: use mold #1056

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions src/modules/languages/rust.nix
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ in
description = "The rustup toolchain to install.";
};

mold.enable = lib.mkOption {
type = lib.types.bool;
default = pkgs.stdenv.isLinux && pkgs.stdenv.isx86_64 && cfg.targets == [ ];
description = "Enable mold as the linker.";
};

toolchain = lib.mkOption {
type = lib.types.submodule ({
freeformType = lib.types.attrsOf lib.types.package;
Expand Down Expand Up @@ -95,23 +101,29 @@ in
# enable compiler tooling by default to expose things like cc
languages.c.enable = lib.mkDefault true;

# RUST_SRC_PATH is necessary when rust-src is not at the same location as
# as rustc. This is the case with the rust toolchain from nixpkgs.
env.RUST_SRC_PATH =
if cfg.toolchain ? rust-src
then "${cfg.toolchain.rust-src}/lib/rustlib/src/rust/library"
else pkgs.rustPlatform.rustLibSrc;

env =
let
darwinFlags = lib.optionalString pkgs.stdenv.isDarwin "-L framework=${config.devenv.profile}/Library/Frameworks";
moldFlags = lib.optionalString cfg.mold.enable "-C link-arg=-fuse-ld=${pkgs.mold-wrapped}/bin/ld.mold";
in
{
# RUST_SRC_PATH is necessary when rust-src is not at the same location as
# as rustc. This is the case with the rust toolchain from nixpkgs.
RUST_SRC_PATH =
if cfg.toolchain ? rust-src
then "${cfg.toolchain.rust-src}/lib/rustlib/src/rust/library"
else pkgs.rustPlatform.rustLibSrc;
RUSTFLAGS = "${darwinFlags} ${moldFlags}";
RUSTDOCFLAGS = "${darwinFlags} ${moldFlags}";
CFLAGS = lib.optionalString pkgs.stdenv.isDarwin "-iframework ${config.devenv.profile}/Library/Frameworks";
};

pre-commit.tools.cargo = mkOverrideTools cfg.toolchain.cargo or null;
pre-commit.tools.rustfmt = mkOverrideTools cfg.toolchain.rustfmt or null;
pre-commit.tools.clippy = mkOverrideTools cfg.toolchain.clippy or null;
}
))
(lib.mkIf (cfg.enable && pkgs.stdenv.isDarwin) {
env.RUSTFLAGS = "-L framework=${config.devenv.profile}/Library/Frameworks";
env.RUSTDOCFLAGS = "-L framework=${config.devenv.profile}/Library/Frameworks";
env.CFLAGS = "-iframework ${config.devenv.profile}/Library/Frameworks";
})
(lib.mkIf (cfg.channel != "nixpkgs") (
let
rustPackages = fenix.packages.${pkgs.stdenv.system};
Expand Down
Loading