Is CARGO_INSTALL_ROOT my only runtime option?

According to the man page for cargo install:

       The installation root is determined, in order of precedence:

       •  --root option

       •  CARGO_INSTALL_ROOT environment variable

       •  install.root Cargo config value

       •  CARGO_HOME environment variable

       •  $HOME/.cargo

I have a multi-binary install. One binary is a CLI app for users, the other is a daemon that runs in the background. The CLI needs to set up some things with the daemon, but to do so, it must know where the daemon binary is installed.

I can use the env! macro from the std library to find the CARGO_INSTALL_ROOT or CARGO_HOME environment variables and get a path to my binary. But only if the default install happens.

If the user installs using --root or for some reason has some other uncommon configuration, then I see no way to find the installation location. Should I just include a note to never do those things in my documentation, or is there another way to get the install path for certain? Thank you.

You can use std::env::current_exe() to obtain the path to where your CLI is run from, and then assume the daemon will be in the same directory.

But also, cargo install is not a full-featured installation tool; it's primarily useful for installing small Rust build-related tools. If you have a complex application and cargo install starts not being helpful enough (whether you find that to be now or in the future), it's time to switch to creating real package-manager packages. They will have some approach to arranging so you can know that path — often compile-time configuration.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.