nix: build with crane
All checks were successful
Build and test / check (push) Successful in 1m10s
Build and test / build (push) Successful in 1m35s
Build and test / test (push) Successful in 2m47s
Build and test / docs (push) Successful in 3m1s

This commit is contained in:
2026-02-12 11:41:08 +09:00
parent ba7a7f2b26
commit f9e60b0f03
4 changed files with 96 additions and 18 deletions

16
flake.lock generated
View File

@@ -1,5 +1,20 @@
{
"nodes": {
"crane": {
"locked": {
"lastModified": 1770419512,
"narHash": "sha256-o8Vcdz6B6bkiGUYkZqFwH3Pv1JwZyXht3dMtS7RchIo=",
"owner": "ipetkov",
"repo": "crane",
"rev": "2510f2cbc3ccd237f700bb213756a8f35c32d8d7",
"type": "github"
},
"original": {
"owner": "ipetkov",
"repo": "crane",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1769170682,
@@ -18,6 +33,7 @@
},
"root": {
"inputs": {
"crane": "crane",
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
}

View File

@@ -4,9 +4,11 @@
rust-overlay.url = "github:oxalica/rust-overlay";
rust-overlay.inputs.nixpkgs.follows = "nixpkgs";
crane.url = "github:ipetkov/crane";
};
outputs = { self, nixpkgs, rust-overlay}:
outputs = { self, nixpkgs, rust-overlay, crane }:
let
inherit (nixpkgs) lib;
@@ -71,6 +73,9 @@
roowho2 = final: prev: {
inherit (self.packages.${prev.stdenv.hostPlatform.system}) roowho2;
};
roowho2-crane = final: prev: {
roowho2 = self.packages.${prev.stdenv.hostPlatform.system}.roowho2-crane;
};
};
nixosModules.default = ./nix/module.nix;
@@ -79,12 +84,13 @@
let
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
cargoLock = ./Cargo.lock;
craneLib = (crane.mkLib pkgs).overrideToolchain(p: p.rust-bin.nightly.latest.default);
src = lib.fileset.toSource {
root = ./.;
fileset = lib.fileset.unions [
./src
./Cargo.toml
./Cargo.lock
(craneLib.fileset.commonCargoSources ./.)
# ./assets
];
};
@@ -93,13 +99,25 @@
cargo = pkgs.rust-bin.nightly.latest.cargo;
};
in {
default = self.packages.${system}.roowho2;
default = self.packages.${system}.roowho2-crane;
roowho2 = pkgs.callPackage ./nix/package.nix { inherit cargoToml cargoLock src rustPlatform; };
roowho2 = pkgs.callPackage ./nix/package.nix {
inherit cargoToml cargoLock src rustPlatform;
};
roowho2-crane = pkgs.callPackage ./nix/package.nix {
useCrane = true;
inherit cargoToml cargoLock src craneLib;
};
filteredSource = pkgs.runCommandLocal "filtered-source" { } ''
ln -s ${src} $out
'';
});
checks = forAllSystems (system: pkgs: _: {
# NOTE: the non-crane build runs tests during checkPhase
inherit (self.packages.${system}) roowho2;
});
};
}

View File

@@ -10,22 +10,62 @@
, cargoToml
, cargoLock
, src
, useCrane ? false
, craneLib ? null
}:
rustPlatform.buildRustPackage {
pname = "roowho2";
let
mainProgram = "roowhod";
buildFunction = if useCrane then craneLib.buildPackage else rustPlatform.buildRustPackage;
pnameCraneSuffix = lib.optionalString useCrane "-crane";
pname = "${cargoToml.package.name}${pnameCraneSuffix}";
rustPlatformArgs = {
buildType = "releaselto";
buildFeatures = lib.optionals stdenv.hostPlatform.isLinux [
"systemd"
];
cargoLock.lockFile = cargoLock;
doCheck = true;
useNextest = true;
nativeCheckInputs = [
versionCheckHook
];
cargoCheckFeatures = lib.optionals stdenv.hostPlatform.isLinux [
"systemd"
];
};
craneArgs = {
cargoLock = cargoLock;
cargoExtraArgs = lib.escapeShellArgs [ "--features" (lib.concatStringsSep "," (
lib.optionals stdenv.hostPlatform.isLinux [
"systemd"
]
)) ];
cargoArtifacts = craneLib.buildDepsOnly {
inherit pname;
inherit (cargoToml.package) version;
src = lib.fileset.toSource {
root = ../.;
fileset = lib.fileset.unions [
(craneLib.fileset.cargoTomlAndLock ../.)
];
};
cargoLock = cargoLock;
};
};
in
buildFunction ({
inherit pname;
inherit (cargoToml.package) version;
inherit src;
cargoLock.lockFile = cargoLock;
buildType = "releaselto";
RUSTFLAGS = "-Zhigher-ranked-assumptions";
buildFeatures = lib.optionals stdenv.hostPlatform.isLinux [
"systemd"
];
buildInputs = lib.optionals stdenv.hostPlatform.isLinux [
systemdLibs
];
@@ -46,7 +86,11 @@ rustPlatform.buildRustPackage {
in lib.concatStringsSep "\n" installShellCompletions;
meta = with lib; {
license = licenses.mit;
license = licenses.bsdOriginalUC;
platforms = platforms.linux ++ platforms.darwin;
inherit mainProgram;
};
}
//
(if useCrane then craneArgs else rustPlatformArgs)
)

View File

@@ -4,7 +4,7 @@ let
pkgs = import nixpkgs {
inherit system;
overlays = [
self.overlays.default
self.overlays.roowho2-crane
];
};
in