forked from lix-project/lix
Merge branch 'issue-6075' of https://github.com/kamadorueda/nix
This commit is contained in:
commit
48a467f2b9
|
@ -528,6 +528,16 @@ struct CmdFlakeCheck : FlakeCommand
|
|||
}
|
||||
}
|
||||
|
||||
else if (name == "formatter") {
|
||||
state->forceAttrs(vOutput, pos);
|
||||
for (auto & attr : *vOutput.attrs) {
|
||||
checkSystemName(attr.name, *attr.pos);
|
||||
checkApp(
|
||||
fmt("%s.%s", name, attr.name),
|
||||
*attr.value, *attr.pos);
|
||||
}
|
||||
}
|
||||
|
||||
else if (name == "packages" || name == "devShells") {
|
||||
state->forceAttrs(vOutput, pos);
|
||||
for (auto & attr : *vOutput.attrs) {
|
||||
|
@ -1011,6 +1021,7 @@ struct CmdFlakeShow : FlakeCommand, MixJSON
|
|||
|| (attrPath.size() == 1 && (
|
||||
attrPath[0] == "defaultPackage"
|
||||
|| attrPath[0] == "devShell"
|
||||
|| attrPath[0] == "formatter"
|
||||
|| attrPath[0] == "nixosConfigurations"
|
||||
|| attrPath[0] == "nixosModules"
|
||||
|| attrPath[0] == "defaultApp"
|
||||
|
@ -1060,6 +1071,7 @@ struct CmdFlakeShow : FlakeCommand, MixJSON
|
|||
|
||||
else if (
|
||||
(attrPath.size() == 2 && attrPath[0] == "defaultApp") ||
|
||||
(attrPath.size() == 2 && attrPath[0] == "formatter") ||
|
||||
(attrPath.size() == 3 && attrPath[0] == "apps"))
|
||||
{
|
||||
auto aType = visitor.maybeGetAttr("type");
|
||||
|
|
53
src/nix/fmt.cc
Normal file
53
src/nix/fmt.cc
Normal file
|
@ -0,0 +1,53 @@
|
|||
#include "command.hh"
|
||||
#include "run.hh"
|
||||
|
||||
using namespace nix;
|
||||
|
||||
struct CmdFmt : SourceExprCommand {
|
||||
std::vector<std::string> args;
|
||||
|
||||
CmdFmt() { expectArgs({.label = "args", .handler = {&args}}); }
|
||||
|
||||
std::string description() override {
|
||||
return "reformat your code in the standard style";
|
||||
}
|
||||
|
||||
std::string doc() override {
|
||||
return
|
||||
#include "fmt.md"
|
||||
;
|
||||
}
|
||||
|
||||
Category category() override { return catSecondary; }
|
||||
|
||||
Strings getDefaultFlakeAttrPaths() override {
|
||||
return Strings{"formatter." + settings.thisSystem.get()};
|
||||
}
|
||||
|
||||
Strings getDefaultFlakeAttrPathPrefixes() override { return Strings{}; }
|
||||
|
||||
void run(ref<Store> store) {
|
||||
auto evalState = getEvalState();
|
||||
auto evalStore = getEvalStore();
|
||||
|
||||
auto installable = parseInstallable(store, ".");
|
||||
auto app = installable->toApp(*evalState).resolve(evalStore, store);
|
||||
|
||||
Strings programArgs{app.program};
|
||||
|
||||
// Propagate arguments from the CLI
|
||||
if (args.empty()) {
|
||||
// Format the current flake out of the box
|
||||
programArgs.push_back(".");
|
||||
} else {
|
||||
// User wants more power, let them decide which paths to include/exclude
|
||||
for (auto &i : args) {
|
||||
programArgs.push_back(i);
|
||||
}
|
||||
}
|
||||
|
||||
runProgramInStore(store, app.program, programArgs);
|
||||
};
|
||||
};
|
||||
|
||||
static auto r2 = registerCommand<CmdFmt>("fmt");
|
53
src/nix/fmt.md
Normal file
53
src/nix/fmt.md
Normal file
|
@ -0,0 +1,53 @@
|
|||
R""(
|
||||
|
||||
# Examples
|
||||
|
||||
With [nixpkgs-fmt](https://github.com/nix-community/nixpkgs-fmt):
|
||||
|
||||
```nix
|
||||
# flake.nix
|
||||
{
|
||||
outputs = { nixpkgs, self }: {
|
||||
formatter.x86_64-linux = nixpkgs.legacyPackages.x86_64-linux.nixpkgs-fmt;
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
- Format the current flake: `$ nix fmt`
|
||||
|
||||
- Format a specific folder or file: `$ nix fmt ./folder ./file.nix`
|
||||
|
||||
With [nixfmt](https://github.com/serokell/nixfmt):
|
||||
|
||||
```nix
|
||||
# flake.nix
|
||||
{
|
||||
outputs = { nixpkgs, self }: {
|
||||
formatter.x86_64-linux = nixpkgs.legacyPackages.x86_64-linux.nixfmt;
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
- Format specific files: `$ nix fmt ./file1.nix ./file2.nix`
|
||||
|
||||
With [Alejandra](https://github.com/kamadorueda/alejandra):
|
||||
|
||||
```nix
|
||||
# flake.nix
|
||||
{
|
||||
outputs = { nixpkgs, self }: {
|
||||
formatter.x86_64-linux = nixpkgs.legacyPackages.x86_64-linux.alejandra;
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
- Format the current flake: `$ nix fmt`
|
||||
|
||||
- Format a specific folder or file: `$ nix fmt ./folder ./file.nix`
|
||||
|
||||
# Description
|
||||
|
||||
`nix fmt` will rewrite all Nix files (\*.nix) to a canonical format
|
||||
using the formatter specified in your flake.
|
||||
|
||||
)""
|
28
tests/fmt.sh
Normal file
28
tests/fmt.sh
Normal file
|
@ -0,0 +1,28 @@
|
|||
source common.sh
|
||||
|
||||
set -o pipefail
|
||||
|
||||
clearStore
|
||||
rm -rf $TEST_HOME/.cache $TEST_HOME/.config $TEST_HOME/.local
|
||||
|
||||
cp ./simple.nix ./simple.builder.sh ./fmt.simple.sh ./config.nix $TEST_HOME
|
||||
|
||||
cd $TEST_HOME
|
||||
|
||||
nix fmt --help | grep "Format"
|
||||
|
||||
cat << EOF > flake.nix
|
||||
{
|
||||
outputs = _: {
|
||||
formatter.$system = {
|
||||
type = "app";
|
||||
program = ./fmt.simple.sh;
|
||||
};
|
||||
};
|
||||
}
|
||||
EOF
|
||||
nix fmt ./file ./folder | grep 'Formatting: ./file ./folder'
|
||||
nix flake check
|
||||
nix flake show | grep -P 'x86_64-linux|x86_64-darwin'
|
||||
|
||||
clearStore
|
1
tests/fmt.simple.sh
Executable file
1
tests/fmt.simple.sh
Executable file
|
@ -0,0 +1 @@
|
|||
echo Formatting: "${@}"
|
|
@ -80,6 +80,7 @@ nix_tests = \
|
|||
post-hook.sh \
|
||||
function-trace.sh \
|
||||
flake-local-settings.sh \
|
||||
fmt.sh \
|
||||
eval-store.sh \
|
||||
why-depends.sh \
|
||||
import-derivation.sh \
|
||||
|
|
Loading…
Reference in a new issue