-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathflake.nix
More file actions
195 lines (172 loc) · 6.65 KB
/
Copy pathflake.nix
File metadata and controls
195 lines (172 loc) · 6.65 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
{
description = "txm -- Terminal TeX math rendering engine";
inputs = {
nixpkgs.url = "nixpkgs/nixos-26.05";
rust-overlay.url = "github:oxalica/rust-overlay";
rust-overlay.inputs.nixpkgs.follows = "nixpkgs";
};
outputs =
{ self, nixpkgs, rust-overlay }:
let
supportedSystems = [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
# Per-system pkgs with rust-overlay applied
pkgsFor = system: import nixpkgs {
inherit system;
overlays = [ rust-overlay.overlays.default ];
};
forAllSystems =
f:
builtins.listToAttrs (
map (system: { name = system; value = f (pkgsFor system); }) supportedSystems
);
# Read version from Cargo.toml — single source of truth
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
# Shared arguments for buildRustPackage (used by all build strategies)
txmArgs = {
pname = cargoToml.package.name;
version = cargoToml.package.version;
src = ./.;
cargoLock.lockFile = ./Cargo.lock;
meta = {
description = cargoToml.package.description or "Terminal math rendering engine with LaTeX support";
homepage = cargoToml.package.repository or "https://github.com/thatmagicalcat/txm";
license = [
nixpkgs.lib.licenses.mit
nixpkgs.lib.licenses.asl20
];
mainProgram = "txm";
};
};
# -- Native build (pure Nix, works on the building system) --
mkTxm = pkgs: pkgs.rustPlatform.buildRustPackage txmArgs;
# -- Cross-compile any target using cargo-zigbuild + rust-overlay --
# rust-overlay provides the Rust toolchain with target stdlibs pre-built,
# so the derivation is fully sandboxed (no rustup/network needed at build time).
# Uses musl targets for Linux → fully static binaries, portable across distros.
mkTxmCross =
{ rustTarget # e.g. "x86_64-unknown-linux-musl"
, nixPlatform # Nix system string, e.g. "x86_64-linux"
}:
let
pkgsOverlay = pkgsFor "x86_64-linux";
pkgsPlain = import nixpkgs { system = "x86_64-linux"; };
# Rust toolchain with target stdlibs baked in (from overlay)
rustWithTargets = pkgsOverlay.rust-bin.stable.latest.minimal.override {
targets = [ rustTarget ];
};
# rustPlatform that uses the overlay's cargo/rustc (which has target stdlibs)
# but with auditable disabled — Zig's linker doesn't support the
# `--undefined` flag that nixpkgs' auditable feature injects.
crossPlatform = pkgsPlain.makeRustPlatform {
cargo = rustWithTargets;
rustc = rustWithTargets;
};
# Detect Windows targets (produce .exe binaries)
isWindows = pkgsPlain.lib.hasSuffix "windows-gnu" rustTarget;
binName = if isWindows then "txm.exe" else "txm";
in
crossPlatform.buildRustPackage (txmArgs // {
auditable = false;
nativeBuildInputs = (txmArgs.nativeBuildInputs or [ ]) ++ [
pkgsOverlay.cargo-zigbuild
pkgsOverlay.zig
];
buildPhase = ''
runHook preBuild
export HOME="$PWD"
export ZIG_GLOBAL_CACHE_DIR="$TMPDIR/zig-cache"
export CARGO_ZIGBUILD_CACHE_DIR="$TMPDIR/cargo-zigbuild-cache"
cargo zigbuild --release --target ${rustTarget}
runHook postBuild
'';
# Zig's Nix setup hook adds a checkPhase that runs `zig check`, but we
# don't have a build.zig — zig is only used as a cross-linker.
checkPhase = "true";
installPhase = ''
runHook preInstall
mkdir -p $out/bin
cp "target/${rustTarget}/release/${binName}" "$out/bin/${binName}"
runHook postInstall
'';
meta = txmArgs.meta // {
platforms = [ "x86_64-linux" nixPlatform ];
};
});
in
{
# Per-system native packages (build for the system you're on)
packages = forAllSystems (pkgs: {
default = mkTxm pkgs;
})
# On x86_64-linux, also provide cross-compiled packages for all targets.
# This lets a single Linux CI runner produce every platform binary.
#
# Linux targets use musl (fully static, portable across distros).
//
{
x86_64-linux = let
pkgs = pkgsFor "x86_64-linux";
in {
default = mkTxm pkgs;
# Static Linux builds (musl, fully static, work on any Linux)
x86_64-linux = mkTxmCross {
rustTarget = "x86_64-unknown-linux-musl";
nixPlatform = "x86_64-linux";
};
aarch64-linux = mkTxmCross {
rustTarget = "aarch64-unknown-linux-musl";
nixPlatform = "aarch64-linux";
};
# Cross-OS via cargo-zigbuild + rust-overlay (pure Nix, no rustup needed)
x86_64-darwin = mkTxmCross {
rustTarget = "x86_64-apple-darwin";
nixPlatform = "x86_64-darwin";
};
aarch64-darwin = mkTxmCross {
rustTarget = "aarch64-apple-darwin";
nixPlatform = "aarch64-darwin";
};
x86_64-windows = mkTxmCross {
rustTarget = "x86_64-pc-windows-gnu";
nixPlatform = "x86_64-windows";
};
};
};
# Development shell with everything needed to build and cross-compile
devShells = forAllSystems (pkgs: {
default = pkgs.mkShell {
nativeBuildInputs = with pkgs; [
# Rust toolchain with target stdlibs pre-installed (no rustup required)
(rust-bin.stable.latest.default.override {
targets = [
"x86_64-apple-darwin"
"aarch64-apple-darwin"
"x86_64-pc-windows-gnu"
];
})
# Development tools
clippy
rustfmt
rust-analyzer
# Cross-compilation via Zig linker
cargo-zigbuild
zig
];
shellHook = ''
echo "txm devShell — tools available:"
echo " cargo build --release (native build)"
echo " cargo clippy (lints)"
echo " cargo fmt (formatting)"
echo " cargo test (tests)"
echo " cargo zigbuild (cross-compile, see flake targets)"
echo " rust-analyzer (LSP)"
'';
};
});
};
}