Revamp examples & README

This commit is contained in:
Patryk Wychowaniec 2022-07-09 12:08:57 +02:00
parent cddffb5aa2
commit c6a45e4277
20 changed files with 786 additions and 694 deletions

317
README.md
View file

@ -2,43 +2,193 @@
[![GitHub Actions](https://github.com/nix-community/naersk/workflows/test/badge.svg?branch=master)](https://github.com/nix-community/naersk/actions) [![GitHub Actions](https://github.com/nix-community/naersk/workflows/test/badge.svg?branch=master)](https://github.com/nix-community/naersk/actions)
Nix support for building [cargo] crates. Build Rust projects with ease!
* [Install](#install) * [Setup](#setup)
* [Configuration](#configuration) * [Usage](#usage)
## Install ## Setup
Use [niv]: ### Using Flakes
``` shell ``` shell
$ nix flake init -t github:nix-community/naersk
$ nix flake lock
```
Alternatively, store this as `flake.nix` in your repository:
``` nix
{
inputs = {
flake-utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};
outputs = { self, flake-utils, naersk, nixpkgs }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = (import nixpkgs) {
inherit system;
};
naersk' = pkgs.callPackage naersk {};
in rec {
# For `nix build` & `nix run`:
defaultPackage = naersk'.buildPackage {
src = ./.;
};
# For `nix develop` (optional, can be skipped):
devShell = pkgs.mkShell {
nativeBuildInputs = with pkgs; [ rustc cargo ];
};
}
);
}
```
This assumes `flake.nix` is created next to `Cargo.toml` & `Cargo.lock` - if
that's not the case for you, adjust `./.` in `naersk'.buildPackage`.
Note that Naersk by default ignores the `rust-toolchain` file, using whatever
Rust compiler version is present in `nixpkgs`.
If you have a custom `rust-toolchain` file, you can make Naersk use it this way:
``` nix
{
inputs = {
flake-utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk";
nixpkgs-mozilla = {
url = "github:mozilla/nixpkgs-mozilla";
flake = false;
};
};
outputs = { self, flake-utils, naersk, nixpkgs, nixpkgs-mozilla }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = (import nixpkgs) {
inherit system;
overlays = [
(import nixpkgs-mozilla)
];
};
toolchain = (pkgs.rustChannelOf {
rustToolchain = ./rust-toolchain;
sha256 = "";
# ^ After you run `nix build`, replace this with the actual
# hash from the error message
}).rust;
naersk' = pkgs.callPackage naersk {
cargo = toolchain;
rustc = toolchain;
};
in rec {
# For `nix build` & `nix run`:
defaultPackage = naersk'.buildPackage {
src = ./.;
};
# For `nix develop` (optional, can be skipped):
devShell = pkgs.mkShell {
nativeBuildInputs = [ toolchain ];
};
}
);
}
```
### Using Niv
``` shell
$ niv init
$ niv add nix-community/naersk $ niv add nix-community/naersk
``` ```
And then ... and then create `default.nix` with:
``` nix ``` nix
let let
pkgs = import <nixpkgs> {}; pkgs = import <nixpkgs> {};
sources = import ./nix/sources.nix; sources = import ./nix/sources.nix;
naersk = pkgs.callPackage sources.naersk {}; naersk = pkgs.callPackage sources.naersk {};
in naersk.buildPackage ./path/to/rust
in
naersk.buildPackage ./.
``` ```
_NOTE_: `./path/to/rust/` should contain a `Cargo.lock`. This assumes `default.nix` is created next to `Cargo.toml` & `Cargo.lock` - if
that's not the case for you, adjust `./.` in `naersk.buildPackage`.
## Configuration Note that Naersk by default ignores the `rust-toolchain` file, using whatever
Rust compiler version is present in `nixpkgs`.
The `buildPackage` function also accepts an attribute set. The attributes are If you have a custom `rust-toolchain` file, you can make Naersk use it this way:
described below. Any attribute that is _not_ listed below will be forwarded _as
is_ to `stdenv.mkDerivation`. When the argument passed in _not_ an attribute ``` shell
set, e.g. $ niv add mozilla/nixpkgs-mozilla
```
... and then:
``` nix ``` nix
naersk.buildPackage theArg let
sources = import ./nix/sources.nix;
nixpkgs-mozilla = import sources.nixpkgs-mozilla;
pkgs = import sources.nixpkgs {
overlays = [
nixpkgs-mozilla
];
};
toolchain = (pkgs.rustChannelOf {
rustToolchain = ./rust-toolchain;
sha256 = "";
# ^ After you run `nix-build`, replace this with the actual
# hash from the error message
}).rust;
naersk = pkgs.callPackage sources.naersk {
cargo = toolchain;
rustc = toolchain;
};
in
naersk.buildPackage ./.
``` ```
it is converted to an attribute set equivalent to `{ root = theArg; }`. ## Usage
Naersk provides a function called `buildPackage` that takes an attribute set
describing your application's directory, its dependencies etc.; in general, the
usage is:
``` nix
naersk.buildPackage {
# Assuming there's `Cargo.toml` right in this directory:
src = ./.;
someOption = "yass";
someOtherOption = false;
CARGO_ENVIRONMENTAL_VARIABLE = "test";
}
```
Some of the options (described below) are used by Naersk to affect the building
process, rest is passed-through into `mkDerivation`.
### `buildPackage`'s parameters
| Attribute | Description | | Attribute | Description |
| - | - | | - | - |
@ -76,130 +226,17 @@ it is converted to an attribute set equivalent to `{ root = theArg; }`.
| `postInstall` | Optional hook to run after the compilation is done; inside this script, `$out/bin` contains compiled Rust binaries. Useful if your application needs e.g. custom environment variables, in which case you can simply run `wrapProgram $out/bin/your-app-name` in here. Default: `false` | | `postInstall` | Optional hook to run after the compilation is done; inside this script, `$out/bin` contains compiled Rust binaries. Useful if your application needs e.g. custom environment variables, in which case you can simply run `wrapProgram $out/bin/your-app-name` in here. Default: `false` |
| `usePureFromTOML` | Whether to use the `fromTOML` built-in or not. When set to `false` the python package `remarshal` is used instead (in a derivation) and the JSON output is read with `builtins.fromJSON`. This is a workaround for old versions of Nix. May be used safely from Nix 2.3 onwards where all bugs in `builtins.fromTOML` seem to have been fixed. Default: `true` | | `usePureFromTOML` | Whether to use the `fromTOML` built-in or not. When set to `false` the python package `remarshal` is used instead (in a derivation) and the JSON output is read with `builtins.fromJSON`. This is a workaround for old versions of Nix. May be used safely from Nix 2.3 onwards where all bugs in `builtins.fromTOML` seem to have been fixed. Default: `true` |
## Using naersk with nixpkgs-mozilla ## Tips & Tricks
The [nixpkgs-mozilla](https://github.com/mozilla/nixpkgs-mozilla) overlay ### Using OpenSSL
provides nightly versions of `rustc` and `cargo`. Below is an example setup for
using it with naersk: If your application uses OpenSSL (making the build process fail), try:
``` nix ``` nix
let naersk.buildPackage {
sources = import ./nix/sources.nix; # ...
nixpkgs-mozilla = import sources.nixpkgs-mozilla;
pkgs = import sources.nixpkgs { nativeBuildInputs = with pkgs; [ pkg-config ];
overlays = buildInputs = with pkgs; [ openssl ];
[
nixpkgs-mozilla
(self: super:
{
rustc = self.latest.rustChannels.nightly.rust;
cargo = self.latest.rustChannels.nightly.rust;
}
)
];
};
naersk = pkgs.callPackage sources.naersk {};
in
naersk.buildPackage ./my-package
```
[cargo]: https://crates.io/
[niv]: https://github.com/nmattia/niv
## Using with Nix Flakes
Initialize flakes within your repo by running:
``` bash
nix flake init -t github:nix-community/naersk
nix flake lock
```
Alternatively, copy this `flake.nix` into your repo.
``` nix
{
inputs = {
utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk";
};
outputs = { self, nixpkgs, utils, naersk }:
utils.lib.eachDefaultSystem (system: let
pkgs = nixpkgs.legacyPackages."${system}";
naersk-lib = naersk.lib."${system}";
in rec {
# `nix build`
packages.my-project = naersk-lib.buildPackage {
pname = "my-project";
root = ./.;
};
defaultPackage = packages.my-project;
# `nix run`
apps.my-project = utils.lib.mkApp {
drv = packages.my-project;
};
defaultApp = apps.my-project;
# `nix develop`
devShell = pkgs.mkShell {
nativeBuildInputs = with pkgs; [ rustc cargo ];
};
});
}
```
If you want to use a specific toolchain version instead of the latest stable
available in nixpkgs, you can use mozilla's nixpkgs overlay in your flake.
``` nix
{
inputs = {
utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk";
mozillapkgs = {
url = "github:mozilla/nixpkgs-mozilla";
flake = false;
};
};
outputs = { self, nixpkgs, utils, naersk, mozillapkgs }:
utils.lib.eachDefaultSystem (system: let
pkgs = nixpkgs.legacyPackages."${system}";
# Get a specific rust version
mozilla = pkgs.callPackage (mozillapkgs + "/package-set.nix") {};
rust = (mozilla.rustChannelOf {
date = "2020-01-01"; # get the current date with `date -I`
channel = "nightly";
sha256 = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
}).rust;
# Override the version used in naersk
naersk-lib = naersk.lib."${system}".override {
cargo = rust;
rustc = rust;
};
in rec {
# `nix build`
packages.my-project = naersk-lib.buildPackage {
pname = "my-project";
root = ./.;
};
defaultPackage = packages.my-project;
# `nix run`
apps.my-project = utils.lib.mkApp {
drv = packages.my-project;
};
defaultApp = apps.my-project;
# `nix develop`
devShell = pkgs.mkShell {
# supply the specific rust version
nativeBuildInputs = [ rust ];
};
});
} }
``` ```

View file

@ -2,170 +2,207 @@
[![GitHub Actions](https://github.com/nix-community/naersk/workflows/test/badge.svg?branch=master)](https://github.com/nix-community/naersk/actions) [![GitHub Actions](https://github.com/nix-community/naersk/workflows/test/badge.svg?branch=master)](https://github.com/nix-community/naersk/actions)
Nix support for building [cargo] crates. Build Rust projects with ease!
* [Install](#install) * [Setup](#setup)
* [Configuration](#configuration) * [Usage](#usage)
## Install ## Setup
Use [niv]: ### Using Flakes
``` shell ``` shell
$ niv add nix-community/naersk $ nix flake init -t github:nix-community/naersk
$ nix flake lock
``` ```
And then Alternatively, store this as `flake.nix` in your repository:
``` nix
let
pkgs = import <nixpkgs> {};
sources = import ./nix/sources.nix;
naersk = pkgs.callPackage sources.naersk {};
in naersk.buildPackage ./path/to/rust
```
_NOTE_: `./path/to/rust/` should contain a `Cargo.lock`.
## Configuration
The `buildPackage` function also accepts an attribute set. The attributes are
described below. Any attribute that is _not_ listed below will be forwarded _as
is_ to `stdenv.mkDerivation`. When the argument passed in _not_ an attribute
set, e.g.
``` nix
naersk.buildPackage theArg
```
it is converted to an attribute set equivalent to `{ root = theArg; }`.
GEN_CONFIGURATION
## Using naersk with nixpkgs-mozilla
The [nixpkgs-mozilla](https://github.com/mozilla/nixpkgs-mozilla) overlay
provides nightly versions of `rustc` and `cargo`. Below is an example setup for
using it with naersk:
``` nix
let
sources = import ./nix/sources.nix;
nixpkgs-mozilla = import sources.nixpkgs-mozilla;
pkgs = import sources.nixpkgs {
overlays =
[
nixpkgs-mozilla
(self: super:
{
rustc = self.latest.rustChannels.nightly.rust;
cargo = self.latest.rustChannels.nightly.rust;
}
)
];
};
naersk = pkgs.callPackage sources.naersk {};
in
naersk.buildPackage ./my-package
```
[cargo]: https://crates.io/
[niv]: https://github.com/nmattia/niv
## Using with Nix Flakes
Initialize flakes within your repo by running:
``` bash
nix flake init -t github:nix-community/naersk
nix flake lock
```
Alternatively, copy this `flake.nix` into your repo.
``` nix ``` nix
{ {
inputs = { inputs = {
utils.url = "github:numtide/flake-utils"; flake-utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk"; naersk.url = "github:nix-community/naersk";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
}; };
outputs = { self, nixpkgs, utils, naersk }: outputs = { self, flake-utils, naersk, nixpkgs }:
utils.lib.eachDefaultSystem (system: let flake-utils.lib.eachDefaultSystem (system:
pkgs = nixpkgs.legacyPackages."${system}"; let
naersk-lib = naersk.lib."${system}"; pkgs = (import nixpkgs) {
in rec { inherit system;
# `nix build` };
packages.my-project = naersk-lib.buildPackage {
pname = "my-project";
root = ./.;
};
defaultPackage = packages.my-project;
# `nix run` naersk' = pkgs.callPackage naersk {};
apps.my-project = utils.lib.mkApp {
drv = packages.my-project; in rec {
}; # For `nix build` & `nix run`:
defaultApp = apps.my-project; defaultPackage = naersk'.buildPackage {
src = ./.;
};
# `nix develop` # For `nix develop` (optional, can be skipped):
devShell = pkgs.mkShell { devShell = pkgs.mkShell {
nativeBuildInputs = with pkgs; [ rustc cargo ]; nativeBuildInputs = with pkgs; [ rustc cargo ];
}; };
}); }
);
} }
``` ```
If you want to use a specific toolchain version instead of the latest stable This assumes `flake.nix` is created next to `Cargo.toml` & `Cargo.lock` - if
available in nixpkgs, you can use mozilla's nixpkgs overlay in your flake. that's not the case for you, adjust `./.` in `naersk'.buildPackage`.
Note that Naersk by default ignores the `rust-toolchain` file, using whatever
Rust compiler version is present in `nixpkgs`.
If you have a custom `rust-toolchain` file, you can make Naersk use it this way:
``` nix ``` nix
{ {
inputs = { inputs = {
utils.url = "github:numtide/flake-utils"; flake-utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk"; naersk.url = "github:nix-community/naersk";
mozillapkgs = {
nixpkgs-mozilla = {
url = "github:mozilla/nixpkgs-mozilla"; url = "github:mozilla/nixpkgs-mozilla";
flake = false; flake = false;
}; };
}; };
outputs = { self, nixpkgs, utils, naersk, mozillapkgs }: outputs = { self, flake-utils, naersk, nixpkgs, nixpkgs-mozilla }:
utils.lib.eachDefaultSystem (system: let flake-utils.lib.eachDefaultSystem (system:
pkgs = nixpkgs.legacyPackages."${system}"; let
pkgs = (import nixpkgs) {
inherit system;
# Get a specific rust version overlays = [
mozilla = pkgs.callPackage (mozillapkgs + "/package-set.nix") {}; (import nixpkgs-mozilla)
rust = (mozilla.rustChannelOf { ];
date = "2020-01-01"; # get the current date with `date -I` };
channel = "nightly";
sha256 = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
}).rust;
# Override the version used in naersk toolchain = (pkgs.rustChannelOf {
naersk-lib = naersk.lib."${system}".override { rustToolchain = ./rust-toolchain;
cargo = rust; sha256 = "";
rustc = rust; # ^ After you run `nix build`, replace this with the actual
}; # hash from the error message
in rec { }).rust;
# `nix build`
packages.my-project = naersk-lib.buildPackage {
pname = "my-project";
root = ./.;
};
defaultPackage = packages.my-project;
# `nix run` naersk' = pkgs.callPackage naersk {
apps.my-project = utils.lib.mkApp { cargo = toolchain;
drv = packages.my-project; rustc = toolchain;
}; };
defaultApp = apps.my-project;
in rec {
# For `nix build` & `nix run`:
defaultPackage = naersk'.buildPackage {
src = ./.;
};
# `nix develop` # For `nix develop` (optional, can be skipped):
devShell = pkgs.mkShell { devShell = pkgs.mkShell {
# supply the specific rust version nativeBuildInputs = [ toolchain ];
nativeBuildInputs = [ rust ]; };
}; }
}); );
}
```
### Using Niv
``` shell
$ niv init
$ niv add nix-community/naersk
```
... and then create `default.nix` with:
``` nix
let
pkgs = import <nixpkgs> {};
sources = import ./nix/sources.nix;
naersk = pkgs.callPackage sources.naersk {};
in
naersk.buildPackage ./.
```
This assumes `default.nix` is created next to `Cargo.toml` & `Cargo.lock` - if
that's not the case for you, adjust `./.` in `naersk.buildPackage`.
Note that Naersk by default ignores the `rust-toolchain` file, using whatever
Rust compiler version is present in `nixpkgs`.
If you have a custom `rust-toolchain` file, you can make Naersk use it this way:
``` shell
$ niv add mozilla/nixpkgs-mozilla
```
... and then:
``` nix
let
sources = import ./nix/sources.nix;
nixpkgs-mozilla = import sources.nixpkgs-mozilla;
pkgs = import sources.nixpkgs {
overlays = [
nixpkgs-mozilla
];
};
toolchain = (pkgs.rustChannelOf {
rustToolchain = ./rust-toolchain;
sha256 = "";
# ^ After you run `nix-build`, replace this with the actual
# hash from the error message
}).rust;
naersk = pkgs.callPackage sources.naersk {
cargo = toolchain;
rustc = toolchain;
};
in
naersk.buildPackage ./.
```
## Usage
Naersk provides a function called `buildPackage` that takes an attribute set
describing your application's directory, its dependencies etc.; in general, the
usage is:
``` nix
naersk.buildPackage {
# Assuming there's `Cargo.toml` right in this directory:
src = ./.;
someOption = "yass";
someOtherOption = false;
CARGO_ENVIRONMENTAL_VARIABLE = "test";
}
```
Some of the options (described below) are used by Naersk to affect the building
process, rest is passed-through into `mkDerivation`.
### `buildPackage`'s parameters
GEN_CONFIGURATION
## Tips & Tricks
### Using OpenSSL
If your application uses OpenSSL (making the build process fail), try:
``` nix
naersk.buildPackage {
# ...
nativeBuildInputs = with pkgs; [ pkg-config ];
buildInputs = with pkgs; [ openssl ];
} }
``` ```

View file

@ -5,3 +5,85 @@ version = 3
[[package]] [[package]]
name = "cross-windows" name = "cross-windows"
version = "0.1.0" version = "0.1.0"
dependencies = [
"serde",
"serde_json",
]
[[package]]
name = "itoa"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d"
[[package]]
name = "proc-macro2"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd96a1e8ed2596c337f8eae5f24924ec83f5ad5ab21ea8e455d3566c69fbcaf7"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3bcdf212e9776fbcb2d23ab029360416bb1706b1aea2d1a5ba002727cbcab804"
dependencies = [
"proc-macro2",
]
[[package]]
name = "ryu"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3f6f92acf49d1b98f7a81226834412ada05458b7364277387724a237f062695"
[[package]]
name = "serde"
version = "1.0.138"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1578c6245786b9d168c5447eeacfb96856573ca56c9d68fdcf394be134882a47"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.138"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "023e9b1467aef8a10fb88f25611870ada9800ef7e22afce356bb0d2387b6f27c"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82c2c1fdcd807d1098552c5b9a36e425e42e9fbd7c6a37a8425f390f781f7fa7"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "syn"
version = "1.0.98"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c50aef8a904de4c23c788f104b7dddc7d6f79c647c7c8ce4cc8f73eb0ca773dd"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5bd2fe26506023ed7b5e1e315add59d6f584c621d037f9368fea9cfb988f368c"

View file

@ -1,9 +1,10 @@
[package] [package]
name = "cross-windows" name = "cross-windows"
version = "0.1.0" version = "0.1.0"
authors = ["John Doe <example@example.com>"]
edition = "2018" edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
# This example uses Serde to prove that Naersk works correctly even when some of
# the dependencies have build scripts:
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

View file

@ -2,18 +2,15 @@
"nodes": { "nodes": {
"fenix": { "fenix": {
"inputs": { "inputs": {
"naersk": "naersk", "nixpkgs": "nixpkgs",
"nixpkgs": [
"nixpkgs"
],
"rust-analyzer-src": "rust-analyzer-src" "rust-analyzer-src": "rust-analyzer-src"
}, },
"locked": { "locked": {
"lastModified": 1616673789, "lastModified": 1657347958,
"narHash": "sha256-Xo5pOz6GOI9LxME+sTh3nWRWIX9VL2MM8w/T9RAsBEw=", "narHash": "sha256-UViOt1l9Qu1StbAIDe+xMjRbkEnefbISbs256J5IcHE=",
"owner": "nix-community", "owner": "nix-community",
"repo": "fenix", "repo": "fenix",
"rev": "dfd5ba0ef2e343cd9c9c1a2fd25fbc36cf390a74", "rev": "d223a6c360d0873488b70df0a9d27e0f6487b8fe",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -24,11 +21,11 @@
}, },
"flake-utils": { "flake-utils": {
"locked": { "locked": {
"lastModified": 1614513358, "lastModified": 1656928814,
"narHash": "sha256-LakhOx3S1dRjnh0b5Dg3mbZyH0ToC9I8Y2wKSkBaTzU=", "narHash": "sha256-RIFfgBuKz6Hp89yRr7+NR5tzIAbn52h8vT6vXkYjZoM=",
"owner": "numtide", "owner": "numtide",
"repo": "flake-utils", "repo": "flake-utils",
"rev": "5466c5bbece17adaab2d82fae80b46e807611bf3", "rev": "7e2a3b3dfd9af950a856d66b0a7d01e3c18aa249",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -39,51 +36,57 @@
}, },
"naersk": { "naersk": {
"inputs": { "inputs": {
"nixpkgs": [ "nixpkgs": "nixpkgs_2"
"nixpkgs"
]
}, },
"locked": { "locked": {
"lastModified": 1614785451, "lastModified": 1655042882,
"narHash": "sha256-TPw8kQvr2UNCuvndtY+EjyXp6Q5GEW2l9UafXXh1XmI=", "narHash": "sha256-9BX8Fuez5YJlN7cdPO63InoyBy7dm3VlJkkmTt6fS1A=",
"owner": "nmattia", "owner": "nix-community",
"repo": "naersk", "repo": "naersk",
"rev": "e0fe990b478a66178a58c69cf53daec0478ca6f9", "rev": "cddffb5aa211f50c4b8750adbec0bbbdfb26bb9f",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "nmattia", "owner": "nix-community",
"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", "repo": "naersk",
"type": "github" "type": "github"
} }
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1616259034, "lastModified": 1657265485,
"narHash": "sha256-WlMIiGIXJm7J+jemzd+ksoun6znWmabCZNz76szV158=", "narHash": "sha256-PUQ9C7mfi0/BnaAUX2R/PIkoNCb/Jtx9EpnhMBNrO/o=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "b39924fc7764c08ae3b51beef9a3518c414cdb7d",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 0,
"narHash": "sha256-0h1FzkYWei24IdKNpCX93onkF/FMiXQG8SdEbTc0r8A=",
"path": "/nix/store/fj7xz1cv9c8nrvdyd6bxhwq3l55k47xc-source",
"type": "path"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"nixpkgs_3": {
"locked": {
"lastModified": 1657292830,
"narHash": "sha256-ldfVSTveWceDCmW6gf3B4kR6vwmz/XS80y5wsLLHFJU=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "4e0d3868c679da20108db402785f924daa1a7fb5", "rev": "334ec8b503c3981e37a04b817a70e8d026ea9e84",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -97,22 +100,22 @@
"inputs": { "inputs": {
"fenix": "fenix", "fenix": "fenix",
"flake-utils": "flake-utils", "flake-utils": "flake-utils",
"naersk": "naersk_2", "naersk": "naersk",
"nixpkgs": "nixpkgs" "nixpkgs": "nixpkgs_3"
} }
}, },
"rust-analyzer-src": { "rust-analyzer-src": {
"flake": false, "flake": false,
"locked": { "locked": {
"lastModified": 1616625468, "lastModified": 1657292522,
"narHash": "sha256-98nBGTHLLEc0DEk3iskv5Okbey+LBkQ9fplrZCN9dCI=", "narHash": "sha256-mAxbvJzcpozcQpcfNAP1eU27NgAqT6pDB/eaQOe/piI=",
"owner": "rust-analyzer", "owner": "rust-lang",
"repo": "rust-analyzer", "repo": "rust-analyzer",
"rev": "d7db38fff9c251c36d0796309b43678bdf9e5bd8", "rev": "2836dd15f753a490ea5b89e02c6cfcecd2f32984",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "rust-analyzer", "owner": "rust-lang",
"ref": "nightly", "ref": "nightly",
"repo": "rust-analyzer", "repo": "rust-analyzer",
"type": "github" "type": "github"

View file

@ -1,87 +1,60 @@
{ {
inputs = { inputs = {
nixpkgs.url = github:NixOS/nixpkgs/nixpkgs-unstable; fenix.url = "github:nix-community/fenix";
naersk = { flake-utils.url = "github:numtide/flake-utils";
url = github:nix-community/naersk; naersk.url = "github:nix-community/naersk";
inputs.nixpkgs.follows = "nixpkgs"; nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};
fenix = {
url = github:nix-community/fenix;
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils.url = github:numtide/flake-utils;
}; };
outputs = { self, nixpkgs, naersk, fenix, flake-utils }: outputs = { self, fenix, flake-utils, naersk, nixpkgs }:
flake-utils.lib.eachDefaultSystem ( flake-utils.lib.eachDefaultSystem (system:
system: let let
pkgs = nixpkgs.legacyPackages.${system}; pkgs = (import nixpkgs) {
inherit system;
};
toolchain = with fenix.packages.${system}; toolchain = with fenix.packages.${system};
combine [ combine [
minimal.rustc minimal.rustc
minimal.cargo minimal.cargo
targets.x86_64-pc-windows-gnu.latest.rust-std targets.x86_64-pc-windows-gnu.latest.rust-std
]; ];
naersk-lib = naersk.lib.${system}.override {
naersk' = naersk.lib.${system}.override {
cargo = toolchain; cargo = toolchain;
rustc = toolchain; rustc = toolchain;
}; };
in
rec {
defaultPackage = packages.x86_64-pc-windows-gnu;
# The rust compiler is internally a cross compiler, so a single in rec {
# toolchain can be used to compile multiple targets. In a hermetic defaultPackage = packages.x86_64-pc-windows-gnu;
# 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; [ packages.x86_64-pc-windows-gnu = naersk'.buildPackage {
pkgsCross.mingwW64.stdenv.cc src = ./.;
# Used for running tests. strictDeps = true;
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 ]; depsBuildBuild = with pkgs; [
pkgsCross.mingwW64.stdenv.cc
pkgsCross.mingwW64.windows.pthreads
];
# Configures the target which will be built. nativeBuildInputs = with pkgs; [
# ref: https://doc.rust-lang.org/cargo/reference/config.html#buildtarget # We need Wine to run tests:
CARGO_BUILD_TARGET = "x86_64-pc-windows-gnu"; wineWowPackages.stable
];
# Configures the linker which will be used. cc.targetPrefix is doCheck = true;
# 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 # Tells Cargo that we're building for Windows.
# this is compiled for 64-bit Windows, use wine64 to run the tests. # (https://doc.rust-lang.org/cargo/reference/config.html#buildtarget)
# ref: https://doc.rust-lang.org/cargo/reference/config.html#targettriplerunner CARGO_BUILD_TARGET = "x86_64-pc-windows-gnu";
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; # Tells Cargo that it should use Wine to run tests.
# (https://doc.rust-lang.org/cargo/reference/config.html#targettriplerunner)
# Multi-stage builds currently fail for mingwW64. CARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUNNER = pkgs.writeScript "wine-wrapper" ''
singleStep = true; export WINEPREFIX="$(mktemp -d)"
}; exec wine64 $@
} '';
};
}
); );
} }

View file

@ -1,5 +1,16 @@
use serde::Serialize;
#[derive(Serialize)]
struct Message {
msg: String,
}
fn main() { fn main() {
println!("Hello, world!"); let value = serde_json::to_string_pretty(&Message {
msg: "Hello, world!".into(),
});
println!("{}", value.unwrap());
} }
#[cfg(test)] #[cfg(test)]

7
examples/hello-world/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 = "hello-world"
version = "0.1.0"

View file

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

View file

@ -2,11 +2,11 @@
"nodes": { "nodes": {
"flake-utils": { "flake-utils": {
"locked": { "locked": {
"lastModified": 1618217525, "lastModified": 1656928814,
"narHash": "sha256-WGrhVczjXTiswQaoxQ+0PTfbLNeOQM6M36zvLn78AYg=", "narHash": "sha256-RIFfgBuKz6Hp89yRr7+NR5tzIAbn52h8vT6vXkYjZoM=",
"owner": "numtide", "owner": "numtide",
"repo": "flake-utils", "repo": "flake-utils",
"rev": "c6169a2772643c4a93a0b5ac1c61e296cba68544", "rev": "7e2a3b3dfd9af950a856d66b0a7d01e3c18aa249",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -20,25 +20,24 @@
"nixpkgs": "nixpkgs" "nixpkgs": "nixpkgs"
}, },
"locked": { "locked": {
"lastModified": 1618068541, "lastModified": 1655042882,
"narHash": "sha256-enxg0QB53Zis0VJWfJsrX7zCjurpi7lW78EKXbJdzpQ=", "narHash": "sha256-9BX8Fuez5YJlN7cdPO63InoyBy7dm3VlJkkmTt6fS1A=",
"owner": "nmattia", "owner": "nix-community",
"repo": "naersk", "repo": "naersk",
"rev": "b3b099d669fc8b18d361c249091c9fe95d57ebbb", "rev": "cddffb5aa211f50c4b8750adbec0bbbdfb26bb9f",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "nmattia", "owner": "nix-community",
"repo": "naersk", "repo": "naersk",
"type": "github" "type": "github"
} }
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1616527350, "lastModified": 0,
"narHash": "sha256-VkMevY2WLU+K7T/P4wVj18Ms8zyeRfp05ILf556m5Y8=", "narHash": "sha256-0h1FzkYWei24IdKNpCX93onkF/FMiXQG8SdEbTc0r8A=",
"path": "/nix/store/9p64rfhziavzvg45z4gidba52vqbqmgj-source", "path": "/nix/store/fj7xz1cv9c8nrvdyd6bxhwq3l55k47xc-source",
"rev": "d3f7e969b9860fb80750147aeb56dab1c730e756",
"type": "path" "type": "path"
}, },
"original": { "original": {
@ -48,15 +47,18 @@
}, },
"nixpkgs_2": { "nixpkgs_2": {
"locked": { "locked": {
"lastModified": 1616527350, "lastModified": 1657292830,
"narHash": "sha256-VkMevY2WLU+K7T/P4wVj18Ms8zyeRfp05ILf556m5Y8=", "narHash": "sha256-ldfVSTveWceDCmW6gf3B4kR6vwmz/XS80y5wsLLHFJU=",
"path": "/nix/store/9p64rfhziavzvg45z4gidba52vqbqmgj-source", "owner": "NixOS",
"rev": "d3f7e969b9860fb80750147aeb56dab1c730e756", "repo": "nixpkgs",
"type": "path" "rev": "334ec8b503c3981e37a04b817a70e8d026ea9e84",
"type": "github"
}, },
"original": { "original": {
"id": "nixpkgs", "owner": "NixOS",
"type": "indirect" "ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
} }
}, },
"root": { "root": {

View file

@ -2,32 +2,28 @@
inputs = { inputs = {
flake-utils.url = "github:numtide/flake-utils"; flake-utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk"; naersk.url = "github:nix-community/naersk";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
}; };
outputs = { self, nixpkgs, flake-utils, naersk }: outputs = { self, flake-utils, naersk, nixpkgs }:
flake-utils.lib.eachDefaultSystem ( flake-utils.lib.eachDefaultSystem (system:
system: let let
pkgs = nixpkgs.legacyPackages."${system}"; pkgs = (import nixpkgs) {
naersk-lib = naersk.lib."${system}"; inherit system;
in };
rec {
# `nix build`
packages.hello-world = naersk-lib.buildPackage {
pname = "hello-world";
root = ./.;
};
defaultPackage = packages.hello-world;
# `nix run` naersk' = pkgs.callPackage naersk {};
apps.hello-world = flake-utils.lib.mkApp {
drv = packages.hello-world;
};
defaultApp = apps.hello-world;
# `nix develop` in rec {
devShell = pkgs.mkShell { # For `nix build` & `nix run`:
nativeBuildInputs = with pkgs; [ rustc cargo ]; defaultPackage = naersk'.buildPackage {
}; src = ./.;
} };
# For `nix develop`:
devShell = pkgs.mkShell {
nativeBuildInputs = with pkgs; [ rustc cargo ];
};
}
); );
} }

View file

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

View file

@ -2,18 +2,15 @@
"nodes": { "nodes": {
"fenix": { "fenix": {
"inputs": { "inputs": {
"naersk": "naersk", "nixpkgs": "nixpkgs",
"nixpkgs": [
"nixpkgs"
],
"rust-analyzer-src": "rust-analyzer-src" "rust-analyzer-src": "rust-analyzer-src"
}, },
"locked": { "locked": {
"lastModified": 1616673789, "lastModified": 1657347958,
"narHash": "sha256-Xo5pOz6GOI9LxME+sTh3nWRWIX9VL2MM8w/T9RAsBEw=", "narHash": "sha256-UViOt1l9Qu1StbAIDe+xMjRbkEnefbISbs256J5IcHE=",
"owner": "nix-community", "owner": "nix-community",
"repo": "fenix", "repo": "fenix",
"rev": "dfd5ba0ef2e343cd9c9c1a2fd25fbc36cf390a74", "rev": "d223a6c360d0873488b70df0a9d27e0f6487b8fe",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -24,11 +21,11 @@
}, },
"flake-utils": { "flake-utils": {
"locked": { "locked": {
"lastModified": 1614513358, "lastModified": 1656928814,
"narHash": "sha256-LakhOx3S1dRjnh0b5Dg3mbZyH0ToC9I8Y2wKSkBaTzU=", "narHash": "sha256-RIFfgBuKz6Hp89yRr7+NR5tzIAbn52h8vT6vXkYjZoM=",
"owner": "numtide", "owner": "numtide",
"repo": "flake-utils", "repo": "flake-utils",
"rev": "5466c5bbece17adaab2d82fae80b46e807611bf3", "rev": "7e2a3b3dfd9af950a856d66b0a7d01e3c18aa249",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -39,51 +36,57 @@
}, },
"naersk": { "naersk": {
"inputs": { "inputs": {
"nixpkgs": [ "nixpkgs": "nixpkgs_2"
"nixpkgs"
]
}, },
"locked": { "locked": {
"lastModified": 1614785451, "lastModified": 1655042882,
"narHash": "sha256-TPw8kQvr2UNCuvndtY+EjyXp6Q5GEW2l9UafXXh1XmI=", "narHash": "sha256-9BX8Fuez5YJlN7cdPO63InoyBy7dm3VlJkkmTt6fS1A=",
"owner": "nmattia", "owner": "nix-community",
"repo": "naersk", "repo": "naersk",
"rev": "e0fe990b478a66178a58c69cf53daec0478ca6f9", "rev": "cddffb5aa211f50c4b8750adbec0bbbdfb26bb9f",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "nmattia", "owner": "nix-community",
"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", "repo": "naersk",
"type": "github" "type": "github"
} }
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1616259034, "lastModified": 1657265485,
"narHash": "sha256-WlMIiGIXJm7J+jemzd+ksoun6znWmabCZNz76szV158=", "narHash": "sha256-PUQ9C7mfi0/BnaAUX2R/PIkoNCb/Jtx9EpnhMBNrO/o=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "b39924fc7764c08ae3b51beef9a3518c414cdb7d",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 0,
"narHash": "sha256-0h1FzkYWei24IdKNpCX93onkF/FMiXQG8SdEbTc0r8A=",
"path": "/nix/store/fj7xz1cv9c8nrvdyd6bxhwq3l55k47xc-source",
"type": "path"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"nixpkgs_3": {
"locked": {
"lastModified": 1657292830,
"narHash": "sha256-ldfVSTveWceDCmW6gf3B4kR6vwmz/XS80y5wsLLHFJU=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "4e0d3868c679da20108db402785f924daa1a7fb5", "rev": "334ec8b503c3981e37a04b817a70e8d026ea9e84",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -97,22 +100,22 @@
"inputs": { "inputs": {
"fenix": "fenix", "fenix": "fenix",
"flake-utils": "flake-utils", "flake-utils": "flake-utils",
"naersk": "naersk_2", "naersk": "naersk",
"nixpkgs": "nixpkgs" "nixpkgs": "nixpkgs_3"
} }
}, },
"rust-analyzer-src": { "rust-analyzer-src": {
"flake": false, "flake": false,
"locked": { "locked": {
"lastModified": 1616625468, "lastModified": 1657292522,
"narHash": "sha256-98nBGTHLLEc0DEk3iskv5Okbey+LBkQ9fplrZCN9dCI=", "narHash": "sha256-mAxbvJzcpozcQpcfNAP1eU27NgAqT6pDB/eaQOe/piI=",
"owner": "rust-analyzer", "owner": "rust-lang",
"repo": "rust-analyzer", "repo": "rust-analyzer",
"rev": "d7db38fff9c251c36d0796309b43678bdf9e5bd8", "rev": "2836dd15f753a490ea5b89e02c6cfcecd2f32984",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "rust-analyzer", "owner": "rust-lang",
"ref": "nightly", "ref": "nightly",
"repo": "rust-analyzer", "repo": "rust-analyzer",
"type": "github" "type": "github"

View file

@ -1,21 +1,18 @@
{ {
inputs = { inputs = {
nixpkgs.url = github:NixOS/nixpkgs/nixpkgs-unstable; fenix.url = "github:nix-community/fenix";
naersk = { flake-utils.url = "github:numtide/flake-utils";
url = github:nix-community/naersk; naersk.url = "github:nix-community/naersk";
inputs.nixpkgs.follows = "nixpkgs"; nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};
fenix = {
url = github:nix-community/fenix;
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils.url = github:numtide/flake-utils;
}; };
outputs = { self, nixpkgs, naersk, fenix, flake-utils }: outputs = { self, fenix, flake-utils, naersk, nixpkgs }:
flake-utils.lib.eachDefaultSystem ( flake-utils.lib.eachDefaultSystem (system:
system: let let
pkgs = nixpkgs.legacyPackages.${system}; pkgs = (import nixpkgs) {
inherit system;
};
toolchain = with fenix.packages.${system}; toolchain = with fenix.packages.${system};
combine [ combine [
minimal.rustc minimal.rustc
@ -23,96 +20,77 @@
targets.x86_64-unknown-linux-musl.latest.rust-std targets.x86_64-unknown-linux-musl.latest.rust-std
targets.x86_64-pc-windows-gnu.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 { naersk' = naersk.lib.${system}.override {
cargo = toolchain; cargo = toolchain;
rustc = toolchain; rustc = toolchain;
}; };
# Utility for merging the common cargo configuration with the target
# specific configuration. naerskBuildPackage = target: args:
naerskBuildPackage = target: args: naersk-lib.buildPackage naersk'.buildPackage (
(args // { CARGO_BUILD_TARGET = target; } // cargoConfig); args
// { CARGO_BUILD_TARGET = target; }
// cargoConfig
);
# All of the CARGO_* configurations which should be used for all # All of the CARGO_* configurations which should be used for all
# targets. Only use this for options which should be universally # targets.
# applied or which can be applied to a specific target triple. #
# 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. # This is also merged into the devShell.
cargoConfig = { cargoConfig = {
# Enables static compilation. # Tells Cargo to enable static compilation.
# (https://doc.rust-lang.org/cargo/reference/config.html#targettriplerustflags)
# #
# If the resulting executable is still considered dynamically # Note that the resulting binary might still be considered dynamically
# linked by ldd but doesn't have anything actually linked to it, # linked by ldd, but that's just because the binary might have
# don't worry. It's still statically linked. It just has static # position-independent-execution enabled.
# position independent execution enabled. # (see: https://github.com/rust-lang/rust/issues/79624#issuecomment-737415388)
# ref: https://doc.rust-lang.org/cargo/reference/config.html#targettriplerustflags
CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_RUSTFLAGS = "-C target-feature=+crt-static"; CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_RUSTFLAGS = "-C target-feature=+crt-static";
# Configures the script which should be used to run tests. Since # Tells Cargo that it should use Wine to run tests.
# this is compiled for 64-bit Windows, use wine64 to run the tests. # (https://doc.rust-lang.org/cargo/reference/config.html#targettriplerunner)
# ref: https://doc.rust-lang.org/cargo/reference/config.html#targettriplerunner
CARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUNNER = pkgs.writeScript "wine-wrapper" '' 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)" export WINEPREFIX="$(mktemp -d)"
exec wine64 $@ 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 in rec {
# toolchain can be used to compile multiple targets. In a hermetic defaultPackage = packages.x86_64-unknown-linux-musl;
# 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" { # For `nix build .#x86_64-unknown-linux-musl`:
src = ./.; packages.x86_64-unknown-linux-musl = naerskBuildPackage "x86_64-unknown-linux-musl" {
src = ./.;
doCheck = true;
nativeBuildInputs = with pkgs; [ pkgsStatic.stdenv.cc ];
};
nativeBuildInputs = with pkgs; [ # For `nix build .#x86_64-pc-windows-gnu`:
pkgsCross.mingwW64.stdenv.cc packages.x86_64-pc-windows-gnu = naerskBuildPackage "x86_64-pc-windows-gnu" {
# Used for running tests. src = ./.;
wineWowPackages.stable doCheck = true;
# wineWowPackages is overkill, but it's built in CI for nixpkgs, strictDeps = true;
# 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 ]; depsBuildBuild = with pkgs; [
pkgsCross.mingwW64.stdenv.cc
pkgsCross.mingwW64.windows.pthreads
];
doCheck = true; nativeBuildInputs = with pkgs; [
# We need Wine to run tests:
wineWowPackages.stable
];
};
# Multi-stage builds currently fail for mingwW64. devShell = pkgs.mkShell (
singleStep = true; {
}; inputsFrom = with packages; [ x86_64-unknown-linux-musl x86_64-pc-windows-gnu ];
CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl";
devShell = pkgs.mkShell ( } // cargoConfig
{ );
inputsFrom = with packages; [ x86_64-unknown-linux-musl x86_64-pc-windows-gnu ]; }
CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl"; );
} // cargoConfig
);
}
);
} }

View file

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

View file

@ -2,18 +2,15 @@
"nodes": { "nodes": {
"fenix": { "fenix": {
"inputs": { "inputs": {
"naersk": "naersk", "nixpkgs": "nixpkgs",
"nixpkgs": [
"nixpkgs"
],
"rust-analyzer-src": "rust-analyzer-src" "rust-analyzer-src": "rust-analyzer-src"
}, },
"locked": { "locked": {
"lastModified": 1616673789, "lastModified": 1657347958,
"narHash": "sha256-Xo5pOz6GOI9LxME+sTh3nWRWIX9VL2MM8w/T9RAsBEw=", "narHash": "sha256-UViOt1l9Qu1StbAIDe+xMjRbkEnefbISbs256J5IcHE=",
"owner": "nix-community", "owner": "nix-community",
"repo": "fenix", "repo": "fenix",
"rev": "dfd5ba0ef2e343cd9c9c1a2fd25fbc36cf390a74", "rev": "d223a6c360d0873488b70df0a9d27e0f6487b8fe",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -24,11 +21,11 @@
}, },
"flake-utils": { "flake-utils": {
"locked": { "locked": {
"lastModified": 1614513358, "lastModified": 1656928814,
"narHash": "sha256-LakhOx3S1dRjnh0b5Dg3mbZyH0ToC9I8Y2wKSkBaTzU=", "narHash": "sha256-RIFfgBuKz6Hp89yRr7+NR5tzIAbn52h8vT6vXkYjZoM=",
"owner": "numtide", "owner": "numtide",
"repo": "flake-utils", "repo": "flake-utils",
"rev": "5466c5bbece17adaab2d82fae80b46e807611bf3", "rev": "7e2a3b3dfd9af950a856d66b0a7d01e3c18aa249",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -39,51 +36,57 @@
}, },
"naersk": { "naersk": {
"inputs": { "inputs": {
"nixpkgs": [ "nixpkgs": "nixpkgs_2"
"nixpkgs"
]
}, },
"locked": { "locked": {
"lastModified": 1614785451, "lastModified": 1655042882,
"narHash": "sha256-TPw8kQvr2UNCuvndtY+EjyXp6Q5GEW2l9UafXXh1XmI=", "narHash": "sha256-9BX8Fuez5YJlN7cdPO63InoyBy7dm3VlJkkmTt6fS1A=",
"owner": "nmattia", "owner": "nix-community",
"repo": "naersk", "repo": "naersk",
"rev": "e0fe990b478a66178a58c69cf53daec0478ca6f9", "rev": "cddffb5aa211f50c4b8750adbec0bbbdfb26bb9f",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "nmattia", "owner": "nix-community",
"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", "repo": "naersk",
"type": "github" "type": "github"
} }
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1616259034, "lastModified": 1657265485,
"narHash": "sha256-WlMIiGIXJm7J+jemzd+ksoun6znWmabCZNz76szV158=", "narHash": "sha256-PUQ9C7mfi0/BnaAUX2R/PIkoNCb/Jtx9EpnhMBNrO/o=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "b39924fc7764c08ae3b51beef9a3518c414cdb7d",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 0,
"narHash": "sha256-0h1FzkYWei24IdKNpCX93onkF/FMiXQG8SdEbTc0r8A=",
"path": "/nix/store/fj7xz1cv9c8nrvdyd6bxhwq3l55k47xc-source",
"type": "path"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"nixpkgs_3": {
"locked": {
"lastModified": 1657292830,
"narHash": "sha256-ldfVSTveWceDCmW6gf3B4kR6vwmz/XS80y5wsLLHFJU=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "4e0d3868c679da20108db402785f924daa1a7fb5", "rev": "334ec8b503c3981e37a04b817a70e8d026ea9e84",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -97,22 +100,22 @@
"inputs": { "inputs": {
"fenix": "fenix", "fenix": "fenix",
"flake-utils": "flake-utils", "flake-utils": "flake-utils",
"naersk": "naersk_2", "naersk": "naersk",
"nixpkgs": "nixpkgs" "nixpkgs": "nixpkgs_3"
} }
}, },
"rust-analyzer-src": { "rust-analyzer-src": {
"flake": false, "flake": false,
"locked": { "locked": {
"lastModified": 1616625468, "lastModified": 1657292522,
"narHash": "sha256-98nBGTHLLEc0DEk3iskv5Okbey+LBkQ9fplrZCN9dCI=", "narHash": "sha256-mAxbvJzcpozcQpcfNAP1eU27NgAqT6pDB/eaQOe/piI=",
"owner": "rust-analyzer", "owner": "rust-lang",
"repo": "rust-analyzer", "repo": "rust-analyzer",
"rev": "d7db38fff9c251c36d0796309b43678bdf9e5bd8", "rev": "2836dd15f753a490ea5b89e02c6cfcecd2f32984",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "rust-analyzer", "owner": "rust-lang",
"ref": "nightly", "ref": "nightly",
"repo": "rust-analyzer", "repo": "rust-analyzer",
"type": "github" "type": "github"

View file

@ -1,73 +1,49 @@
{ {
inputs = { inputs = {
nixpkgs.url = github:NixOS/nixpkgs/nixpkgs-unstable; fenix.url = "github:nix-community/fenix";
naersk = { flake-utils.url = "github:numtide/flake-utils";
url = github:nix-community/naersk; naersk.url = "github:nix-community/naersk";
inputs.nixpkgs.follows = "nixpkgs"; nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};
fenix = {
url = github:nix-community/fenix;
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils.url = github:numtide/flake-utils;
}; };
outputs = { self, nixpkgs, naersk, fenix, flake-utils }: outputs = { self, fenix, flake-utils, naersk, nixpkgs }:
flake-utils.lib.eachDefaultSystem ( flake-utils.lib.eachDefaultSystem (
system: let system: let
pkgs = nixpkgs.legacyPackages.${system}; pkgs = (import nixpkgs) {
inherit system;
};
toolchain = with fenix.packages.${system}; toolchain = with fenix.packages.${system};
combine [ combine [
minimal.rustc minimal.rustc
minimal.cargo minimal.cargo
targets.x86_64-unknown-linux-musl.latest.rust-std targets.x86_64-unknown-linux-musl.latest.rust-std
]; ];
naersk-lib = naersk.lib.${system}.override {
naersk' = naersk.lib.${system}.override {
cargo = toolchain; cargo = toolchain;
rustc = toolchain; rustc = toolchain;
}; };
in
rec {
defaultPackage = packages.x86_64-unknown-linux-musl;
# The rust compiler is internally a cross compiler, so a single in rec {
# toolchain can be used to compile multiple targets. In a hermetic defaultPackage = naersk'.buildPackage {
# build system like nix flakes, there's effectively one package for src = ./.;
# every permutation of the supported hosts and targets. doCheck = true;
# i.e.: nix build .#packages.x86_64-linux.x86_64-pc-windows-gnu nativeBuildInputs = with pkgs; [ pkgsStatic.stdenv.cc ];
# 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 ]; # Tells Cargo that we're building for musl.
# (https://doc.rust-lang.org/cargo/reference/config.html#buildtarget)
CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl";
# Configures the target which will be built. # Tells Cargo to enable static compilation.
# ref: https://doc.rust-lang.org/cargo/reference/config.html#buildtarget # (https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags)
CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl"; #
# Note that the resulting binary might still be considered dynamically
# Enables static compilation. # linked by ldd, but that's just because the binary might have
# # position-independent-execution enabled.
# If the resulting executable is still considered dynamically # (see: https://github.com/rust-lang/rust/issues/79624#issuecomment-737415388)
# linked by ldd but doesn't have anything actually linked to it, CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static";
# 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

@ -1,42 +1,41 @@
{ {
description = "Build rust crates in Nix. No configuration, no code generation. IFD and sandbox friendly."; description = "Build Rust projects with ease. No configuration, no code generation; IFD and sandbox friendly.";
outputs = { self, nixpkgs }: outputs = { self, nixpkgs }:
let let
forAllSystems = nixpkgs.lib.genAttrs [ "x86_64-linux" "x86_64-darwin" "i686-linux" "aarch64-linux" "aarch64-darwin" ]; forAllSystems = nixpkgs.lib.genAttrs [ "x86_64-linux" "x86_64-darwin" "i686-linux" "aarch64-linux" "aarch64-darwin" ];
in
rec { 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 { }); lib = forAllSystems (system: nixpkgs.legacyPackages."${system}".callPackage ./default.nix { });
# Useful when composing with other flakes
# Useful when composing with other flakes:
overlay = import ./overlay.nix; overlay = import ./overlay.nix;
# Expose the examples as templates.
defaultTemplate = templates.hello-world; defaultTemplate = templates.hello-world;
templates = { templates = {
hello-world = { hello-world = {
path = path =
builtins.filterSource (path: type: baseNameOf path == "flake.nix") builtins.filterSource (path: type: baseNameOf path == "flake.nix")
./examples/hello-world; ./examples/hello-world;
description = "Build a rust project with naersk.";
}; };
cross-windows = { cross-windows = {
path = path =
builtins.filterSource (path: type: baseNameOf path == "flake.nix") builtins.filterSource (path: type: baseNameOf path == "flake.nix")
./examples/cross-windows; ./examples/cross-windows;
description = "Cross compile a rust project for use on Windows.";
}; };
static-musl = { static-musl = {
path = path =
builtins.filterSource (path: type: baseNameOf path == "flake.nix") builtins.filterSource (path: type: baseNameOf path == "flake.nix")
./examples/static-musl; ./examples/static-musl;
description = "Compile a rust project statically using musl.";
}; };
multi-target = { multi-target = {
path = path =
builtins.filterSource (path: type: baseNameOf path == "flake.nix") builtins.filterSource (path: type: baseNameOf path == "flake.nix")
./examples/multi-target; ./examples/multi-target;
description = "Compile a rust project to multiple targets.";
}; };
}; };
}; };

View file

@ -1,4 +1,3 @@
final: prev: final: prev: {
{
naersk = prev.callPackage ./. {}; naersk = prev.callPackage ./. {};
} }

View file

@ -8,7 +8,7 @@ fn main() {
fs::write( fs::write(
&dest_path, &dest_path,
"pub fn message() -> &'static str { "pub fn message() -> &'static str {
\"Hello, World!\" \"Hello, world!\"
} }
" "
).unwrap(); ).unwrap();