From 38539206d0637bc951f01192e32b4e64d91d0941 Mon Sep 17 00:00:00 2001 From: Utkarsh Gupta Date: Mon, 2 Mar 2026 10:17:48 +0000 Subject: [PATCH] cargo: add cargoHome option --- modules/programs/cargo.nix | 32 +++++++++++++++----- tests/modules/programs/cargo/cargo-home.nix | 33 +++++++++++++++++++++ tests/modules/programs/cargo/default.nix | 1 + 3 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 tests/modules/programs/cargo/cargo-home.nix diff --git a/modules/programs/cargo.nix b/modules/programs/cargo.nix index 0a9f07d1..a251c944 100644 --- a/modules/programs/cargo.nix +++ b/modules/programs/cargo.nix @@ -18,11 +18,19 @@ in package = lib.mkPackageOption pkgs "cargo" { nullable = true; }; + cargoHome = lib.mkOption { + type = lib.types.nullOr lib.types.str; + apply = p: if p != null then lib.removePrefix "${config.home.homeDirectory}/" p else p; + default = null; + example = lib.literalExpression "\${config.xdg.dataHome}/cargo"; + description = "Directory to store cargo configuration & state. Setting this also sets $CARGO_HOME."; + }; + settings = lib.mkOption { inherit (tomlFormat) type; default = { }; description = '' - Available configuration options for the .cargo/config see: + Available configuration options for the $CARGO_HOME/config see: https://doc.rust-lang.org/cargo/reference/config.html ''; }; @@ -30,15 +38,23 @@ in }; }; - config = lib.mkIf cfg.enable { - home = { - packages = lib.mkIf (cfg.package != null) [ cfg.package ]; + config = + let + cargoHome = if cfg.cargoHome != null then cfg.cargoHome else ".cargo"; + in + lib.mkIf cfg.enable { + home = { + packages = lib.mkIf (cfg.package != null) [ cfg.package ]; - file = { - ".cargo/config.toml" = { - source = tomlFormat.generate "config.toml" cfg.settings; + sessionVariables = lib.mkIf (cfg.cargoHome != null) { + CARGO_HOME = "${config.home.homeDirectory}/${cfg.cargoHome}"; + }; + + file = { + "${cargoHome}/config.toml" = { + source = tomlFormat.generate "config.toml" cfg.settings; + }; }; }; }; - }; } diff --git a/tests/modules/programs/cargo/cargo-home.nix b/tests/modules/programs/cargo/cargo-home.nix new file mode 100644 index 00000000..c81285c9 --- /dev/null +++ b/tests/modules/programs/cargo/cargo-home.nix @@ -0,0 +1,33 @@ +{ + config, + lib, + pkgs, + ... +}: + +{ + programs.cargo = { + enable = true; + + cargoHome = "${config.xdg.dataHome}/cargo"; + + settings = { + net = { + git-fetch-with-cli = true; + }; + }; + }; + + nmt.script = + let + cargoTestPath = "home-files/.local/share/cargo"; + configTestPath = "${cargoTestPath}/config.toml"; + in + '' + assertPathNotExists home-files/.cargo/config + assertDirectoryExists ${cargoTestPath} + assertFileExists ${configTestPath} + assertFileContent ${configTestPath} \ + ${./example-config.toml} + ''; +} diff --git a/tests/modules/programs/cargo/default.nix b/tests/modules/programs/cargo/default.nix index 34280847..58cf051d 100644 --- a/tests/modules/programs/cargo/default.nix +++ b/tests/modules/programs/cargo/default.nix @@ -1,4 +1,5 @@ { cargo = ./example-config.nix; cargo-empty-config = ./empty-config.nix; + cargo-home = ./cargo-home.nix; }