Skip to content

Commit 449f706

Browse files
committed
feat: document cross-compilation with nix
1 parent aef82ae commit 449f706

1 file changed

Lines changed: 119 additions & 1 deletion

File tree

README.md

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Requirements
5959
overlays = [steiger.overlays.ociTools];
6060
pkgs = import nixpkgs { inherit system overlays; };
6161
in {
62-
packages.${system} = {
62+
steigerImages.${system} = {
6363
default = pkgs.ociTools.buildImage {
6464
name = "hello";
6565
@@ -83,6 +83,124 @@ Requirements
8383
}
8484
```
8585

86+
#### Cross-compilation
87+
88+
Steiger provides a nested outputs structure for organizing packages when you need to
89+
configure cross-compilation yourself using specialized tools like crane for Rust projects.
90+
91+
##### Configuration
92+
93+
Enable the nested path structure by adding the following to your `steiger.yaml`:
94+
95+
```yaml
96+
build:
97+
services:
98+
type: nix
99+
platformStrategy: crossSystem
100+
packages:
101+
service: default
102+
```
103+
104+
This changes how packages should be organized in your flake outputs,
105+
creating a nested structure that separates build host and target systems.
106+
107+
##### Attribute Path Structure
108+
109+
When `platformStrategy: crossSystem` is enabled, packages must be organized as:
110+
`<flake-path>#steigerImages.<host-system>.<target-system>.<package-name>`
111+
112+
Examples:
113+
114+
- `#steigerImages.x86_64-linux.aarch64-linux.default` - Build on x86_64-linux, targeting aarch64-linux
115+
- `#steigerImages.aarch64-darwin.x86_64-linux.default` - Build on aarch64-darwin, targeting x86_64-linux
116+
- `#steigerImages.x86_64-linux.x86_64-linux.default` - Native build on x86_64-linux
117+
118+
This nested structure allows you to:
119+
120+
- Build for all combinations of host and target systems
121+
- Configure your own cross-compilation toolchains
122+
- Maintain clear separation between build-time and runtime dependencies
123+
124+
<details>
125+
<summary>Example of rust cross-compilation with crane</summary>
126+
127+
```nix
128+
{
129+
inputs = {
130+
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
131+
steiger.url = "github:brainhivenl/steiger";
132+
crane.url = "github:ipetkov/crane";
133+
rust-overlay = {
134+
url = "github:oxalica/rust-overlay";
135+
inputs.nixpkgs.follows = "nixpkgs";
136+
};
137+
};
138+
139+
outputs = {
140+
nixpkgs,
141+
steiger,
142+
crane,
143+
rust-overlay,
144+
...
145+
}: let
146+
systems = ["aarch64-darwin" "x86_64-darwin" "x86_64-linux" "aarch64-linux"];
147+
overlays = [steiger.overlays.ociTools (import rust-overlay)];
148+
149+
eachCrossSystem = packages:
150+
nixpkgs.lib.genAttrs systems (localSystem:
151+
nixpkgs.lib.genAttrs systems (crossSystem:
152+
packages localSystem crossSystem));
153+
154+
# for more information see:
155+
# https://github.com/ipetkov/crane/blob/master/examples/cross-rust-overlay/flake.nix
156+
crateExpression = {
157+
craneLib,
158+
openssl,
159+
libiconv,
160+
lib,
161+
pkg-config,
162+
stdenv,
163+
}:
164+
craneLib.buildPackage {
165+
src = craneLib.cleanCargoSource ./.;
166+
strictDeps = true;
167+
168+
nativeBuildInputs =
169+
[pkg-config]
170+
++ lib.optionals stdenv.buildPlatform.isDarwin [libiconv];
171+
172+
buildInputs = [openssl];
173+
};
174+
in {
175+
steigerImages = eachCrossSystem (localSystem: crossSystem: let
176+
pkgs = import nixpkgs {
177+
system = localSystem;
178+
inherit overlays;
179+
};
180+
pkgsCross = import nixpkgs {
181+
inherit localSystem crossSystem overlays;
182+
};
183+
184+
craneLib = crane.mkLib pkgsCross;
185+
package = pkgsCross.callPackage crateExpression {inherit craneLib;};
186+
in {
187+
default = pkgs.ociTools.buildImage {
188+
name = "my-service";
189+
190+
copyToRoot = pkgsCross.buildEnv {
191+
name = "service-env";
192+
paths = [package];
193+
pathsToLink = ["/bin"];
194+
};
195+
196+
config.Cmd = ["/bin/${package.pname}"];
197+
compressor = "none";
198+
};
199+
});
200+
};
201+
}
202+
```
203+
86204
</details>
87205

88206
## Build Caching

0 commit comments

Comments
 (0)