on 376c19e help sign in
helix/flake.nix
171 lines 5.7 kB history raw
1{
2 description = "A post-modern text editor.";
4 inputs = {
5 nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
6 rust-overlay = {
7 url = "github:oxalica/rust-overlay";
8 inputs.nixpkgs.follows = "nixpkgs";
9 };
10 nci = {
11 url = "github:yusdacra/nix-cargo-integration";
12 inputs.nixpkgs.follows = "nixpkgs";
13 inputs.rust-overlay.follows = "rust-overlay";
14 };
15 parts.url = "github:hercules-ci/flake-parts";
16 };
18 outputs = inp: let
19 mkRootPath = rel:
20 builtins.path {
21 path = "${toString ./.}/${rel}";
22 name = rel;
23 };
24 filteredSource = let
25 pathsToIgnore = [
26 ".envrc"
27 ".ignore"
28 ".github"
29 ".gitignore"
30 "logo.svg"
31 "logo_dark.svg"
32 "logo_light.svg"
33 "rust-toolchain.toml"
34 "rustfmt.toml"
35 "runtime"
36 "screenshot.png"
37 "book"
38 "contrib"
39 "docs"
40 "README.md"
41 "CHANGELOG.md"
42 "shell.nix"
43 "default.nix"
44 "grammars.nix"
45 "flake.nix"
46 "flake.lock"
47 ];
48 ignorePaths = path: type: let
49 inherit (inp.nixpkgs) lib;
50 # split the nix store path into its components
51 components = lib.splitString "/" path;
52 # drop off the `/nix/hash-source` section from the path
53 relPathComponents = lib.drop 4 components;
54 # reassemble the path components
55 relPath = lib.concatStringsSep "/" relPathComponents;
56 in
57 lib.all (p: ! (lib.hasPrefix p relPath)) pathsToIgnore;
58 in
59 builtins.path {
60 name = "helix-source";
61 path = toString ./.;
62 # filter out unnecessary paths
63 filter = ignorePaths;
64 };
65 in
66 inp.parts.lib.mkFlake {inputs = inp;} {
67 imports = [inp.nci.flakeModule];
68 systems = [
69 "x86_64-linux"
70 "x86_64-darwin"
71 "aarch64-linux"
72 "aarch64-darwin"
73 "i686-linux"
74 ];
75 perSystem = {
76 config,
77 pkgs,
78 lib,
79 ...
80 }: let
81 makeOverridableHelix = old: config: let
82 grammars = pkgs.callPackage ./grammars.nix config;
83 runtimeDir = pkgs.runCommand "helix-runtime" {} ''
84 mkdir -p $out
85 ln -s ${mkRootPath "runtime"}/* $out
86 rm -r $out/grammars
87 ln -s ${grammars} $out/grammars
88 '';
89 helix-wrapped =
90 pkgs.runCommand
91 old.name
92 {
93 inherit (old) pname version;
94 meta = old.meta or {};
95 passthru =
96 (old.passthru or {})
97 // {
98 unwrapped = old;
99 };
100 nativeBuildInputs = [pkgs.makeWrapper];
101 makeWrapperArgs = config.makeWrapperArgs or [];
102 }
103 ''
104 cp -rs --no-preserve=mode,ownership ${old} $out
105 wrapProgram "$out/bin/hx" ''${makeWrapperArgs[@]} --set HELIX_RUNTIME "${runtimeDir}"
106 '';
107 in
108 helix-wrapped
109 // {
110 override = makeOverridableHelix old;
111 passthru =
112 helix-wrapped.passthru
113 // {
114 wrapper = old: makeOverridableHelix old config;
115 };
116 };
117 stdenv =
118 if pkgs.stdenv.isLinux
119 then pkgs.stdenv
120 else pkgs.clangStdenv;
121 rustFlagsEnv =
122 if stdenv.isLinux
123 then ''$RUSTFLAGS -C link-arg=-fuse-ld=lld -C target-cpu=native -Clink-arg=-Wl,--no-rosegment''
124 else "$RUSTFLAGS";
125 in {
126 # by default NCI adds rust-analyzer component, but helix toolchain doesn't have rust-analyzer
127 nci.toolchains.shell.components = ["rust-src" "rustfmt" "clippy"];
128 nci.projects."helix-project".relPath = "";
129 nci.crates."helix-term" = {
130 overrides = {
131 add-meta.override = _: {meta.mainProgram = "hx";};
132 add-inputs.overrideAttrs = prev: {
133 buildInputs = (prev.buildInputs or []) ++ [stdenv.cc.cc.lib];
134 };
135 disable-grammar-builds = {
136 # disable fetching and building of tree-sitter grammars in the helix-term build.rs
137 HELIX_DISABLE_AUTO_GRAMMAR_BUILD = "1";
138 };
139 disable-tests = {checkPhase = ":";};
140 set-stdenv.override = _: {inherit stdenv;};
141 set-filtered-src.override = _: {src = filteredSource;};
142 };
143 };
145 packages.helix-unwrapped = config.nci.outputs."helix-term".packages.release;
146 packages.helix-unwrapped-dev = config.nci.outputs."helix-term".packages.dev;
147 packages.helix = makeOverridableHelix config.packages.helix-unwrapped {};
148 packages.helix-dev = makeOverridableHelix config.packages.helix-unwrapped-dev {};
149 packages.default = config.packages.helix;
151 devShells.default = config.nci.outputs."helix-project".devShell.overrideAttrs (old: {
152 nativeBuildInputs =
153 (old.nativeBuildInputs or [])
154 ++ (with pkgs; [lld_13 cargo-flamegraph rust-analyzer])
155 ++ (lib.optional (stdenv.isx86_64 && stdenv.isLinux) pkgs.cargo-tarpaulin)
156 ++ (lib.optional stdenv.isLinux pkgs.lldb)
157 ++ (lib.optional stdenv.isDarwin pkgs.darwin.apple_sdk.frameworks.CoreFoundation);
158 shellHook = ''
159 export HELIX_RUNTIME="$PWD/runtime"
160 export RUST_BACKTRACE="1"
161 export RUSTFLAGS="${rustFlagsEnv}"
162 '';
163 });
164 };
165 };
167 nixConfig = {
168 extra-substituters = ["https://helix.cachix.org"];
169 extra-trusted-public-keys = ["helix.cachix.org-1:ejp9KQpR1FBI2onstMQ34yogDm4OgU2ru6lIwPvuCVs="];
170 };