fork/helix

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

Asger Juul Brunshøjin flake mkShell default RUSTFLAGS to an empty string if unset (#10880)a1cda3c1

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