Skip to content

Commit

Permalink
chore: move to crane for building the package
Browse files Browse the repository at this point in the history
Crane makes it easier to reuse artifacts when building multiple features
at the same time.

(almost) scientific test showed reduction of build time from ~5 minutes
(naersk) to ~2 (crane).

This should not change user experience.
  • Loading branch information
VTimofeenko committed Dec 4, 2023
1 parent b63ad27 commit 1bfd363
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 45 deletions.
57 changes: 22 additions & 35 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,21 @@
flake-parts.url = "github:hercules-ci/flake-parts";
devshell.url = "github:numtide/devshell";
# Utils for building Rust stuff
naersk.url = "github:nmattia/naersk/master";

crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
};
# fenix = {
# url = "github:nix-community/fenix";
# inputs.nixpkgs.follows = "nixpkgs";
# inputs.rust-analyzer-src.follows = "";
# };
# advisory-db = {
# url = "github:rustsec/advisory-db";
# flake = false;
# };

# The Rust source for xremap
xremap = {
url = "github:k0kubun/xremap?ref=v0.8.12";
Expand Down Expand Up @@ -39,11 +53,11 @@
];
perSystem = { config, self', inputs', pkgs, system, ... }:
let
naersk-lib = pkgs.callPackage inputs.naersk { };
craneLib = inputs.crane.lib.${system};
in
{
formatter = pkgs.nixpkgs-fmt;
packages = import ./overlay { inherit (inputs) xremap; inherit naersk-lib pkgs; };
packages = import ./overlay { inherit (inputs) xremap; inherit craneLib pkgs; };
# This way all packages get immediately added to the overlay except for the one called literally called "default"
overlayAttrs = builtins.removeAttrs config.packages [ "default" ];
devshells.default = {
Expand Down
28 changes: 21 additions & 7 deletions overlay/default.nix
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
# Overlay file that contains the definition of building a package
{ xremap, naersk-lib, pkgs }:
{ xremap, craneLib, pkgs }:
let
packageWithFeature = feature: naersk-lib.buildPackage {
root = xremap;
cargoBuildOptions = (x: x ++ pkgs.lib.optional (feature != null) "--features ${feature}");
commonArgs = {
src = xremap;
strictDeps = true;

buildInputs = [
# Add additional build inputs here
];

# Additional environment variables can be set directly
# MY_CUSTOM_VAR = "some value";
};
cargoArtifacts = craneLib.buildDepsOnly commonArgs;

packageWithFeature = feature: craneLib.buildPackage (commonArgs // {
inherit cargoArtifacts;
cargoExtraArgs = "--locked${if (feature != null) then " --features ${feature}" else ""}";
# cargoBuildOptions = (x: x ++ pkgs.lib.optional (feature != null) "--features ${feature}");
# The following two options are for introspection to be able to see if sway/gnome were actually pulled in
# To see that - visually inspect the deps directory inside result/target/ and check for swayipc/zbus
# See cargo.toml for feature-specific deps
copyTarget = true;
compressTarget = false;
# copyTarget = true;
# compressTarget = false;
meta.mainProgram = "xremap";
};
});
in
rec {
# No features
Expand Down

0 comments on commit 1bfd363

Please sign in to comment.