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:
parent
03f9ff6ea5
commit
55b691b2e4
34
src/nix/flake-root.md
Normal file
34
src/nix/flake-root.md
Normal 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.
|
||||||
|
|
||||||
|
)""
|
|
@ -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
|
struct CmdFlake : NixMultiCommand
|
||||||
{
|
{
|
||||||
CmdFlake()
|
CmdFlake()
|
||||||
|
@ -1358,6 +1398,7 @@ struct CmdFlake : NixMultiCommand
|
||||||
{"archive", []() { return make_ref<CmdFlakeArchive>(); }},
|
{"archive", []() { return make_ref<CmdFlakeArchive>(); }},
|
||||||
{"show", []() { return make_ref<CmdFlakeShow>(); }},
|
{"show", []() { return make_ref<CmdFlakeShow>(); }},
|
||||||
{"prefetch", []() { return make_ref<CmdFlakePrefetch>(); }},
|
{"prefetch", []() { return make_ref<CmdFlakePrefetch>(); }},
|
||||||
|
{"root", []() { return make_ref<CmdFlakeRoot>(); }},
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue