This commit is contained in:
Lucas Eduardo 2025-04-14 00:05:14 -05:00 committed by GitHub
commit 88e5122784
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 75 additions and 0 deletions

34
src/nix/flake-root.md Normal file
View File

@ -0,0 +1,34 @@
R""(
# Limitations
- This subcommand doesn't support codebases that keep the flake.nix in a subdirectory.
# Examples
* Get the root folder of a codebase with the shell in folder /path/to/folder and flake.nix in /path/to:
```console
/path/to/folder$ nix flake root
/path/to
/path/to/folder$ nix flake root -r
path:/tmp/eoq
```
* Get the root folder of a codebase with the shell in folder /path/to/folder, a flake.nix in /path/to and a git repo initialized
```console
/path/to/folder$ nix flake root
/path/to
/path/to/folder$ nix flake root -r
git+file:///path/to
```
# Description
This command uses the logic used to find flake.nix for commands
such as `nix build` and shows the absolute path, or optionally,
the flake reference.
)""

View File

@ -1514,6 +1514,46 @@ struct CmdFlakePrefetch : FlakeCommand, MixJSON
}
};
struct CmdFlakeRoot : FlakeCommand
{
bool asRef = true;
CmdFlakeRoot()
{
addFlag({
.longName = "as-ref",
.shortName = 'r',
.description = "Show the root as a flakeref in URL-like representation.",
.handler = {&asRef, false}
});
}
std::string description() override
{
return "get the root directory of a flake";
}
std::string doc() override
{
return
#include "flake-root.md"
;
}
void run(nix::ref<nix::Store> store) override
{
std::string rootRef = getFlakeRef().to_string();
if (asRef) {
int slashIndex = rootRef.find('/');
while (rootRef[slashIndex + 1] == '/') {
slashIndex++;
}
rootRef = rootRef.substr(slashIndex);
}
std::cout << rootRef << std::endl;
}
};
struct CmdFlake : NixMultiCommand
{
CmdFlake()
@ -1531,6 +1571,7 @@ struct CmdFlake : NixMultiCommand
{"archive", []() { return make_ref<CmdFlakeArchive>(); }},
{"show", []() { return make_ref<CmdFlakeShow>(); }},
{"prefetch", []() { return make_ref<CmdFlakePrefetch>(); }},
{"root", []() { return make_ref<CmdFlakeRoot>(); }},
})
{
}