From 8c101df12a9f84e4c214efc78cd4d4a67c9b4977 Mon Sep 17 00:00:00 2001 From: Amneesh Singh Date: Tue, 8 Aug 2023 20:07:30 +0530 Subject: [PATCH] nix: reorganize Signed-off-by: Amneesh Singh --- .envrc | 1 + .github/workflows/main.yml | 8 +-- .github/workflows/web.yml | 15 ++++- flake.lock | 73 +++++++++++---------- flake.nix | 131 ++++++++----------------------------- nix/default.nix | 18 +++++ nix/torana-web.nix | 48 ++++++++++++++ nix/torana.nix | 73 +++++++++++++++++++++ 8 files changed, 221 insertions(+), 146 deletions(-) create mode 100644 .envrc create mode 100644 nix/default.nix create mode 100644 nix/torana-web.nix create mode 100644 nix/torana.nix diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6e6037f..d543827 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,16 +17,16 @@ jobs: authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}' - name: fmt checks - run: nix build .#checks.fmt -L + run: nix build .#torana-fmt -L - name: clippy checks - run: nix build .#checks.clippy -L + run: nix build .#torana-clippy -L - name: nextest checks - run: nix build .#checks.nextest -L + run: nix build .#torana-nextest -L - name: doc checks - run: nix build .#checks.doc -L + run: nix build .#torana-doc -L - name: build run: nix build .#torana -L \ No newline at end of file diff --git a/.github/workflows/web.yml b/.github/workflows/web.yml index d574c62..479b2a1 100644 --- a/.github/workflows/web.yml +++ b/.github/workflows/web.yml @@ -7,7 +7,7 @@ on: workflow_dispatch: env: - OUT_LINK: toranaWeb + OUT_LINK: torana-web jobs: build: @@ -25,12 +25,21 @@ jobs: name: pain authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}' + - name: fmt checks + run: nix build .#torana-web-fmt -L + + - name: clippy checks + run: nix build .#torana-web-clippy -L + + - name: doc checks + run: nix build .#torana-web-doc -L + - name: build - run: nix build .#toranaWeb --out-link $OUT_LINK -L + run: nix build .#torana-web --out-link $OUT_LINK -L - name: wasm-bindgen run: > - nix develop .#toranaWeb + nix develop .#torana-web -c wasm-bindgen --target web $OUT_LINK/bin/torana.wasm --out-dir www \ No newline at end of file diff --git a/flake.lock b/flake.lock index bf49459..de1498d 100644 --- a/flake.lock +++ b/flake.lock @@ -39,6 +39,24 @@ "type": "github" } }, + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1690933134, + "narHash": "sha256-ab989mN63fQZBFrkk4Q8bYxQCktuHmBIBqUG1jl6/FQ=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "59cf3f1447cfc75087e7273b04b31e689a8599fb", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, "flake-utils": { "inputs": { "systems": "systems" @@ -91,12 +109,30 @@ "type": "github" } }, + "nixpkgs-lib": { + "locked": { + "dir": "lib", + "lastModified": 1690881714, + "narHash": "sha256-h/nXluEqdiQHs1oSgkOOWF+j8gcJMWhwnZ9PFabN6q0=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "9e1960bc196baf6881340d53dccb203a951745a2", + "type": "github" + }, + "original": { + "dir": "lib", + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, "root": { "inputs": { "crane": "crane", + "flake-parts": "flake-parts", "nixpkgs": "nixpkgs", - "rust-overlay": "rust-overlay_2", - "utils": "utils" + "rust-overlay": "rust-overlay_2" } }, "rust-overlay": { @@ -174,39 +210,6 @@ "repo": "default", "type": "github" } - }, - "systems_3": { - "locked": { - "lastModified": 1681028828, - "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", - "owner": "nix-systems", - "repo": "default", - "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", - "type": "github" - }, - "original": { - "owner": "nix-systems", - "repo": "default", - "type": "github" - } - }, - "utils": { - "inputs": { - "systems": "systems_3" - }, - "locked": { - "lastModified": 1689068808, - "narHash": "sha256-6ixXo3wt24N/melDWjq70UuHQLxGV8jZvooRanIHXw0=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "919d646de7be200f3bf08cb76ae1f09402b6f9b4", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" - } } }, "root": "root", diff --git a/flake.nix b/flake.nix index e1d79a6..3f81078 100644 --- a/flake.nix +++ b/flake.nix @@ -14,126 +14,49 @@ inputs.nixpkgs.follows = "nixpkgs"; }; - utils.url = github:numtide/flake-utils; + flake-parts.url = github:hercules-ci/flake-parts; }; - outputs = { self, nixpkgs, utils, crane, rust-overlay, }: - utils.lib.eachDefaultSystem - ( - system: + outputs = inputs@{ self, nixpkgs, crane, rust-overlay, flake-parts }: + flake-parts.lib.mkFlake { inherit inputs; } { + + systems = [ + "x86_64-linux" + "aarch64-linux" + "i686-linux" + "x86_64-darwin" + "aarch64-darwin" + ]; + + imports = [ + ./nix + ]; + + perSystem = { self', system, ... }: let pkgs = import nixpkgs { inherit system; overlays = [ rust-overlay.overlays.default ]; }; - runtimeDependencies = with pkgs; [ - freetype - fontconfig - vulkan-loader - ]; - - xorgInputs = with pkgs; (with xorg; [ - libX11 - libXcursor - libXrandr - libXi - ]) ++ [ - # libxkbcommon - # wayland - ]; - - nativeBuildInputs = with pkgs; [ - cmake - openssl.dev - fontconfig.dev - pkg-config - ] - ++ lib.optionals (with stdenv.hostPlatform; (isx86 || isi686 || isAarch64)) [ mold ] - ++ lib.optionals stdenv.hostPlatform.isDarwin [ darwin.apple_sdk.frameworks.Cocoa ]; - toolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain; - craneLib = (crane.mkLib pkgs).overrideToolchain toolchain; - src = craneLib.cleanCargoSource (craneLib.path ./.); + filterWgsl = path: _: builtins.match ".*\.wgsl$" path != null; + filterSrc = path: type: + (filterWgsl path type) || (craneLib.filterCargoSources path type); - commonArgs = { - inherit src nativeBuildInputs; - buildInputs = xorgInputs; - doCheck = false; + src = pkgs.lib.cleanSourceWith { + src = craneLib.path ./.; + filter = filterSrc; }; - - cargoArtifacts = craneLib.buildDepsOnly commonArgs; - - torana = craneLib.buildPackage (commonArgs // { - inherit cargoArtifacts runtimeDependencies; - nativeBuildInputs = nativeBuildInputs ++ [ pkgs.autoPatchelfHook ]; - }); - - commonArgsWeb = { - inherit src; - cargoExtraArgs = "--target wasm32-unknown-unknown"; - doCheck = false; - }; - - cargoArtifactsWeb = craneLib.buildDepsOnly commonArgsWeb; - - toranaWeb = craneLib.buildPackage (commonArgsWeb // { - inherit cargoArtifactsWeb; - }); - in rec { - packages = { - inherit torana toranaWeb toolchain; - default = torana; - - # not using flake checks to run them individually - checks = { - clippy = craneLib.cargoClippy (commonArgs // { - inherit cargoArtifacts; - }); - - fmt = craneLib.cargoFmt { - inherit src; - }; - - doc = craneLib.cargoDoc (commonArgs // { - inherit cargoArtifacts; - }); - - nextest = craneLib.cargoNextest (commonArgs // { - inherit cargoArtifacts; - partitions = 1; - partitionType = "count"; - }); - }; - + _module.args = { + inherit src craneLib toolchain pkgs; }; - devShells = rec { - torana = with pkgs; - mkShell { - name = "torana"; - nativeBuildInputs = nativeBuildInputs ++ - (with pkgs; [ wasm-bindgen toolchain ]); - - LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath - (xorgInputs ++ runtimeDependencies); - }; - - default = torana; - - toranaWeb = with pkgs; - mkShell { - name = "toranaWeb"; - nativeBuildInputs = with pkgs; [ - wasm-bindgen-cli - ]; - }; - }; formatter = pkgs.nixpkgs-fmt; - } - ); + }; + }; } diff --git a/nix/default.nix b/nix/default.nix new file mode 100644 index 0000000..964cab5 --- /dev/null +++ b/nix/default.nix @@ -0,0 +1,18 @@ +{ ... }: { + imports = [ + ./torana.nix + ./torana-web.nix + ]; + + perSystem = { self', pkgs, ... }: { + packages.default = self'.packages.torana; + + devShells.default = pkgs.mkShell { + name = "torana-dev"; + inputsFrom = with self'.devShells; [ + torana + torana-web + ]; + }; + }; +} diff --git a/nix/torana-web.nix b/nix/torana-web.nix new file mode 100644 index 0000000..7c1ac29 --- /dev/null +++ b/nix/torana-web.nix @@ -0,0 +1,48 @@ +{ ... }: { + perSystem = { pkgs, src, craneLib, toolchain, ... }: + let + commonArgs = { + inherit src; + cargoExtraArgs = "--target wasm32-unknown-unknown"; + doCheck = false; + }; + + cargoArtifacts = craneLib.buildDepsOnly commonArgs; + + torana = craneLib.buildPackage (commonArgs // { + inherit cargoArtifacts; + }); + in + { + packages = { + torana-web = torana; + + torana-web-clippy = craneLib.cargoClippy (commonArgs // { + inherit cargoArtifacts; + }); + + torana-web-fmt = craneLib.cargoFmt { + inherit src; + }; + + torana-web-doc = craneLib.cargoDoc (commonArgs // { + inherit cargoArtifacts; + }); + + torana-web-nextest = craneLib.cargoNextest (commonArgs // { + inherit cargoArtifacts; + partitions = 1; + partitionType = "count"; + }); + }; + + devShells.torana-web = pkgs.mkShell { + name = "torana-web"; + nativeBuildInputs = [ + pkgs.wasm-bindgen-cli + toolchain + ]; + }; + + }; +} diff --git a/nix/torana.nix b/nix/torana.nix new file mode 100644 index 0000000..f907d89 --- /dev/null +++ b/nix/torana.nix @@ -0,0 +1,73 @@ +{ ... }: { + perSystem = { pkgs, src, craneLib, toolchain, ... }: + let + runtimeDependencies = with pkgs; [ + freetype + fontconfig + vulkan-loader + ]; + + buildInputs = with pkgs; (with xorg; [ + libX11 + libXcursor + libXrandr + libXi + ]) ++ [ + # libxkbcommon + # wayland + ]; + + nativeBuildInputs = with pkgs; [ + cmake + openssl.dev + fontconfig.dev + pkg-config + ] + ++ lib.optionals (with stdenv.hostPlatform; (isx86 || isi686 || isAarch64)) [ mold ] + ++ lib.optionals stdenv.hostPlatform.isDarwin [ darwin.apple_sdk.frameworks.Cocoa ]; + + commonArgs = { + inherit src nativeBuildInputs buildInputs; + doCheck = false; + }; + + cargoArtifacts = craneLib.buildDepsOnly commonArgs; + + torana = craneLib.buildPackage (commonArgs // { + inherit cargoArtifacts runtimeDependencies; + nativeBuildInputs = nativeBuildInputs ++ [ pkgs.autoPatchelfHook ]; + }); + in + { + packages = { + inherit torana; + + torana-clippy = craneLib.cargoClippy (commonArgs // { + inherit cargoArtifacts; + }); + + torana-fmt = craneLib.cargoFmt { + inherit src; + }; + + torana-doc = craneLib.cargoDoc (commonArgs // { + inherit cargoArtifacts; + }); + + torana-nextest = craneLib.cargoNextest (commonArgs // { + inherit cargoArtifacts; + partitions = 1; + partitionType = "count"; + }); + }; + + devShells.torana = pkgs.mkShell { + name = "torana"; + nativeBuildInputs = nativeBuildInputs + ++ [ toolchain ]; + LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath + (buildInputs ++ runtimeDependencies); + }; + + }; +}