-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
NIX_BIN_DIR
and all logic using it to the Nix executable itself
This is because with the split packages of the Meson build, we simply have no idea what directory the binaries will be installed in when we build the library. In the process of doing so, consolidate and make more sophisticated the logic to cope with a few corner cases (e.g. `NIX_BIN_DIR` exists, but no binaries are inside it).
- Loading branch information
1 parent
dba1142
commit df11dd3
Showing
14 changed files
with
158 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include "current-process.hh" | ||
#include "file-system.hh" | ||
#include "globals.hh" | ||
#include "bin-dir.hh" | ||
|
||
namespace nix { | ||
|
||
namespace fs = std::filesystem; | ||
|
||
fs::path getNixBin(std::optional<std::string_view> binaryNameOpt) | ||
{ | ||
auto getBinaryName = [&] { return binaryNameOpt ? *binaryNameOpt : "nix"; }; | ||
|
||
// If the environment variable is set, use it unconditionally | ||
if (auto envOpt = getEnvNonEmpty("NIX_BIN_DIR")) | ||
return fs::path{*envOpt} / std::string{getBinaryName()}; | ||
|
||
// Use some-times avaiable OS tricks to get to the path of this Nix, and try that | ||
if (auto selfOpt = getSelfExe()) { | ||
fs::path path{*selfOpt}; | ||
if (binaryNameOpt) | ||
path = path.parent_path() / std::string{*binaryNameOpt}; | ||
if (fs::exists(path)) | ||
return path; | ||
} | ||
|
||
// If `nix` exists at the hardcoded fallback path, use it. | ||
{ | ||
auto path = fs::path{NIX_BIN_DIR} / std::string{getBinaryName()}; | ||
if (fs::exists(path)) | ||
return path; | ||
} | ||
|
||
// return just the name, hoping the exe is on the `PATH` | ||
return getBinaryName(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
///@file | ||
|
||
#include <filesystem> | ||
|
||
namespace nix { | ||
|
||
/** | ||
* Get a path to the given Nix binary. | ||
* | ||
* Normally, nix is installed according to `NIX_BIN_DIR`, which is set | ||
* at compile time, but can be overridden. | ||
* | ||
* However, it may not have been installed at all. For example, if it's | ||
* a static build, there's a good chance that it has been moved out of | ||
* its installation directory. That makes `NIX_BIN_DIR` useless. | ||
* Instead, we'll query the OS for the path to the current executable, | ||
* using `getSelfExe()`. | ||
* | ||
* As a last resort, we resort to `PATH`. Hopefully we find a `nix` | ||
* there that's compatible. If you're porting Nix to a new platform, | ||
* that might be good enough for a while, but you'll want to improve | ||
* `getSelfExe()` to work on your platform. | ||
* | ||
* @param binary_name the exact binary name we're looking up. Might be | ||
* `nix-*` instead of `nix` for the legacy CLI commands. Optional to use | ||
* current binary name. | ||
*/ | ||
std::filesystem::path getNixBin(std::optional<std::string_view> binary_name = {}); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.