Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: standardizes the plugin interface #10

Merged
merged 14 commits into from
Jun 7, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
docs: updates quick start with API changes
  • Loading branch information
jmgilman committed Jun 5, 2022
commit a54817c878e9bf3a18b0fcff4ea6050516008026
184 changes: 91 additions & 93 deletions docs/src/quick_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ Choose a plugin to use. The below example uses the plugin for [pre-commit][1]:
};
};

preCommit = nixago.plugins.pre-commit.mkLocalConfig {
preCommit = nixago.lib.make {
name = "pre-commit";
type = "simple";
configData = preCommitConfig;
};
# ...
Expand Down Expand Up @@ -73,6 +75,25 @@ repos:
repo: local
```

## Changing Types

Some plugins can generate different types of configuration files. See the
documentation for each plugin for more information. Our previous example used
the `simple` type provided by the pre-commit plugin. This type allows passing a
simplified configuration that is geared towards executing local hooks. Another
example is the plugin for prettier, which can create regular configuration files
as well as ignore files:

```nix
{
prettier = nixago.lib.make {
name = "prettier";
configData = ["file1.txt" "file2.yml" ];
type = "ignore";
};
}
```

## Changing Output Path

By default, the shell hook for each plugin will generate the configuration file
Expand All @@ -90,7 +111,8 @@ The file name and relative location can be modified:
};
};

preCommit = nixago.plugins.pre-commit.mkLocalConfig {
preCommit = nixago.lib.make {
name = "pre-commit";
configData = preCommitConfig;
output = ".config/pre-commit-config.yaml";
};
Expand All @@ -103,9 +125,9 @@ The above example would place the configuration file in

## Changing Generation Mode

By default, the shell hook manages a symbolic link from the Nix store to the
output path. It automatically synchronizes any changes by updating the link if
the generated configuration file changes. This mode can be altered to instead
The shell hook manages a symbolic link from the Nix store to the output path by
default. It automatically synchronizes any changes by updating the link if the
generated configuration file changes. This mode can be altered to instead
maintain a local copy of the generated configuration file. In this mode, the
shell hook compares the contents of the local copy to the one in the Nix store
and updates it accordingly. The primary benefit of this change is that it allows
Expand All @@ -123,7 +145,8 @@ edited locally.
};
};

preCommit = nixago.plugins.pre-commit.mkLocalConfig {
preCommit = nixago.lib.make {
name = "pre-commit";
configData = preCommitConfig;
mode = "copy";
};
Expand All @@ -139,103 +162,78 @@ following is an excerpt from the `flake.nix` that manages this project:
```nix
{
# Define development tool configuration
configurations = {
# Just configuration
"just" = {
tasks = {
check = [
"@${tools.nixpkgs-fmt.exe} --check flake.nix $(git ls-files '**/*.nix')"
"@${tools.prettier.exe} --check ."
"@${tools.typos.exe}"
"@nix flake check"
];
check-docs = [
"@${tools.typos.exe}"
];
make-docs = [
"@cd docs && mdbook build"
];
fmt = [
"@${tools.nixpkgs-fmt.exe} flake.nix $(git ls-files '**/*.nix')"
"@${tools.prettier.exe} -w ."
];
};
};
# Lefthook configuration
"lefthook" = {
pre-commit = {
commands = {
nixpkgs-fmt = {
run = "${tools.nixpkgs-fmt.exe} --check {staged_files}";
glob = "*.nix";
};
prettier = {
run = "${tools.prettier.exe} --check {staged_files}";
glob = "*.{yaml,yml,md}";
};
typos = {
run = "${tools.typos.exe} {staged_files}";
};
};
configurations = [
# Conform configuration
{
name = "conform";
configData = {
commit = {
header = { length = 89; };
conventional = {
types = [
"build"
"chore"
"ci"
"docs"
"feat"
"fix"
"perf"
"refactor"
"style"
"test"
];
scopes = [
"conform"
"just"
"lefthook"
"pre-commit"
"prettier"
"core"
"flake"
];
};
};
# Prettier
"prettier.mkIgnoreConfig" = [
".direnv"
"tests"
"lefthook.yml"
];
};
};
}

# ...
# ....

# Local development shell
devShells = {
default = pkgs.mkShell {
shellHook = (lib.mkAll configurations).shellHook;
packages = tools.all;
};
# Prettier
{
name = "prettier";
configData = {
proseWrap = "always";
};
}
```

The input to `mkAll` expects an attribute set where the name is one of the
following:

1. A path to the function to be called, relative to the `plugins` set (i.e.,
`prettier.mkIgnoreConfig`)
2. The name of a plugin with `.opts` appended (i.e., `prettier.opts`)
}
{
name = "prettier";
type = "ignore";
configData = [
".direnv"
".conform.yaml"
".prettierrc.json"
"tests"
"CHANGELOG.md"
"lefthook.yml"
];
}
];

Appending the function name after the plugin name is optional. In this case, the
`mkAll` function will call the `default` function. This function is specific to
each plugin; refer to the source code for what is called.

The value of the attribute should be the raw configuration data that will be
passed to the function. All plugin functions take a `configData` argument which
contains the configuration data. This argument is supplied with the value of the
attribute.

The `default` function will be called from the plugin if none is provided. See
the individual plugins for which function this is. Most plugin functions accept
additional arguments beyond `configData`. To pass other arguments using the
`mkAll` function, you must add an extra attribute in the format of
`pluginName.opts`. The value of this attribute should be a set that will be
merged into the final call to the plugin function. The below example changes the
mode of the Prettier plugin:
# ...

```nix
{
# ...
"prettier.opts" = {
mode = "copy";
# Local development shell
devShells = {
default = pkgs.mkShell {
shellHook = (lib.mkAll configurations).shellHook;
packages = tools.all;
};
# ...
};
}
```

The second parameter is the configuration to be passed. The result is an
attribute set with a unified `shellHook` attribute that contains all of the
logic for managing the configurations. This result makes adding additional
configuration files to your existing projects seamless.
The input to `mkAll` is a list of configurations. The attribute sets in the list
match the same arguments that the `lib.make` function accepts. The only
difference is that `mkAll` returns a list of the created derivations and a
single `shellHook` encompassing all generated configuration files.

[1]: https://pre-commit.com/