examples: Add multiple-binaries

Closes https://github.com/nix-community/naersk/issues/272.
This commit is contained in:
Patryk Wychowaniec 2023-03-14 23:19:13 +01:00
parent 2b41e666c1
commit 5c8dbab3d9
12 changed files with 192 additions and 17 deletions

View file

@ -6,6 +6,8 @@ Build Rust projects with ease!
* [Setup](#setup)
* [Usage](#usage)
* [Examples](#examples)
* [Tips & Tricks](#tips--tricks)
## Setup
@ -225,6 +227,11 @@ process, rest is passed-through into `mkDerivation`.
| `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` |
## Examples
See: [./examples](./examples).
## Tips & Tricks
### Using OpenSSL

View file

@ -6,6 +6,8 @@ Build Rust projects with ease!
* [Setup](#setup)
* [Usage](#usage)
* [Examples](#examples)
* [Tips & Tricks](#tips--tricks)
## Setup
@ -190,7 +192,11 @@ process, rest is passed-through into `mkDerivation`.
### `buildPackage`'s parameters
GEN_CONFIGURATION
{{ params }}
## Examples
See: [./examples](./examples).
## Tips & Tricks

11
examples/multiple-binaries/Cargo.lock generated Normal file
View file

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

View file

@ -0,0 +1,2 @@
[workspace]
members = [ "crates/bar", "crates/foo" ]

View file

@ -0,0 +1,12 @@
# multiple-binaries
This example shows a multi-crate setup with two crates, `bar` and `foo`, that
provide separate binaries within a single workspace:
``` shell
$ nix run .#bar
Hello, Bar!
$ nix run .#foo
Hello, Foo!
```

View file

@ -0,0 +1,4 @@
[package]
name = "bar"
version = "0.1.0"
edition = "2021"

View file

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

View file

@ -0,0 +1,4 @@
[package]
name = "foo"
version = "0.1.0"
edition = "2021"

View file

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

View file

@ -0,0 +1,74 @@
{
"nodes": {
"flake-utils": {
"locked": {
"lastModified": 1656928814,
"narHash": "sha256-RIFfgBuKz6Hp89yRr7+NR5tzIAbn52h8vT6vXkYjZoM=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "7e2a3b3dfd9af950a856d66b0a7d01e3c18aa249",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"naersk": {
"inputs": {
"nixpkgs": "nixpkgs"
},
"locked": {
"lastModified": 1655042882,
"narHash": "sha256-9BX8Fuez5YJlN7cdPO63InoyBy7dm3VlJkkmTt6fS1A=",
"owner": "nix-community",
"repo": "naersk",
"rev": "cddffb5aa211f50c4b8750adbec0bbbdfb26bb9f",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "naersk",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 0,
"narHash": "sha256-0h1FzkYWei24IdKNpCX93onkF/FMiXQG8SdEbTc0r8A=",
"path": "/nix/store/fj7xz1cv9c8nrvdyd6bxhwq3l55k47xc-source",
"type": "path"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1657292830,
"narHash": "sha256-ldfVSTveWceDCmW6gf3B4kR6vwmz/XS80y5wsLLHFJU=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "334ec8b503c3981e37a04b817a70e8d026ea9e84",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"naersk": "naersk",
"nixpkgs": "nixpkgs_2"
}
}
},
"root": "root",
"version": 7
}

View file

@ -0,0 +1,37 @@
{
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
{
# For `nix build .#bar` and `nix run .#bar`:
packages.bar = naersk'.buildPackage {
pname = "bar";
src = ./.;
};
# For `nix build .#foo` and `nix run .#foo`:
packages.foo = naersk'.buildPackage {
pname = "foo";
src = ./.;
};
# For `nix develop`:
devShell = pkgs.mkShell {
nativeBuildInputs = with pkgs; [ rustc cargo ];
};
}
);
}

View file

@ -15,25 +15,37 @@ let
docparse = naersk.buildPackage {
root = ./docparse;
src = builtins.filterSource (
p: t:
let
p' = pkgs.lib.removePrefix (toString ./docparse + "/") p;
in
p' == "Cargo.lock" || p' == "Cargo.toml" || p' == "src" || p' == "src/main.rs"
) ./docparse;
src = builtins.filterSource
(
p: t:
let
p' = pkgs.lib.removePrefix (toString ./docparse + "/") p;
in
p' == "Cargo.lock" || p' == "Cargo.toml" || p' == "src" || p' == "src/main.rs"
) ./docparse;
};
in rec {
body = pkgs.runCommand "readme-body" {
buildInputs = [ docparse ];
} ''
cat ${./README.tpl.md} > $out
docparse ${./config.nix} >> gen
sed -e '/GEN_CONFIGURATION/{r gen' -e 'd}' -i $out
'';
in
rec {
body =
let
readme = builtins.readFile ./README.tpl.md;
test = pkgs.runCommand "readme-test" {} ''
params = builtins.readFile (
pkgs.runCommand "docparse"
{ buildInputs = [ docparse ]; }
"docparse ${./config.nix} > $out"
);
in
pkgs.writeText "readme" (
builtins.replaceStrings
[ "{{ params }}" ]
[ params ]
readme
);
test = pkgs.runCommand "readme-test" { } ''
diff ${./README.md} ${body}
touch $out
'';