Home

helix.drv @ca81e960f7faf55a686e2d5cb24e40840e85cc3d - refs - log -
-
https://git.jolheiser.com/helix.drv.git
My Helix configuration as a Nix derivation
helix.drv / default.nix
- raw -
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
{
  pkgs ? import <nixpkgs> { },
}:
let
  tomlFormat = pkgs.formats.toml { };
  config = import ./config { pkgs = pkgs; };
  buildGrammar =
    grammar:
    let
      source = pkgs.fetchgit {
        inherit (grammar) url rev sha256;
      };
      linkQueries = pkgs.lib.optionalString (builtins.hasAttr "queries" grammar) "cp -r ${source}/${grammar.queries} $out/queries";
    in
    pkgs.stdenv.mkDerivation {
      pname = "helix-tree-sitter-grammar-${grammar.name}";
      version = grammar.rev;
      buildInputs = [
        pkgs.helix
        pkgs.git
      ];
      src = source;
      dontInstall = true;
      buildPhase = ''
        runHook preBuild

        mkdir .helix
        cat << EOF > .helix/languages.toml
        use-grammars = { only = ["${grammar.name}"] }
        [[grammar]]
        name = "${grammar.name}"
        source = { git = "${grammar.url}", rev = "${grammar.rev}" }
        EOF

        mkdir -p runtime/grammars/sources
        cp -r ${source} runtime/grammars/sources/${grammar.name}
        export CARGO_MANIFEST_DIR=$(pwd)/.helix

        #hx -g fetch
        hx -g build

        mkdir $out
        cp runtime/grammars/${grammar.name}.so $out/${grammar.name}.so
        ${linkQueries}

        runHook postBuild
      '';
    };
  builtGrammars = builtins.map (grammar: {
    inherit (grammar) name;
    artifact = buildGrammar grammar;
  }) config.grammars;
  ignoreFile = pkgs.writeText "ignore" (builtins.concatStringsSep "\n" config.ignore);
  configFile = pkgs.writeText "config.toml" (
    builtins.readFile (tomlFormat.generate "helix-config" config.settings)
  );
  languageFile = pkgs.writeText "languages.toml" (
    builtins.readFile (tomlFormat.generate "helix-languages" config.languages)
  );
  themeFiles = pkgs.lib.mapAttrsToList (name: value: {
    inherit name;
    file = pkgs.writeText "${name}.toml" (
      builtins.readFile (tomlFormat.generate "helix-theme-${name}" value)
    );
  }) config.themes;
  themeLinks = builtins.map (
    theme: "ln -s ${theme.file} $out/home/helix/themes/${theme.name}.toml"
  ) themeFiles;
  grammarLinks = builtins.map (
    grammar: "ln -s ${grammar.artifact}/${grammar.name}.so $out/lib/runtime/grammars/${grammar.name}.so"
  ) builtGrammars;
  queryLinks = builtins.map (
    grammar: "ln -s ${grammar.artifact}/queries $out/lib/runtime/queries/${grammar.name}"
  ) builtGrammars;
  helix =
    pkgs.runCommand "hx"
      {
        buildInputs = with pkgs; [
          makeWrapper
        ];
      }
      ''
        mkdir $out
        ln -s ${pkgs.helix}/* $out
        rm $out/bin

        rm $out/lib
        mkdir -p $out/lib/runtime
        ln -s ${pkgs.helix}/lib/runtime/* $out/lib/runtime

        rm $out/lib/runtime/grammars
        mkdir $out/lib/runtime/grammars
        ln -s ${pkgs.helix}/lib/runtime/grammars/* $out/lib/runtime/grammars
        ${builtins.concatStringsSep "\n" grammarLinks}

        rm $out/lib/runtime/queries
        mkdir $out/lib/runtime/queries
        ln -s ${pkgs.helix}/lib/runtime/queries/* $out/lib/runtime/queries
        ${builtins.concatStringsSep "\n" queryLinks}

        mkdir -p $out/home/helix/themes
        ln -s ${configFile} $out/home/helix/config.toml
        ln -s ${languageFile} $out/home/helix/languages.toml
        ${builtins.concatStringsSep "\n" themeLinks}

        mkdir -p $out/home/git
        ln -s ${ignoreFile} $out/home/git/ignore

        makeWrapper ${pkgs.helix}/bin/hx $out/bin/hx \
          --set HELIX_RUNTIME $out/lib/runtime \
          --set XDG_CONFIG_HOME $out/home \
      '';
in
{
  inherit helix;
  default = helix;
}