nix: incremental builds with crane

This commit is contained in:
Rexiel Scarlet 2025-08-14 01:05:58 +04:00
commit d2996ba521
No known key found for this signature in database
3 changed files with 65 additions and 18 deletions

16
flake.lock generated
View file

@ -1,5 +1,20 @@
{ {
"nodes": { "nodes": {
"crane": {
"locked": {
"lastModified": 1754269165,
"narHash": "sha256-0tcS8FHd4QjbCVoxN9jI+PjHgA4vc/IjkUSp+N3zy0U=",
"owner": "ipetkov",
"repo": "crane",
"rev": "444e81206df3f7d92780680e45858e31d2f07a08",
"type": "github"
},
"original": {
"owner": "ipetkov",
"repo": "crane",
"type": "github"
}
},
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1754725699, "lastModified": 1754725699,
@ -18,6 +33,7 @@
}, },
"root": { "root": {
"inputs": { "inputs": {
"crane": "crane",
"nixpkgs": "nixpkgs" "nixpkgs": "nixpkgs"
} }
} }

View file

@ -1,16 +1,20 @@
{ {
inputs.nixpkgs.url = "github:NixOS/nixpkgs?ref=nixos-unstable"; inputs.nixpkgs.url = "github:NixOS/nixpkgs?ref=nixos-unstable";
inputs.crane.url = "github:ipetkov/crane";
outputs = { outputs = {
self, self,
nixpkgs, nixpkgs,
crane,
}: let }: let
systems = ["x86_64-linux" "aarch64-linux"]; systems = ["x86_64-linux" "aarch64-linux"];
forEachSystem = nixpkgs.lib.genAttrs systems; forEachSystem = nixpkgs.lib.genAttrs systems;
pkgsForEach = nixpkgs.legacyPackages; pkgsForEach = nixpkgs.legacyPackages;
in { in {
packages = forEachSystem (system: { packages = forEachSystem (system: {
default = pkgsForEach.${system}.callPackage ./nix/package.nix {}; default = pkgsForEach.${system}.callPackage ./nix/package.nix {
craneLib = crane.mkLib pkgsForEach.${system};
};
}); });
devShells = forEachSystem (system: { devShells = forEachSystem (system: {

View file

@ -1,11 +1,12 @@
{ {
lib, lib,
rustPlatform, craneLib,
}: runCommandNoCCLocal,
rustPlatform.buildRustPackage (finalAttrs: { }: let
inherit (craneLib) buildDepsOnly buildPackage mkDummySrc;
pname = "stash"; pname = "stash";
version = (builtins.fromTOML (builtins.readFile ../Cargo.toml)).package.version; version = (builtins.fromTOML (builtins.readFile ../Cargo.toml)).package.version;
src = let src = let
fs = lib.fileset; fs = lib.fileset;
s = ../.; s = ../.;
@ -19,18 +20,44 @@ rustPlatform.buildRustPackage (finalAttrs: {
]; ];
}; };
cargoLock.lockFile = "${finalAttrs.src}/Cargo.lock"; # basically avoid crane rebuilding everything
enableParallelBuilding = true; # when the package version changes
replacedSrc = let
rgxIn = ''
name = "${pname}"
version = "${version}"
'';
rgxOut = ''
name = "${pname}"
version = "0.9.6"
'';
in
runCommandNoCCLocal "bakaSrc" {} ''
cp -r ${src} $out
substituteInPlace $out/Cargo.toml \
--replace-fail '${rgxIn}' '${rgxOut}'
substituteInPlace $out/Cargo.lock \
--replace-fail '${rgxIn}' '${rgxOut}'
'';
postInstall = '' cargoArtifacts = buildDepsOnly {
mkdir -p $out name = "${pname}-deps";
install -Dm755 ${../vendor/stash.service} $out/share/stash.service strictDeps = true;
''; dummySrc = mkDummySrc {src = replacedSrc;};
meta = {
description = "Wayland clipboard manager with fast persistent history and multi-media support";
maintainers = [lib.maintainers.NotAShelf];
license = lib.licenses.mpl20;
mainProgram = "stash";
}; };
}) in
buildPackage {
inherit cargoArtifacts pname src version;
strictDeps = true;
postInstall = ''
mkdir -p $out
install -Dm755 ${../vendor/stash.service} $out/share/stash.service
'';
meta = {
description = "Wayland clipboard manager with fast persistent history and multi-media support";
maintainers = [lib.maintainers.NotAShelf];
license = lib.licenses.mpl20;
mainProgram = "stash";
};
}