Skip to content

Commit

Permalink
Multiple targets on nix flakes (#48)
Browse files Browse the repository at this point in the history
* flake

* remove duplicate code

* improve readme
  • Loading branch information
Findeton authored Jul 11, 2022
1 parent 91a795b commit ff13f90
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ This repository doesn´t include a `Cargo.lock` file as it is intended to work a

## building

This project uses [nix](https://nixos.org/) to create reproducible builds. In order to build the project as a library for the host system, run:

```nix build```

You can build the project as a WASM library with:

```nix build .#strand-wasm```

If you don't want to use nix, you can build the project with:

```cargo build```

### Build with parallelism
Expand Down
47 changes: 30 additions & 17 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,37 @@
inherit system overlays;
};
stdenv = pkgs.clangStdenv;
rust-nightly = pkgs
configureRustTargets = targets : pkgs
.rust-bin
.nightly
."2022-04-07"
.default
.override {
extensions = [ "rust-src" ];
targets = [ "wasm32-unknown-unknown" ];
}
;
${if (builtins.length targets) > 0 then "targets" else null} = targets;

};
rust-wasm = configureRustTargets [ "wasm32-unknown-unknown" ];
rust-system = configureRustTargets [];
# see https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/rust.section.md#importing-a-cargolock-file-importing-a-cargolock-file
cargoPatches = {
cargoLock = let
fixupLockFile = path: (builtins.readFile path);
in {
lockFileContents = fixupLockFile ./Cargo.lock.copy;
};
postPatch = ''
cp ${./Cargo.lock.copy} Cargo.lock
'';
};
buildRustPackageWithCargo = cargoArgs: pkgs.rustPlatform.buildRustPackage (cargoPatches // cargoArgs);
in rec {
defaultPackage = pkgs.rustPlatform.buildRustPackage {
pname = "strand";
packages.strand-wasm = buildRustPackageWithCargo {
pname = "strand-wasm";
version = "0.0.1";
src = ./.;
nativeBuildInputs = [
rust-nightly
rust-wasm
pkgs.nodePackages.npm
pkgs.wasm-pack
pkgs.wasm-bindgen-cli
Expand All @@ -50,17 +64,16 @@
rm -Rf $out/temp_home
cp pkg/strand-*.tgz $out
";

# see https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/rust.section.md#importing-a-cargolock-file-importing-a-cargolock-file
cargoLock = let
fixupLockFile = path: (builtins.readFile path);
in {
lockFileContents = fixupLockFile ./Cargo.lock.copy;
};
postPatch = ''
cp ${./Cargo.lock.copy} Cargo.lock
'';
};
packages.strand-lib = buildRustPackageWithCargo {
pname = "strand-lib";
version = "0.0.1";
src = ./.;
nativeBuildInputs = [
rust-system
];
};
defaultPackage = self.packages.${system}.strand-wasm;

# configure the dev shell
devShell = (
Expand Down

0 comments on commit ff13f90

Please sign in to comment.