From 1a3086df792ba8e3ed1c3ffa149a8946ac1c1e95 Mon Sep 17 00:00:00 2001 From: Brooklyn Zelenka Date: Fri, 14 Jul 2023 15:12:45 -0700 Subject: [PATCH 1/2] chore: Add Nix Flake --- flake.lock | 102 +++++++++++++++++++ flake.nix | 232 ++++++++++++++++++++++++++++++++++++++++++++ rust-toolchain.toml | 2 + 3 files changed, 336 insertions(+) create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 rust-toolchain.toml diff --git a/flake.lock b/flake.lock new file mode 100644 index 00000000..d3f4b552 --- /dev/null +++ b/flake.lock @@ -0,0 +1,102 @@ +{ + "nodes": { + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1673956053, + "narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1689068808, + "narHash": "sha256-6ixXo3wt24N/melDWjq70UuHQLxGV8jZvooRanIHXw0=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "919d646de7be200f3bf08cb76ae1f09402b6f9b4", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1689321787, + "narHash": "sha256-ifk7hrfWnJaLlcjCf8YaWDR+9kQ0uT3x9eCz31D9qB0=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "c11464c6625d9a71d91a3718a3567394638efc3e", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-compat": "flake-compat", + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "flake-utils": [ + "flake-utils" + ], + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1689302058, + "narHash": "sha256-yD74lcHTrw4niXcE9goJLbzsgyce48rQQoy5jK5ZK40=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "7b8dbbf4c67ed05a9bf3d9e658c12d4108bc24c8", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 00000000..0120ead9 --- /dev/null +++ b/flake.nix @@ -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; + } + ); +} diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 00000000..292fe499 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "stable" From 7e8323d8bfb0a5235aafe2cabe590b3f38869020 Mon Sep 17 00:00:00 2001 From: Brooklyn Zelenka Date: Fri, 14 Jul 2023 15:17:41 -0700 Subject: [PATCH 2/2] chore: add Nix build check workflow --- .github/workflows/nix_build.yml | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 .github/workflows/nix_build.yml diff --git a/.github/workflows/nix_build.yml b/.github/workflows/nix_build.yml new file mode 100644 index 00000000..23f2cf5a --- /dev/null +++ b/.github/workflows/nix_build.yml @@ -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