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)
Nix support for building [cargo] crates.
Build Rust projects with ease!
* [Install](#install)
* [Configuration](#configuration)
* [Setup](#setup)
* [Usage](#usage)
## Install
## Setup
Use [niv]:
### Using Flakes
``` 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
```
And then
... and then create `default.nix` with:
``` nix
let
pkgs = import <nixpkgs> {};
sources = import ./nix/sources.nix;
naersk = pkgs.callPackage sources.naersk {};
in naersk.buildPackage ./path/to/rust
pkgs = import <nixpkgs> {};
sources = import ./nix/sources.nix;
naersk = pkgs.callPackage sources.naersk {};
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
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.
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
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 |
| - | - |
@ -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` |
| `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
provides nightly versions of `rustc` and `cargo`. Below is an example setup for
using it with naersk:
### Using OpenSSL
If your application uses OpenSSL (making the build process fail), try:
``` 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
{
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 ];
};
});
naersk.buildPackage {
# ...
nativeBuildInputs = with pkgs; [ pkg-config ];
buildInputs = with pkgs; [ openssl ];
}
```

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)
Nix support for building [cargo] crates.
Build Rust projects with ease!
* [Install](#install)
* [Configuration](#configuration)
* [Setup](#setup)
* [Usage](#usage)
## Install
## Setup
Use [niv]:
### Using Flakes
``` shell
$ niv add nix-community/naersk
$ nix flake init -t github:nix-community/naersk
$ nix flake lock
```
And then
``` 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.
Alternatively, store this as `flake.nix` in your repository:
``` nix
{
inputs = {
utils.url = "github:numtide/flake-utils";
flake-utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};
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;
outputs = { self, flake-utils, naersk, nixpkgs }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = (import nixpkgs) {
inherit system;
};
# `nix run`
apps.my-project = utils.lib.mkApp {
drv = packages.my-project;
};
defaultApp = apps.my-project;
naersk' = pkgs.callPackage naersk {};
in rec {
# For `nix build` & `nix run`:
defaultPackage = naersk'.buildPackage {
src = ./.;
};
# `nix develop`
devShell = pkgs.mkShell {
nativeBuildInputs = with pkgs; [ rustc cargo ];
};
});
# For `nix develop` (optional, can be skipped):
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.
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 = {
utils.url = "github:numtide/flake-utils";
flake-utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk";
mozillapkgs = {
nixpkgs-mozilla = {
url = "github:mozilla/nixpkgs-mozilla";
flake = false;
};
};
outputs = { self, nixpkgs, utils, naersk, mozillapkgs }:
utils.lib.eachDefaultSystem (system: let
pkgs = nixpkgs.legacyPackages."${system}";
outputs = { self, flake-utils, naersk, nixpkgs, nixpkgs-mozilla }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = (import nixpkgs) {
inherit 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;
overlays = [
(import nixpkgs-mozilla)
];
};
# 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;
toolchain = (pkgs.rustChannelOf {
rustToolchain = ./rust-toolchain;
sha256 = "";
# ^ After you run `nix build`, replace this with the actual
# hash from the error message
}).rust;
# `nix run`
apps.my-project = utils.lib.mkApp {
drv = packages.my-project;
};
defaultApp = apps.my-project;
naersk' = pkgs.callPackage naersk {
cargo = toolchain;
rustc = toolchain;
};
in rec {
# For `nix build` & `nix run`:
defaultPackage = naersk'.buildPackage {
src = ./.;
};
# `nix develop`
devShell = pkgs.mkShell {
# supply the specific rust version
nativeBuildInputs = [ rust ];
};
});
# For `nix develop` (optional, can be skipped):
devShell = pkgs.mkShell {
nativeBuildInputs = [ toolchain ];
};
}
);
}
```
### 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]]
name = "cross-windows"
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]
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]
# 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": {
"fenix": {
"inputs": {
"naersk": "naersk",
"nixpkgs": [
"nixpkgs"
],
"nixpkgs": "nixpkgs",
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1616673789,
"narHash": "sha256-Xo5pOz6GOI9LxME+sTh3nWRWIX9VL2MM8w/T9RAsBEw=",
"lastModified": 1657347958,
"narHash": "sha256-UViOt1l9Qu1StbAIDe+xMjRbkEnefbISbs256J5IcHE=",
"owner": "nix-community",
"repo": "fenix",
"rev": "dfd5ba0ef2e343cd9c9c1a2fd25fbc36cf390a74",
"rev": "d223a6c360d0873488b70df0a9d27e0f6487b8fe",
"type": "github"
},
"original": {
@ -24,11 +21,11 @@
},
"flake-utils": {
"locked": {
"lastModified": 1614513358,
"narHash": "sha256-LakhOx3S1dRjnh0b5Dg3mbZyH0ToC9I8Y2wKSkBaTzU=",
"lastModified": 1656928814,
"narHash": "sha256-RIFfgBuKz6Hp89yRr7+NR5tzIAbn52h8vT6vXkYjZoM=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "5466c5bbece17adaab2d82fae80b46e807611bf3",
"rev": "7e2a3b3dfd9af950a856d66b0a7d01e3c18aa249",
"type": "github"
},
"original": {
@ -39,51 +36,57 @@
},
"naersk": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1614785451,
"narHash": "sha256-TPw8kQvr2UNCuvndtY+EjyXp6Q5GEW2l9UafXXh1XmI=",
"owner": "nmattia",
"lastModified": 1655042882,
"narHash": "sha256-9BX8Fuez5YJlN7cdPO63InoyBy7dm3VlJkkmTt6fS1A=",
"owner": "nix-community",
"repo": "naersk",
"rev": "e0fe990b478a66178a58c69cf53daec0478ca6f9",
"rev": "cddffb5aa211f50c4b8750adbec0bbbdfb26bb9f",
"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",
"owner": "nix-community",
"repo": "naersk",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1616259034,
"narHash": "sha256-WlMIiGIXJm7J+jemzd+ksoun6znWmabCZNz76szV158=",
"lastModified": 1657265485,
"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",
"repo": "nixpkgs",
"rev": "4e0d3868c679da20108db402785f924daa1a7fb5",
"rev": "334ec8b503c3981e37a04b817a70e8d026ea9e84",
"type": "github"
},
"original": {
@ -97,22 +100,22 @@
"inputs": {
"fenix": "fenix",
"flake-utils": "flake-utils",
"naersk": "naersk_2",
"nixpkgs": "nixpkgs"
"naersk": "naersk",
"nixpkgs": "nixpkgs_3"
}
},
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1616625468,
"narHash": "sha256-98nBGTHLLEc0DEk3iskv5Okbey+LBkQ9fplrZCN9dCI=",
"owner": "rust-analyzer",
"lastModified": 1657292522,
"narHash": "sha256-mAxbvJzcpozcQpcfNAP1eU27NgAqT6pDB/eaQOe/piI=",
"owner": "rust-lang",
"repo": "rust-analyzer",
"rev": "d7db38fff9c251c36d0796309b43678bdf9e5bd8",
"rev": "2836dd15f753a490ea5b89e02c6cfcecd2f32984",
"type": "github"
},
"original": {
"owner": "rust-analyzer",
"owner": "rust-lang",
"ref": "nightly",
"repo": "rust-analyzer",
"type": "github"

View file

@ -1,87 +1,60 @@
{
inputs = {
nixpkgs.url = github:NixOS/nixpkgs/nixpkgs-unstable;
naersk = {
url = github:nix-community/naersk;
inputs.nixpkgs.follows = "nixpkgs";
};
fenix = {
url = github:nix-community/fenix;
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils.url = github:numtide/flake-utils;
fenix.url = "github:nix-community/fenix";
flake-utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};
outputs = { self, nixpkgs, naersk, fenix, flake-utils }:
flake-utils.lib.eachDefaultSystem (
system: let
pkgs = nixpkgs.legacyPackages.${system};
outputs = { self, fenix, flake-utils, naersk, nixpkgs }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = (import nixpkgs) {
inherit 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 {
naersk' = 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 = ./.;
in rec {
defaultPackage = packages.x86_64-pc-windows-gnu;
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"; })
];
packages.x86_64-pc-windows-gnu = naersk'.buildPackage {
src = ./.;
strictDeps = true;
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.
# ref: https://doc.rust-lang.org/cargo/reference/config.html#buildtarget
CARGO_BUILD_TARGET = "x86_64-pc-windows-gnu";
nativeBuildInputs = with pkgs; [
# We need Wine to run tests:
wineWowPackages.stable
];
# 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";
doCheck = true;
# 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 $@
'';
# Tells Cargo that we're building for Windows.
# (https://doc.rust-lang.org/cargo/reference/config.html#buildtarget)
CARGO_BUILD_TARGET = "x86_64-pc-windows-gnu";
doCheck = true;
# Multi-stage builds currently fail for mingwW64.
singleStep = true;
};
}
# Tells Cargo that it should use Wine to run tests.
# (https://doc.rust-lang.org/cargo/reference/config.html#targettriplerunner)
CARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUNNER = pkgs.writeScript "wine-wrapper" ''
export WINEPREFIX="$(mktemp -d)"
exec wine64 $@
'';
};
}
);
}

View file

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

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

View file

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

View file

@ -1,9 +1,4 @@
[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

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

View file

@ -1,21 +1,18 @@
{
inputs = {
nixpkgs.url = github:NixOS/nixpkgs/nixpkgs-unstable;
naersk = {
url = github:nix-community/naersk;
inputs.nixpkgs.follows = "nixpkgs";
};
fenix = {
url = github:nix-community/fenix;
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils.url = github:numtide/flake-utils;
fenix.url = "github:nix-community/fenix";
flake-utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};
outputs = { self, nixpkgs, naersk, fenix, flake-utils }:
flake-utils.lib.eachDefaultSystem (
system: let
pkgs = nixpkgs.legacyPackages.${system};
outputs = { self, fenix, flake-utils, naersk, nixpkgs }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = (import nixpkgs) {
inherit system;
};
toolchain = with fenix.packages.${system};
combine [
minimal.rustc
@ -23,96 +20,77 @@
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 {
naersk' = 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);
naerskBuildPackage = target: args:
naersk'.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.
# 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.
# Tells Cargo to enable static compilation.
# (https://doc.rust-lang.org/cargo/reference/config.html#targettriplerustflags)
#
# 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
# Note that the resulting binary might still be considered dynamically
# linked by ldd, but that's just because the binary might have
# position-independent-execution enabled.
# (see: https://github.com/rust-lang/rust/issues/79624#issuecomment-737415388)
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
# Tells Cargo that it should use Wine to run tests.
# (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;
};
in rec {
defaultPackage = packages.x86_64-unknown-linux-musl;
packages.x86_64-pc-windows-gnu = naerskBuildPackage "x86_64-pc-windows-gnu" {
src = ./.;
# For `nix build .#x86_64-unknown-linux-musl`:
packages.x86_64-unknown-linux-musl = naerskBuildPackage "x86_64-unknown-linux-musl" {
src = ./.;
doCheck = true;
nativeBuildInputs = with pkgs; [ pkgsStatic.stdenv.cc ];
};
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"; })
];
# For `nix build .#x86_64-pc-windows-gnu`:
packages.x86_64-pc-windows-gnu = naerskBuildPackage "x86_64-pc-windows-gnu" {
src = ./.;
doCheck = true;
strictDeps = true;
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.
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
);
}
);
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

@ -1,9 +1,4 @@
[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

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

View file

@ -1,73 +1,49 @@
{
inputs = {
nixpkgs.url = github:NixOS/nixpkgs/nixpkgs-unstable;
naersk = {
url = github:nix-community/naersk;
inputs.nixpkgs.follows = "nixpkgs";
};
fenix = {
url = github:nix-community/fenix;
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils.url = github:numtide/flake-utils;
fenix.url = "github:nix-community/fenix";
flake-utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};
outputs = { self, nixpkgs, naersk, fenix, flake-utils }:
outputs = { self, fenix, flake-utils, naersk, nixpkgs }:
flake-utils.lib.eachDefaultSystem (
system: let
pkgs = nixpkgs.legacyPackages.${system};
pkgs = (import nixpkgs) {
inherit 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 {
naersk' = 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 = ./.;
in rec {
defaultPackage = naersk'.buildPackage {
src = ./.;
doCheck = true;
nativeBuildInputs = with pkgs; [ pkgsStatic.stdenv.cc ];
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.
# 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;
};
}
# Tells Cargo to enable static compilation.
# (https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags)
#
# Note that the resulting binary might still be considered dynamically
# linked by ldd, but that's just because the binary might have
# position-independent-execution enabled.
# (see: https://github.com/rust-lang/rust/issues/79624#issuecomment-737415388)
CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static";
};
}
);
}

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 }:
let
forAllSystems = nixpkgs.lib.genAttrs [ "x86_64-linux" "x86_64-darwin" "i686-linux" "aarch64-linux" "aarch64-darwin" ];
in
rec {
# Naersk is not a package, not an app, not a module... It's just a Library
in rec {
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;
# 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.";
};
};
};

View file

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

View file

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