Building local Rust package in home-manager?

Hi all,

I built a local utility script in Rust that I’d like to include as a package in my home-manager configuration. I have this currently, which I saw example code from How to package a Rust app using Nix - DEV Community

home.packages = [
    (pkgs.rustPlatform.buildRustPackage {
        pname = "couleur";
        version = "0.1.0";
                                                                                                  
        src = pkgs.lib.cleanSource "${config.home.homeDirectory}/.dotfiles/scripts/couleur";
        cargoLock.lockFile = "${config.home.homeDirectory}/.dotfiles/scripts/couleur/Cargo.lock";
    })
];

But when I run home-manager build switch, I get the error log

Running phase: unpackPhase
@nix { "action": "setPhase", "phase": "unpackPhase" }
unpacking source archive /nix/store/a39621yq98k6rri5gz1icalarypl6r5w-source
source root is source
Executing cargoSetupPostUnpackHook
cp: cannot stat '/nix/store/kzgz9i0dvm7qmwf7jm7f55aqz1adxlwy-cargo-vendor-dir/Cargo.lock': No such file or directory

I’m not sure if home-manager is trying to access my cargoLock.lockFile from the cargo build in the store? It already successfully downloaded all of my project’s dependencies, so it is definitely reading from the correct src. I also have a shell.nix and a “use nix” .envrc in the project directory:

{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
    buildInputs = with pkgs; [
        rustup
    ];
    packages = with pkgs; [
        clang
        gnumake
        cmake
    ];
}

But I’m not sure if it’s being read or not (I would assume so because I don’t have rustup installed user or system-wide, I only use it in shells).

If anyone could help, I would really appreciate it. Thanks!

Don’t use absolute paths. Use relative paths only, and make sure they’re git-add-ed if you’re in a git repo.

I initially was using relative paths, but I get this error when I use them: error: string '../scripts/couleur/Cargo.lock' doesn't represent an absolute path

I should be more specific.
Use relative paths, not path strings, and make sure you stay within your flake (i.e. don’t go above the level of the dir with your flake.nix in it).

1 Like

Ahhh yep that instantly fixed it

(rustPlatform.buildRustPackage {
    pname = "couleur";
    version = "0.1.0";
                                                        
    src = pkgs.lib.cleanSource ../scripts/couleur;
    cargoLock.lockFile = ../scripts/couleur/Cargo.lock;
})

And I’m not using flakes currently, but you make a good point. Maybe I should move scripts under the directory where my home.nix is, that’s probably more appropriate.

Thanks!!

Right, I should’ve said if you’re using flakes :slight_smile: Flakes enforce more restrictions on nix evaluations.