From de6e1f8553bfa8e12821d332427065558efdc209 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Domen=20Ko=C5=BEar?= Date: Tue, 26 Mar 2024 02:57:35 +0000 Subject: [PATCH] rust: use mold --- src/modules/languages/rust.nix | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/modules/languages/rust.nix b/src/modules/languages/rust.nix index c5a8af43a..e9cae4f89 100644 --- a/src/modules/languages/rust.nix +++ b/src/modules/languages/rust.nix @@ -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; @@ -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};