1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-19 10:50:24 -04:00

nix: add nix flake root

Nix already has a logic to go up levels and find the flake.nix. This subcommand just exposes what it finds.

Signed-off-by: lucasew <lucas59356@gmail.com>
This commit is contained in:
lucasew 2023-06-09 22:34:42 -03:00
parent 03f9ff6ea5
commit 55b691b2e4
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

@ -1343,6 +1343,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()
@ -1358,6 +1398,7 @@ struct CmdFlake : NixMultiCommand
{"archive", []() { return make_ref<CmdFlakeArchive>(); }},
{"show", []() { return make_ref<CmdFlakeShow>(); }},
{"prefetch", []() { return make_ref<CmdFlakePrefetch>(); }},
{"root", []() { return make_ref<CmdFlakeRoot>(); }},
})
{
}