Skip to content

Commit

Permalink
chore: Add Nix Flake (#116)
Browse files Browse the repository at this point in the history
* chore: Add Nix Flake

* chore: add Nix build check workflow
  • Loading branch information
expede authored Jul 18, 2023
1 parent b14e331 commit 71bb83a
Show file tree
Hide file tree
Showing 4 changed files with 374 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/nix_build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: 📦 Nix Build

on:
push:
branches: [ main ]

pull_request:
branches: [ '**' ]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
run-checks:
runs-on: ubuntu-latest
strategy:
fail-fast: false
steps:
- name: Checkout Repository
uses: actions/checkout@v3

- name: Install Nix
uses: DeterminateSystems/nix-installer-action@v4

- name: Cache Magic
uses: DeterminateSystems/magic-nix-cache-action@v2

- name: Check Nix flake inputs
uses: DeterminateSystems/flake-checker-action@v5
with:
ignore-missing-flake-lock: false
fail-mode: true

- name: Nix Build
run: |
nix develop --show-trace -c irust --version
nix develop --show-trace -c rustc --version
102 changes: 102 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

232 changes: 232 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
{
description = "homestar";

inputs = {
# we leverage unstable due to wasm-tools/wasm updates
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";

flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};

rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-utils.follows = "flake-utils";
};
};

outputs = {
self,
nixpkgs,
flake-compat,
flake-utils,
rust-overlay,
} @ inputs:
flake-utils.lib.eachDefaultSystem (
system: let
overlays = [(import rust-overlay)];
pkgs = import nixpkgs {inherit system overlays;};

rust-toolchain = (pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml).override {
extensions = ["cargo" "clippy" "rustfmt" "rust-src" "rust-std"];
targets = ["wasm32-unknown-unknown" "wasm32-wasi"];
};

nightly-rustfmt = pkgs.rust-bin.nightly.latest.rustfmt;

format-pkgs = with pkgs; [
nixpkgs-fmt
alejandra
];

cargo-installs = with pkgs; [
cargo-deny
cargo-expand
cargo-nextest
cargo-outdated
cargo-sort
cargo-udeps
cargo-watch
twiggy
wasm-tools
];

ci = pkgs.writeScriptBin "ci" ''
cargo fmt --check
cargo clippy
cargo build --release
nx-test
nx-test-0
'';

db = pkgs.writeScriptBin "db" ''
diesel setup
diesel migration run
'';

dbReset = pkgs.writeScriptBin "db-reset" ''
diesel database reset
diesel setup
diesel migration run
'';

doc = pkgs.writeScriptBin "doc" ''
cargo doc --no-deps --document-private-items --open
'';

compileWasm = pkgs.writeScriptBin "compile-wasm" ''
cargo build -p homestar-functions --target wasm32-unknown-unknown --release
'';

dockerBuild = arch:
pkgs.writeScriptBin "docker-${arch}" ''
docker buildx build --file docker/Dockerfile --platform=linux/${arch} -t homestar-runtime --progress=plain .
'';

xFunc = cmd:
pkgs.writeScriptBin "x-${cmd}" ''
cargo watch -c -x ${cmd}
'';

xFuncAll = cmd:
pkgs.writeScriptBin "x-${cmd}-all" ''
cargo watch -c -s "cargo ${cmd} --all-features"
'';

xFuncNoDefault = cmd:
pkgs.writeScriptBin "x-${cmd}-0" ''
cargo watch -c -s "cargo ${cmd} --no-default-features"
'';

xFuncPackage = cmd: crate:
pkgs.writeScriptBin "x-${cmd}-${crate}" ''
cargo watch -c -s "cargo ${cmd} -p homestar-${crate} --all-features"
'';

xFuncTest = pkgs.writeScriptBin "x-test" ''
cargo watch -c -s "cargo nextest run --nocapture && cargo test --doc"
'';

xFuncTestAll = pkgs.writeScriptBin "x-test-all" ''
cargo watch -c -s "cargo nextest run --all-features --nocapture \
&& cargo test --doc --all-features"
'';

xFuncTestNoDefault = pkgs.writeScriptBin "x-test-0" ''
cargo watch -c -s "cargo nextest run --no-default-features --nocapture \
&& cargo test --doc --no-default-features"
'';

xFuncTestPackage = crate:
pkgs.writeScriptBin "x-test-${crate}" ''
cargo watch -c -s "cargo nextest run -p homestar-${crate} --all-features \
&& cargo test --doc -p homestar-${crate} --all-features"
'';

nxTest = pkgs.writeScriptBin "nx-test" ''
cargo nextest run
cargo test --doc
'';

nxTestAll = pkgs.writeScriptBin "nx-test-all" ''
cargo nextest run --all-features --nocapture
cargo test --doc --all-features
'';

nxTestNoDefault = pkgs.writeScriptBin "nx-test-0" ''
cargo nextest run --no-default-features --nocapture
cargo test --doc --no-default-features
'';

wasmTest = pkgs.writeScriptBin "wasm-ex-test" ''
cargo build -p homestar-functions --features example-test --target wasm32-unknown-unknown --release
cp target/wasm32-unknown-unknown/release/homestar_functions.wasm homestar-wasm/fixtures/example_test.wasm
wasm-tools component new homestar-wasm/fixtures/example_test.wasm -o homestar-wasm/fixtures/example_test_component.wasm
'';

wasmAdd = pkgs.writeScriptBin "wasm-ex-add" ''
cargo build -p homestar-functions --features example-add --target wasm32-unknown-unknown --release
cp target/wasm32-unknown-unknown/release/homestar_functions.wasm homestar-wasm/fixtures/example_add.wasm
wasm-tools component new homestar-wasm/fixtures/example_add.wasm -o homestar-wasm/fixtures/example_add_component.wasm
wasm-tools print homestar-wasm/fixtures/example_add.wasm -o homestar-wasm/fixtures/example_add.wat
wasm-tools print homestar-wasm/fixtures/example_add_component.wasm -o homestar-wasm/fixtures/example_add_component.wat
'';

scripts = [
ci
db
dbReset
doc
compileWasm
(builtins.map (arch: dockerBuild arch) ["amd64" "arm64"])
(builtins.map (cmd: xFunc cmd) ["build" "check" "run" "clippy"])
(builtins.map (cmd: xFuncAll cmd) ["build" "check" "run" "clippy"])
(builtins.map (cmd: xFuncNoDefault cmd) ["build" "check" "run" "clippy"])
(builtins.map (cmd: xFuncPackage cmd "core") ["build" "check" "run" "clippy"])
(builtins.map (cmd: xFuncPackage cmd "wasm") ["build" "check" "run" "clippy"])
(builtins.map (cmd: xFuncPackage cmd "runtime") ["build" "check" "run" "clippy"])
xFuncTest
xFuncTestAll
xFuncTestNoDefault
(builtins.map (crate: xFuncTestPackage crate) ["core" "wasm" "guest-wasm" "runtime"])
nxTest
nxTestAll
nxTestNoDefault
wasmTest
wasmAdd
];
in rec
{
devShells.default = pkgs.mkShell {
name = "homestar";
nativeBuildInputs = with pkgs;
[
# The ordering of these two items is important. For nightly rustfmt to be used instead of
# the rustfmt provided by `rust-toolchain`, it must appear first in the list. This is
# because native build inputs are added to $PATH in the order they're listed here.
nightly-rustfmt
rust-toolchain
rust-analyzer
pkg-config
pre-commit
diesel-cli
direnv
self.packages.${system}.irust
]
++ format-pkgs
++ cargo-installs
++ scripts
++ lib.optionals stdenv.isDarwin [
darwin.apple_sdk.frameworks.Security
darwin.apple_sdk.frameworks.CoreFoundation
darwin.apple_sdk.frameworks.Foundation
];
NIX_PATH = "nixpkgs=" + pkgs.path;
RUST_BACKTRACE = 1;

shellHook = ''
[ -e .git/hooks/pre-commit ] || pre-commit install --install-hooks && pre-commit install --hook-type commit-msg
'';
};

packages.irust = pkgs.rustPlatform.buildRustPackage rec {
pname = "irust";
version = "1.70.0";
src = pkgs.fetchFromGitHub {
owner = "sigmaSd";
repo = "IRust";
rev = "v${version}";
sha256 = "sha256-chZKesbmvGHXwhnJRZbXyX7B8OwJL9dJh0O1Axz/n2E=";
};

doCheck = false;
cargoSha256 = "sha256-FmsD3ajMqpPrTkXCX2anC+cmm0a2xuP+3FHqzj56Ma4=";
};

formatter = pkgs.alejandra;
}
);
}
2 changes: 2 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "stable"

0 comments on commit 71bb83a

Please sign in to comment.