@@ -23,12 +23,19 @@ ourArgs:
23
23
cfg = config . crate2nix ;
24
24
25
25
crate2nix = ourArgs . self . packages . ${ pkgs . system } . default ;
26
+
27
+ customBuildAttrs = [
28
+ "pkgs" "rust" "cargo" "target" "profile"
29
+ "targetFeatures" "globalRustcOpts" "includePaths"
30
+ ] ;
31
+
26
32
27
33
cargoModule = { config , ... } :
28
34
let
35
+ cargoRoot = builtins . dirOf config . cargoNix . path ;
29
36
importCargoNix =
30
37
{
31
- pkgs ? perSystem . pkgs
38
+ pkgs ? config . pkgs
32
39
, rust ? config . toolchain . rust
33
40
, cargo ? config . toolchain . cargo
34
41
, target ? null
@@ -41,9 +48,34 @@ ourArgs:
41
48
let
42
49
# Remove target from final crate overrides
43
50
# 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
+
44
76
cleanedCrateOverrides =
45
77
lib . mapAttrs
46
- ( _ : override : attrs : lib . removeAttrs ( override attrs ) [ "target" "globalRustcOpts" ] )
78
+ ( _ : fixOverride )
47
79
config . crateOverrides ;
48
80
49
81
finalPkgs =
@@ -80,13 +112,9 @@ ourArgs:
80
112
} ;
81
113
overrideAttrNames = builtins . attrNames crateOverrides ;
82
114
isInOverride = n : lib . elem n overrideAttrNames ;
83
- globalBuildAttrs = [
84
- "pkgs" "rust" "cargo" "target" "profile"
85
- "targetFeatures" "globalRustcOpts"
86
- ] ;
87
115
build = ( importCargoNix importArgs ) . workspaceMembers . ${ name } . build ;
88
116
in
89
- if lib . any isInOverride globalBuildAttrs
117
+ if lib . any isInOverride customBuildAttrs
90
118
then member // { inherit build ; }
91
119
else member ;
92
120
@@ -204,6 +232,12 @@ ourArgs:
204
232
Used for conditional compilation based on CPU feature detection.
205
233
'' ;
206
234
} ;
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
+ } ;
207
241
packages = lib . mkOption {
208
242
type = types . attrsOf types . package ;
209
243
readOnly = true ;
0 commit comments