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
Show file tree
Hide file tree
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
feat: simplifies Nixago's API interface
  • Loading branch information
jmgilman committed Jun 4, 2022
commit b24f0fc4e3ab9161cade8ed08eb73237ca88d254
172 changes: 94 additions & 78 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
plugins = self.plugins.${system};

# Test runner
runTest = import ./tests/common.nix { inherit pkgs plugins; };
runTest = import ./tests/common.nix { inherit pkgs lib plugins; };

# Helper for aggregating development tools
mkTools = tools: (builtins.listToAttrs
Expand Down Expand Up @@ -60,95 +60,111 @@
];

# Define development tool configuration
configurations = {
configurations = [
# Conform configuration
"conform" = {
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"
];
{
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"
];
};
};
};
};
}
# 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 ."
];
{
name = "just";
configData = {
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" = {
commit-msg = {
commands = {
conform = {
run = "${tools.conform.exe} enforce --commit-msg-file {1}";
{
name = "lefthook";
configData = {
commit-msg = {
commands = {
conform = {
run = "${tools.conform.exe} enforce --commit-msg-file {1}";
};
};
};
};
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}";
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}";
};
};
};
};
};
}
# Prettier
"prettier" = {
proseWrap = "always";
};
"prettier.mkIgnoreConfig" = [
".direnv"
".conform.yaml"
".prettierrc.json"
"tests"
"CHANGELOG.md"
"lefthook.yml"
];
};
{
name = "prettier";
configData = {
proseWrap = "always";
};
}
{
name = "prettier";
type = "ignore";
configData = [
".direnv"
".conform.yaml"
".prettierrc.json"
"tests"
"CHANGELOG.md"
"lefthook.yml"
];
}
];
in
rec {
# Load lib functions
Expand Down
54 changes: 1 addition & 53 deletions lib/all.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,7 @@
all:
with pkgs.lib;
let
# Create separate sets of plugin calls and plugin options
filteredOpts = filterAttrs
(path: data: (
let
parts = splitString "." path;
partsLength = builtins.length parts;
in
if partsLength > 1 then (builtins.elemAt parts 1) == "opts" else false
))
all;
filteredFuncs = filterAttrs
(path: data: (
let
parts = splitString "." path;
partsLength = builtins.length parts;
in
if partsLength > 1 then (builtins.elemAt parts 1) != "opts" else true
))
all;

# Convert option keys to `pluginName` instead of `pluginName.opts`
opts = mapAttrs'
(name: data: (
let
parts = splitString "." name;
pluginName = builtins.elemAt parts 0;
in
nameValuePair pluginName data
))
filteredOpts;

# Convert function keys to `pluginName.default` if name was omitted
funcs = mapAttrs'
(path: data: (
let
parts = splitString "." path;
partsLength = builtins.length parts;
in
nameValuePair (if partsLength > 1 then path else "${path}.default") data
))
filteredFuncs;

makeAll = path: configData: (
let
parts = splitString "." path;
make = getAttrFromPath parts plugins;
pluginName = builtins.elemAt parts 0;
in
make ({ inherit configData; }
// (optionalAttrs (opts ? "${pluginName}") opts.${pluginName}))
);

result = mapAttrsToList makeAll funcs;
result = builtins.map lib.make all;
in
{
configs = catAttrs "configFile" result;
Expand Down
8 changes: 7 additions & 1 deletion lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
{
eval = import ./eval.nix { inherit pkgs lib plugins; };

genConfig = import ./generate.nix { inherit pkgs lib plugins; };

make = import ./make.nix { inherit pkgs lib plugins; };

mkAll = import ./all.nix { inherit pkgs lib plugins; };
Expand All @@ -11,5 +13,9 @@
mkShellHook = configs:
builtins.concatStringsSep "\n" (pkgs.lib.catAttrs "shellHook" configs);

mkTemplate = import ./template.nix { inherit pkgs lib plugins; };
overrideData = config: newData:
pkgs.lib.recursiveUpdateUntil
(p: l: r: p == [ "configData" ])
config
{ configData = newData; };
}
74 changes: 74 additions & 0 deletions lib/generate.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{ pkgs, lib, plugins }:
{ config
, files
, defaultOutput
, postBuild ? ""
, shellHookExtra ? ""
, flags ? { }
}:
with pkgs.lib;
let
# Build the configuration file derivation
output = if config.output == "" then defaultOutput else config.output;
configFile = lib.eval
({
inherit (config) configData;
inherit postBuild;
inputFiles = files;
outputFile = output;
} // flags);

# Build shell hook
# This hook creates a local symlink to the file in the Nix store
linkHook = ''
# Check if the link is pointing to the existing derivation result
if readlink ${output} >/dev/null \
&& [[ $(readlink ${output}) == ${configFile} ]]; then
echo 1>&2 "nixago: ${output} link is up to date"
elif [[ -L ${output} || ! -f ${output} ]]; then
# otherwise we need to update
echo 1>&2 "nixago: ${output} link updated"

# Relink to the new result
unlink ${output} &>/dev/null
ln -s ${configFile} ${output}

# Run extra shell hook
${shellHookExtra}
else # this was an existing file
echo 1>&2 "nixago: ERROR refusing to overwrite ${output}"
fi
'';

# This hook creates a local copy of the file in the Nix store
copyHook = ''
# Check if the file exists
if [[ -f ${output} ]]; then
# Check if we need to update the local copy
cmp ${configFile} ${output} >/dev/null
if [[ $? -gt 0 ]]; then
# We need to update the local copy
echo "nixago: ${output} copy updated"
install -m 644 ${configFile} ${output}

# Run extra shell hook
${config.shellHookExtra}
else
echo "nixago: ${output} copy is up to date"
fi
else
# We need to create the first iteration of the file
echo "nixago: ${output} copy created"
install -m 644 ${configFile} ${output}

# Run extra shell hook
${config.shellHookExtra}
fi
'';

shellHook = if config.mode == "copy" then copyHook else linkHook;
in
{
inherit configFile shellHook;
}

11 changes: 6 additions & 5 deletions lib/make.nix
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
{ pkgs, lib, plugins }:
{ name, config, type ? "", output ? "", mode ? "", options ? { } }:
{ name, configData, type ? "", output ? "", mode ? "", options ? { } }:
with pkgs.lib;
let
# Build the plugin configuration
plugin = plugins.${name};
options = ({ configData = config; }
allOptions = ({ inherit configData; }
// optionalAttrs (type != "") { inherit type; }
// optionalAttrs (output != "") { inherit output; }
// optionalAttrs (mode != "") { inherit mode; }
// optionalAttrs (options != { }) { inherit options; });

config = (evalModules {
pluginConfig = (evalModules {
modules = [
../modules/plugin.nix
options
allOptions
];
}).config;
in
plugin config
plugin pluginConfig
Loading