fork/helix

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

Michael DavisBuild flake packages with stable Rust (#8133)7cf775d5

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