Use crane to build the nix package

crane (https://github.com/ipetkov/crane) builds the dependencies separately and
stores them in the nix store, so they get reused on subsequent builds.
This commit is contained in:
Erwin Boskma 2023-01-09 11:21:22 +01:00
parent 64acda457c
commit eb12b75604
Signed by: erwin
SSH key fingerprint: SHA256:CyeNoWXd3kjX2Nwu6pDxxdS7OqmPVOy0NavA/KU/ntU
2 changed files with 84 additions and 55 deletions

View file

@ -1,5 +1,32 @@
{ {
"nodes": { "nodes": {
"crane": {
"inputs": {
"flake-compat": "flake-compat",
"flake-utils": [
"flake-utils"
],
"nixpkgs": [
"nixpkgs"
],
"rust-overlay": [
"rust-overlay"
]
},
"locked": {
"lastModified": 1673056065,
"narHash": "sha256-a68tMDTDqdAauxq377ALl4Uwm6oh9MeoY2WbTYRWZoo=",
"owner": "ipetkov",
"repo": "crane",
"rev": "0144134311767fcee80213321f079a8ffa0b9cc1",
"type": "github"
},
"original": {
"owner": "ipetkov",
"repo": "crane",
"type": "github"
}
},
"flake-compat": { "flake-compat": {
"flake": false, "flake": false,
"locked": { "locked": {
@ -16,6 +43,22 @@
"type": "github" "type": "github"
} }
}, },
"flake-compat_2": {
"flake": false,
"locked": {
"lastModified": 1668681692,
"narHash": "sha256-Ht91NGdewz8IQLtWZ9LCeNXMSXHUss+9COoqu6JLmXU=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "009399224d5e398d03b22badca40a37ac85412a1",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"flake-utils": { "flake-utils": {
"locked": { "locked": {
"lastModified": 1667395993, "lastModified": 1667395993,
@ -52,27 +95,6 @@
"type": "github" "type": "github"
} }
}, },
"naersk": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1662220400,
"narHash": "sha256-9o2OGQqu4xyLZP9K6kNe1pTHnyPz0Wr3raGYnr9AIgY=",
"owner": "nmattia",
"repo": "naersk",
"rev": "6944160c19cb591eb85bbf9b2f2768a935623ed3",
"type": "github"
},
"original": {
"owner": "nmattia",
"ref": "master",
"repo": "naersk",
"type": "github"
}
},
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1669542132, "lastModified": 1669542132,
@ -123,7 +145,7 @@
}, },
"pre-commit-hooks": { "pre-commit-hooks": {
"inputs": { "inputs": {
"flake-compat": "flake-compat", "flake-compat": "flake-compat_2",
"flake-utils": [ "flake-utils": [
"flake-utils" "flake-utils"
], ],
@ -147,8 +169,8 @@
}, },
"root": { "root": {
"inputs": { "inputs": {
"crane": "crane",
"flake-utils": "flake-utils", "flake-utils": "flake-utils",
"naersk": "naersk",
"nixpkgs": "nixpkgs", "nixpkgs": "nixpkgs",
"pre-commit-hooks": "pre-commit-hooks", "pre-commit-hooks": "pre-commit-hooks",
"rust-overlay": "rust-overlay" "rust-overlay": "rust-overlay"

View file

@ -3,10 +3,7 @@
inputs = { inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils"; flake-utils.url = "github:numtide/flake-utils";
naersk = {
url = "github:nmattia/naersk/master";
inputs.nixpkgs.follows = "nixpkgs";
};
rust-overlay = { rust-overlay = {
url = "github:oxalica/rust-overlay"; url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs"; inputs.nixpkgs.follows = "nixpkgs";
@ -16,48 +13,46 @@
url = "github:cachix/pre-commit-hooks.nix"; url = "github:cachix/pre-commit-hooks.nix";
inputs.flake-utils.follows = "flake-utils"; inputs.flake-utils.follows = "flake-utils";
}; };
crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-utils.follows = "flake-utils";
inputs.rust-overlay.follows = "rust-overlay";
};
}; };
outputs = { self, nixpkgs, flake-utils, naersk, rust-overlay, pre-commit-hooks }: outputs = { self, nixpkgs, flake-utils, rust-overlay, pre-commit-hooks, crane }:
flake-utils.lib.eachDefaultSystem (system: flake-utils.lib.eachDefaultSystem (system:
let let
overlays = [ (import rust-overlay) ]; overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs { inherit system overlays; }; pkgs = import nixpkgs { inherit system overlays; };
naersk-lib = pkgs.callPackage naersk { };
rustToolchain = rustToolchain =
pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
crane-lib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
src = crane-lib.cleanCargoSource ./.;
buildInputs = with pkgs; [
openssl
] ++ lib.optionals pkgs.stdenv.isDarwin [ pkgs.libiconv ];
nativeBuildInputs = with pkgs; [
pkgconf
];
cargoArtifacts = crane-lib.buildDepsOnly {
inherit src buildInputs nativeBuildInputs;
};
ha-now-playing = crane-lib.buildPackage {
inherit cargoArtifacts src buildInputs nativeBuildInputs;
};
in in
{ {
formatter = pkgs.nixpkgs-fmt; formatter = pkgs.nixpkgs-fmt;
packages = { packages = {
ha-now-playing = (naersk-lib.override { inherit ha-now-playing;
cargo = rustToolchain;
rustc = rustToolchain;
}).buildPackage
{
pname = "ha-now-playing";
root = ./.;
buildInputs = with pkgs; [
rustToolchain
openssl
];
nativeBuildInputs = with pkgs; [
pkgconf
cargo-edit
cargo-expand
cargo-diet
cargo-feature
cargo-outdated
rust-analyzer
pre-commit
jq
gitflow
yaml-language-server
];
};
}; };
packages.default = self.packages.${system}.ha-now-playing; packages.default = self.packages.${system}.ha-now-playing;
@ -71,6 +66,17 @@
}; };
checks = { checks = {
inherit ha-now-playing;
ha-now-playing-clippy = crane-lib.cargoClippy {
inherit cargoArtifacts src buildInputs nativeBuildInputs;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
};
ha-now-playing-fmt = crane-lib.cargoFmt {
inherit src;
};
pre-commit-check = pre-commit-hooks.lib.${system}.run { pre-commit-check = pre-commit-hooks.lib.${system}.run {
src = ./.; src = ./.;
hooks = { hooks = {
@ -97,6 +103,7 @@
name = "ha-now-playing"; name = "ha-now-playing";
inputsFrom = [ self.packages.${system}.ha-now-playing ]; inputsFrom = [ self.packages.${system}.ha-now-playing ];
nativeBuildInputs = [ rustToolchain ];
}; };
}); });