Skip to content

Commit 626cb25

Browse files
committed
feat: make it easy to add paths in workspace
1 parent a23e393 commit 626cb25

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

nix/public-flake-module.nix

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,19 @@ ourArgs:
2323
cfg = config.crate2nix;
2424

2525
crate2nix = ourArgs.self.packages.${pkgs.system}.default;
26+
27+
customBuildAttrs = [
28+
"pkgs" "rust" "cargo" "target" "profile"
29+
"targetFeatures" "globalRustcOpts" "includePaths"
30+
];
31+
2632

2733
cargoModule = { config, ... }:
2834
let
35+
cargoRoot = builtins.dirOf config.cargoNix.path;
2936
importCargoNix =
3037
{
31-
pkgs ? perSystem.pkgs
38+
pkgs ? config.pkgs
3239
, rust ? config.toolchain.rust
3340
, cargo ? config.toolchain.cargo
3441
, target ? null
@@ -41,9 +48,34 @@ ourArgs:
4148
let
4249
# Remove target from final crate overrides
4350
# If target is an attrset, mkDerivation tries to coerce it to a string
51+
fixOverride = override: attrs:
52+
let
53+
overrideAttrs = override attrs;
54+
origSrc = attrs.src.origSrc or cargoRoot;
55+
src = if overrideAttrs ? includePaths
56+
then lib.cleanSourceWith {
57+
filter = cfg.cargoNix.internal.sourceFilter;
58+
src = lib.fileset.toSource {
59+
root = cargoRoot;
60+
fileset = lib.fileset.unions ([
61+
origSrc
62+
] ++ overrideAttrs.includePaths);
63+
};
64+
}
65+
else overrideAttrs.src or attrs.src or cargoRoot;
66+
cratePath = lib.path.removePrefix cargoRoot origSrc;
67+
in
68+
(lib.removeAttrs overrideAttrs customBuildAttrs) // ({
69+
inherit src;
70+
# automatically perform a deep merge on meta attributes in override
71+
meta = (attrs.meta or {}) // (overrideAttrs.meta or {});
72+
} // (lib.optionalAttrs (overrideAttrs ? includePaths) {
73+
sourceRoot = overrideAttrs.sourceRoot or "${src.name}/${cratePath}";
74+
}));
75+
4476
cleanedCrateOverrides =
4577
lib.mapAttrs
46-
(_: override: attrs: lib.removeAttrs (override attrs) [ "target" "globalRustcOpts"])
78+
(_: fixOverride)
4779
config.crateOverrides;
4880

4981
finalPkgs =
@@ -80,13 +112,9 @@ ourArgs:
80112
};
81113
overrideAttrNames = builtins.attrNames crateOverrides;
82114
isInOverride = n: lib.elem n overrideAttrNames;
83-
globalBuildAttrs = [
84-
"pkgs" "rust" "cargo" "target" "profile"
85-
"targetFeatures" "globalRustcOpts"
86-
];
87115
build = (importCargoNix importArgs).workspaceMembers.${name}.build;
88116
in
89-
if lib.any isInOverride globalBuildAttrs
117+
if lib.any isInOverride customBuildAttrs
90118
then member // { inherit build; }
91119
else member;
92120

@@ -204,6 +232,12 @@ ourArgs:
204232
Used for conditional compilation based on CPU feature detection.
205233
'';
206234
};
235+
pkgs = lib.mkOption {
236+
type = types.unspecified;
237+
description = "Package set to use for Cargo.nix.";
238+
default = pkgs;
239+
defaultText = lib.literalExpression "pkgs (module argument)";
240+
};
207241
packages = lib.mkOption {
208242
type = types.attrsOf types.package;
209243
readOnly = true;

0 commit comments

Comments
 (0)