allow overwriting cargo toml/lock location

This commit is contained in:
Robin Appelman 2025-06-09 16:24:04 +02:00
commit 8a5baa8225
2 changed files with 30 additions and 9 deletions

View file

@ -2,16 +2,17 @@
lib, lib,
src, src,
config, config,
cargoLock,
}: let }: let
inherit (builtins) readFile pathExists attrNames hasAttr; inherit (builtins) readFile pathExists attrNames hasAttr;
inherit (lib) map intersectLists foldl splitString getAttrFromPath; inherit (lib) map intersectLists foldl splitString getAttrFromPath;
cargoLockDeps = cargoLockDeps =
if pathExists (src + /Cargo.lock) if pathExists cargoLock
then let then let
cargoLock = fromTOML (readFile (src + /Cargo.lock)); cargoLockToml = fromTOML (readFile cargoLock);
in in
map (package: package.name) cargoLock.package map (package: package.name) cargoLockToml.package
else []; else [];
availableAutoDeps = import ./deps.nix; availableAutoDeps = import ./deps.nix;
detectedDeps = intersectLists cargoLockDeps (attrNames availableAutoDeps); detectedDeps = intersectLists cargoLockDeps (attrNames availableAutoDeps);

View file

@ -10,7 +10,7 @@
... ...
}: let }: let
inherit (builtins) elem readFile pathExists match any concatLists; inherit (builtins) elem readFile pathExists match any concatLists;
inherit (lib) getExe map mkDefault mkIf mkMerge mkOption warnIf optionalAttrs types optionalString genAttrs hasInfix makeBinPath makeSearchPathOutput; inherit (lib) getExe map mkDefault mkIf mkMerge mkOption warnIf optionalAttrs types optionalString genAttrs hasInfix makeSearchPathOutput;
inherit (lib.fileset) fileFilter toSource unions; inherit (lib.fileset) fileFilter toSource unions;
inherit (flakelight.types) fileset function optFunctionTo; inherit (flakelight.types) fileset function optFunctionTo;
makePkgConfigPath = makeSearchPathOutput "dev" "lib/pkgconfig"; makePkgConfigPath = makeSearchPathOutput "dev" "lib/pkgconfig";
@ -20,15 +20,22 @@
fileset = unions (config.extraPaths ++ [config.fileset]); fileset = unions (config.extraPaths ++ [config.fileset]);
}; };
cargoToml = fromTOML (readFile (src + /Cargo.toml)); cargoToml = fromTOML (readFile config.cargoToml);
cargoMeta = (import ./cargo-meta.nix {inherit lib;}) cargoToml; cargoMeta =
(import ./cargo-meta.nix {
inherit lib;
})
cargoToml;
inherit (cargoMeta) tomlPackage hasMsrv hasWorkspace hasNonDefaultFeatures hasDefaultFeatures msrv; inherit (cargoMeta) tomlPackage hasMsrv hasWorkspace hasNonDefaultFeatures hasDefaultFeatures msrv;
maybeWorkspace = optionalString hasWorkspace "--workspace"; maybeWorkspace = optionalString hasWorkspace "--workspace";
hasExamples = pathExists (src + /examples); hasExamples = pathExists (src + /examples);
hasDefaultPackage = pathExists (src + /nix/package.nix); hasDefaultPackage = pathExists (src + /nix/package.nix);
autoDeps = import ./autodeps {inherit lib src config;}; autoDeps = import ./autodeps {
inherit (config) cargoLock;
inherit lib src config;
};
buildDeps = pkgs: rec { buildDeps = pkgs: rec {
buildInputs = (autoDeps pkgs).buildInputs ++ (config.buildInputs pkgs); buildInputs = (autoDeps pkgs).buildInputs ++ (config.buildInputs pkgs);
nativeBuildInputs = (autoDeps pkgs).nativeBuildInputs ++ (config.nativeBuildInputs pkgs); nativeBuildInputs = (autoDeps pkgs).nativeBuildInputs ++ (config.nativeBuildInputs pkgs);
@ -56,6 +63,14 @@ in
warnIf (! builtins ? readFileType) "Unsupported Nix version in use." warnIf (! builtins ? readFileType) "Unsupported Nix version in use."
{ {
options = { options = {
cargoToml = mkOption {
type = with types; path;
default = src + /Cargo.toml;
};
cargoLock = mkOption {
type = with types; path;
default = src + /Cargo.lock;
};
extraFiles = mkOption { extraFiles = mkOption {
type = with types; listOf str; type = with types; listOf str;
default = []; default = [];
@ -144,7 +159,7 @@ in
}; };
config = mkMerge [ config = mkMerge [
(mkIf (pathExists (src + /Cargo.toml)) { (mkIf (pathExists config.cargoToml) {
withOverlays = [ withOverlays = [
(import inputs.rust-overlay) (import inputs.rust-overlay)
(final: { (final: {
@ -156,6 +171,7 @@ in
} @ prev: rec { } @ prev: rec {
commonCraneArgs = commonCraneArgs =
{ {
inherit (config) cargoToml cargoLock;
src = filteredSrc; src = filteredSrc;
strictDeps = true; strictDeps = true;
doCheck = false; doCheck = false;
@ -181,7 +197,11 @@ in
pname = "${crateName}-msrv"; pname = "${crateName}-msrv";
}; };
crateName = (craneLib.crateNameFromCargoToml {inherit src;}).pname; crateName =
(craneLib.crateNameFromCargoToml {
inherit (config) cargoToml;
inherit src;
}).pname;
craneLib = (inputs.crane.mkLib final).overrideToolchain (p: p.rustToolchain); craneLib = (inputs.crane.mkLib final).overrideToolchain (p: p.rustToolchain);
craneLibForTargets = targets: (inputs.crane.mkLib final).overrideToolchain (p: p.rustToolchain.override {inherit targets;}); craneLibForTargets = targets: (inputs.crane.mkLib final).overrideToolchain (p: p.rustToolchain.override {inherit targets;});
craneLibMsrv = (inputs.crane.mkLib final).overrideToolchain (p: p.msrvRustToolchain); craneLibMsrv = (inputs.crane.mkLib final).overrideToolchain (p: p.msrvRustToolchain);