feat: use crane

This commit is contained in:
Tobias Berger 2025-03-05 10:18:38 +01:00
parent 22278b4f44
commit b554c59883
Signed by: toby
GPG key ID: 2D05EFAB764D6A88
5 changed files with 98 additions and 26 deletions

View file

@ -1,5 +0,0 @@
{
"editor.formatOnSave": true,
// https://twitter.com/sridca/status/1674947342607216641
// "editor.inlayHints.enabled": "offUnlessPressed",
}

2
Cargo.lock generated
View file

@ -1,6 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
version = 4
[[package]]
name = "rust-nix-template"

View file

@ -3,3 +3,17 @@ authors = ["Toby <toby@tobot.dev>"]
edition = "2024"
name = "rust-nix-template"
version = "0.1.0"
[profile.release]
strip = "symbols"
lto = true
opt-level = 3
codegen-units = 1
[profile.dev.package."*"]
opt-level = 3
codegen-units = 1
[profile.release.package."*"]
opt-level = 3
codegen-units = 1
[profile.release.build-override]
codegen-units = 1

34
flake.lock generated
View file

@ -1,12 +1,27 @@
{
"nodes": {
"crane": {
"locked": {
"lastModified": 1741148495,
"narHash": "sha256-EV8KUaIZ2/CdBXlutXrHoZYbWPeB65p5kKZk71gvDRI=",
"owner": "ipetkov",
"repo": "crane",
"rev": "75390a36cd0c2cdd5f1aafd8a9f827d7107f2e53",
"type": "github"
},
"original": {
"owner": "ipetkov",
"repo": "crane",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1738797219,
"narHash": "sha256-KRwX9Z1XavpgeSDVM/THdFd6uH8rNm/6R+7kIbGa+2s=",
"lastModified": 1741037377,
"narHash": "sha256-SvtvVKHaUX4Owb+PasySwZsoc5VUeTf1px34BByiOxw=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "1da52dd49a127ad74486b135898da2cef8c62665",
"rev": "02032da4af073d0f6110540c8677f16d4be0117f",
"type": "github"
},
"original": {
@ -18,6 +33,7 @@
},
"root": {
"inputs": {
"crane": "crane",
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay",
"treefmt-nix": "treefmt-nix"
@ -30,11 +46,11 @@
]
},
"locked": {
"lastModified": 1738808867,
"narHash": "sha256-m5rbY/ck0NAlfSBxo++vl7EZn8fkZ02H3kGGc7q883c=",
"lastModified": 1741141853,
"narHash": "sha256-FauVtC+FbOgkKpGVuQTNxSqrvgbmVc7hFkjn/DacwMo=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "ae46f37fb727030ddc2ef65a675b751484c90032",
"rev": "02edad1f19d6dec824e0812e4cdc0aa7930ff8ae",
"type": "github"
},
"original": {
@ -50,11 +66,11 @@
]
},
"locked": {
"lastModified": 1738680491,
"narHash": "sha256-8X7tR3kFGkE7WEF5EXVkt4apgaN85oHZdoTGutCFs6I=",
"lastModified": 1739829690,
"narHash": "sha256-mL1szCeIsjh6Khn3nH2cYtwO5YXG6gBiTw1A30iGeDU=",
"owner": "numtide",
"repo": "treefmt-nix",
"rev": "64dbb922d51a42c0ced6a7668ca008dded61c483",
"rev": "3d0579f5cc93436052d94b73925b48973a104204",
"type": "github"
},
"original": {

View file

@ -4,6 +4,8 @@
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
crane.url = "github:ipetkov/crane";
treefmt-nix = {
url = "github:numtide/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";
@ -19,6 +21,7 @@
{
self,
nixpkgs,
crane,
treefmt-nix,
rust-overlay,
}:
@ -38,7 +41,17 @@
inherit system;
overlays = [ (import rust-overlay) ];
};
rustToolchain = pkgs.rust-bin.nightly.latest.minimal;
rustToolchainFor =
pkgs:
pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
rustToolchain = rustToolchainFor pkgs;
# NB: we don't need to overlay our custom toolchain for the *entire*
# pkgs (which would require rebuidling anything else which uses rust).
# Instead, we just want to update the scope that crane will use by appending
# our specific toolchain there.
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchainFor;
})
);
in
@ -46,8 +59,8 @@
formatter = forEachSupportedSystem (
{ pkgs, ... }: (treefmt-nix.lib.evalModule pkgs ./treefmt.nix).config.build.wrapper
);
devShells = forEachSupportedSystem (
{ pkgs, rustToolchain }:
devShells = forEachSupportedSystem (
{ pkgs, rustToolchain, ... }:
{
default = pkgs.mkShell {
packages = [
@ -57,24 +70,58 @@
}
);
packages = forEachSupportedSystem (
{ pkgs, rustToolchain }:
{
pkgs,
rustToolchain,
craneLib,
...
}:
{
default = (
let
manifest = (pkgs.lib.importTOML ./Cargo.toml).package;
rustPlatform = pkgs.makeRustPlatform {
cargo = rustToolchain;
rustc = rustToolchain;
};
src = craneLib.cleanCargoSource ./.;
in
rustPlatform.buildRustPackage {
craneLib.buildPackage {
pname = manifest.name;
version = manifest.version;
cargoLock.lockFile = ./Cargo.lock;
src = pkgs.lib.cleanSource ./.;
inherit src;
strictDeps = true;
cargoVendorDir = craneLib.vendorMultipleCargoDeps {
inherit (craneLib.findCargoFiles src) cargoConfigs;
cargoLockList = [
./Cargo.lock
# Unfortunately this approach requires IFD (import-from-derivation)
# otherwise Nix will refuse to read the Cargo.lock from our toolchain
# (unless we build with `--impure`).
#
# Another way around this is to manually copy the rustlib `Cargo.lock`
# to the repo and import it with `./path/to/rustlib/Cargo.lock` which
# will avoid IFD entirely but will require manually keeping the file
# up to date!
"${rustToolchain.passthru.availableComponents.rust-src}/lib/rustlib/src/rust/library/Cargo.lock"
];
};
cargoExtraArgs = "-Z build-std='' --target x86_64-unknown-linux-gnu";
buildInputs = [
# Add additional build inputs here
];
}
);
}
);
overlays.default = final: prev: {
rust-nix-template = self.packages.default.${final.system};
};
nixosModules.rust-nix-template =
{ pkgs, ... }:
{
nixpkgs.overlays = [ self.overlay ];
};
};
}