improve rust template for ensuring package overriable

This commit is contained in:
ulic-youthlic 2025-05-25 16:37:32 +08:00
parent dc5dfddf74
commit 18bfe1ce9a
Signed by: youthlic
GPG key ID: 63E86C3C14A0D721
6 changed files with 100 additions and 38 deletions

View file

@ -1,3 +1,3 @@
watch_file rust-toolchain.toml
watch_file rust-toolchain.toml nix/*.nix
use flake

View file

@ -3,11 +3,11 @@
"advisory-db": {
"flake": false,
"locked": {
"lastModified": 1746689539,
"narHash": "sha256-rVUs0CjpuO7FKVHecsuMaYiUr8iKscsgeo/b2XlnPmQ=",
"lastModified": 1747937073,
"narHash": "sha256-52H8P6jAHEwRvg7rXr4Z7h1KHZivO8T1Z9tN6R0SWJg=",
"owner": "rustsec",
"repo": "advisory-db",
"rev": "796d034fbcb1c5bc83c0d0912dc31eb4e34458bf",
"rev": "bccf313a98c034573ac4170e6271749113343d97",
"type": "github"
},
"original": {
@ -18,11 +18,11 @@
},
"crane": {
"locked": {
"lastModified": 1746291859,
"narHash": "sha256-DdWJLA+D5tcmrRSg5Y7tp/qWaD05ATI4Z7h22gd1h7Q=",
"lastModified": 1748047550,
"narHash": "sha256-t0qLLqb4C1rdtiY8IFRH5KIapTY/n3Lqt57AmxEv9mk=",
"owner": "ipetkov",
"repo": "crane",
"rev": "dfd9a8dfd09db9aad544c4d3b6c47b12562544a5",
"rev": "b718a78696060df6280196a6f992d04c87a16aef",
"type": "github"
},
"original": {
@ -51,11 +51,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1746300365,
"narHash": "sha256-thYTdWqCRipwPRxWiTiH1vusLuAy0okjOyzRx4hLWh4=",
"lastModified": 1747958103,
"narHash": "sha256-qmmFCrfBwSHoWw7cVK4Aj+fns+c54EBP8cGqp/yK410=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "f21e4546e3ede7ae34d12a84602a22246b31f7e0",
"rev": "fe51d34885f7b5e3e7b59572796e1bcb427eccb1",
"type": "github"
},
"original": {
@ -81,11 +81,11 @@
]
},
"locked": {
"lastModified": 1746326315,
"narHash": "sha256-IDqSls/r6yBfdOBRSMQ/noTUoigmsKnTQ7TqpsBtN4Y=",
"lastModified": 1748140821,
"narHash": "sha256-GZcjWLQtDifSYMd1ueLDmuVTcQQdD5mONIBTqABooOk=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "dd280c436961ec5adccf0135efe5b66a23d84497",
"rev": "476b2ba7dc99ddbf70b1f45357dbbdbdbdfb4422",
"type": "github"
},
"original": {

View file

@ -31,12 +31,17 @@
inherit (pkgs) lib;
pkgs = import nixpkgs {
inherit system;
overlays = [(import rust-overlay)];
overlays = [
(import rust-overlay)
(final: prev: {
lib = prev.lib // (import ./nix/lib.nix prev.lib);
})
];
};
rustToolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
srcFilters = path: type:
builtins.any (suffix: lib.hasSuffix suffix path) [
builtins.any (lib.flip lib.hasSuffix path) [
".sql"
".diff"
".md"
@ -52,20 +57,15 @@
inherit src;
strictDeps = true;
};
nativeBuildInputs = with pkgs; [];
buildInputs =
(with pkgs; [])
++ lib.optional pkgs.stdenv.buildPlatform.isDarwin (with pkgs; [
darwin.apple_sdk.frameworks.Security
]);
cargoArtifacts = craneLib.buildDepsOnly (basicArgs
// {
inherit buildInputs nativeBuildInputs;
});
nativeBuildInputs = [];
buildInputs = [];
genInputs = lib.genInputsWith pkgs;
commonArgs =
basicArgs
// {
inherit cargoArtifacts buildInputs nativeBuildInputs;
cargoArtifacts = self.packages.${system}.cargo-artifacts;
buildInputs = genInputs buildInputs;
nativeBuildInputs = genInputs nativeBuildInputs;
};
in {
formatter = pkgs.alejandra;
@ -93,18 +93,17 @@
};
});
};
packages = rec {
rust-demo = craneLib.buildPackage (commonArgs
// {
inherit
(craneLib.crateNameFromCargoToml {
cargoToml = "${toString src}/Cargo.toml";
})
pname
version
;
doCheck = false;
});
packages = let
callPackage = lib.callPackageWith (pkgs // {inherit craneLib callPackage;});
packageArgs = {
inherit lib basicArgs buildInputs nativeBuildInputs;
};
importWithArgs = with lib; flip import packageArgs;
in rec {
cargo-artifacts = callPackage (importWithArgs ./nix/cargo-artifacts.nix) {};
rust-demo = callPackage (importWithArgs ./nix/package.nix) {
cargoArtifacts = cargo-artifacts;
};
default = rust-demo;
};
apps.default = flake-utils.lib.mkApp {

View file

@ -0,0 +1,20 @@
{
lib,
basicArgs,
buildInputs,
nativeBuildInputs,
}: let
f = {
craneLib,
lib,
...
} @ args: let
genInputs = lib.genInputsWith args;
in
craneLib.buildDepsOnly (basicArgs
// {
buildInputs = genInputs buildInputs;
nativeBuildInputs = genInputs nativeBuildInputs;
});
in
with lib; setFunctionArgs f ((functionArgs f) // (genFunctionArgs (buildInputs ++ nativeBuildInputs)))

View file

@ -0,0 +1,13 @@
{
flip,
pipe,
splitString,
head,
listToAttrs,
nameValuePair,
getAttrFromPath,
...
}: {
genFunctionArgs = flip pipe [(map (flip pipe [(splitString ".") head (flip nameValuePair false)])) listToAttrs];
genInputsWith = pkgs: map (flip pipe [(splitString ".") (flip getAttrFromPath pkgs)]);
}

View file

@ -0,0 +1,30 @@
{
lib,
basicArgs,
buildInputs,
nativeBuildInputs,
}: let
f = {
craneLib,
lib,
cargoArtifacts,
...
} @ args: let
genInputs = lib.genInputsWith args;
in
craneLib.buildPackage (basicArgs
// {
inherit
(craneLib.crateNameFromCargoToml {
cargoToml = "${toString basicArgs.src}/Cargo.toml";
})
pname
version
;
inherit cargoArtifacts;
buildInputs = genInputs buildInputs;
nativeBuildInputs = genInputs nativeBuildInputs;
doCheck = false;
});
in
with lib; setFunctionArgs f ((functionArgs f) // (genFunctionArgs (buildInputs ++ nativeBuildInputs)))