Merge pull request #11 from Rexcrazy804/nix-incremental

nix: incremental builds with crane
This commit is contained in:
raf 2025-08-14 18:29:13 +03:00 committed by GitHub
commit dd1c3b22da
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 84 additions and 18 deletions

19
.github/workflows/nix-cache.yaml vendored Normal file
View file

@ -0,0 +1,19 @@
name: "Populate cachix cache"
on:
workflow_dispatch:
push:
branches: [ master ]
paths: [ 'src/**.rs', 'Cargo.toml', 'Cargo.lock', 'nix/package.nix' ]
jobs:
populate-cache:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: cachix/install-nix-action@v25
with:
nix_path: nixpkgs=channel:nixos-unstable
- uses: cachix/cachix-action@v14
with:
name: nyx
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
- run: nix build

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,9 +20,35 @@ 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}'
'';
cargoArtifacts = buildDepsOnly {
name = "${pname}-deps";
strictDeps = true;
dummySrc = mkDummySrc {src = replacedSrc;};
};
in
buildPackage {
inherit cargoArtifacts pname src version;
strictDeps = true;
postInstall = '' postInstall = ''
mkdir -p $out mkdir -p $out
install -Dm755 ${../vendor/stash.service} $out/share/stash.service install -Dm755 ${../vendor/stash.service} $out/share/stash.service
@ -33,4 +60,4 @@ rustPlatform.buildRustPackage (finalAttrs: {
license = lib.licenses.mpl20; license = lib.licenses.mpl20;
mainProgram = "stash"; mainProgram = "stash";
}; };
}) }