Skip to content

Commit

Permalink
chore(nix): format nix files with alejandra, update deps, minor code …
Browse files Browse the repository at this point in the history
…refactors (helix-editor#2683)
  • Loading branch information
yusdacra authored Jun 6, 2022
1 parent f0d1c85 commit bb83ea8
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 122 deletions.
65 changes: 24 additions & 41 deletions flake.lock

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

99 changes: 66 additions & 33 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,85 @@
};
};

outputs = inputs@{ nixCargoIntegration, ... }:
outputs = inputs @ {
nixpkgs,
nixCargoIntegration,
...
}:
nixCargoIntegration.lib.makeOutputs {
root = ./.;
renameOutputs = { "helix-term" = "helix"; };
renameOutputs = {"helix-term" = "helix";};
# Set default app to hx (binary is from helix-term release build)
# Set default package to helix-term release build
defaultOutputs = {
app = "hx";
package = "helix";
};
overrides = {
cCompiler = common: if common.pkgs.stdenv.isLinux then common.pkgs.gcc else common.pkgs.clang;
cCompiler = common:
with common.pkgs;
if stdenv.isLinux
then gcc
else clang;
crateOverrides = common: _: {
helix-term = prev:
let
inherit (common) pkgs;
grammars = pkgs.callPackage ./grammars.nix { };
runtimeDir = pkgs.runCommand "helix-runtime" { } ''
mkdir -p $out
ln -s ${common.root}/runtime/* $out
rm -r $out/grammars
ln -s ${grammars} $out/grammars
'';
in
{
# disable fetching and building of tree-sitter grammars in the helix-term build.rs
HELIX_DISABLE_AUTO_GRAMMAR_BUILD = "1";
# link languages and theme toml files since helix-term expects them (for tests)
preConfigure = "ln -s ${common.root}/{languages.toml,theme.toml,base16_theme.toml} ..";
buildInputs = (prev.buildInputs or [ ]) ++ [ common.cCompiler.cc.lib ];
nativeBuildInputs = [ pkgs.makeWrapper ];
helix-term = prev: let
inherit (common) pkgs;
mkRootPath = rel:
builtins.path {
path = "${common.root}/${rel}";
name = rel;
};
grammars = pkgs.callPackage ./grammars.nix {};
runtimeDir = pkgs.runCommandNoCC "helix-runtime" {} ''
mkdir -p $out
ln -s ${mkRootPath "runtime"}/* $out
rm -r $out/grammars
ln -s ${grammars} $out/grammars
'';
in {
# disable fetching and building of tree-sitter grammars in the helix-term build.rs
HELIX_DISABLE_AUTO_GRAMMAR_BUILD = "1";
# link languages and theme toml files since helix-term expects them (for tests)
preConfigure =
pkgs.lib.concatMapStringsSep
"\n"
(path: "ln -sf ${mkRootPath path} ..")
["languages.toml" "theme.toml" "base16_theme.toml"];
buildInputs = (prev.buildInputs or []) ++ [common.cCompiler.cc.lib];
nativeBuildInputs = [pkgs.makeWrapper];

postFixup = ''
if [ -f "$out/bin/hx" ]; then
wrapProgram "$out/bin/hx" --set HELIX_RUNTIME "${runtimeDir}"
fi
'';
};
postFixup = ''
if [ -f "$out/bin/hx" ]; then
wrapProgram "$out/bin/hx" --set HELIX_RUNTIME "${runtimeDir}"
fi
'';
};
};
shell = common: prev: {
packages = prev.packages ++ (with common.pkgs; [ lld_13 lldb cargo-tarpaulin cargo-flamegraph ]);
env = prev.env ++ [
{ name = "HELIX_RUNTIME"; eval = "$PWD/runtime"; }
{ name = "RUST_BACKTRACE"; value = "1"; }
{ name = "RUSTFLAGS"; value = if common.pkgs.stdenv.isLinux then "-C link-arg=-fuse-ld=lld -C target-cpu=native -Clink-arg=-Wl,--no-rosegment" else ""; }
];
packages =
prev.packages
++ (
with common.pkgs; [lld_13 lldb cargo-tarpaulin cargo-flamegraph]
);
env =
prev.env
++ [
{
name = "HELIX_RUNTIME";
eval = "$PWD/runtime";
}
{
name = "RUST_BACKTRACE";
value = "1";
}
{
name = "RUSTFLAGS";
value =
if common.pkgs.stdenv.isLinux
then "-C link-arg=-fuse-ld=lld -C target-cpu=native -Clink-arg=-Wl,--no-rosegment"
else "";
}
];
};
};
};
Expand Down
96 changes: 54 additions & 42 deletions grammars.nix
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
{ stdenv, lib, runCommand, yj }:
let
{
stdenv,
lib,
runCommandLocal,
runCommandNoCC,
yj,
}: let
# HACK: nix < 2.6 has a bug in the toml parser, so we convert to JSON
# before parsing
languages-json = runCommand "languages-toml-to-json" { } ''
languages-json = runCommandLocal "languages-toml-to-json" {} ''
${yj}/bin/yj -t < ${./languages.toml} > $out
'';
languagesConfig = if lib.versionAtLeast builtins.nixVersion "2.6.0" then
builtins.fromTOML (builtins.readFile ./languages.toml)
else
builtins.fromJSON (builtins.readFile (builtins.toPath languages-json));
isGitGrammar = (grammar:
builtins.hasAttr "source" grammar && builtins.hasAttr "git" grammar.source
&& builtins.hasAttr "rev" grammar.source);
languagesConfig =
if lib.versionAtLeast builtins.nixVersion "2.6.0"
then builtins.fromTOML (builtins.readFile ./languages.toml)
else builtins.fromJSON (builtins.readFile (builtins.toPath languages-json));
isGitGrammar = grammar:
builtins.hasAttr "source" grammar
&& builtins.hasAttr "git" grammar.source
&& builtins.hasAttr "rev" grammar.source;
isGitHubGrammar = grammar: lib.hasPrefix "https://github.com" grammar.source.git;
toGitHubFetcher = url: let
match = builtins.match "https://github\.com/([^/]*)/([^/]*)/?" url;
Expand All @@ -20,33 +26,36 @@ let
repo = builtins.elemAt match 1;
};
gitGrammars = builtins.filter isGitGrammar languagesConfig.grammar;
buildGrammar = grammar:
let
gh = toGitHubFetcher grammar.source.git;
sourceGit = builtins.fetchTree {
type = "git";
url = grammar.source.git;
rev = grammar.source.rev;
ref = grammar.source.ref or "HEAD";
shallow = true;
};
sourceGitHub = builtins.fetchTree {
type = "github";
owner = gh.owner;
repo = gh.repo;
inherit (grammar.source) rev;
};
source = if isGitHubGrammar grammar then sourceGitHub else sourceGit;
in stdenv.mkDerivation rec {
buildGrammar = grammar: let
gh = toGitHubFetcher grammar.source.git;
sourceGit = builtins.fetchTree {
type = "git";
url = grammar.source.git;
rev = grammar.source.rev;
ref = grammar.source.ref or "HEAD";
shallow = true;
};
sourceGitHub = builtins.fetchTree {
type = "github";
owner = gh.owner;
repo = gh.repo;
inherit (grammar.source) rev;
};
source =
if isGitHubGrammar grammar
then sourceGitHub
else sourceGit;
in
stdenv.mkDerivation rec {
# see https://github.com/NixOS/nixpkgs/blob/fbdd1a7c0bc29af5325e0d7dd70e804a972eb465/pkgs/development/tools/parsing/tree-sitter/grammar.nix

pname = "helix-tree-sitter-${grammar.name}";
version = grammar.source.rev;

src = if builtins.hasAttr "subpath" grammar.source then
"${source}/${grammar.source.subpath}"
else
source;
src =
if builtins.hasAttr "subpath" grammar.source
then "${source}/${grammar.source.subpath}"
else source;

dontUnpack = true;
dontConfigure = true;
Expand Down Expand Up @@ -93,14 +102,17 @@ let
runHook postFixup
'';
};
builtGrammars = builtins.map (grammar: {
inherit (grammar) name;
artifact = buildGrammar grammar;
}) gitGrammars;
grammarLinks = builtins.map (grammar:
"ln -s ${grammar.artifact}/${grammar.name}.so $out/${grammar.name}.so")
builtGrammars =
builtins.map (grammar: {
inherit (grammar) name;
artifact = buildGrammar grammar;
})
gitGrammars;
grammarLinks =
builtins.map (grammar: "ln -s ${grammar.artifact}/${grammar.name}.so $out/${grammar.name}.so")
builtGrammars;
in runCommand "consolidated-helix-grammars" { } ''
mkdir -p $out
${builtins.concatStringsSep "\n" grammarLinks}
''
in
runCommandNoCC "consolidated-helix-grammars" {} ''
mkdir -p $out
${builtins.concatStringsSep "\n" grammarLinks}
''
11 changes: 5 additions & 6 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Flake's devShell for non-flake-enabled nix instances
let
compat =
builtins.fetchGit {
url = "https://github.com/edolstra/flake-compat.git";
rev = "b4a34015c698c7793d592d66adbab377907a2be8";
};
compat = builtins.fetchGit {
url = "https://github.com/edolstra/flake-compat.git";
rev = "b4a34015c698c7793d592d66adbab377907a2be8";
};
in
(import compat { src = ./.; }).shellNix.default
(import compat {src = ./.;}).shellNix.default

0 comments on commit bb83ea8

Please sign in to comment.