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: improve flakes structure #108

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if ! has nix_direnv_version || ! nix_direnv_version 3.0.6; then
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.6/direnvrc" "sha256-RYcUJaRMf8oF5LznDrlCXbkOQrywm0HDv1VjYGaJGdM="
fi

use flake
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
### direnv ###
.direnv

### nix ###
result*
repl-result-*

### tree-sitter ###
build
node_modules
3 changes: 3 additions & 0 deletions .yamlfmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
formatter:
type: basic
retain_line_breaks: true
38 changes: 21 additions & 17 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
{ pkgs ? import <nixpkgs> { }
, lib ? pkgs.lib
, src ? lib.cleanSource ./.
}:
{system ? builtins.currentSystem}: let
lock = builtins.fromJSON (builtins.readFile ./flake.lock);

pkgs.tree-sitter-grammars.tree-sitter-nix.overrideAttrs (old: {
name = "tree-sitter-nix-dev";
version = "dev";
inherit src;
root = lock.nodes.${lock.root};
inherit
(lock.nodes.${root.inputs.flake-compat}.locked)
owner
repo
rev
narHash
;

doCheck = true;
checkInputs = [
pkgs.tree-sitter
pkgs.nodejs
];
checkPhase = ''
HOME=$(mktemp -d) tree-sitter test
'';
})
flake-compat = fetchTarball {
url = "https://github.com/${owner}/${repo}/archive/${rev}.tar.gz";
sha256 = narHash;
};

flake = import flake-compat {
inherit system;
src = ./.;
};
in
flake.defaultNix
16 changes: 16 additions & 0 deletions devshell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{pkgs, ...}:
pkgs.mkShell {
packages = with pkgs; [
nodejs
python3

tree-sitter
editorconfig-checker

rustc
cargo

go
gcc
];
}
39 changes: 38 additions & 1 deletion flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

183 changes: 94 additions & 89 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,114 +8,119 @@

nix-github-actions.url = "github:nix-community/nix-github-actions";
nix-github-actions.inputs.nixpkgs.follows = "nixpkgs";

flake-compat.url = "github:nix-community/flake-compat";

treefmt-nix = {
url = "github:numtide/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};

outputs = { self, nixpkgs, flake-utils, nix-github-actions }: (
(flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
inherit (pkgs) lib;
outputs = {
self,
flake-utils,
nix-github-actions,
nixpkgs,
treefmt-nix,
...
}: (
(flake-utils.lib.eachDefaultSystem (system: let
pkgs = nixpkgs.legacyPackages.${system};
inherit (pkgs) lib;
treefmtEval = treefmt-nix.lib.evalModule pkgs ./treefmt.nix;
in {
checks = let
# shellPackages = (pkgs.callPackage ./shell.nix { }).packages;
# If the generated code differs from the checked in we need
# to check in the newly generated sources.
mkCheck = name: check:
pkgs.runCommand name
{
inherit (self.devShells.${system}.default) nativeBuildInputs;
} ''
cp -rv ${self} src
chmod +w -R src
cd src

${check}

touch $out
'';
in
{
checks =
let
# shellPackages = (pkgs.callPackage ./shell.nix { }).packages;

# If the generated code differs from the checked in we need
# to check in the newly generated sources.
mkCheck = name: check: pkgs.runCommand name
{
inherit (self.devShells.${system}.default) nativeBuildInputs;
} ''
cp -rv ${self} src
chmod +w -R src
cd src

${check}

touch $out
'';
{
build = self.packages.${system}.tree-sitter-nix;

editorconfig = mkCheck "editorconfig" "editorconfig-checker";

# If the generated code differs from the checked in we need
# to check in the newly generated sources.
generated-diff = mkCheck "generated-diff" ''
HOME=. npm run generate
diff -r src/ ${self}/src
'';

treefmt = treefmtEval.config.build.check self;

rust-bindings = let
cargo' = lib.importTOML ./Cargo.toml;
in
{
build = self.packages.${system}.tree-sitter-nix;

editorconfig = mkCheck "editorconfig" "editorconfig-checker";

# If the generated code differs from the checked in we need
# to check in the newly generated sources.
generated-diff = mkCheck "generated-diff" ''
HOME=. npm run generate
diff -r src/ ${self}/src
'';

treefmt = mkCheck "treefmt" "treefmt --no-cache --fail-on-change";

rust-bindings =
let
cargo' = lib.importTOML ./Cargo.toml;
in
pkgs.rustPlatform.buildRustPackage {
pname = cargo'.package.name;
inherit (cargo'.package) version;
src = self;
cargoLock = {
lockFile = ./Cargo.lock;
};
pkgs.rustPlatform.buildRustPackage {
pname = cargo'.package.name;
inherit (cargo'.package) version;
src = self;
cargoLock = {
lockFile = ./Cargo.lock;
};

} // lib.optionalAttrs (!pkgs.stdenv.isDarwin) {
# Requires xcode
node-bindings =
let
package' = lib.importJSON ./package.json;
in
pkgs.stdenv.mkDerivation {
pname = package'.name;
inherit (package') version;
src = self;
nativeBuildInputs = with pkgs; [
importNpmLock.hooks.npmConfigHook
nodejs
nodejs.passthru.python # for node-gyp
npmHooks.npmBuildHook
npmHooks.npmInstallHook
tree-sitter
];
npmDeps = pkgs.importNpmLock {
npmRoot = ./.;
};
buildPhase = ''
runHook preBuild
${pkgs.nodePackages.node-gyp}/bin/node-gyp configure
npm run build
runHook postBuild
'';
installPhase = "touch $out";
};
}
// lib.optionalAttrs (!pkgs.stdenv.isDarwin) {
# Requires xcode
node-bindings = let
package' = lib.importJSON ./package.json;
in
pkgs.stdenv.mkDerivation {
pname = package'.name;
inherit (package') version;
src = self;
nativeBuildInputs = with pkgs; [
importNpmLock.hooks.npmConfigHook
nodejs
nodejs.passthru.python # for node-gyp
npmHooks.npmBuildHook
npmHooks.npmInstallHook
tree-sitter
];
npmDeps = pkgs.importNpmLock {
npmRoot = ./.;
};
};

packages.tree-sitter-nix = pkgs.callPackage ./default.nix { src = self; };
packages.default = self.packages.${system}.tree-sitter-nix;
devShells.default = pkgs.callPackage ./shell.nix { };
buildPhase = ''
runHook preBuild
${pkgs.nodePackages.node-gyp}/bin/node-gyp configure
npm run build
runHook postBuild
'';
installPhase = "touch $out";
};
};

formatter = pkgs.writeShellScriptBin "tree-sitter-nix-fmt" ''
exec ${pkgs.treefmt}/bin/treefmt --config-file ${./treefmt.toml} "$@"
'';
packages.tree-sitter-nix = pkgs.callPackage ./package.nix {src = self;};
packages.default = self.packages.${system}.tree-sitter-nix;

})) // {
devShells.default = pkgs.callPackage ./devshell.nix {};

formatter = treefmtEval.config.build.wrapper;
}))
// {
githubActions = nix-github-actions.lib.mkGithubMatrix {
# Inherit GHA actions matrix from a subset of platforms supported by hosted runners
checks = {
inherit (self.checks) x86_64-linux;

# Don't run linters on darwin as it's just scheduling overhead
x86_64-darwin = builtins.removeAttrs self.checks.x86_64-darwin [ "editorconfig" "generated-diff" "treefmt" ];
x86_64-darwin = builtins.removeAttrs self.checks.x86_64-darwin ["editorconfig" "generated-diff" "treefmt"];
};
};

}
);
}
6 changes: 3 additions & 3 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ module.exports = grammar({
prec(
precedence,
seq(field("operator", operator), field("argument", $._expr_op)),
),
)
),
),

Expand Down Expand Up @@ -234,7 +234,7 @@ module.exports = grammar({
field("operator", operator),
field("right", $._expr_op),
),
),
)
),
// right assoc.
...[
Expand All @@ -249,7 +249,7 @@ module.exports = grammar({
field("operator", operator),
field("right", $._expr_op),
),
),
)
),
),

Expand Down
19 changes: 19 additions & 0 deletions package.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
pkgs ? import <nixpkgs> {},
lib ? pkgs.lib,
src ? lib.cleanSource ./.,
}:
pkgs.tree-sitter-grammars.tree-sitter-nix.overrideAttrs (_old: {
name = "tree-sitter-nix-dev";
version = "dev";
inherit src;

doCheck = true;
checkInputs = [
pkgs.tree-sitter
pkgs.nodejs
];
checkPhase = ''
HOME=$(mktemp -d) tree-sitter test
'';
})
Loading
Loading