on master help sign in
Refactor Nix flake to use crane (#7763)

This resolves a build issue with nci/dream2nix and git dependencies. We can keep most of the helix-specific parts of the flake, we just need to switch from configuring nci to calling craneLib functions. We also switch to flake-utils from flake-parts: * Using rust-overlay with flake-parts directly is unergonomic (see https://github.com/hercules-ci/flake-parts/discussions/83). * Removing flake-parts reduces the overall dependencies: rust-overlay already depends on flake-utils.

commitd6856cfeec14305b3405a6aee90c73c80b41e48f authorMichael Davis <mcarsondavis@gmail.com> dateThu 27 Jul 2023, 23:30 (3 years ago) parent8a28f30
2 files changed+192−361
flake.lock+25−218 flake.nix+167−143
flake.lock+25−218
@@ -1,110 +1,29 @@
11{
22 "nodes": {
33 "crane": {
4− "flake": false,
5− "locked": {
6− "lastModified": 1681175776,
7− "narHash": "sha256-7SsUy9114fryHAZ8p1L6G6YSu7jjz55FddEwa2U8XZc=",
8− "owner": "ipetkov",
9− "repo": "crane",
10− "rev": "445a3d222947632b5593112bb817850e8a9cf737",
11− "type": "github"
12− },
13− "original": {
14− "owner": "ipetkov",
15− "ref": "v0.12.1",
16− "repo": "crane",
17− "type": "github"
18− }
19− },
20− "dream2nix": {
214 "inputs": {
22− "all-cabal-json": [
23− "nci"
24− ],
25− "crane": "crane",
26− "devshell": [
27− "nci"
28− ],
29− "drv-parts": "drv-parts",
305 "flake-compat": "flake-compat",
31− "flake-parts": [
32− "nci",
33− "parts"
34− ],
35− "flake-utils-pre-commit": [
36− "nci"
37− ],
38− "ghc-utils": [
39− "nci"
40− ],
41− "gomod2nix": [
42− "nci"
43− ],
44− "mach-nix": [
45− "nci"
46− ],
47− "nix-pypi-fetcher": [
48− "nci"
6+ "flake-utils": [
7+ "flake-utils"
498 ],
509 "nixpkgs": [
51− "nci",
5210 "nixpkgs"
5311 ],
54− "nixpkgsV1": "nixpkgsV1",
55− "poetry2nix": [
56− "nci"
57− ],
58− "pre-commit-hooks": [
59− "nci"
60− ],
61− "pruned-racket-catalog": [
62− "nci"
63− ]
64− },
65− "locked": {
66− "lastModified": 1683212002,
67− "narHash": "sha256-EObtqyQsv9v+inieRY5cvyCMCUI5zuU5qu+1axlJCPM=",
68− "owner": "nix-community",
69− "repo": "dream2nix",
70− "rev": "fbfb09d2ab5ff761d822dd40b4a1def81651d096",
71− "type": "github"
72− },
73− "original": {
74− "owner": "nix-community",
75− "repo": "dream2nix",
76− "type": "github"
77− }
78− },
79− "drv-parts": {
80− "inputs": {
81− "flake-compat": [
82− "nci",
83− "dream2nix",
84− "flake-compat"
85− ],
86− "flake-parts": [
87− "nci",
88− "dream2nix",
89− "flake-parts"
90− ],
91− "nixpkgs": [
92− "nci",
93− "dream2nix",
94− "nixpkgs"
12+ "rust-overlay": [
13+ "rust-overlay"
9514 ]
9615 },
9716 "locked": {
98− "lastModified": 1680698112,
99− "narHash": "sha256-FgnobN/DvCjEsc0UAZEAdPLkL4IZi2ZMnu2K2bUaElc=",
100− "owner": "davhau",
101− "repo": "drv-parts",
102− "rev": "e8c2ec1157dc1edb002989669a0dbd935f430201",
17+ "lastModified": 1688772518,
18+ "narHash": "sha256-ol7gZxwvgLnxNSZwFTDJJ49xVY5teaSvF7lzlo3YQfM=",
19+ "owner": "ipetkov",
20+ "repo": "crane",
21+ "rev": "8b08e96c9af8c6e3a2b69af5a7fa168750fcf88e",
10322 "type": "github"
10423 },
10524 "original": {
106− "owner": "davhau",
107− "repo": "drv-parts",
25+ "owner": "ipetkov",
26+ "repo": "crane",
10827 "type": "github"
10928 }
11029 },
@@ -129,11 +48,11 @@
12948 "systems": "systems"
13049 },
13150 "locked": {
132− "lastModified": 1681202837,
133− "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=",
51+ "lastModified": 1689068808,
52+ "narHash": "sha256-6ixXo3wt24N/melDWjq70UuHQLxGV8jZvooRanIHXw0=",
13453 "owner": "numtide",
13554 "repo": "flake-utils",
136− "rev": "cfacdce06f30d2b68473a46042957675eebb3401",
55+ "rev": "919d646de7be200f3bf08cb76ae1f09402b6f9b4",
13756 "type": "github"
13857 },
13958 "original": {
@@ -142,55 +61,13 @@
14261 "type": "github"
14362 }
14463 },
145− "mk-naked-shell": {
146− "flake": false,
147− "locked": {
148− "lastModified": 1681286841,
149− "narHash": "sha256-3XlJrwlR0nBiREnuogoa5i1b4+w/XPe0z8bbrJASw0g=",
150− "owner": "yusdacra",
151− "repo": "mk-naked-shell",
152− "rev": "7612f828dd6f22b7fb332cc69440e839d7ffe6bd",
153− "type": "github"
154− },
155− "original": {
156− "owner": "yusdacra",
157− "repo": "mk-naked-shell",
158− "type": "github"
159− }
160− },
161− "nci": {
162− "inputs": {
163− "dream2nix": "dream2nix",
164− "mk-naked-shell": "mk-naked-shell",
165− "nixpkgs": [
166− "nixpkgs"
167− ],
168− "parts": "parts",
169− "rust-overlay": [
170− "rust-overlay"
171− ]
172− },
173− "locked": {
174− "lastModified": 1683699050,
175− "narHash": "sha256-UWKQpzVcSshB+sU2O8CCHjOSTQrNS7Kk9V3+UeBsJpg=",
176− "owner": "yusdacra",
177− "repo": "nix-cargo-integration",
178− "rev": "ed27173cd1b223f598343ea3c15aacb1d140feac",
179− "type": "github"
180− },
181− "original": {
182− "owner": "yusdacra",
183− "repo": "nix-cargo-integration",
184− "type": "github"
185− }
186− },
18764 "nixpkgs": {
18865 "locked": {
189− "lastModified": 1683408522,
190− "narHash": "sha256-9kcPh6Uxo17a3kK3XCHhcWiV1Yu1kYj22RHiymUhMkU=",
66+ "lastModified": 1690272529,
67+ "narHash": "sha256-MakzcKXEdv/I4qJUtq/k/eG+rVmyOZLnYNC2w1mB59Y=",
19168 "owner": "nixos",
19269 "repo": "nixpkgs",
193− "rev": "897876e4c484f1e8f92009fd11b7d988a121a4e7",
70+ "rev": "ef99fa5c5ed624460217c31ac4271cfb5cb2502c",
19471 "type": "github"
19572 },
19673 "original": {
@@ -200,99 +77,29 @@
20077 "type": "github"
20178 }
20279 },
203− "nixpkgs-lib": {
204− "locked": {
205− "dir": "lib",
206− "lastModified": 1682879489,
207− "narHash": "sha256-sASwo8gBt7JDnOOstnps90K1wxmVfyhsTPPNTGBPjjg=",
208− "owner": "NixOS",
209− "repo": "nixpkgs",
210− "rev": "da45bf6ec7bbcc5d1e14d3795c025199f28e0de0",
211− "type": "github"
212− },
213− "original": {
214− "dir": "lib",
215− "owner": "NixOS",
216− "ref": "nixos-unstable",
217− "repo": "nixpkgs",
218− "type": "github"
219− }
220− },
221− "nixpkgsV1": {
222− "locked": {
223− "lastModified": 1678500271,
224− "narHash": "sha256-tRBLElf6f02HJGG0ZR7znMNFv/Uf7b2fFInpTHiHaSE=",
225− "owner": "NixOS",
226− "repo": "nixpkgs",
227− "rev": "5eb98948b66de29f899c7fe27ae112a47964baf8",
228− "type": "github"
229− },
230− "original": {
231− "id": "nixpkgs",
232− "ref": "nixos-22.11",
233− "type": "indirect"
234− }
235− },
236− "parts": {
237− "inputs": {
238− "nixpkgs-lib": [
239− "nci",
240− "nixpkgs"
241− ]
242− },
243− "locked": {
244− "lastModified": 1683560683,
245− "narHash": "sha256-XAygPMN5Xnk/W2c1aW0jyEa6lfMDZWlQgiNtmHXytPc=",
246− "owner": "hercules-ci",
247− "repo": "flake-parts",
248− "rev": "006c75898cf814ef9497252b022e91c946ba8e17",
249− "type": "github"
250− },
251− "original": {
252− "owner": "hercules-ci",
253− "repo": "flake-parts",
254− "type": "github"
255− }
256− },
257− "parts_2": {
258− "inputs": {
259− "nixpkgs-lib": "nixpkgs-lib"
260− },
261− "locked": {
262− "lastModified": 1683560683,
263− "narHash": "sha256-XAygPMN5Xnk/W2c1aW0jyEa6lfMDZWlQgiNtmHXytPc=",
264− "owner": "hercules-ci",
265− "repo": "flake-parts",
266− "rev": "006c75898cf814ef9497252b022e91c946ba8e17",
267− "type": "github"
268− },
269− "original": {
270− "owner": "hercules-ci",
271− "repo": "flake-parts",
272− "type": "github"
273− }
274− },
27580 "root": {
27681 "inputs": {
277− "nci": "nci",
82+ "crane": "crane",
83+ "flake-utils": "flake-utils",
27884 "nixpkgs": "nixpkgs",
279− "parts": "parts_2",
28085 "rust-overlay": "rust-overlay"
28186 }
28287 },
28388 "rust-overlay": {
28489 "inputs": {
285− "flake-utils": "flake-utils",
90+ "flake-utils": [
91+ "flake-utils"
92+ ],
28693 "nixpkgs": [
28794 "nixpkgs"
28895 ]
28996 },
29097 "locked": {
291− "lastModified": 1683771545,
292− "narHash": "sha256-we0GYcKTo2jRQGmUGrzQ9VH0OYAUsJMCsK8UkF+vZUA=",
98+ "lastModified": 1690424156,
99+ "narHash": "sha256-Bpml+L280tHTQpwpC5/BJbU4HSvEzMvW8IZ4gAXimhE=",
293100 "owner": "oxalica",
294101 "repo": "rust-overlay",
295− "rev": "c57e210faf68e5d5386f18f1b17ad8365d25e4ed",
102+ "rev": "f335a0213504c7e6481c359dc1009be9cf34432c",
296103 "type": "github"
297104 },
298105 "original": {
flake.nix+167−143
@@ -3,166 +3,190 @@
33
44 inputs = {
55 nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
6+ flake-utils.url = "github:numtide/flake-utils";
67 rust-overlay = {
78 url = "github:oxalica/rust-overlay";
8− inputs.nixpkgs.follows = "nixpkgs";
9+ inputs = {
10+ nixpkgs.follows = "nixpkgs";
11+ flake-utils.follows = "flake-utils";
12+ };
913 };
10− nci = {
11− url = "github:yusdacra/nix-cargo-integration";
12− inputs.nixpkgs.follows = "nixpkgs";
14+ crane = {
15+ url = "github:ipetkov/crane";
1316 inputs.rust-overlay.follows = "rust-overlay";
17+ inputs.flake-utils.follows = "flake-utils";
18+ inputs.nixpkgs.follows = "nixpkgs";
1419 };
15− parts.url = "github:hercules-ci/flake-parts";
1620 };
1721
18− outputs = inp: let
19− mkRootPath = rel:
20− builtins.path {
21− path = "${toString ./.}/${rel}";
22− name = rel;
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)];
2334 };
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 inp.parts.flakeModules.easyOverlay];
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− '';
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.svg"
47+ "logo_dark.svg"
48+ "logo_light.svg"
49+ "rust-toolchain.toml"
50+ "rustfmt.toml"
51+ "runtime"
52+ "screenshot.png"
53+ "book"
54+ "contrib"
55+ "docs"
56+ "README.md"
57+ "CHANGELOG.md"
58+ "shell.nix"
59+ "default.nix"
60+ "grammars.nix"
61+ "flake.nix"
62+ "flake.lock"
63+ ];
64+ ignorePaths = path: type: let
65+ inherit (nixpkgs) lib;
66+ # split the nix store path into its components
67+ components = lib.splitString "/" path;
68+ # drop off the `/nix/hash-source` section from the path
69+ relPathComponents = lib.drop 4 components;
70+ # reassemble the path components
71+ relPath = lib.concatStringsSep "/" relPathComponents;
10772 in
108− helix-wrapped
109− // {
110− override = makeOverridableHelix old;
73+ lib.all (p: ! (lib.hasPrefix p relPath)) pathsToIgnore;
74+ in
75+ builtins.path {
76+ name = "helix-source";
77+ path = toString ./.;
78+ # filter out unnecessary paths
79+ filter = ignorePaths;
80+ };
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 {};
11195 passthru =
112− helix-wrapped.passthru
96+ (old.passthru or {})
11397 // {
114− wrapper = old: makeOverridableHelix old config;
98+ unwrapped = old;
11599 };
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− nci.projects."helix-project".relPath = "";
127− nci.crates."helix-term" = {
128− overrides = {
129− add-meta.override = _: {meta.mainProgram = "hx";};
130− add-inputs.overrideAttrs = prev: {
131− buildInputs = (prev.buildInputs or []) ++ [stdenv.cc.cc.lib];
132− };
133− disable-grammar-builds = {
134− # disable fetching and building of tree-sitter grammars in the helix-term build.rs
135− HELIX_DISABLE_AUTO_GRAMMAR_BUILD = "1";
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;
136115 };
137− disable-tests = {checkPhase = ":";};
138− set-stdenv.override = _: {inherit stdenv;};
139− set-filtered-src.override = _: {src = filteredSource;};
140− };
141116 };
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+ rustToolchain = pkgs.pkgsBuildHost.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
126+ craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
127+ commonArgs =
128+ {
129+ inherit stdenv;
130+ src = filteredSource;
131+ # disable fetching and building of tree-sitter grammars in the helix-term build.rs
132+ HELIX_DISABLE_AUTO_GRAMMAR_BUILD = "1";
133+ buildInputs = [stdenv.cc.cc.lib];
134+ # disable tests
135+ doCheck = false;
136+ meta.mainProgram = "hx";
137+ }
138+ // craneLib.crateNameFromCargoToml {cargoToml = ./helix-term/Cargo.toml;};
139+ cargoArtifacts = craneLib.buildDepsOnly commonArgs;
140+ in {
141+ packages = {
142+ helix-unwrapped = craneLib.buildPackage (commonArgs
143+ // {
144+ inherit cargoArtifacts;
145+ });
146+ helix = makeOverridableHelix self.packages.${system}.helix-unwrapped {};
147+ default = self.packages.${system}.helix;
148+ };
142149
143− packages.helix-unwrapped = config.nci.outputs."helix-term".packages.release;
144− packages.helix-unwrapped-dev = config.nci.outputs."helix-term".packages.dev;
145− packages.helix = makeOverridableHelix config.packages.helix-unwrapped {};
146− packages.helix-dev = makeOverridableHelix config.packages.helix-unwrapped-dev {};
147− packages.default = config.packages.helix;
150+ checks = {
151+ # Build the crate itself
152+ inherit (self.packages.${system}) helix;
148153
149− overlayAttrs = {
150− inherit (config.packages) helix;
151− };
154+ clippy = craneLib.cargoClippy (commonArgs
155+ // {
156+ inherit cargoArtifacts;
157+ cargoClippyExtraArgs = "--all-targets -- --deny warnings";
158+ });
152159
153− devShells.default = config.nci.outputs."helix-project".devShell.overrideAttrs (old: {
154− nativeBuildInputs =
155− (old.nativeBuildInputs or [])
156− ++ (with pkgs; [lld_13 cargo-flamegraph rust-analyzer])
157− ++ (lib.optional (stdenv.isx86_64 && stdenv.isLinux) pkgs.cargo-tarpaulin)
158− ++ (lib.optional stdenv.isLinux pkgs.lldb)
159− ++ (lib.optional stdenv.isDarwin pkgs.darwin.apple_sdk.frameworks.CoreFoundation);
160− shellHook = ''
161− export HELIX_RUNTIME="$PWD/runtime"
162− export RUST_BACKTRACE="1"
163− export RUSTFLAGS="${rustFlagsEnv}"
164− '';
165− });
160+ fmt = craneLib.cargoFmt commonArgs;
161+
162+ doc = craneLib.cargoDoc (commonArgs
163+ // {
164+ inherit cargoArtifacts;
165+ });
166+
167+ test = craneLib.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="${rustFlagsEnv}"
184+ '';
185+ };
186+ })
187+ // {
188+ overlays.default = final: prev: {
189+ inherit (self.packages.${final.system}) helix;
166190 };
167191 };
168192