fork/helix

git clone https://git.julia.blue/fork/helix

0rpheeMake git revision available for nix flake builds (#12331)7b9b9329

25.01.1
6.5 KiB195 linesraw
1{
2  description = "A post-modern text editor.";
3
4  inputs = {
5    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
6    flake-utils.url = "github:numtide/flake-utils";
7    rust-overlay = {
8      url = "github:oxalica/rust-overlay";
9      inputs.nixpkgs.follows = "nixpkgs";
10    };
11    crane.url = "github:ipetkov/crane";
12  };
13
14  outputs = {
15    self,
16    nixpkgs,
17    crane,
18    flake-utils,
19    rust-overlay,
20    ...
21  }:
22    flake-utils.lib.eachDefaultSystem (system: let
23      pkgs = import nixpkgs {
24        inherit system;
25        overlays = [(import rust-overlay)];
26      };
27      mkRootPath = rel:
28        builtins.path {
29          path = "${toString ./.}/${rel}";
30          name = rel;
31        };
32      filteredSource = let
33        pathsToIgnore = [
34          ".envrc"
35          ".ignore"
36          ".github"
37          ".gitignore"
38          "logo_dark.svg"
39          "logo_light.svg"
40          "rust-toolchain.toml"
41          "rustfmt.toml"
42          "runtime"
43          "screenshot.png"
44          "book"
45          "docs"
46          "README.md"
47          "CHANGELOG.md"
48          "shell.nix"
49          "default.nix"
50          "grammars.nix"
51          "flake.nix"
52          "flake.lock"
53        ];
54        ignorePaths = path: type: let
55          inherit (nixpkgs) lib;
56          # split the nix store path into its components
57          components = lib.splitString "/" path;
58          # drop off the `/nix/hash-source` section from the path
59          relPathComponents = lib.drop 4 components;
60          # reassemble the path components
61          relPath = lib.concatStringsSep "/" relPathComponents;
62        in
63          lib.all (p: ! (lib.hasPrefix p relPath)) pathsToIgnore;
64      in
65        builtins.path {
66          name = "helix-source";
67          path = toString ./.;
68          # filter out unnecessary paths
69          filter = ignorePaths;
70        };
71      makeOverridableHelix = old: config: let
72        grammars = pkgs.callPackage ./grammars.nix config;
73        runtimeDir = pkgs.runCommand "helix-runtime" {} ''
74          mkdir -p $out
75          ln -s ${mkRootPath "runtime"}/* $out
76          rm -r $out/grammars
77          ln -s ${grammars} $out/grammars
78        '';
79        helix-wrapped =
80          pkgs.runCommand
81          old.name
82          {
83            inherit (old) pname version;
84            meta = old.meta or {};
85            passthru =
86              (old.passthru or {})
87              // {
88                unwrapped = old;
89              };
90            nativeBuildInputs = [pkgs.makeWrapper];
91            makeWrapperArgs = config.makeWrapperArgs or [];
92          }
93          ''
94            cp -rs --no-preserve=mode,ownership ${old} $out
95            wrapProgram "$out/bin/hx" ''${makeWrapperArgs[@]} --set HELIX_RUNTIME "${runtimeDir}"
96          '';
97      in
98        helix-wrapped
99        // {
100          override = makeOverridableHelix old;
101          passthru =
102            helix-wrapped.passthru
103            // {
104              wrapper = old: makeOverridableHelix old config;
105            };
106        };
107      stdenv =
108        if pkgs.stdenv.isLinux
109        then pkgs.stdenv
110        else pkgs.clangStdenv;
111      rustFlagsEnv = pkgs.lib.optionalString stdenv.isLinux "-C link-arg=-fuse-ld=lld -C target-cpu=native -Clink-arg=-Wl,--no-rosegment --cfg tokio_unstable";
112      rustToolchain = pkgs.pkgsBuildHost.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
113      craneLibMSRV = (crane.mkLib pkgs).overrideToolchain rustToolchain;
114      craneLibStable = (crane.mkLib pkgs).overrideToolchain pkgs.pkgsBuildHost.rust-bin.stable.latest.default;
115      commonArgs = {
116        inherit stdenv;
117        inherit (craneLibMSRV.crateNameFromCargoToml {cargoToml = ./helix-term/Cargo.toml;}) pname;
118        inherit (craneLibMSRV.crateNameFromCargoToml {cargoToml = ./Cargo.toml;}) version;
119        src = filteredSource;
120        # disable fetching and building of tree-sitter grammars in the helix-term build.rs
121        HELIX_DISABLE_AUTO_GRAMMAR_BUILD = "1";
122        buildInputs = [stdenv.cc.cc.lib];
123        nativeBuildInputs = [pkgs.installShellFiles];
124        # disable tests
125        doCheck = false;
126        meta.mainProgram = "hx";
127      };
128      cargoArtifacts = craneLibMSRV.buildDepsOnly commonArgs;
129    in {
130      packages = {
131        helix-unwrapped = craneLibStable.buildPackage (commonArgs
132          // {
133            cargoArtifacts = craneLibStable.buildDepsOnly commonArgs;
134            postInstall = ''
135              mkdir -p $out/share/applications $out/share/icons/hicolor/scalable/apps $out/share/icons/hicolor/256x256/apps
136              cp contrib/Helix.desktop $out/share/applications
137              cp logo.svg $out/share/icons/hicolor/scalable/apps/helix.svg
138              cp contrib/helix.png $out/share/icons/hicolor/256x256/apps
139              installShellCompletion contrib/completion/hx.{bash,fish,zsh}
140            '';
141            # set git revision for nix flake builds, see 'git_hash' in helix-loader/build.rs
142            HELIX_NIX_BUILD_REV = self.rev or self.dirtyRev or null;
143          });
144        helix = makeOverridableHelix self.packages.${system}.helix-unwrapped {};
145        default = self.packages.${system}.helix;
146      };
147
148      checks = {
149        # Build the crate itself
150        inherit (self.packages.${system}) helix;
151
152        clippy = craneLibMSRV.cargoClippy (commonArgs
153          // {
154            inherit cargoArtifacts;
155            cargoClippyExtraArgs = "--all-targets -- --deny warnings";
156          });
157
158        fmt = craneLibMSRV.cargoFmt commonArgs;
159
160        doc = craneLibMSRV.cargoDoc (commonArgs
161          // {
162            inherit cargoArtifacts;
163          });
164
165        test = craneLibMSRV.cargoTest (commonArgs
166          // {
167            inherit cargoArtifacts;
168          });
169      };
170
171      devShells.default = pkgs.mkShell {
172        inputsFrom = builtins.attrValues self.checks.${system};
173        nativeBuildInputs = with pkgs;
174          [lld_13 cargo-flamegraph rust-analyzer]
175          ++ (lib.optional (stdenv.isx86_64 && stdenv.isLinux) pkgs.cargo-tarpaulin)
176          ++ (lib.optional stdenv.isLinux pkgs.lldb)
177          ++ (lib.optional stdenv.isDarwin pkgs.darwin.apple_sdk.frameworks.CoreFoundation);
178        shellHook = ''
179          export HELIX_RUNTIME="$PWD/runtime"
180          export RUST_BACKTRACE="1"
181          export RUSTFLAGS="''${RUSTFLAGS:-""} ${rustFlagsEnv}"
182        '';
183      };
184    })
185    // {
186      overlays.default = final: prev: {
187        inherit (self.packages.${final.system}) helix;
188      };
189    };
190
191  nixConfig = {
192    extra-substituters = ["https://helix.cachix.org"];
193    extra-trusted-public-keys = ["helix.cachix.org-1:ejp9KQpR1FBI2onstMQ34yogDm4OgU2ru6lIwPvuCVs="];
194  };
195}