From d998cf90e63460477dece9fb29649a80d541832e Mon Sep 17 00:00:00 2001 From: Ross Smyth <18294397+RossSmyth@users.noreply.github.com> Date: Fri, 7 Feb 2025 18:53:40 -0500 Subject: [PATCH 01/22] Format grammars.nix with alejandra --- grammars.nix | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/grammars.nix b/grammars.nix index 967b1b136de4..bc99d21dfae2 100644 --- a/grammars.nix +++ b/grammars.nix @@ -32,10 +32,10 @@ # If `use-grammars.except` is set, use all other grammars. # Otherwise use all grammars. useGrammar = grammar: - if languagesConfig?use-grammars.only then - builtins.elem grammar.name languagesConfig.use-grammars.only - else if languagesConfig?use-grammars.except then - !(builtins.elem grammar.name languagesConfig.use-grammars.except) + if languagesConfig ? use-grammars.only + then builtins.elem grammar.name languagesConfig.use-grammars.only + else if languagesConfig ? use-grammars.except + then !(builtins.elem grammar.name languagesConfig.use-grammars.except) else true; grammarsToUse = builtins.filter useGrammar languagesConfig.grammar; gitGrammars = builtins.filter isGitGrammar grammarsToUse; @@ -66,10 +66,10 @@ version = grammar.source.rev; src = source; - sourceRoot = if builtins.hasAttr "subpath" grammar.source then - "source/${grammar.source.subpath}" - else - "source"; + sourceRoot = + if builtins.hasAttr "subpath" grammar.source + then "source/${grammar.source.subpath}" + else "source"; dontConfigure = true; @@ -116,15 +116,19 @@ ''; }; grammarsToBuild = builtins.filter includeGrammarIf gitGrammars; - builtGrammars = builtins.map (grammar: { - inherit (grammar) name; - value = buildGrammar grammar; - }) grammarsToBuild; + builtGrammars = + builtins.map (grammar: { + inherit (grammar) name; + value = buildGrammar grammar; + }) + grammarsToBuild; extensibleGrammars = lib.makeExtensible (self: builtins.listToAttrs builtGrammars); - overlaidGrammars = lib.pipe extensibleGrammars + overlaidGrammars = + lib.pipe extensibleGrammars (builtins.map (overlay: grammar: grammar.extend overlay) grammarOverlays); - grammarLinks = lib.mapAttrsToList + grammarLinks = + lib.mapAttrsToList (name: artifact: "ln -s ${artifact}/${name}.so $out/${name}.so") (lib.filterAttrs (n: v: lib.isDerivation v) overlaidGrammars); in From 248875413f19463c16ed0aef7a4e8a685dea67fe Mon Sep 17 00:00:00 2001 From: Ross Smyth <18294397+RossSmyth@users.noreply.github.com> Date: Fri, 7 Feb 2025 18:54:02 -0500 Subject: [PATCH 02/22] Update nix flake to be easier to understand and overrideable --- flake.nix | 204 +++++++++++++++++++++++------------------------------- 1 file changed, 87 insertions(+), 117 deletions(-) diff --git a/flake.nix b/flake.nix index 1fda3fa9455c..06ba4fd292f4 100644 --- a/flake.nix +++ b/flake.nix @@ -20,128 +20,92 @@ ... }: flake-utils.lib.eachDefaultSystem (system: let + # Apply the rust-overlay to nixpkgs so we can access all version. pkgs = import nixpkgs { inherit system; overlays = [(import rust-overlay)]; }; - mkRootPath = rel: - builtins.path { - path = "${toString ./.}/${rel}"; - name = rel; - }; - filteredSource = let - pathsToIgnore = [ - ".envrc" - ".ignore" - ".github" - ".gitignore" - "logo_dark.svg" - "logo_light.svg" - "rust-toolchain.toml" - "rustfmt.toml" - "runtime" - "screenshot.png" - "book" - "docs" - "README.md" - "CHANGELOG.md" - "shell.nix" - "default.nix" - "grammars.nix" - "flake.nix" - "flake.lock" - ]; - ignorePaths = path: type: let - inherit (nixpkgs) lib; - # split the nix store path into its components - components = lib.splitString "/" path; - # drop off the `/nix/hash-source` section from the path - relPathComponents = lib.drop 4 components; - # reassemble the path components - relPath = lib.concatStringsSep "/" relPathComponents; - in - lib.all (p: ! (lib.hasPrefix p relPath)) pathsToIgnore; - in - builtins.path { - name = "helix-source"; - path = toString ./.; - # filter out unnecessary paths - filter = ignorePaths; - }; - makeOverridableHelix = old: config: let - grammars = pkgs.callPackage ./grammars.nix config; - runtimeDir = pkgs.runCommand "helix-runtime" {} '' - mkdir -p $out - ln -s ${mkRootPath "runtime"}/* $out - rm -r $out/grammars - ln -s ${grammars} $out/grammars - ''; - helix-wrapped = - pkgs.runCommand - old.name - { - inherit (old) pname version; - meta = old.meta or {}; - passthru = - (old.passthru or {}) - // { - unwrapped = old; - }; - nativeBuildInputs = [pkgs.makeWrapper]; - makeWrapperArgs = config.makeWrapperArgs or []; - } - '' - cp -rs --no-preserve=mode,ownership ${old} $out - wrapProgram "$out/bin/hx" ''${makeWrapperArgs[@]} --set HELIX_RUNTIME "${runtimeDir}" - ''; - in - helix-wrapped - // { - override = makeOverridableHelix old; - passthru = - helix-wrapped.passthru - // { - wrapper = old: makeOverridableHelix old config; - }; - }; - stdenv = - if pkgs.stdenv.isLinux - then pkgs.stdenv - else pkgs.clangStdenv; - rustFlagsEnv = pkgs.lib.optionalString stdenv.isLinux "-C link-arg=-fuse-ld=lld -C target-cpu=native -Clink-arg=-Wl,--no-rosegment --cfg tokio_unstable"; + + # Get Helix's MSRV toolchain to build with by default. rustToolchain = pkgs.pkgsBuildHost.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; craneLibMSRV = (crane.mkLib pkgs).overrideToolchain rustToolchain; - craneLibStable = (crane.mkLib pkgs).overrideToolchain pkgs.pkgsBuildHost.rust-bin.stable.latest.default; + + # Common args for most things commonArgs = { - inherit stdenv; + # Helix attempts to reach out to the network and get the grammars. Nix doesn't allow this. + HELIX_DISABLE_AUTO_GRAMMAR_BUILD = "1"; + + # Get the name and version from the cargo.toml inherit (craneLibMSRV.crateNameFromCargoToml {cargoToml = ./helix-term/Cargo.toml;}) pname; inherit (craneLibMSRV.crateNameFromCargoToml {cargoToml = ./Cargo.toml;}) version; - src = filteredSource; - # disable fetching and building of tree-sitter grammars in the helix-term build.rs - HELIX_DISABLE_AUTO_GRAMMAR_BUILD = "1"; - buildInputs = [stdenv.cc.cc.lib]; + + # Clean the source. + src = craneLibMSRV.cleanCargoSource ./.; + + # Common build inputs. nativeBuildInputs = [pkgs.installShellFiles]; + # disable tests doCheck = false; - meta.mainProgram = "hx"; + strictDeps = true; }; + + # Next we actually need to build the grammars and the runtime directory + # that they reside in. It is built by calling the derivation in the + # grammars.nix file, then taking the runtime directory in the git repo + # and hooking symlinks up to it. + grammars = pkgs.callPackage ./grammars.nix {}; + runtimeDir = pkgs.runCommand "helix-runtime" {} '' + mkdir -p $out + ln -s ${./runtime}/* $out + rm -r $out/grammars + ln -s ${grammars} $out/grammars + ''; + + # Cranelib allows us to put the dependencies in the nix store. This means + # it is semi-incremental if the Cargo.lock doesn't change. cargoArtifacts = craneLibMSRV.buildDepsOnly commonArgs; - in { - packages = { - helix-unwrapped = craneLibStable.buildPackage (commonArgs - // { - cargoArtifacts = craneLibStable.buildDepsOnly commonArgs; + + # This allows for an overridable helix build. + # + build_helix = pkgs.lib.makeOverridable ({ + pkgs, + craneLib, + runtimeDir, + cargoExtraArgs ? "", + }: + craneLib.buildPackage (commonArgs + // rec { + inherit cargoArtifacts cargoExtraArgs; + nativeBuildInputs = [ + pkgs.installShellFiles + pkgs.git + ]; + env.HELIX_DEFAULT_RUNTIME = "${runtimeDir}"; + postInstall = '' - mkdir -p $out/share/applications $out/share/icons/hicolor/scalable/apps $out/share/icons/hicolor/256x256/apps + mkdir -p $out/lib + installShellCompletion contrib/completion/hx.{bash,fish,zsh} + mkdir -p $out/share/{applications,icons/hicolor/256x256/apps} cp contrib/Helix.desktop $out/share/applications - cp logo.svg $out/share/icons/hicolor/scalable/apps/helix.svg cp contrib/helix.png $out/share/icons/hicolor/256x256/apps - installShellCompletion contrib/completion/hx.{bash,fish,zsh} ''; - # set git revision for nix flake builds, see 'git_hash' in helix-loader/build.rs - HELIX_NIX_BUILD_REV = self.rev or self.dirtyRev or null; - }); - helix = makeOverridableHelix self.packages.${system}.helix-unwrapped {}; + + meta = { + mainProgram = "hx"; + }; + })); + in { + packages = { + helix = build_helix { + inherit pkgs runtimeDir; + craneLib = craneLibMSRV; + }; + + # The default Helix build. Uses the default MSRV Rust toolchain, and the + # default nixpkgs, which is the one in the Flake.lock of Helix. + # + # This can be overridden though to add Cargo Features, flags, and different toolchains. default = self.packages.${system}.helix; }; @@ -168,19 +132,25 @@ }); }; - devShells.default = pkgs.mkShell { - inputsFrom = builtins.attrValues self.checks.${system}; - nativeBuildInputs = with pkgs; - [lld_13 cargo-flamegraph rust-analyzer] - ++ (lib.optional (stdenv.isx86_64 && stdenv.isLinux) pkgs.cargo-tarpaulin) - ++ (lib.optional stdenv.isLinux pkgs.lldb) - ++ (lib.optional stdenv.isDarwin pkgs.darwin.apple_sdk.frameworks.CoreFoundation); - shellHook = '' - export HELIX_RUNTIME="$PWD/runtime" - export RUST_BACKTRACE="1" - export RUSTFLAGS="''${RUSTFLAGS:-""} ${rustFlagsEnv}" - ''; - }; + formatter = pkgs.alejandra; + + devShells.default = let + rustFlagsEnv = pkgs.lib.optionalString pkgs.stdenv.isLinux "-C link-arg=-fuse-ld=lld -C target-cpu=native -Clink-arg=-Wl,--no-rosegment --cfg tokio_unstable"; + in + pkgs.mkShell + { + inputsFrom = builtins.attrValues self.checks.${system}; + nativeBuildInputs = with pkgs; + [lld_13 cargo-flamegraph rust-analyzer] + ++ (lib.optional (stdenv.isx86_64 && stdenv.isLinux) pkgs.cargo-tarpaulin) + ++ (lib.optional stdenv.isLinux pkgs.lldb) + ++ (lib.optional stdenv.isDarwin pkgs.darwin.apple_sdk.frameworks.CoreFoundation); + shellHook = '' + export HELIX_RUNTIME="$PWD/runtime" + export RUST_BACKTRACE="1" + export RUSTFLAGS="''${RUSTFLAGS:-""} ${rustFlagsEnv}" + ''; + }; }) // { overlays.default = final: prev: { From 4ba82e49602e714efb8e1857e33b08c7ff368c98 Mon Sep 17 00:00:00 2001 From: Ross Smyth <18294397+RossSmyth@users.noreply.github.com> Date: Mon, 10 Feb 2025 12:13:43 -0500 Subject: [PATCH 03/22] Fix dependencies and application crates being build with different rustc versions --- flake.nix | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/flake.nix b/flake.nix index 06ba4fd292f4..93421642de84 100644 --- a/flake.nix +++ b/flake.nix @@ -69,18 +69,21 @@ # This allows for an overridable helix build. # build_helix = pkgs.lib.makeOverridable ({ - pkgs, - craneLib, - runtimeDir, + pkgs ? pkgs, + craneLib ? craneLibMSRV, + runtimeDir ? runtimeDir, cargoExtraArgs ? "", }: craneLib.buildPackage (commonArgs // rec { - inherit cargoArtifacts cargoExtraArgs; + inherit cargoExtraArgs; nativeBuildInputs = [ pkgs.installShellFiles pkgs.git ]; + + cargoArtifacts = craneLib.buildDepsOnly commonArgs; + env.HELIX_DEFAULT_RUNTIME = "${runtimeDir}"; postInstall = '' @@ -97,10 +100,7 @@ })); in { packages = { - helix = build_helix { - inherit pkgs runtimeDir; - craneLib = craneLibMSRV; - }; + helix = build_helix { }; # The default Helix build. Uses the default MSRV Rust toolchain, and the # default nixpkgs, which is the one in the Flake.lock of Helix. From 86ba656f1370c435c6b2189ef0ff065545c27a40 Mon Sep 17 00:00:00 2001 From: Ross Smyth <18294397+RossSmyth@users.noreply.github.com> Date: Mon, 10 Feb 2025 12:17:27 -0500 Subject: [PATCH 04/22] Fix recursion --- flake.nix | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/flake.nix b/flake.nix index 93421642de84..479f4b41fcb3 100644 --- a/flake.nix +++ b/flake.nix @@ -55,7 +55,7 @@ # grammars.nix file, then taking the runtime directory in the git repo # and hooking symlinks up to it. grammars = pkgs.callPackage ./grammars.nix {}; - runtimeDir = pkgs.runCommand "helix-runtime" {} '' + runtime = pkgs.runCommand "helix-runtime" {} '' mkdir -p $out ln -s ${./runtime}/* $out rm -r $out/grammars @@ -69,21 +69,21 @@ # This allows for an overridable helix build. # build_helix = pkgs.lib.makeOverridable ({ - pkgs ? pkgs, + npkgs ? pkgs, craneLib ? craneLibMSRV, - runtimeDir ? runtimeDir, + runtimeDir ? runtime, cargoExtraArgs ? "", }: craneLib.buildPackage (commonArgs // rec { inherit cargoExtraArgs; nativeBuildInputs = [ - pkgs.installShellFiles - pkgs.git + npkgs.installShellFiles + npkgs.git ]; cargoArtifacts = craneLib.buildDepsOnly commonArgs; - + env.HELIX_DEFAULT_RUNTIME = "${runtimeDir}"; postInstall = '' @@ -100,7 +100,7 @@ })); in { packages = { - helix = build_helix { }; + helix = build_helix {}; # The default Helix build. Uses the default MSRV Rust toolchain, and the # default nixpkgs, which is the one in the Flake.lock of Helix. From fdf42718a7112ed322f7662384cade34928eaad3 Mon Sep 17 00:00:00 2001 From: Ross Smyth <18294397+RossSmyth@users.noreply.github.com> Date: Mon, 10 Feb 2025 12:25:22 -0500 Subject: [PATCH 05/22] Fix completions --- flake.nix | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/flake.nix b/flake.nix index 479f4b41fcb3..a3f5b390a0ce 100644 --- a/flake.nix +++ b/flake.nix @@ -88,10 +88,10 @@ postInstall = '' mkdir -p $out/lib - installShellCompletion contrib/completion/hx.{bash,fish,zsh} + installShellCompletion ${./contrib/completion}/hx.{bash,fish,zsh} mkdir -p $out/share/{applications,icons/hicolor/256x256/apps} - cp contrib/Helix.desktop $out/share/applications - cp contrib/helix.png $out/share/icons/hicolor/256x256/apps + cp ${./contrib/Helix.desktop} $out/share/applications + cp ${./contrib/helix.png} $out/share/icons/hicolor/256x256/apps ''; meta = { From f6b768935cc8c10a9815280e3224793a33e7e36d Mon Sep 17 00:00:00 2001 From: Ross Smyth <18294397+RossSmyth@users.noreply.github.com> Date: Mon, 10 Feb 2025 15:01:53 -0500 Subject: [PATCH 06/22] Add docs and the rustFlags argument --- flake.nix | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/flake.nix b/flake.nix index a3f5b390a0ce..83ab3da3b902 100644 --- a/flake.nix +++ b/flake.nix @@ -66,26 +66,59 @@ # it is semi-incremental if the Cargo.lock doesn't change. cargoArtifacts = craneLibMSRV.buildDepsOnly commonArgs; - # This allows for an overridable helix build. + # This allows for an easily overridable Helix build. # + # Example: + # Overrides to use Nightly toolchain + # + # pkgs = import nixpkgs { + # inherit syste; + # overlays = [ (import rust-overlay) ] + # }; + # craneLib = crane.mkLib pkgs; + # nightly-crane = craneLib.overrideToolchain (p: p.rust-bin.nightly.latest.default); + # helix.packages.${pkgs.system}.default.override { + # craneLib = nightly-crane; + # }; build_helix = pkgs.lib.makeOverridable ({ + # nixpkgs used npkgs ? pkgs, + # CraneLib instance used craneLib ? craneLibMSRV, + # The runtime directory derivation runtimeDir ? runtime, + # Any extra arguments to Cargo such as features or optlevel cargoExtraArgs ? "", + # RUSTFLAGS environment variable + rustFlags ? "", }: craneLib.buildPackage (commonArgs // rec { - inherit cargoExtraArgs; + inherit cargoExtraArgs rustFlags; + + # The tools required on the build host nativeBuildInputs = [ npkgs.installShellFiles npkgs.git ]; - cargoArtifacts = craneLib.buildDepsOnly commonArgs; - + # The dependencies. Would be nice to capture them from the + # above derivation, but cannot with the way it is currently setup. + # This derivation must use the same craneLib instance, RUSTFLAGS, + # and cargoExtraFlags to be both sound and useful. + cargoArtifacts = craneLib.buildDepsOnly ({ + inherit cargoExtraArgs; + RUSTFLAGS = rustFlags; + } + // commonArgs); + + # Set the environment variable at runtime for the runtime dir env.HELIX_DEFAULT_RUNTIME = "${runtimeDir}"; + # The build-time envar to get the git hash + HELIX_NIX_BUILD_REV = self.rev or self.dirtyRev or null; + + # Get all the application stuff in the output directory. postInstall = '' mkdir -p $out/lib installShellCompletion ${./contrib/completion}/hx.{bash,fish,zsh} @@ -100,6 +133,7 @@ })); in { packages = { + # Use all the defaults. helix = build_helix {}; # The default Helix build. Uses the default MSRV Rust toolchain, and the @@ -109,6 +143,8 @@ default = self.packages.${system}.helix; }; + # Note that if overrides are used, none of the checks will use the cached override artifacts + # as they are all MSRV only. checks = { # Build the crate itself inherit (self.packages.${system}) helix; @@ -134,6 +170,7 @@ formatter = pkgs.alejandra; + # Devshell behavior is preserved. devShells.default = let rustFlagsEnv = pkgs.lib.optionalString pkgs.stdenv.isLinux "-C link-arg=-fuse-ld=lld -C target-cpu=native -Clink-arg=-Wl,--no-rosegment --cfg tokio_unstable"; in From c3139937b6ece701865feb680e8b2c5c7c1b7016 Mon Sep 17 00:00:00 2001 From: Ross Smyth <18294397+RossSmyth@users.noreply.github.com> Date: Mon, 10 Feb 2025 15:26:09 -0500 Subject: [PATCH 07/22] Fix typo --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 83ab3da3b902..8b658f0c7b8a 100644 --- a/flake.nix +++ b/flake.nix @@ -72,7 +72,7 @@ # Overrides to use Nightly toolchain # # pkgs = import nixpkgs { - # inherit syste; + # inherit system; # overlays = [ (import rust-overlay) ] # }; # craneLib = crane.mkLib pkgs; From 93bf32a8e82dd6e19e2407b7cc967929cc4807a9 Mon Sep 17 00:00:00 2001 From: Ross Smyth <18294397+RossSmyth@users.noreply.github.com> Date: Tue, 11 Feb 2025 15:01:50 -0500 Subject: [PATCH 08/22] Refactor Flake to only use rust-overlay --- flake.lock | 16 ----- flake.nix | 204 +++++++++++++++++++---------------------------------- 2 files changed, 74 insertions(+), 146 deletions(-) diff --git a/flake.lock b/flake.lock index 62ff344776aa..53f4cf7f0276 100644 --- a/flake.lock +++ b/flake.lock @@ -1,20 +1,5 @@ { "nodes": { - "crane": { - "locked": { - "lastModified": 1737563566, - "narHash": "sha256-GLJvkOG29XCynQm8XWPyykMRqIhxKcBARVu7Ydrz02M=", - "owner": "ipetkov", - "repo": "crane", - "rev": "849376434956794ebc7a6b487d31aace395392ba", - "type": "github" - }, - "original": { - "owner": "ipetkov", - "repo": "crane", - "type": "github" - } - }, "flake-utils": { "inputs": { "systems": "systems" @@ -51,7 +36,6 @@ }, "root": { "inputs": { - "crane": "crane", "flake-utils": "flake-utils", "nixpkgs": "nixpkgs", "rust-overlay": "rust-overlay" diff --git a/flake.nix b/flake.nix index 8b658f0c7b8a..e8cf433d952b 100644 --- a/flake.nix +++ b/flake.nix @@ -8,133 +8,92 @@ url = "github:oxalica/rust-overlay"; inputs.nixpkgs.follows = "nixpkgs"; }; - crane.url = "github:ipetkov/crane"; }; outputs = { self, nixpkgs, - crane, flake-utils, rust-overlay, ... - }: - flake-utils.lib.eachDefaultSystem (system: let - # Apply the rust-overlay to nixpkgs so we can access all version. - pkgs = import nixpkgs { - inherit system; - overlays = [(import rust-overlay)]; - }; - - # Get Helix's MSRV toolchain to build with by default. - rustToolchain = pkgs.pkgsBuildHost.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; - craneLibMSRV = (crane.mkLib pkgs).overrideToolchain rustToolchain; - - # Common args for most things - commonArgs = { - # Helix attempts to reach out to the network and get the grammars. Nix doesn't allow this. - HELIX_DISABLE_AUTO_GRAMMAR_BUILD = "1"; - - # Get the name and version from the cargo.toml - inherit (craneLibMSRV.crateNameFromCargoToml {cargoToml = ./helix-term/Cargo.toml;}) pname; - inherit (craneLibMSRV.crateNameFromCargoToml {cargoToml = ./Cargo.toml;}) version; - - # Clean the source. - src = craneLibMSRV.cleanCargoSource ./.; - - # Common build inputs. - nativeBuildInputs = [pkgs.installShellFiles]; - - # disable tests - doCheck = false; - strictDeps = true; - }; - + }: let + mkHelix = { + pkgs, + rustPlatform, + ... + }: let # Next we actually need to build the grammars and the runtime directory # that they reside in. It is built by calling the derivation in the # grammars.nix file, then taking the runtime directory in the git repo # and hooking symlinks up to it. grammars = pkgs.callPackage ./grammars.nix {}; - runtime = pkgs.runCommand "helix-runtime" {} '' + runtimeDir = pkgs.runCommand "helix-runtime" {} '' mkdir -p $out ln -s ${./runtime}/* $out rm -r $out/grammars ln -s ${grammars} $out/grammars ''; + in + rustPlatform.buildRustPackage { + name = with builtins; (fromTOML (readFile ./helix-term/Cargo.toml)).package.name; + version = with builtins; (fromTOML (readFile ./Cargo.toml)).workspace.package.version; + + src = pkgs.lib.sources.cleanSource ./.; + + cargoLock = { + lockFile = ./Cargo.lock; + }; + + nativeBuildInputs = [ + pkgs.installShellFiles + pkgs.git + ]; - # Cranelib allows us to put the dependencies in the nix store. This means - # it is semi-incremental if the Cargo.lock doesn't change. - cargoArtifacts = craneLibMSRV.buildDepsOnly commonArgs; - - # This allows for an easily overridable Helix build. - # - # Example: - # Overrides to use Nightly toolchain - # - # pkgs = import nixpkgs { - # inherit system; - # overlays = [ (import rust-overlay) ] - # }; - # craneLib = crane.mkLib pkgs; - # nightly-crane = craneLib.overrideToolchain (p: p.rust-bin.nightly.latest.default); - # helix.packages.${pkgs.system}.default.override { - # craneLib = nightly-crane; - # }; - build_helix = pkgs.lib.makeOverridable ({ - # nixpkgs used - npkgs ? pkgs, - # CraneLib instance used - craneLib ? craneLibMSRV, - # The runtime directory derivation - runtimeDir ? runtime, - # Any extra arguments to Cargo such as features or optlevel - cargoExtraArgs ? "", - # RUSTFLAGS environment variable - rustFlags ? "", - }: - craneLib.buildPackage (commonArgs - // rec { - inherit cargoExtraArgs rustFlags; - - # The tools required on the build host - nativeBuildInputs = [ - npkgs.installShellFiles - npkgs.git - ]; - - # The dependencies. Would be nice to capture them from the - # above derivation, but cannot with the way it is currently setup. - # This derivation must use the same craneLib instance, RUSTFLAGS, - # and cargoExtraFlags to be both sound and useful. - cargoArtifacts = craneLib.buildDepsOnly ({ - inherit cargoExtraArgs; - RUSTFLAGS = rustFlags; - } - // commonArgs); - - # Set the environment variable at runtime for the runtime dir - env.HELIX_DEFAULT_RUNTIME = "${runtimeDir}"; - - # The build-time envar to get the git hash - HELIX_NIX_BUILD_REV = self.rev or self.dirtyRev or null; - - # Get all the application stuff in the output directory. - postInstall = '' - mkdir -p $out/lib - installShellCompletion ${./contrib/completion}/hx.{bash,fish,zsh} - mkdir -p $out/share/{applications,icons/hicolor/256x256/apps} - cp ${./contrib/Helix.desktop} $out/share/applications - cp ${./contrib/helix.png} $out/share/icons/hicolor/256x256/apps - ''; - - meta = { - mainProgram = "hx"; - }; - })); + # Helix attempts to reach out to the network and get the grammars. Nix doesn't allow this. + HELIX_DISABLE_AUTO_GRAMMAR_BUILD = "1"; + + # So Helix knows what rev it is. + HELIX_NIX_BUILD_REV = self.rev or self.dirtyRev or null; + + # Use Helix's opt profile for the build. + buildType = "opt"; + + doCheck = false; + strictDeps = true; + + # Sets the Helix runtimedir to the grammars + env.HELIX_DEFAULT_RUNTIME = "${runtimeDir}"; + + # Get all the application stuff in the output directory. + postInstall = '' + mkdir -p $out/lib + installShellCompletion ${./contrib/completion}/hx.{bash,fish,zsh} + mkdir -p $out/share/{applications,icons/hicolor/256x256/apps} + cp ${./contrib/Helix.desktop} $out/share/applications + cp ${./contrib/helix.png} $out/share/icons/hicolor/256x256/apps + ''; + + meta = { + mainProgram = "hx"; + }; + }; + in + flake-utils.lib.eachDefaultSystem (system: let + pkgs = import nixpkgs { + inherit system; + overlays = [(import rust-overlay)]; + }; + + # Get Helix's MSRV toolchain to build with by default. + msrvToolchain = pkgs.pkgsBuildHost.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; + msrvPlatform = pkgs.makeRustPlatform { + cargo = msrvToolchain; + rustc = msrvToolchain; + }; in { packages = { - # Use all the defaults. - helix = build_helix {}; + # Make MSRV Helix + helix = pkgs.callPackage mkHelix {rustPlatform = msrvPlatform;}; # The default Helix build. Uses the default MSRV Rust toolchain, and the # default nixpkgs, which is the one in the Flake.lock of Helix. @@ -143,29 +102,12 @@ default = self.packages.${system}.helix; }; - # Note that if overrides are used, none of the checks will use the cached override artifacts - # as they are all MSRV only. checks = { - # Build the crate itself - inherit (self.packages.${system}) helix; - - clippy = craneLibMSRV.cargoClippy (commonArgs - // { - inherit cargoArtifacts; - cargoClippyExtraArgs = "--all-targets -- --deny warnings"; - }); - - fmt = craneLibMSRV.cargoFmt commonArgs; - - doc = craneLibMSRV.cargoDoc (commonArgs - // { - inherit cargoArtifacts; - }); - - test = craneLibMSRV.cargoTest (commonArgs - // { - inherit cargoArtifacts; - }); + helix = self.outputs.packages.${system}.helix.overrideAttrs (prev: + { + buildType = "debug"; + } + // prev); }; formatter = pkgs.alejandra; @@ -190,8 +132,10 @@ }; }) // { - overlays.default = final: prev: { - inherit (self.packages.${final.system}) helix; + overlays = { + default = final: prev: { + helix = final.callPackage mkHelix {}; + }; }; }; From 2ae9ac3bf51ff5277214778aa914588129b6b046 Mon Sep 17 00:00:00 2001 From: Ross Smyth <18294397+RossSmyth@users.noreply.github.com> Date: Wed, 12 Feb 2025 12:16:42 -0500 Subject: [PATCH 09/22] Use mkDerivation directly so that customizablity is greatly enhanced --- flake.nix | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/flake.nix b/flake.nix index e8cf433d952b..cd354cfa157f 100644 --- a/flake.nix +++ b/flake.nix @@ -20,6 +20,7 @@ mkHelix = { pkgs, rustPlatform, + stdenv, ... }: let # Next we actually need to build the grammars and the runtime directory @@ -34,30 +35,49 @@ ln -s ${grammars} $out/grammars ''; in - rustPlatform.buildRustPackage { - name = with builtins; (fromTOML (readFile ./helix-term/Cargo.toml)).package.name; - version = with builtins; (fromTOML (readFile ./Cargo.toml)).workspace.package.version; - - src = pkgs.lib.sources.cleanSource ./.; - - cargoLock = { + # Currently rustPlatform.buildRustPackage doesn't have the finalAttrs pattern + # hooked up. To get around this while having good customization, mkDerivation is + # used instead. + stdenv.mkDerivation (self: { + # START: Reelvalute the below attrs when + # https://github.com/NixOS/nixpkgs/pull/354999 + # or + # https://github.com/NixOS/nixpkgs/pull/194475 + # Are merged. + + # TODO: Probably change to cargoLock + cargoDeps = rustPlatform.importCargoLock { lockFile = ./Cargo.lock; }; nativeBuildInputs = [ + rustPlatform.rust.rustc # TODO: Remove + rustPlatform.rust.cargo # TODO: Remove pkgs.installShellFiles pkgs.git ]; + # TODO: Remove entire attr + buildInputs = with rustPlatform; [ + cargoSetupHook + cargoBuildHook + cargoInstallHook + ]; + + # Use Helix's opt profile for the build. + # TODO: s/cargoBuildType/buildType + cargoBuildType = "opt"; + # END: Funny attrs to reevaluate + + name = with builtins; (fromTOML (readFile ./helix-term/Cargo.toml)).package.name; + src = pkgs.lib.sources.cleanSource ./.; + # Helix attempts to reach out to the network and get the grammars. Nix doesn't allow this. HELIX_DISABLE_AUTO_GRAMMAR_BUILD = "1"; # So Helix knows what rev it is. HELIX_NIX_BUILD_REV = self.rev or self.dirtyRev or null; - # Use Helix's opt profile for the build. - buildType = "opt"; - doCheck = false; strictDeps = true; @@ -76,7 +96,7 @@ meta = { mainProgram = "hx"; }; - }; + }); in flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs { @@ -103,11 +123,9 @@ }; checks = { - helix = self.outputs.packages.${system}.helix.overrideAttrs (prev: - { - buildType = "debug"; - } - // prev); + helix = self.outputs.packages.${system}.helix.overrideAttrs { + cargoBuildType = "debug"; + }; }; formatter = pkgs.alejandra; From de02aa37ddcfe1d59ce080bff079685b19def4b1 Mon Sep 17 00:00:00 2001 From: Ross Smyth <18294397+RossSmyth@users.noreply.github.com> Date: Sat, 15 Feb 2025 18:29:21 -0500 Subject: [PATCH 10/22] Fix nits --- flake.nix | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/flake.nix b/flake.nix index cd354cfa157f..745047a5c10b 100644 --- a/flake.nix +++ b/flake.nix @@ -39,7 +39,7 @@ # hooked up. To get around this while having good customization, mkDerivation is # used instead. stdenv.mkDerivation (self: { - # START: Reelvalute the below attrs when + # START: Reevaluate the below attrs when # https://github.com/NixOS/nixpkgs/pull/354999 # or # https://github.com/NixOS/nixpkgs/pull/194475 @@ -93,9 +93,7 @@ cp ${./contrib/helix.png} $out/share/icons/hicolor/256x256/apps ''; - meta = { - mainProgram = "hx"; - }; + meta.mainProgram = "hx"; }); in flake-utils.lib.eachDefaultSystem (system: let @@ -122,10 +120,8 @@ default = self.packages.${system}.helix; }; - checks = { - helix = self.outputs.packages.${system}.helix.overrideAttrs { - cargoBuildType = "debug"; - }; + checks.helix = self.outputs.packages.${system}.helix.overrideAttrs { + cargoBuildType = "debug"; }; formatter = pkgs.alejandra; @@ -150,10 +146,8 @@ }; }) // { - overlays = { - default = final: prev: { - helix = final.callPackage mkHelix {}; - }; + overlays.default = final: prev: { + helix = final.callPackage mkHelix {}; }; }; From e4e7646b941118986d97a22e584655bdee98bc86 Mon Sep 17 00:00:00 2001 From: Ross Smyth <18294397+RossSmyth@users.noreply.github.com> Date: Sat, 15 Feb 2025 18:32:29 -0500 Subject: [PATCH 11/22] Forgot to add rec --- flake.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flake.nix b/flake.nix index 745047a5c10b..f8b4c0a1d483 100644 --- a/flake.nix +++ b/flake.nix @@ -109,7 +109,7 @@ rustc = msrvToolchain; }; in { - packages = { + packages = rec { # Make MSRV Helix helix = pkgs.callPackage mkHelix {rustPlatform = msrvPlatform;}; @@ -117,7 +117,7 @@ # default nixpkgs, which is the one in the Flake.lock of Helix. # # This can be overridden though to add Cargo Features, flags, and different toolchains. - default = self.packages.${system}.helix; + default = helix; }; checks.helix = self.outputs.packages.${system}.helix.overrideAttrs { From 57b3891da5c68660a469a3a8a25af6d5b1ee35d8 Mon Sep 17 00:00:00 2001 From: Ross Smyth <18294397+RossSmyth@users.noreply.github.com> Date: Sat, 15 Feb 2025 18:58:33 -0500 Subject: [PATCH 12/22] Ignore some files --- flake.nix | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index f8b4c0a1d483..0ef06c6d8786 100644 --- a/flake.nix +++ b/flake.nix @@ -23,6 +23,23 @@ stdenv, ... }: let + fs = pkgs.lib.fileset; + + src = fs.difference (fs.gitTracked ./.) (fs.unions [ + ./.envrc + ./.gitignore + ./rustfmt.toml + ./screenshot.png + ./book + ./docs + ./flake.lock + (fs.maybeMissing ./.github) + (fs.maybeMissing ./.ignore) + (fs.fileFilter (file: file.hasExt ".svg") ./.) + (fs.fileFilter (file: file.hasExt ".md") ./.) + (fs.fileFilter (file: file.hasExt ".nix") ./.) + ]); + # Next we actually need to build the grammars and the runtime directory # that they reside in. It is built by calling the derivation in the # grammars.nix file, then taking the runtime directory in the git repo @@ -70,7 +87,10 @@ # END: Funny attrs to reevaluate name = with builtins; (fromTOML (readFile ./helix-term/Cargo.toml)).package.name; - src = pkgs.lib.sources.cleanSource ./.; + src = fs.toSource { + root = ./.; + fileset = src; + }; # Helix attempts to reach out to the network and get the grammars. Nix doesn't allow this. HELIX_DISABLE_AUTO_GRAMMAR_BUILD = "1"; From 1fe71da6d25e3d674c581343d82506171b230ab3 Mon Sep 17 00:00:00 2001 From: Ross Smyth <18294397+RossSmyth@users.noreply.github.com> Date: Mon, 17 Feb 2025 15:40:50 -0500 Subject: [PATCH 13/22] Build Helix with the latest stable Rust toolchain by default --- flake.nix | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/flake.nix b/flake.nix index 0ef06c6d8786..eae18f0a67db 100644 --- a/flake.nix +++ b/flake.nix @@ -130,22 +130,21 @@ }; in { packages = rec { - # Make MSRV Helix - helix = pkgs.callPackage mkHelix {rustPlatform = msrvPlatform;}; + helix = pkgs.callPackage mkHelix { }; - # The default Helix build. Uses the default MSRV Rust toolchain, and the - # default nixpkgs, which is the one in the Flake.lock of Helix. + # The default Helix build. Uses the latest stable Rust toolchain, and unstable + # nixpkgs. # - # This can be overridden though to add Cargo Features, flags, and different toolchains. + # This can be overridden though to add Cargo Features, flags, and different toolchains with + # packages.${system}.default.overrideAttrs { ... }; default = helix; }; checks.helix = self.outputs.packages.${system}.helix.overrideAttrs { cargoBuildType = "debug"; + rustPlatform = msrvPlatform; }; - formatter = pkgs.alejandra; - # Devshell behavior is preserved. devShells.default = let rustFlagsEnv = pkgs.lib.optionalString pkgs.stdenv.isLinux "-C link-arg=-fuse-ld=lld -C target-cpu=native -Clink-arg=-Wl,--no-rosegment --cfg tokio_unstable"; From 901f9642c19f25718444ed8b1e31c90461005931 Mon Sep 17 00:00:00 2001 From: Ross Smyth <18294397+RossSmyth@users.noreply.github.com> Date: Tue, 18 Feb 2025 10:28:39 -0500 Subject: [PATCH 14/22] Fix fileset extension filtering --- flake.nix | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/flake.nix b/flake.nix index eae18f0a67db..deba301777fc 100644 --- a/flake.nix +++ b/flake.nix @@ -33,11 +33,9 @@ ./book ./docs ./flake.lock - (fs.maybeMissing ./.github) - (fs.maybeMissing ./.ignore) - (fs.fileFilter (file: file.hasExt ".svg") ./.) - (fs.fileFilter (file: file.hasExt ".md") ./.) - (fs.fileFilter (file: file.hasExt ".nix") ./.) + (fs.fileFilter (file: file.hasExt "svg") ./.) + (fs.fileFilter (file: file.hasExt "md") ./.) + (fs.fileFilter (file: file.hasExt "nix") ./.) ]); # Next we actually need to build the grammars and the runtime directory From 7bbfd57b5386df2bdce070144cbec3a95516c90c Mon Sep 17 00:00:00 2001 From: Ross Smyth <18294397+RossSmyth@users.noreply.github.com> Date: Wed, 19 Feb 2025 14:23:10 -0500 Subject: [PATCH 15/22] Ignore all *.git* files --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index deba301777fc..bda9a6191c21 100644 --- a/flake.nix +++ b/flake.nix @@ -27,12 +27,12 @@ src = fs.difference (fs.gitTracked ./.) (fs.unions [ ./.envrc - ./.gitignore ./rustfmt.toml ./screenshot.png ./book ./docs ./flake.lock + (fs.fileFilter (file: pkgs.lib.strings.hasInfix ".git" file.name) ./.) (fs.fileFilter (file: file.hasExt "svg") ./.) (fs.fileFilter (file: file.hasExt "md") ./.) (fs.fileFilter (file: file.hasExt "nix") ./.) From 9ed6cbd5ab0cf50f7fd0a3d4ed946cf438e252cf Mon Sep 17 00:00:00 2001 From: Ross Smyth <18294397+RossSmyth@users.noreply.github.com> Date: Wed, 26 Feb 2025 20:06:10 -0500 Subject: [PATCH 16/22] Fix devShell --- flake.nix | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/flake.nix b/flake.nix index bda9a6191c21..915108154989 100644 --- a/flake.nix +++ b/flake.nix @@ -37,7 +37,7 @@ (fs.fileFilter (file: file.hasExt "md") ./.) (fs.fileFilter (file: file.hasExt "nix") ./.) ]); - + # Next we actually need to build the grammars and the runtime directory # that they reside in. It is built by calling the derivation in the # grammars.nix file, then taking the runtime directory in the git repo @@ -128,7 +128,7 @@ }; in { packages = rec { - helix = pkgs.callPackage mkHelix { }; + helix = pkgs.callPackage mkHelix {}; # The default Helix build. Uses the latest stable Rust toolchain, and unstable # nixpkgs. @@ -149,12 +149,16 @@ in pkgs.mkShell { - inputsFrom = builtins.attrValues self.checks.${system}; nativeBuildInputs = with pkgs; - [lld_13 cargo-flamegraph rust-analyzer] - ++ (lib.optional (stdenv.isx86_64 && stdenv.isLinux) pkgs.cargo-tarpaulin) - ++ (lib.optional stdenv.isLinux pkgs.lldb) - ++ (lib.optional stdenv.isDarwin pkgs.darwin.apple_sdk.frameworks.CoreFoundation); + [ + lld_13 + cargo-flamegraph + rust-bin.nightly.latest.rust-analyzer + msrvToolchain + ] + ++ (lib.optional (stdenv.isx86_64 && stdenv.isLinux) cargo-tarpaulin) + ++ (lib.optional stdenv.isLinux lldb) + ++ (lib.optional stdenv.isDarwin darwin.apple_sdk.frameworks.CoreFoundation); shellHook = '' export HELIX_RUNTIME="$PWD/runtime" export RUST_BACKTRACE="1" From 2dc3dd3f661842af2df70b41e082f38158bc9a96 Mon Sep 17 00:00:00 2001 From: Ross Smyth <18294397+RossSmyth@users.noreply.github.com> Date: Thu, 27 Feb 2025 11:01:51 -0500 Subject: [PATCH 17/22] Fix the msrv override --- flake.nix | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/flake.nix b/flake.nix index 915108154989..746a96392e54 100644 --- a/flake.nix +++ b/flake.nix @@ -134,11 +134,11 @@ # nixpkgs. # # This can be overridden though to add Cargo Features, flags, and different toolchains with - # packages.${system}.default.overrideAttrs { ... }; + # packages.${system}.default.override { ... }; default = helix; }; - checks.helix = self.outputs.packages.${system}.helix.overrideAttrs { + checks.helix = self.outputs.packages.${system}.helix.override { cargoBuildType = "debug"; rustPlatform = msrvPlatform; }; @@ -149,12 +149,12 @@ in pkgs.mkShell { + inputsFrom = [self.checks.${system}.helix]; nativeBuildInputs = with pkgs; [ lld_13 cargo-flamegraph rust-bin.nightly.latest.rust-analyzer - msrvToolchain ] ++ (lib.optional (stdenv.isx86_64 && stdenv.isLinux) cargo-tarpaulin) ++ (lib.optional stdenv.isLinux lldb) From 703a962d6aa0c994d344c525cc0d4962b7f995d9 Mon Sep 17 00:00:00 2001 From: Ross Smyth <18294397+RossSmyth@users.noreply.github.com> Date: Thu, 27 Feb 2025 11:02:48 -0500 Subject: [PATCH 18/22] buildType opt -> release fat LTO takes a long time --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 746a96392e54..16907940a3f4 100644 --- a/flake.nix +++ b/flake.nix @@ -81,7 +81,7 @@ # Use Helix's opt profile for the build. # TODO: s/cargoBuildType/buildType - cargoBuildType = "opt"; + cargoBuildType = "release"; # END: Funny attrs to reevaluate name = with builtins; (fromTOML (readFile ./helix-term/Cargo.toml)).package.name; From bb2141c2bebbac47f22bca641e05a758925bac69 Mon Sep 17 00:00:00 2001 From: Ross Smyth <18294397+RossSmyth@users.noreply.github.com> Date: Thu, 27 Feb 2025 11:11:11 -0500 Subject: [PATCH 19/22] Move mkHelix to default.nix --- default.nix | 102 ++++++++++++++++++++++++++++++++++++++++++++++++---- flake.nix | 100 ++------------------------------------------------- 2 files changed, 97 insertions(+), 105 deletions(-) diff --git a/default.nix b/default.nix index d2c51ec3a33c..d8b74e2ce116 100644 --- a/default.nix +++ b/default.nix @@ -1,8 +1,96 @@ -# Flake's default package for non-flake-enabled nix instances -let - compat = builtins.fetchTarball { - url = "https://github.com/edolstra/flake-compat/archive/b4a34015c698c7793d592d66adbab377907a2be8.tar.gz"; - sha256 = "sha256:1qc703yg0babixi6wshn5wm2kgl5y1drcswgszh4xxzbrwkk9sv7"; - }; +{ + pkgs, + rustPlatform, + stdenv, + ... +}: let + fs = pkgs.lib.fileset; + + src = fs.difference (fs.gitTracked ./.) (fs.unions [ + ./.envrc + ./rustfmt.toml + ./screenshot.png + ./book + ./docs + ./flake.lock + (fs.fileFilter (file: pkgs.lib.strings.hasInfix ".git" file.name) ./.) + (fs.fileFilter (file: file.hasExt "svg") ./.) + (fs.fileFilter (file: file.hasExt "md") ./.) + (fs.fileFilter (file: file.hasExt "nix") ./.) + ]); + + # Next we actually need to build the grammars and the runtime directory + # that they reside in. It is built by calling the derivation in the + # grammars.nix file, then taking the runtime directory in the git repo + # and hooking symlinks up to it. + grammars = pkgs.callPackage ./grammars.nix {}; + runtimeDir = pkgs.runCommand "helix-runtime" {} '' + mkdir -p $out + ln -s ${./runtime}/* $out + rm -r $out/grammars + ln -s ${grammars} $out/grammars + ''; in - (import compat {src = ./.;}).defaultNix + # Currently rustPlatform.buildRustPackage doesn't have the finalAttrs pattern + # hooked up. To get around this while having good customization, mkDerivation is + # used instead. + stdenv.mkDerivation (self: { + # START: Reevaluate the below attrs when + # https://github.com/NixOS/nixpkgs/pull/354999 + # or + # https://github.com/NixOS/nixpkgs/pull/194475 + # Are merged. + + # TODO: Probably change to cargoLock + cargoDeps = rustPlatform.importCargoLock { + lockFile = ./Cargo.lock; + }; + + nativeBuildInputs = [ + rustPlatform.rust.rustc # TODO: Remove + rustPlatform.rust.cargo # TODO: Remove + pkgs.installShellFiles + pkgs.git + ]; + + # TODO: Remove entire attr + buildInputs = with rustPlatform; [ + cargoSetupHook + cargoBuildHook + cargoInstallHook + ]; + + # Use Helix's opt profile for the build. + # TODO: s/cargoBuildType/buildType + cargoBuildType = "release"; + # END: Funny attrs to reevaluate + + name = with builtins; (fromTOML (readFile ./helix-term/Cargo.toml)).package.name; + src = fs.toSource { + root = ./.; + fileset = src; + }; + + # Helix attempts to reach out to the network and get the grammars. Nix doesn't allow this. + HELIX_DISABLE_AUTO_GRAMMAR_BUILD = "1"; + + # So Helix knows what rev it is. + HELIX_NIX_BUILD_REV = self.rev or self.dirtyRev or null; + + doCheck = false; + strictDeps = true; + + # Sets the Helix runtimedir to the grammars + env.HELIX_DEFAULT_RUNTIME = "${runtimeDir}"; + + # Get all the application stuff in the output directory. + postInstall = '' + mkdir -p $out/lib + installShellCompletion ${./contrib/completion}/hx.{bash,fish,zsh} + mkdir -p $out/share/{applications,icons/hicolor/256x256/apps} + cp ${./contrib/Helix.desktop} $out/share/applications + cp ${./contrib/helix.png} $out/share/icons/hicolor/256x256/apps + ''; + + meta.mainProgram = "hx"; + }) diff --git a/flake.nix b/flake.nix index 16907940a3f4..586321ba4348 100644 --- a/flake.nix +++ b/flake.nix @@ -17,102 +17,6 @@ rust-overlay, ... }: let - mkHelix = { - pkgs, - rustPlatform, - stdenv, - ... - }: let - fs = pkgs.lib.fileset; - - src = fs.difference (fs.gitTracked ./.) (fs.unions [ - ./.envrc - ./rustfmt.toml - ./screenshot.png - ./book - ./docs - ./flake.lock - (fs.fileFilter (file: pkgs.lib.strings.hasInfix ".git" file.name) ./.) - (fs.fileFilter (file: file.hasExt "svg") ./.) - (fs.fileFilter (file: file.hasExt "md") ./.) - (fs.fileFilter (file: file.hasExt "nix") ./.) - ]); - - # Next we actually need to build the grammars and the runtime directory - # that they reside in. It is built by calling the derivation in the - # grammars.nix file, then taking the runtime directory in the git repo - # and hooking symlinks up to it. - grammars = pkgs.callPackage ./grammars.nix {}; - runtimeDir = pkgs.runCommand "helix-runtime" {} '' - mkdir -p $out - ln -s ${./runtime}/* $out - rm -r $out/grammars - ln -s ${grammars} $out/grammars - ''; - in - # Currently rustPlatform.buildRustPackage doesn't have the finalAttrs pattern - # hooked up. To get around this while having good customization, mkDerivation is - # used instead. - stdenv.mkDerivation (self: { - # START: Reevaluate the below attrs when - # https://github.com/NixOS/nixpkgs/pull/354999 - # or - # https://github.com/NixOS/nixpkgs/pull/194475 - # Are merged. - - # TODO: Probably change to cargoLock - cargoDeps = rustPlatform.importCargoLock { - lockFile = ./Cargo.lock; - }; - - nativeBuildInputs = [ - rustPlatform.rust.rustc # TODO: Remove - rustPlatform.rust.cargo # TODO: Remove - pkgs.installShellFiles - pkgs.git - ]; - - # TODO: Remove entire attr - buildInputs = with rustPlatform; [ - cargoSetupHook - cargoBuildHook - cargoInstallHook - ]; - - # Use Helix's opt profile for the build. - # TODO: s/cargoBuildType/buildType - cargoBuildType = "release"; - # END: Funny attrs to reevaluate - - name = with builtins; (fromTOML (readFile ./helix-term/Cargo.toml)).package.name; - src = fs.toSource { - root = ./.; - fileset = src; - }; - - # Helix attempts to reach out to the network and get the grammars. Nix doesn't allow this. - HELIX_DISABLE_AUTO_GRAMMAR_BUILD = "1"; - - # So Helix knows what rev it is. - HELIX_NIX_BUILD_REV = self.rev or self.dirtyRev or null; - - doCheck = false; - strictDeps = true; - - # Sets the Helix runtimedir to the grammars - env.HELIX_DEFAULT_RUNTIME = "${runtimeDir}"; - - # Get all the application stuff in the output directory. - postInstall = '' - mkdir -p $out/lib - installShellCompletion ${./contrib/completion}/hx.{bash,fish,zsh} - mkdir -p $out/share/{applications,icons/hicolor/256x256/apps} - cp ${./contrib/Helix.desktop} $out/share/applications - cp ${./contrib/helix.png} $out/share/icons/hicolor/256x256/apps - ''; - - meta.mainProgram = "hx"; - }); in flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs { @@ -128,7 +32,7 @@ }; in { packages = rec { - helix = pkgs.callPackage mkHelix {}; + helix = pkgs.callPackage ./default.nix {}; # The default Helix build. Uses the latest stable Rust toolchain, and unstable # nixpkgs. @@ -168,7 +72,7 @@ }) // { overlays.default = final: prev: { - helix = final.callPackage mkHelix {}; + helix = final.callPackage ./default.nix {}; }; }; From 4edfad05ca668ff022878c7011693054446b3c76 Mon Sep 17 00:00:00 2001 From: Ross Smyth <18294397+RossSmyth@users.noreply.github.com> Date: Thu, 27 Feb 2025 11:22:07 -0500 Subject: [PATCH 20/22] Bump flake lock and use rustPackage finalAttrs --- default.nix | 22 +++------------------- flake.lock | 12 ++++++------ flake.nix | 2 +- 3 files changed, 10 insertions(+), 26 deletions(-) diff --git a/default.nix b/default.nix index d8b74e2ce116..eeb910577734 100644 --- a/default.nix +++ b/default.nix @@ -34,36 +34,20 @@ in # Currently rustPlatform.buildRustPackage doesn't have the finalAttrs pattern # hooked up. To get around this while having good customization, mkDerivation is # used instead. - stdenv.mkDerivation (self: { + rustPlatform.buildRustPackage (self: { # START: Reevaluate the below attrs when # https://github.com/NixOS/nixpkgs/pull/354999 # or # https://github.com/NixOS/nixpkgs/pull/194475 # Are merged. - - # TODO: Probably change to cargoLock - cargoDeps = rustPlatform.importCargoLock { - lockFile = ./Cargo.lock; - }; + cargoLock.lockFile = ./Cargo.lock; nativeBuildInputs = [ - rustPlatform.rust.rustc # TODO: Remove - rustPlatform.rust.cargo # TODO: Remove pkgs.installShellFiles pkgs.git ]; - # TODO: Remove entire attr - buildInputs = with rustPlatform; [ - cargoSetupHook - cargoBuildHook - cargoInstallHook - ]; - - # Use Helix's opt profile for the build. - # TODO: s/cargoBuildType/buildType - cargoBuildType = "release"; - # END: Funny attrs to reevaluate + buildType = "release"; name = with builtins; (fromTOML (readFile ./helix-term/Cargo.toml)).package.name; src = fs.toSource { diff --git a/flake.lock b/flake.lock index 53f4cf7f0276..7e3d5bd3c982 100644 --- a/flake.lock +++ b/flake.lock @@ -20,11 +20,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1728018373, - "narHash": "sha256-NOiTvBbRLIOe5F6RbHaAh6++BNjsb149fGZd1T4+KBg=", + "lastModified": 1740560979, + "narHash": "sha256-Vr3Qi346M+8CjedtbyUevIGDZW8LcA1fTG0ugPY/Hic=", "owner": "nixos", "repo": "nixpkgs", - "rev": "bc947f541ae55e999ffdb4013441347d83b00feb", + "rev": "5135c59491985879812717f4c9fea69604e7f26f", "type": "github" }, "original": { @@ -48,11 +48,11 @@ ] }, "locked": { - "lastModified": 1737599167, - "narHash": "sha256-S2rHCrQWCDVp63XxL/AQbGr1g5M8Zx14C7Jooa4oM8o=", + "lastModified": 1740623427, + "narHash": "sha256-3SdPQrZoa4odlScFDUHd4CUPQ/R1gtH4Mq9u8CBiK8M=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "38374302ae9edf819eac666d1f276d62c712dd06", + "rev": "d342e8b5fd88421ff982f383c853f0fc78a847ab", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 586321ba4348..23d6141c6ba5 100644 --- a/flake.nix +++ b/flake.nix @@ -43,7 +43,7 @@ }; checks.helix = self.outputs.packages.${system}.helix.override { - cargoBuildType = "debug"; + buildType = "debug"; rustPlatform = msrvPlatform; }; From f261ac85963bd5e811321e661dde91f878961886 Mon Sep 17 00:00:00 2001 From: Ross Smyth <18294397+RossSmyth@users.noreply.github.com> Date: Sun, 2 Mar 2025 16:54:25 -0500 Subject: [PATCH 21/22] Fix some nits in the default --- default.nix | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/default.nix b/default.nix index eeb910577734..950d8b855d6c 100644 --- a/default.nix +++ b/default.nix @@ -1,10 +1,15 @@ { pkgs, + lib, rustPlatform, stdenv, + callPackage, + runCommand, + installShellFiles, + git, ... }: let - fs = pkgs.lib.fileset; + fs = lib.fileset; src = fs.difference (fs.gitTracked ./.) (fs.unions [ ./.envrc @@ -13,7 +18,7 @@ ./book ./docs ./flake.lock - (fs.fileFilter (file: pkgs.lib.strings.hasInfix ".git" file.name) ./.) + (fs.fileFilter (file: lib.strings.hasInfix ".git" file.name) ./.) (fs.fileFilter (file: file.hasExt "svg") ./.) (fs.fileFilter (file: file.hasExt "md") ./.) (fs.fileFilter (file: file.hasExt "nix") ./.) @@ -23,8 +28,8 @@ # that they reside in. It is built by calling the derivation in the # grammars.nix file, then taking the runtime directory in the git repo # and hooking symlinks up to it. - grammars = pkgs.callPackage ./grammars.nix {}; - runtimeDir = pkgs.runCommand "helix-runtime" {} '' + grammars = callPackage ./grammars.nix {}; + runtimeDir = runCommand "helix-runtime" {} '' mkdir -p $out ln -s ${./runtime}/* $out rm -r $out/grammars @@ -35,16 +40,11 @@ in # hooked up. To get around this while having good customization, mkDerivation is # used instead. rustPlatform.buildRustPackage (self: { - # START: Reevaluate the below attrs when - # https://github.com/NixOS/nixpkgs/pull/354999 - # or - # https://github.com/NixOS/nixpkgs/pull/194475 - # Are merged. cargoLock.lockFile = ./Cargo.lock; nativeBuildInputs = [ - pkgs.installShellFiles - pkgs.git + installShellFiles + git ]; buildType = "release"; From f8b8da5ec2448e31b99b2f42afcdd0ab2fc2b977 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Tue, 4 Mar 2025 11:21:12 -0500 Subject: [PATCH 22/22] minor: Remove unused let+in, unused bindings from default.nix --- default.nix | 2 -- flake.nix | 3 +-- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/default.nix b/default.nix index 950d8b855d6c..0efa75bb4dab 100644 --- a/default.nix +++ b/default.nix @@ -1,8 +1,6 @@ { - pkgs, lib, rustPlatform, - stdenv, callPackage, runCommand, installShellFiles, diff --git a/flake.nix b/flake.nix index 23d6141c6ba5..7d176cb01e48 100644 --- a/flake.nix +++ b/flake.nix @@ -16,8 +16,7 @@ flake-utils, rust-overlay, ... - }: let - in + }: flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs { inherit system;