-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.nix
179 lines (152 loc) · 5.16 KB
/
default.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
let
inherit (builtins) isString;
in
rec {
# v1.82.0
rustToolchainFileSha256 = "yMuSb5eQPO/bHv+Bcf/US8LVMbf/G/0MSfiPwBhiPpk=";
crossSystems = {
aarch64-darwin = [
"aarch64-apple-darwin"
];
aarch64-linux = [
"aarch64-unknown-linux-musl"
];
x86_64-darwin = [
"x86_64-apple-darwin"
];
x86_64-linux = [
"aarch64-unknown-linux-musl"
"armv6l-unknown-linux-musleabihf"
"armv7l-unknown-linux-musleabihf"
"i686-unknown-linux-musl"
"x86_64-unknown-linux-musl"
"x86_64-w64-mingw32"
];
};
# make shell.nix
mkShell =
{ nixpkgs ? <nixpkgs>
, system ? builtins.currentSystem
, pkgs ? import nixpkgs { inherit system; }
, fenix ? import (fetchTarball "https://github.com/nix-community/fenix/archive/main.tar.gz") { }
, extraBuildInputs ? null
, rustToolchainFile
}:
let
inherit (pkgs.lib) optionals attrVals splitString;
rust = fenix.fromToolchainFile {
file = rustToolchainFile;
sha256 = rustToolchainFileSha256;
};
extraBuildInputs' = optionals
(isString extraBuildInputs)
(attrVals (splitString "," extraBuildInputs) pkgs);
in
pkgs.mkShell {
buildInputs = [ rust ] ++ extraBuildInputs';
};
# make default.nix
mkDefault =
{ nixpkgs ? <nixpkgs>
, system ? builtins.currentSystem
, pkgs ? import nixpkgs { inherit system; }
, crossPkgs ? import nixpkgs ({ inherit system; } // (if target == null then { } else { crossSystem = { inherit isStatic; config = target; }; }))
, fenix ? import (fetchTarball "https://github.com/nix-community/fenix/archive/main.tar.gz") { }
, target ? null
, isStatic ? false
, defaultFeatures ? true
, features ? ""
, rustToolchainFile ? src + "/rust-toolchain.toml"
, cargoLockFile ? src + "/Cargo.lock"
, src
, mkPackage
, version
}:
let
inherit (pkgs) binutils lib stdenv;
inherit (crossPkgs) buildPlatform hostPlatform;
inherit (lib) getExe' importTOML optional;
inherit (hostPlatform) isWindows;
# HACK: https://github.com/NixOS/nixpkgs/issues/177129
# create an empty libgcc_eh for Windows compiler to be happy
libgcc_eh = stdenv.mkDerivation {
pname = "empty-libgcc_eh";
version = "0";
dontUnpack = true;
installPhase = ''
mkdir -p "$out"/lib
"${getExe' binutils "ar"}" r "$out"/lib/libgcc_eh.a
'';
};
rustToolchain =
let
name = (importTOML rustToolchainFile).toolchain.channel;
spec = { inherit name; sha256 = rustToolchainFileSha256; };
toolchain = fenix.fromToolchainName spec;
target = if buildPlatform == hostPlatform then null else hostPlatform.rust.rustcTarget;
crossToolchain = fenix.targets.${target}.fromToolchainName spec;
components = [ toolchain.rustc toolchain.cargo ]
++ optional (target != null) crossToolchain.rust-std;
in
fenix.combine components;
rustPlatform = crossPkgs.makeRustPlatform {
rustc = rustToolchain;
cargo = rustToolchain;
};
package = mkPackage {
inherit lib rustPlatform;
inherit defaultFeatures features;
pkgs = crossPkgs;
};
in
package.overrideAttrs (drv: {
inherit version;
propagatedBuildInputs = (drv.propagatedBuildInputs or [ ])
++ optional isWindows libgcc_eh;
src = pkgs.nix-gitignore.gitignoreSource [ ] src;
cargoDeps = rustPlatform.importCargoLock {
lockFile = cargoLockFile;
allowBuiltinFetchGit = true;
};
});
# make flake outputs
mkFlakeOutputs =
{ self, nixpkgs, fenix, ... } @ inputs:
{ shell, default }:
let
inherit (nixpkgs) lib;
pimalaya = import inputs.pimalaya;
mkShell = args: import shell ({ inherit pimalaya; } // args);
mkDefault = args: import default ({ inherit pimalaya; } // args);
eachSystem = lib.genAttrs (lib.attrNames crossSystems);
withGitEnvs = package: package.overrideAttrs (drv: {
GIT_REV = drv.GIT_REV or self.rev or self.dirtyRev or "unknown";
GIT_DESCRIBE = drv.GIT_DESCRIBE or "nix-flake-" + self.lastModifiedDate;
});
mkDevShell = system: {
default = mkShell {
inherit nixpkgs system;
fenix = fenix.packages.${system};
};
};
mkPackages = system: mkCrossPackages system // {
default = withGitEnvs (mkDefault {
inherit nixpkgs system;
fenix = fenix.packages.${system};
});
};
mkCrossPackages = system:
lib.attrsets.mergeAttrsList (map (mkCrossPackage system) crossSystems.${system});
mkCrossPackage = system: target:
let
crossSystem = { config = target; isStatic = true; };
crossPkgs = import nixpkgs { inherit system crossSystem; };
crossPkg = mkDefault { inherit nixpkgs system crossPkgs; fenix = fenix.packages.${system}; };
in
{ "cross-${crossPkgs.hostPlatform.system}" = withGitEnvs crossPkg; };
in
{
devShells = eachSystem mkDevShell;
packages = eachSystem mkPackages;
};
}