doc(flake): add examples and templates for cross compilation

This implements very basic examples of how to configure a project to
build mingw-w64 and static musl targets.
This commit is contained in:
Gregory C. Oakes 2021-03-24 23:26:11 -05:00 committed by Nicolas Mattia
parent b3b099d669
commit 380e143955
23 changed files with 883 additions and 3 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
docparse/target
/flake.lock
result*

View file

@ -104,7 +104,14 @@ naersk.buildPackage ./my-package
## Using with Nix Flakes
Copy this `flake.nix` into your repo.
Initialize flakes within your repo by running:
``` bash
nix flake init -t github:nmattia/naersk
nix flake lock
```
Alternatively, copy this `flake.nix` into your repo.
``` nix
{

View file

@ -75,7 +75,14 @@ naersk.buildPackage ./my-package
## Using with Nix Flakes
Copy this `flake.nix` into your repo.
Initialize flakes within your repo by running:
``` bash
nix flake init github:nmattia/naersk
nix flake lock
```
Alternatively, copy this `flake.nix` into your repo.
``` nix
{

7
examples/cross-windows/Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "cross-windows"
version = "0.1.0"

View file

@ -0,0 +1,9 @@
[package]
name = "cross-windows"
version = "0.1.0"
authors = ["John Doe <example@example.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View file

@ -0,0 +1,124 @@
{
"nodes": {
"fenix": {
"inputs": {
"naersk": "naersk",
"nixpkgs": [
"nixpkgs"
],
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1616673789,
"narHash": "sha256-Xo5pOz6GOI9LxME+sTh3nWRWIX9VL2MM8w/T9RAsBEw=",
"owner": "nix-community",
"repo": "fenix",
"rev": "dfd5ba0ef2e343cd9c9c1a2fd25fbc36cf390a74",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "fenix",
"type": "github"
}
},
"flake-utils": {
"locked": {
"lastModified": 1614513358,
"narHash": "sha256-LakhOx3S1dRjnh0b5Dg3mbZyH0ToC9I8Y2wKSkBaTzU=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "5466c5bbece17adaab2d82fae80b46e807611bf3",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"naersk": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1614785451,
"narHash": "sha256-TPw8kQvr2UNCuvndtY+EjyXp6Q5GEW2l9UafXXh1XmI=",
"owner": "nmattia",
"repo": "naersk",
"rev": "e0fe990b478a66178a58c69cf53daec0478ca6f9",
"type": "github"
},
"original": {
"owner": "nmattia",
"repo": "naersk",
"type": "github"
}
},
"naersk_2": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1614785451,
"narHash": "sha256-TPw8kQvr2UNCuvndtY+EjyXp6Q5GEW2l9UafXXh1XmI=",
"owner": "nmattia",
"repo": "naersk",
"rev": "e0fe990b478a66178a58c69cf53daec0478ca6f9",
"type": "github"
},
"original": {
"owner": "nmattia",
"repo": "naersk",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1616259034,
"narHash": "sha256-WlMIiGIXJm7J+jemzd+ksoun6znWmabCZNz76szV158=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "4e0d3868c679da20108db402785f924daa1a7fb5",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"fenix": "fenix",
"flake-utils": "flake-utils",
"naersk": "naersk_2",
"nixpkgs": "nixpkgs"
}
},
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1616625468,
"narHash": "sha256-98nBGTHLLEc0DEk3iskv5Okbey+LBkQ9fplrZCN9dCI=",
"owner": "rust-analyzer",
"repo": "rust-analyzer",
"rev": "d7db38fff9c251c36d0796309b43678bdf9e5bd8",
"type": "github"
},
"original": {
"owner": "rust-analyzer",
"ref": "nightly",
"repo": "rust-analyzer",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

View file

@ -0,0 +1,87 @@
{
inputs = {
nixpkgs.url = github:NixOS/nixpkgs/nixpkgs-unstable;
naersk = {
url = github:nmattia/naersk;
inputs.nixpkgs.follows = "nixpkgs";
};
fenix = {
url = github:nix-community/fenix;
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils.url = github:numtide/flake-utils;
};
outputs = { self, nixpkgs, naersk, fenix, flake-utils }:
flake-utils.lib.eachDefaultSystem (
system: let
pkgs = nixpkgs.legacyPackages.${system};
toolchain = with fenix.packages.${system};
combine [
minimal.rustc
minimal.cargo
targets.x86_64-pc-windows-gnu.latest.rust-std
];
naersk-lib = naersk.lib.${system}.override {
cargo = toolchain;
rustc = toolchain;
};
in
rec {
defaultPackage = packages.x86_64-pc-windows-gnu;
# The rust compiler is internally a cross compiler, so a single
# toolchain can be used to compile multiple targets. In a hermetic
# build system like nix flakes, there's effectively one package for
# every permutation of the supported hosts and targets.
# i.e.: nix build .#packages.x86_64-linux.x86_64-pc-windows-gnu
# where x86_64-linux is the host and x86_64-pc-windows-gnu is the
# target
packages.x86_64-pc-windows-gnu = naersk-lib.buildPackage {
src = ./.;
nativeBuildInputs = with pkgs; [
pkgsCross.mingwW64.stdenv.cc
# Used for running tests.
wineWowPackages.stable
# wineWowPackages is overkill, but it's built in CI for nixpkgs,
# so it doesn't need to be built from source. It needs to provide
# wine64 not just wine. An alternative would be this:
# (wineMinimal.override { wineBuild = "wine64"; })
];
buildInputs = with pkgs.pkgsCross.mingwW64.windows; [ mingw_w64_pthreads pthreads ];
# Configures the target which will be built.
# ref: https://doc.rust-lang.org/cargo/reference/config.html#buildtarget
CARGO_BUILD_TARGET = "x86_64-pc-windows-gnu";
# Configures the linker which will be used. cc.targetPrefix is
# sometimes different than the targets used by rust. i.e.: the
# mingw-w64 linker is "x86_64-w64-mingw32-gcc" whereas the rust
# target is "x86_64-pc-windows-gnu".
#
# This is only necessary if rustc doesn't already know the correct linker to use.
#
# ref: https://doc.rust-lang.org/cargo/reference/config.html#targettriplelinker
# CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER = with pkgs.pkgsCross.mingwW64.stdenv;
# "${cc}/bin/${cc.targetPrefix}gcc";
# Configures the script which should be used to run tests. Since
# this is compiled for 64-bit Windows, use wine64 to run the tests.
# ref: https://doc.rust-lang.org/cargo/reference/config.html#targettriplerunner
CARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUNNER = pkgs.writeScript "wine-wrapper" ''
# Without this, wine will error out when attempting to create the
# prefix in the build's homeless shelter.
export WINEPREFIX="$(mktemp -d)"
exec wine64 $@
'';
doCheck = true;
# Multi-stage builds currently fail for mingwW64.
singleStep = true;
};
}
);
}

View file

@ -0,0 +1,11 @@
fn main() {
println!("Hello, world!");
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

View file

@ -0,0 +1,9 @@
[package]
name = "hello-world"
version = "0.1.0"
authors = ["Gregory C. Oakes <gregcoakes@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View file

@ -0,0 +1,72 @@
{
"nodes": {
"flake-utils": {
"locked": {
"lastModified": 1618217525,
"narHash": "sha256-WGrhVczjXTiswQaoxQ+0PTfbLNeOQM6M36zvLn78AYg=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "c6169a2772643c4a93a0b5ac1c61e296cba68544",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"naersk": {
"inputs": {
"nixpkgs": "nixpkgs"
},
"locked": {
"lastModified": 1618068541,
"narHash": "sha256-enxg0QB53Zis0VJWfJsrX7zCjurpi7lW78EKXbJdzpQ=",
"owner": "nmattia",
"repo": "naersk",
"rev": "b3b099d669fc8b18d361c249091c9fe95d57ebbb",
"type": "github"
},
"original": {
"owner": "nmattia",
"repo": "naersk",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1616527350,
"narHash": "sha256-VkMevY2WLU+K7T/P4wVj18Ms8zyeRfp05ILf556m5Y8=",
"path": "/nix/store/9p64rfhziavzvg45z4gidba52vqbqmgj-source",
"rev": "d3f7e969b9860fb80750147aeb56dab1c730e756",
"type": "path"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1616527350,
"narHash": "sha256-VkMevY2WLU+K7T/P4wVj18Ms8zyeRfp05ILf556m5Y8=",
"path": "/nix/store/9p64rfhziavzvg45z4gidba52vqbqmgj-source",
"rev": "d3f7e969b9860fb80750147aeb56dab1c730e756",
"type": "path"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"naersk": "naersk",
"nixpkgs": "nixpkgs_2"
}
}
},
"root": "root",
"version": 7
}

View file

@ -0,0 +1,33 @@
{
inputs = {
flake-utils.url = "github:numtide/flake-utils";
naersk.url = "github:nmattia/naersk";
};
outputs = { self, nixpkgs, flake-utils, naersk }:
flake-utils.lib.eachDefaultSystem (
system: let
pkgs = nixpkgs.legacyPackages."${system}";
naersk-lib = naersk.lib."${system}";
in
rec {
# `nix build`
packages.hello-world = naersk-lib.buildPackage {
pname = "hello-world";
root = ./.;
};
defaultPackage = packages.hello-world;
# `nix run`
apps.hello-world = flake-utils.lib.mkApp {
drv = packages.hello-world;
};
defaultApp = apps.hello-world;
# `nix develop`
devShell = pkgs.mkShell {
nativeBuildInputs = with pkgs; [ rustc cargo ];
};
}
);
}

View file

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

5
examples/multi-target/Cargo.lock generated Normal file
View file

@ -0,0 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "multi-target"
version = "0.1.0"

View file

@ -0,0 +1,9 @@
[package]
name = "multi-target"
version = "0.1.0"
authors = ["John Doe <example@example.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View file

@ -0,0 +1,124 @@
{
"nodes": {
"fenix": {
"inputs": {
"naersk": "naersk",
"nixpkgs": [
"nixpkgs"
],
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1616673789,
"narHash": "sha256-Xo5pOz6GOI9LxME+sTh3nWRWIX9VL2MM8w/T9RAsBEw=",
"owner": "nix-community",
"repo": "fenix",
"rev": "dfd5ba0ef2e343cd9c9c1a2fd25fbc36cf390a74",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "fenix",
"type": "github"
}
},
"flake-utils": {
"locked": {
"lastModified": 1614513358,
"narHash": "sha256-LakhOx3S1dRjnh0b5Dg3mbZyH0ToC9I8Y2wKSkBaTzU=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "5466c5bbece17adaab2d82fae80b46e807611bf3",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"naersk": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1614785451,
"narHash": "sha256-TPw8kQvr2UNCuvndtY+EjyXp6Q5GEW2l9UafXXh1XmI=",
"owner": "nmattia",
"repo": "naersk",
"rev": "e0fe990b478a66178a58c69cf53daec0478ca6f9",
"type": "github"
},
"original": {
"owner": "nmattia",
"repo": "naersk",
"type": "github"
}
},
"naersk_2": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1614785451,
"narHash": "sha256-TPw8kQvr2UNCuvndtY+EjyXp6Q5GEW2l9UafXXh1XmI=",
"owner": "nmattia",
"repo": "naersk",
"rev": "e0fe990b478a66178a58c69cf53daec0478ca6f9",
"type": "github"
},
"original": {
"owner": "nmattia",
"repo": "naersk",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1616259034,
"narHash": "sha256-WlMIiGIXJm7J+jemzd+ksoun6znWmabCZNz76szV158=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "4e0d3868c679da20108db402785f924daa1a7fb5",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"fenix": "fenix",
"flake-utils": "flake-utils",
"naersk": "naersk_2",
"nixpkgs": "nixpkgs"
}
},
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1616625468,
"narHash": "sha256-98nBGTHLLEc0DEk3iskv5Okbey+LBkQ9fplrZCN9dCI=",
"owner": "rust-analyzer",
"repo": "rust-analyzer",
"rev": "d7db38fff9c251c36d0796309b43678bdf9e5bd8",
"type": "github"
},
"original": {
"owner": "rust-analyzer",
"ref": "nightly",
"repo": "rust-analyzer",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

View file

@ -0,0 +1,118 @@
{
inputs = {
nixpkgs.url = github:NixOS/nixpkgs/nixpkgs-unstable;
naersk = {
url = github:nmattia/naersk;
inputs.nixpkgs.follows = "nixpkgs";
};
fenix = {
url = github:nix-community/fenix;
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils.url = github:numtide/flake-utils;
};
outputs = { self, nixpkgs, naersk, fenix, flake-utils }:
flake-utils.lib.eachDefaultSystem (
system: let
pkgs = nixpkgs.legacyPackages.${system};
toolchain = with fenix.packages.${system};
combine [
minimal.rustc
minimal.cargo
targets.x86_64-unknown-linux-musl.latest.rust-std
targets.x86_64-pc-windows-gnu.latest.rust-std
];
# Make naersk aware of the tool chain which is to be used.
naersk-lib = naersk.lib.${system}.override {
cargo = toolchain;
rustc = toolchain;
};
# Utility for merging the common cargo configuration with the target
# specific configuration.
naerskBuildPackage = target: args: naersk-lib.buildPackage
(args // { CARGO_BUILD_TARGET = target; } // cargoConfig);
# All of the CARGO_* configurations which should be used for all
# targets. Only use this for options which should be universally
# applied or which can be applied to a specific target triple.
# This is also merged into the devShell.
cargoConfig = {
# Enables static compilation.
#
# If the resulting executable is still considered dynamically
# linked by ldd but doesn't have anything actually linked to it,
# don't worry. It's still statically linked. It just has static
# position independent execution enabled.
# ref: https://doc.rust-lang.org/cargo/reference/config.html#targettriplerustflags
CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_RUSTFLAGS = "-C target-feature=+crt-static";
# Configures the script which should be used to run tests. Since
# this is compiled for 64-bit Windows, use wine64 to run the tests.
# ref: https://doc.rust-lang.org/cargo/reference/config.html#targettriplerunner
CARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUNNER = pkgs.writeScript "wine-wrapper" ''
# Without this, wine will error out when attempting to create the
# prefix in the build's homeless shelter.
export WINEPREFIX="$(mktemp -d)"
exec wine64 $@
'';
# Configures the linker which will be used. cc.targetPrefix is
# sometimes different than the targets used by rust. i.e.: the
# mingw-w64 linker is "x86_64-w64-mingw32-gcc" whereas the rust
# target is "x86_64-pc-windows-gnu".
#
# This is only necessary if rustc doesn't already know the correct linker to use.
#
# ref: https://doc.rust-lang.org/cargo/reference/config.html#targettriplelinker
# CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER = with pkgs.pkgsCross.mingwW64.stdenv;
# "${cc}/bin/${cc.targetPrefix}gcc";
# CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER = with pkgs.pkgsStatic.stdenv;
# "${cc}/bin/${cc.targetPrefix}gcc";
};
in
rec {
defaultPackage = packages.x86_64-unknown-linux-musl;
# The rust compiler is internally a cross compiler, so a single
# toolchain can be used to compile multiple targets. In a hermetic
# build system like nix flakes, there's effectively one package for
# every permutation of the supported hosts and targets.
# i.e.: nix build .#packages.x86_64-linux.x86_64-pc-windows-gnu
# where x86_64-linux is the host and x86_64-pc-windows-gnu is the
# target
packages.x86_64-unknown-linux-musl = naerskBuildPackage "x86_64-unknown-linux-musl" {
src = ./.;
nativeBuildInputs = with pkgs; [ pkgsStatic.stdenv.cc ];
doCheck = true;
};
packages.x86_64-pc-windows-gnu = naerskBuildPackage "x86_64-pc-windows-gnu" {
src = ./.;
nativeBuildInputs = with pkgs; [
pkgsCross.mingwW64.stdenv.cc
# Used for running tests.
wineWowPackages.stable
# wineWowPackages is overkill, but it's built in CI for nixpkgs,
# so it doesn't need to be built from source. It needs to provide
# wine64 not just wine. An alternative would be this:
# (wineMinimal.override { wineBuild = "wine64"; })
];
buildInputs = with pkgs.pkgsCross.mingwW64.windows; [ mingw_w64_pthreads pthreads ];
doCheck = true;
# Multi-stage builds currently fail for mingwW64.
singleStep = true;
};
devShell = pkgs.mkShell (
{
inputsFrom = with packages; [ x86_64-unknown-linux-musl x86_64-pc-windows-gnu ];
CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl";
} // cargoConfig
);
}
);
}

View file

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

5
examples/static-musl/Cargo.lock generated Normal file
View file

@ -0,0 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "static-musl"
version = "0.1.0"

View file

@ -0,0 +1,9 @@
[package]
name = "static-musl"
version = "0.1.0"
authors = ["John Doe <example@example.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View file

@ -0,0 +1,124 @@
{
"nodes": {
"fenix": {
"inputs": {
"naersk": "naersk",
"nixpkgs": [
"nixpkgs"
],
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1616673789,
"narHash": "sha256-Xo5pOz6GOI9LxME+sTh3nWRWIX9VL2MM8w/T9RAsBEw=",
"owner": "nix-community",
"repo": "fenix",
"rev": "dfd5ba0ef2e343cd9c9c1a2fd25fbc36cf390a74",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "fenix",
"type": "github"
}
},
"flake-utils": {
"locked": {
"lastModified": 1614513358,
"narHash": "sha256-LakhOx3S1dRjnh0b5Dg3mbZyH0ToC9I8Y2wKSkBaTzU=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "5466c5bbece17adaab2d82fae80b46e807611bf3",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"naersk": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1614785451,
"narHash": "sha256-TPw8kQvr2UNCuvndtY+EjyXp6Q5GEW2l9UafXXh1XmI=",
"owner": "nmattia",
"repo": "naersk",
"rev": "e0fe990b478a66178a58c69cf53daec0478ca6f9",
"type": "github"
},
"original": {
"owner": "nmattia",
"repo": "naersk",
"type": "github"
}
},
"naersk_2": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1614785451,
"narHash": "sha256-TPw8kQvr2UNCuvndtY+EjyXp6Q5GEW2l9UafXXh1XmI=",
"owner": "nmattia",
"repo": "naersk",
"rev": "e0fe990b478a66178a58c69cf53daec0478ca6f9",
"type": "github"
},
"original": {
"owner": "nmattia",
"repo": "naersk",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1616259034,
"narHash": "sha256-WlMIiGIXJm7J+jemzd+ksoun6znWmabCZNz76szV158=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "4e0d3868c679da20108db402785f924daa1a7fb5",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"fenix": "fenix",
"flake-utils": "flake-utils",
"naersk": "naersk_2",
"nixpkgs": "nixpkgs"
}
},
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1616625468,
"narHash": "sha256-98nBGTHLLEc0DEk3iskv5Okbey+LBkQ9fplrZCN9dCI=",
"owner": "rust-analyzer",
"repo": "rust-analyzer",
"rev": "d7db38fff9c251c36d0796309b43678bdf9e5bd8",
"type": "github"
},
"original": {
"owner": "rust-analyzer",
"ref": "nightly",
"repo": "rust-analyzer",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

View file

@ -0,0 +1,73 @@
{
inputs = {
nixpkgs.url = github:NixOS/nixpkgs/nixpkgs-unstable;
naersk = {
url = github:nmattia/naersk;
inputs.nixpkgs.follows = "nixpkgs";
};
fenix = {
url = github:nix-community/fenix;
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils.url = github:numtide/flake-utils;
};
outputs = { self, nixpkgs, naersk, fenix, flake-utils }:
flake-utils.lib.eachDefaultSystem (
system: let
pkgs = nixpkgs.legacyPackages.${system};
toolchain = with fenix.packages.${system};
combine [
minimal.rustc
minimal.cargo
targets.x86_64-unknown-linux-musl.latest.rust-std
];
naersk-lib = naersk.lib.${system}.override {
cargo = toolchain;
rustc = toolchain;
};
in
rec {
defaultPackage = packages.x86_64-unknown-linux-musl;
# The rust compiler is internally a cross compiler, so a single
# toolchain can be used to compile multiple targets. In a hermetic
# build system like nix flakes, there's effectively one package for
# every permutation of the supported hosts and targets.
# i.e.: nix build .#packages.x86_64-linux.x86_64-pc-windows-gnu
# where x86_64-linux is the host and x86_64-pc-windows-gnu is the
# target
packages.x86_64-unknown-linux-musl = naersk-lib.buildPackage {
src = ./.;
nativeBuildInputs = with pkgs; [ pkgsStatic.stdenv.cc ];
# Configures the target which will be built.
# ref: https://doc.rust-lang.org/cargo/reference/config.html#buildtarget
CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl";
# Enables static compilation.
#
# If the resulting executable is still considered dynamically
# linked by ldd but doesn't have anything actually linked to it,
# don't worry. It's still statically linked. It just has static
# position independent execution enabled.
# ref: https://github.com/rust-lang/rust/issues/79624#issuecomment-737415388
CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static";
# Configures the linker which will be used. cc.targetPrefix is
# sometimes different than the targets used by rust. i.e.: the
# mingw-w64 linker is "x86_64-w64-mingw32-gcc" whereas the rust
# target is "x86_64-pc-windows-gnu".
#
# This is only necessary if rustc doesn't already know the correct linker to use.
#
# ref: https://doc.rust-lang.org/cargo/reference/config.html#targettriplelinker
# CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER = with pkgs.pkgsStatic.stdenv;
# "${cc}/bin/${cc.targetPrefix}gcc";
doCheck = true;
};
}
);
}

View file

@ -0,0 +1,11 @@
fn main() {
println!("Hello, world!");
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

View file

@ -5,10 +5,39 @@
let
forAllSystems = nixpkgs.lib.genAttrs [ "x86_64-linux" "x86_64-darwin" "i686-linux" "aarch64-linux" ];
in
{
rec {
# Naersk is not a package, not an app, not a module... It's just a Library
lib = forAllSystems (system: nixpkgs.legacyPackages."${system}".callPackage ./default.nix { });
# Useful when composing with other flakes
overlay = import ./overlay.nix;
# Expose the examples as templates.
defaultTemplate = templates.hello-world;
templates = {
hello-world = {
path =
builtins.filterSource (path: type: baseNameOf path == "flake.nix")
./examples/hello-world;
description = "Build a rust project with naersk.";
};
cross-windows = {
path =
builtins.filterSource (path: type: baseNameOf path == "flake.nix")
./examples/cross-windows;
description = "Cross compile a rust project for use on Windows.";
};
static-musl = {
path =
builtins.filterSource (path: type: baseNameOf path == "flake.nix")
./examples/static-musl;
description = "Compile a rust project statically using musl.";
};
multi-target = {
path =
builtins.filterSource (path: type: baseNameOf path == "flake.nix")
./examples/multi-target;
description = "Compile a rust project to multiple targets.";
};
};
};
}