diff --git a/.envrc b/.envrc new file mode 100644 index 000000000..ff5954ffb --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake . --impure \ No newline at end of file diff --git a/.gitignore b/.gitignore index 3689e8098..55150a4a4 100644 --- a/.gitignore +++ b/.gitignore @@ -16,5 +16,10 @@ rust-toolchain.toml # Ignore local .cargo .cargo/ - -*.blend1 + +*.blend1 + +# ignore nix stuff +.direnv/ +.devenv +.pre-commit-config.yaml diff --git a/flake.lock b/flake.lock new file mode 100644 index 000000000..5dcd6bad6 --- /dev/null +++ b/flake.lock @@ -0,0 +1,343 @@ +{ + "nodes": { + "devenv": { + "inputs": { + "flake-compat": "flake-compat", + "nix": "nix", + "nixpkgs": "nixpkgs", + "pre-commit-hooks": "pre-commit-hooks" + }, + "locked": { + "lastModified": 1697058441, + "narHash": "sha256-gjtW+nkM9suMsjyid63HPmt6WZQEvuVqA5cOAf4lLM0=", + "owner": "cachix", + "repo": "devenv", + "rev": "55294461a62d90c8626feca22f52b0d3d0e18e39", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "devenv", + "type": "github" + } + }, + "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-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1696343447, + "narHash": "sha256-B2xAZKLkkeRFG5XcHHSXXcP7To9Xzr59KXeZiRf4vdQ=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "c9afaba3dfa4085dbd2ccb38dfade5141e33d9d4", + "type": "github" + }, + "original": { + "id": "flake-parts", + "type": "indirect" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1685518550, + "narHash": "sha256-o2d0KcvaXzTrPRIo0kOLV0/QXHhDQ5DTi+OxcjO8xqY=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "a1720a10a6cfe8234c0e93907ffe81be440f4cef", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "inputs": { + "systems": "systems_2" + }, + "locked": { + "lastModified": 1694529238, + "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "ff7b65b44d01cf9ba6a71320833626af21126384", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "gitignore": { + "inputs": { + "nixpkgs": [ + "devenv", + "pre-commit-hooks", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1660459072, + "narHash": "sha256-8DFJjXG8zqoONA1vXtgeKXy68KdJL5UaXR8NtVMUbx8=", + "owner": "hercules-ci", + "repo": "gitignore.nix", + "rev": "a20de23b925fd8264fd7fad6454652e142fd7f73", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "gitignore.nix", + "type": "github" + } + }, + "lowdown-src": { + "flake": false, + "locked": { + "lastModified": 1633514407, + "narHash": "sha256-Dw32tiMjdK9t3ETl5fzGrutQTzh2rufgZV4A/BbxuD4=", + "owner": "kristapsdz", + "repo": "lowdown", + "rev": "d2c2b44ff6c27b936ec27358a2653caaef8f73b8", + "type": "github" + }, + "original": { + "owner": "kristapsdz", + "repo": "lowdown", + "type": "github" + } + }, + "mk-shell-bin": { + "locked": { + "lastModified": 1677004959, + "narHash": "sha256-/uEkr1UkJrh11vD02aqufCxtbF5YnhRTIKlx5kyvf+I=", + "owner": "rrbutani", + "repo": "nix-mk-shell-bin", + "rev": "ff5d8bd4d68a347be5042e2f16caee391cd75887", + "type": "github" + }, + "original": { + "owner": "rrbutani", + "repo": "nix-mk-shell-bin", + "type": "github" + } + }, + "nix": { + "inputs": { + "lowdown-src": "lowdown-src", + "nixpkgs": [ + "devenv", + "nixpkgs" + ], + "nixpkgs-regression": "nixpkgs-regression" + }, + "locked": { + "lastModified": 1676545802, + "narHash": "sha256-EK4rZ+Hd5hsvXnzSzk2ikhStJnD63odF7SzsQ8CuSPU=", + "owner": "domenkozar", + "repo": "nix", + "rev": "7c91803598ffbcfe4a55c44ac6d49b2cf07a527f", + "type": "github" + }, + "original": { + "owner": "domenkozar", + "ref": "relaxed-flakes", + "repo": "nix", + "type": "github" + } + }, + "nix2container": { + "inputs": { + "flake-utils": "flake-utils_2", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1697285352, + "narHash": "sha256-tRGLm/DO8+anGUCgLYxTF5EPqWv8EZ9MVFWRdh285HU=", + "owner": "nlewo", + "repo": "nix2container", + "rev": "9d7f33ef0058f4df4c0912025f43c758a3289d76", + "type": "github" + }, + "original": { + "owner": "nlewo", + "repo": "nix2container", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1678875422, + "narHash": "sha256-T3o6NcQPwXjxJMn2shz86Chch4ljXgZn746c2caGxd8=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "126f49a01de5b7e35a43fd43f891ecf6d3a51459", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib": { + "locked": { + "dir": "lib", + "lastModified": 1696019113, + "narHash": "sha256-X3+DKYWJm93DRSdC5M6K5hLqzSya9BjibtBsuARoPco=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "f5892ddac112a1e9b3612c39af1b72987ee5783a", + "type": "github" + }, + "original": { + "dir": "lib", + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-regression": { + "locked": { + "lastModified": 1643052045, + "narHash": "sha256-uGJ0VXIhWKGXxkeNnq4TvV3CIOkUJ3PAoLZ3HMzNVMw=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2", + "type": "github" + } + }, + "nixpkgs-stable": { + "locked": { + "lastModified": 1685801374, + "narHash": "sha256-otaSUoFEMM+LjBI1XL/xGB5ao6IwnZOXc47qhIgJe8U=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "c37ca420157f4abc31e26f436c1145f8951ff373", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-23.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1697059129, + "narHash": "sha256-9NJcFF9CEYPvHJ5ckE8kvINvI84SZZ87PvqMbH6pro0=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "5e4c2ada4fcd54b99d56d7bd62f384511a7e2593", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "pre-commit-hooks": { + "inputs": { + "flake-compat": [ + "devenv", + "flake-compat" + ], + "flake-utils": "flake-utils", + "gitignore": "gitignore", + "nixpkgs": [ + "devenv", + "nixpkgs" + ], + "nixpkgs-stable": "nixpkgs-stable" + }, + "locked": { + "lastModified": 1688056373, + "narHash": "sha256-2+SDlNRTKsgo3LBRiMUcoEUb6sDViRNQhzJquZ4koOI=", + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "rev": "5843cf069272d92b60c3ed9e55b7a8989c01d4c7", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "type": "github" + } + }, + "root": { + "inputs": { + "devenv": "devenv", + "flake-parts": "flake-parts", + "mk-shell-bin": "mk-shell-bin", + "nix2container": "nix2container", + "nixpkgs": "nixpkgs_2" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_2": { + "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 000000000..a7d287b72 --- /dev/null +++ b/flake.nix @@ -0,0 +1,169 @@ +{ + description = "Description for the project"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + devenv.url = "github:cachix/devenv"; + nix2container.url = "github:nlewo/nix2container"; + nix2container.inputs.nixpkgs.follows = "nixpkgs"; + mk-shell-bin.url = "github:rrbutani/nix-mk-shell-bin"; + }; + + outputs = inputs @ {flake-parts, ...}: + flake-parts.lib.mkFlake {inherit inputs;} { + imports = [ + inputs.devenv.flakeModule + ]; + systems = ["x86_64-linux" "i686-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin"]; + + perSystem = { + config, + self', + inputs', + pkgs, + system, + ... + }: let + cargoToml = builtins.fromTOML (builtins.readFile ./emergence_game/Cargo.toml); + # https://github.com/bevyengine/bevy/blob/latest/docs/linux_dependencies.md#nix + # https://github.com/NixOS/nixpkgs/blob/master/pkgs/games/jumpy/default.nix + + buildInputs = with pkgs; [ + zstd # may need to config pkg-config + udev + alsa-lib + vulkan-loader + xorg.libX11 + xorg.libXcursor + xorg.libXi + xorg.libXrandr # To use the x11 feature + libxkbcommon + wayland # To use the wayland feature + ]; + + env = { + ZSTD_SYS_USE_PKG_CONFIG = true; + }; + # ++ lib.optionals stdenv.isDarwin [ + # darwin.apple_sdk.frameworks.Cocoa + # rustPlatform.bindgenHook + # ]; + nativeBuildInputs = with pkgs; [ + pkg-config + llvmPackages.bintools # To use lld linker + ]; + # LD_LIBRARY_PATH = lib.makeLibraryPath buildInputs; + pname = cargoToml.package.name; + version = cargoToml.package.version; + ide = with pkgs; + vscode-with-extensions.override { + vscode = vscodium; + vscodeExtensions = with vscode-extensions; [ + jnoortheen.nix-ide + tamasfe.even-better-toml + serayuzgur.crates + mkhl.direnv + rust-lang.rust-analyzer + github.copilot-chat + github.copilot + vadimcn.vscode-lldb + ]; + }; + lib = pkgs.lib; + filterCargoSources = orig_path: type: let + path = toString orig_path; + base = baseNameOf path; + parentDir = baseNameOf (dirOf path); + + matchesSuffix = lib.any (suffix: lib.hasSuffix suffix base) [ + # Keep rust sources + ".rs" + # Keep all toml files as they are commonly used to configure other + # cargo-based tools + ".toml" + ]; + + # Cargo.toml already captured above + isCargoFile = base == "Cargo.lock"; + + # .cargo/config.toml already captured above + isCargoConfig = parentDir == ".cargo" && base == "config"; + in + type == "directory" || matchesSuffix || isCargoFile || isCargoConfig; + src = lib.cleanSourceWith { + # Apply the default source cleaning from nixpkgs + src = lib.cleanSource ./.; + + # Then add our own filter on top + filter = filterCargoSources; + }; + assets = ./emergence_game/assets; + built = with pkgs; + rustPlatform.buildRustPackage rec { + inherit pname version buildInputs nativeBuildInputs env src; + + cargoLock = { + lockFile = ./Cargo.lock; + }; + + cargoBuildFlags = ["--bin" pname]; + + postInstall = '' + cp -r ${assets} $out/bin/assets + ''; + + postFixup = lib.optionalString stdenv.isLinux '' + patchelf $out/bin/${pname} \ + --add-rpath ${lib.makeLibraryPath [vulkan-loader]} + ''; + }; + in { + # Per-system attributes can be defined here. The self' and inputs' + # module parameters provide easy access to attributes of the same + # system. + + # Equivalent to inputs'.nixpkgs.legacyPackages.hello; + packages.ide = ide; + packages.default = built; + packages.blender = pkgs.blender; + devenv.shells.default = { + name = cargoToml.package.name; + + imports = [ + # This is just like the imports in devenv.nix. + # See https://devenv.sh/guides/using-with-flake-parts/#import-a-devenv-module + # ./devenv-foo.nix + ]; + + # https://devenv.sh/reference/options/ + packages = buildInputs ++ nativeBuildInputs; + languages.rust.enable = true; + pre-commit.hooks = { + alejandra.enable = true; + rustfmt.enable = true; + }; + difftastic.enable = true; + scripts.ide.exec = '' + ${ide}/bin/codium + ''; + scripts.trunk-serve.exec = '' + ${pkgs.trunk}/bin/trunk serve + ''; + + scripts.build-assets.exec = '' + ${pkgs.blender}/bin/blender --background assets/assets.blend --python "convert.py" + ''; + + scripts.blender-open.exec = '' + ${pkgs.blender}/bin/blender ./assets/assets.blend + ''; + }; + formatter = pkgs.alejandra; + }; + flake = { + # The usual flake attributes can be defined here, including system- + # agnostic ones like nixosModule and system-enumerating ones, although + # those are more easily expressed in perSystem. + }; + }; +}