forked from
nonbinary.computer/jacquard
A better Rust ATProto crate
1{inputs, ...}: {
2 imports = [inputs.rust-flake.flakeModules.nixpkgs];
3
4 perSystem = {
5 pkgs,
6 lib,
7 config,
8 system,
9 ...
10 }: let
11 # Get the filtered source from rust-project
12 src = config.rust-project.src;
13
14 # Import nixpkgs with rust-overlay for getting rust toolchains (not cross)
15 pkgs-rust = import inputs.nixpkgs {
16 inherit system;
17 overlays = [(import inputs.rust-overlay)];
18 };
19
20 # Helper to create a cross-compiled package
21 mkCrossPackage = {
22 crossSystem,
23 rustTarget,
24 extraArgs ? {},
25 }: let
26 # Import nixpkgs with cross-compilation configured (no overlays)
27 pkgs-cross = import inputs.nixpkgs {
28 inherit crossSystem;
29 localSystem = system;
30 };
31
32 # Get rust toolchain from host system with the cross target added
33 rustToolchain = pkgs-rust.rust-bin.stable.latest.default.override {
34 targets = [rustTarget];
35 };
36
37 # Set up crane with the rust toolchain
38 craneLib = (inputs.crane.mkLib pkgs-cross).overrideToolchain rustToolchain;
39
40 # Common crane args
41 commonArgs =
42 {
43 inherit src;
44 pname = "jacquard-lexgen";
45 strictDeps = true;
46 doCheck = false; # Tests require lexicon corpus files
47
48 # Native build inputs (tools that run during build)
49 nativeBuildInputs = with pkgs; [
50 installShellFiles
51 ];
52
53 postInstall = ''
54 # Install man pages and completions from build script output
55 for outdir in target/${rustTarget}/release/build/jacquard-lexgen-*/out; do
56 if [ -d "$outdir/man" ]; then
57 installManPage $outdir/man/*.1
58 fi
59 if [ -d "$outdir/completions" ]; then
60 for completion in $outdir/completions/*; do
61 case "$(basename "$completion")" in
62 *.bash) installShellCompletion --bash "$completion" ;;
63 *.fish) installShellCompletion --fish "$completion" ;;
64 _*) installShellCompletion --zsh "$completion" ;;
65 esac
66 done
67 fi
68 done
69
70 # Install example lexicons.kdl config
71 install -Dm644 ${./../../crates/jacquard-lexgen/lexicons.kdl.example} $out/share/doc/jacquard-lexgen/lexicons.kdl.example
72 '';
73 }
74 // extraArgs;
75 in
76 craneLib.buildPackage commonArgs;
77 in {
78 packages = {
79 # Linux targets
80 jacquard-lexgen-x86_64-linux = mkCrossPackage {
81 crossSystem = {
82 config = "x86_64-unknown-linux-gnu";
83 };
84 rustTarget = "x86_64-unknown-linux-gnu";
85 };
86
87 jacquard-lexgen-aarch64-linux = mkCrossPackage {
88 crossSystem = {
89 config = "aarch64-unknown-linux-gnu";
90 };
91 rustTarget = "aarch64-unknown-linux-gnu";
92 };
93
94 # macOS targets
95 jacquard-lexgen-x86_64-darwin = mkCrossPackage {
96 crossSystem = {
97 config = "x86_64-apple-darwin";
98 };
99 rustTarget = "x86_64-apple-darwin";
100 };
101
102 jacquard-lexgen-aarch64-darwin = mkCrossPackage {
103 crossSystem = {
104 config = "aarch64-apple-darwin";
105 };
106 rustTarget = "aarch64-apple-darwin";
107 };
108
109 # Windows targets
110 jacquard-lexgen-x86_64-windows = mkCrossPackage {
111 crossSystem = {
112 config = "x86_64-w64-mingw32";
113 libc = "msvcrt";
114 };
115 rustTarget = "x86_64-pc-windows-gnu";
116 };
117
118 # TODO: aarch64-windows cross-compilation broken in nixpkgs
119 # Issue: mingw-w64-pthreads build fails with missing winver.h header
120 # The aarch64-w64-mingw32 toolchain setup in nixpkgs is incomplete
121 # Workaround: build on actual Windows with MSVC or wait for nixpkgs fix
122 # jacquard-lexgen-aarch64-windows = mkCrossPackage {
123 # crossSystem = {
124 # config = "aarch64-w64-mingw32";
125 # useLLVM = true;
126 # };
127 # rustTarget = "aarch64-pc-windows-gnullvm";
128 # };
129 };
130 };
131}