From effc1576a1fc342c80a77e33f95315bf08c47921 Mon Sep 17 00:00:00 2001 From: Shish Date: Fri, 29 Sep 2023 00:49:50 +0100 Subject: [PATCH 1/2] [all] remove nix When this works, it's slow; when it doesn't work, I can't figure out how to fix it ;( --- .envrc | 1 - .github/workflows/common.yml | 103 ------------- README.md | 6 +- c/.envrc | 1 - c/derivation.nix | 44 ------ cpp/.envrc | 1 - cpp/derivation.nix | 41 ----- cpp/shell.nix | 10 -- default.nix | 10 -- flake.lock | 283 ---------------------------------- flake.nix | 241 ----------------------------- go/.envrc | 1 - go/derivation.nix | 30 ---- go/shell.nix | 8 - nim/.envrc | 1 - nim/derivation.nix | 64 -------- nim/shell.nix | 9 -- php/.envrc | 1 - php/derivation.nix | 74 --------- php/shell.nix | 25 --- pxd/.envrc | 1 - pxd/derivation.nix | 35 ----- pxd/shell.nix | 13 -- py/.envrc | 1 - py/derivation.nix | 57 ------- py/shell.nix | 11 -- rs/.envrc | 1 - rs/derivation.nix | 43 ------ rs/shell.nix | 9 -- shell.nix | 10 -- utils/.envrc | 1 - utils/clean-source.nix | 35 ----- utils/derivation.nix | 55 ------- utils/get-builds-for-lang.nix | 14 -- utils/shell.nix | 5 - utils/shell.sh | 16 +- zig/.envrc | 1 - zig/derivation.nix | 104 ------------- zig/shell.nix | 39 ----- 39 files changed, 2 insertions(+), 1403 deletions(-) delete mode 100644 .envrc delete mode 120000 c/.envrc delete mode 100644 c/derivation.nix delete mode 120000 cpp/.envrc delete mode 100644 cpp/derivation.nix delete mode 100644 cpp/shell.nix delete mode 100644 default.nix delete mode 100644 flake.lock delete mode 100644 flake.nix delete mode 120000 go/.envrc delete mode 100644 go/derivation.nix delete mode 100644 go/shell.nix delete mode 120000 nim/.envrc delete mode 100644 nim/derivation.nix delete mode 100644 nim/shell.nix delete mode 120000 php/.envrc delete mode 100644 php/derivation.nix delete mode 100644 php/shell.nix delete mode 120000 pxd/.envrc delete mode 100644 pxd/derivation.nix delete mode 100644 pxd/shell.nix delete mode 120000 py/.envrc delete mode 100644 py/derivation.nix delete mode 100644 py/shell.nix delete mode 120000 rs/.envrc delete mode 100644 rs/derivation.nix delete mode 100644 rs/shell.nix delete mode 100644 shell.nix delete mode 100644 utils/.envrc delete mode 100644 utils/clean-source.nix delete mode 100644 utils/derivation.nix delete mode 100644 utils/get-builds-for-lang.nix delete mode 100644 utils/shell.nix delete mode 120000 zig/.envrc delete mode 100644 zig/derivation.nix delete mode 100644 zig/shell.nix diff --git a/.envrc b/.envrc deleted file mode 100644 index 3550a30f..00000000 --- a/.envrc +++ /dev/null @@ -1 +0,0 @@ -use flake diff --git a/.github/workflows/common.yml b/.github/workflows/common.yml index 240941d9..41ab7ffd 100644 --- a/.github/workflows/common.yml +++ b/.github/workflows/common.yml @@ -106,106 +106,3 @@ jobs: - name: Benchmark Test shell: bash run: ./all.py bench ${{ inputs.workdir }} --default --frames 10 - - nix: - name: Nix - runs-on: ubuntu-22.04 - if: ${{ !contains(github.event.head_commit.message, '[skip nix]') }} - outputs: - flake_status: ${{ steps.early.outputs.flake_status }} - shell_status: ${{ steps.early.outputs.shell_status }} - steps: - - name: Checkout - uses: actions/checkout@v3 - - id: early - name: Check if nix files exist - shell: bash - working-directory: ${{ inputs.workdir }} - run: | - flake_status="skip" - if [ -f "derivation.nix" ]; then - flake_status="found" - fi - echo "flake_status=${flake_status}" >> "${GITHUB_OUTPUT}" - - shell_status="skip" - if [ -f "shell.nix" ]; then - shell_status="found" - fi - echo "shell_status=${shell_status}" >> "${GITHUB_OUTPUT}" - - nix-flake: - name: Nix Flake - runs-on: ubuntu-22.04 - needs: nix - if: ${{ !contains(github.event.head_commit.message, '[skip nix flake]') && needs.nix.outputs.flake_status == 'found' }} - steps: - - name: Checkout - uses: actions/checkout@v3 - with: - submodules: recursive - - name: Set Up Build Cache - uses: actions/cache@v3 - id: nix-cache - with: - path: /tmp/nix-store.nar - key: ${{ runner.os }}-${{ inputs.workdir }}-nix-store.nar-${{ hashFiles('flake.nix', 'flake.lock', format('{0}/derivation.nix', inputs.workdir)) }} - restore-keys: ${{ runner.os }}-${{ inputs.workdir }}-nix-store.nar- - - name: Install Deps - uses: cachix/install-nix-action@v15 - with: - nix_path: nixpkgs=channel:nixos-22.11 - # see: https://github.com/cachix/install-nix-action/issues/56#issuecomment-1300421537 - - name: "Import Nix Store Cache" - if: "steps.nix-cache.outputs.cache-hit == 'true'" - run: "nix-store --import < /tmp/nix-store.nar" - - name: Flake Build - shell: bash - run: | - nix eval .#packages --impure --raw \ - --apply '(import ./utils/get-builds-for-lang.nix { lang = "${{ inputs.workdir }}"; attr = "packages"; })' \ - | xargs nix build -L - - name: Flake Run - shell: bash - run: | - nix eval .#packages --impure --raw \ - --apply '(import ./utils/get-builds-for-lang.nix { lang = "${{ inputs.workdir }}"; prefix = ".#"; })' \ - | xargs -I{} nix run {} -- --help - - name: Flake Shell - shell: bash - working-directory: ${{ inputs.workdir }} - run: nix develop ..#${{ inputs.workdir }} --command bash -c './build.sh && ./rosettaboy-release --help' - - name: Flake Checks - shell: bash - run: | - nix eval .#checks --impure --raw \ - --apply '(import ./utils/get-builds-for-lang.nix { lang = "${{ inputs.workdir }}"; attr = "checks"; })' \ - | xargs nix build -L - - name: "Export Nix Store Cache" - shell: bash - # we could add devShells here but it might fill the cache fast... - run: | - nix eval .#packages --impure --raw \ - --apply '(import ./utils/get-builds-for-lang.nix { lang = "${{ inputs.workdir }}"; attr = "packages"; })' \ - | xargs -I{} bash -c "nix eval --raw {}; echo" \ - | xargs nix-store -qR \ - | xargs nix-store --export > /tmp/nix-store.nar - - nix-shell: - name: Nix Shell - runs-on: ubuntu-22.04 - needs: nix - if: ${{ !contains(github.event.head_commit.message, '[skip nix flake]') && needs.nix.outputs.shell_status == 'found' }} - steps: - - name: Checkout - uses: actions/checkout@v3 - with: - submodules: recursive - - name: Install Nix - uses: cachix/install-nix-action@v15 - with: - nix_path: nixpkgs=channel:nixos-22.11 - - name: Run Shell - shell: bash - working-directory: ${{ inputs.workdir }} - run: nix-shell --pure --run './build.sh && ./rosettaboy-release --help' diff --git a/README.md b/README.md index c9e6685a..0902ca4c 100644 --- a/README.md +++ b/README.md @@ -66,11 +66,7 @@ that the user has the standard language dev kits installed (eg we assume anyone who wants to work on the Rust version will have Cargo installed; anyone who wants to work on Python will have virtualenv + pip; etc) -If you have either Nix or Docker available, you can run `./utils/shell.sh` to create and run an environment with all necessary dev tools pre-installed. — `./build.sh && ./rosettaboy-release --headless --silent` should be able to pass tests for all languages. - -If you prefer Docker, you can use `./utils/shell-docker.sh` instead. - -If you prefer Nix, you can manually run `nix develop` or `nix-shell` instead. When run with an implementation as an argument, e.g. `nix develop .#py`, it will only provide what is needed for that language, and when run in the project root it will provide everything needed for all languages. Alternatively, there is also an integration with [nix-direnv](https://github.com/nix-community/nix-direnv). +If you have Docker available, you can run `./utils/shell.sh` to create and run an environment with all necessary dev tools pre-installed. — `./build.sh && ./rosettaboy-release --headless --silent` should be able to pass tests for all languages. Benchmarks diff --git a/c/.envrc b/c/.envrc deleted file mode 120000 index 8be2b97d..00000000 --- a/c/.envrc +++ /dev/null @@ -1 +0,0 @@ -../utils/.envrc \ No newline at end of file diff --git a/c/derivation.nix b/c/derivation.nix deleted file mode 100644 index a0f48b61..00000000 --- a/c/derivation.nix +++ /dev/null @@ -1,44 +0,0 @@ -{ - lib, - stdenv, - cmake, - SDL2, - pkg-config, - cleanSource, - clang-tools ? null, - ltoSupport ? false, - debugSupport ? false -}: - -let - devTools = [ clang-tools ]; -in - -stdenv.mkDerivation rec { - name = "rosettaboy-c"; - src = cleanSource { - inherit name; - src = ./.; - extraRules = '' - .clang-format - ''; - }; - - passthru = { inherit devTools; }; - - enableParallelBuilding = true; - - buildInputs = [ SDL2 ]; - nativeBuildInputs = [ cmake pkg-config ]; - - cmakeFlags = [ ] - ++ lib.optional debugSupport "-DCMAKE_BUILD_TYPE=Debug" - ++ lib.optional (!debugSupport) "-DCMAKE_BUILD_TYPE=Release" - ++ lib.optional ltoSupport "-DENABLE_LTO=On" - ; - - meta = { - description = name; - mainProgram = name; - }; -} diff --git a/cpp/.envrc b/cpp/.envrc deleted file mode 120000 index 8be2b97d..00000000 --- a/cpp/.envrc +++ /dev/null @@ -1 +0,0 @@ -../utils/.envrc \ No newline at end of file diff --git a/cpp/derivation.nix b/cpp/derivation.nix deleted file mode 100644 index 85a08d01..00000000 --- a/cpp/derivation.nix +++ /dev/null @@ -1,41 +0,0 @@ -{ - lib, - stdenv, - cmake, - SDL2, - fmt_8, - pkg-config, - cleanSource, - clang-format ? null, - ltoSupport ? false, - debugSupport ? false, -}: - -stdenv.mkDerivation rec { - name = "rosettaboy-cpp"; - src = cleanSource { - inherit name; - src = ./.; - extraRules = '' - .clang-format - ''; - }; - - buildInputs = [ SDL2 fmt_8 ]; - nativeBuildInputs = [ cmake pkg-config ]; - - passthru = { - devTools = [ clang-format ]; - }; - - cmakeFlags = [ ] - ++ lib.optional debugSupport "-DCMAKE_BUILD_TYPE=Debug" - ++ lib.optional (!debugSupport) "-DCMAKE_BUILD_TYPE=Release" - ++ lib.optional ltoSupport "-DENABLE_LTO=On" - ; - - meta = { - description = name; - mainProgram = name; - }; -} diff --git a/cpp/shell.nix b/cpp/shell.nix deleted file mode 100644 index 2740b043..00000000 --- a/cpp/shell.nix +++ /dev/null @@ -1,10 +0,0 @@ -{ pkgs ? import {} } : pkgs.mkShell { - buildInputs = with pkgs; [ - cmake - gnumake - SDL2 - fmt_8 - - clang-tools # Massive, and only used for format.sh. But I think it may share some dependencies with Nim, Zig (11/generic, I think), Rust. Painful because the other requirements are so small. - ]; -} diff --git a/default.nix b/default.nix deleted file mode 100644 index e6195e86..00000000 --- a/default.nix +++ /dev/null @@ -1,10 +0,0 @@ -(import - ( - let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in - fetchTarball { - url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; - sha256 = lock.nodes.flake-compat.locked.narHash; - } - ) - { src = ./.; } -).defaultNix \ No newline at end of file diff --git a/flake.lock b/flake.lock deleted file mode 100644 index 70cb8549..00000000 --- a/flake.lock +++ /dev/null @@ -1,283 +0,0 @@ -{ - "nodes": { - "cl-gameboy": { - "flake": false, - "locked": { - "lastModified": 1479058009, - "narHash": "sha256-qvT5wbRCrngd5tUPaEWqEUj53mGwjz87+eibn3qWd3k=", - "owner": "sjl", - "repo": "cl-gameboy", - "rev": "cce73054d4bfde332e31a2938f4e0630c162cf6b", - "type": "github" - }, - "original": { - "owner": "sjl", - "repo": "cl-gameboy", - "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-utils": { - "locked": { - "lastModified": 1667395993, - "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" - } - }, - "gb-autotest-roms": { - "flake": false, - "locked": { - "lastModified": 1658898280, - "narHash": "sha256-i5TkoWJ9f+OZYwr0oyaFigCkfPQkRBReYqBfXxcyXmM=", - "owner": "shish", - "repo": "gb-autotest-roms", - "rev": "367b7dc51aed4b84eea8097d7426fe2e3cd2f459", - "type": "github" - }, - "original": { - "owner": "shish", - "repo": "gb-autotest-roms", - "type": "github" - } - }, - "gitignore": { - "inputs": { - "nixpkgs": [ - "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" - } - }, - "gomod2nix-src": { - "flake": false, - "locked": { - "lastModified": 1662501203, - "narHash": "sha256-4BKeqCX2zwgBiTdlc2DjGQ0CttKm0vSw0r/bdFdM/PQ=", - "owner": "nix-community", - "repo": "gomod2nix", - "rev": "89cd0675b96775aa3ee86e7c0cf5bc238dd27976", - "type": "github" - }, - "original": { - "owner": "nix-community", - "repo": "gomod2nix", - "type": "github" - } - }, - "naersk": { - "inputs": { - "nixpkgs": [ - "nixpkgs" - ] - }, - "locked": { - "lastModified": 1671096816, - "narHash": "sha256-ezQCsNgmpUHdZANDCILm3RvtO1xH8uujk/+EqNvzIOg=", - "owner": "nix-community", - "repo": "naersk", - "rev": "d998160d6a076cfe8f9741e56aeec7e267e3e114", - "type": "github" - }, - "original": { - "owner": "nix-community", - "repo": "naersk", - "type": "github" - } - }, - "nim-argparse": { - "flake": false, - "locked": { - "lastModified": 1669660759, - "narHash": "sha256-rtA0NJbA7aQglw/4U/fhX8GTlGf0eYoXfuzlJsA38Q8=", - "owner": "iffy", - "repo": "nim-argparse", - "rev": "98c7c99bfbcaae750ac515a6fd603f85ed68668f", - "type": "github" - }, - "original": { - "owner": "iffy", - "repo": "nim-argparse", - "type": "github" - } - }, - "nixpkgs": { - "locked": { - "lastModified": 1675757258, - "narHash": "sha256-pIRer8vdsoherlRKpzfnHbMZ5TsAcvRlXHCIaHkIUbg=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "af96094e9b8eb162d70a84fa3b39f4b7a8b264d2", - "type": "github" - }, - "original": { - "owner": "NixOS", - "ref": "nixos-22.11", - "repo": "nixpkgs", - "type": "github" - } - }, - "php-sdl-src": { - "flake": false, - "locked": { - "lastModified": 1672500911, - "narHash": "sha256-8BTE9pjoJuomSuNxvoBLjbBE8eA3BkO9+8CjG4kO3M0=", - "owner": "Ponup", - "repo": "php-sdl", - "rev": "655e403b8a9681c418702a74833c68c1a4ae1bd5", - "type": "github" - }, - "original": { - "owner": "Ponup", - "repo": "php-sdl", - "type": "github" - } - }, - "pre-commit-hooks": { - "inputs": { - "flake-compat": [ - "flake-compat" - ], - "flake-utils": [ - "flake-utils" - ], - "gitignore": [ - "gitignore" - ], - "nixpkgs": [ - "nixpkgs" - ], - "nixpkgs-stable": [ - "nixpkgs" - ] - }, - "locked": { - "lastModified": 1675688762, - "narHash": "sha256-oit/SxMk0B380ASuztBGQLe8TttO1GJiXF8aZY9AYEc=", - "owner": "cachix", - "repo": "pre-commit-hooks.nix", - "rev": "ab608394886fb04b8a5df3cb0bab2598400e3634", - "type": "github" - }, - "original": { - "owner": "cachix", - "repo": "pre-commit-hooks.nix", - "type": "github" - } - }, - "root": { - "inputs": { - "cl-gameboy": "cl-gameboy", - "flake-compat": "flake-compat", - "flake-utils": "flake-utils", - "gb-autotest-roms": "gb-autotest-roms", - "gitignore": "gitignore", - "gomod2nix-src": "gomod2nix-src", - "naersk": "naersk", - "nim-argparse": "nim-argparse", - "nixpkgs": "nixpkgs", - "php-sdl-src": "php-sdl-src", - "pre-commit-hooks": "pre-commit-hooks", - "zig-clap": "zig-clap", - "zig-overlay": "zig-overlay", - "zig-sdl": "zig-sdl" - } - }, - "zig-clap": { - "flake": false, - "locked": { - "lastModified": 1675698794, - "narHash": "sha256-p541p6rmQu6uh/uhd72fM/PWo8UeliCEn59g6WJsktQ=", - "owner": "Hejsil", - "repo": "zig-clap", - "rev": "272d8e2088b2cae037349fb260dc05ec46bba422", - "type": "github" - }, - "original": { - "owner": "Hejsil", - "repo": "zig-clap", - "rev": "272d8e2088b2cae037349fb260dc05ec46bba422", - "type": "github" - } - }, - "zig-overlay": { - "inputs": { - "flake-compat": [ - "flake-compat" - ], - "flake-utils": [ - "flake-utils" - ], - "nixpkgs": [ - "nixpkgs" - ] - }, - "locked": { - "lastModified": 1675746787, - "narHash": "sha256-62E0b7i51LNHBsWbsi2ChG5gCqZfTb8/UaZren6DqXs=", - "owner": "mitchellh", - "repo": "zig-overlay", - "rev": "6a76f5697bcf291d1a7d88b0ef8189c2f5c9b38f", - "type": "github" - }, - "original": { - "owner": "mitchellh", - "repo": "zig-overlay", - "type": "github" - } - }, - "zig-sdl": { - "flake": false, - "locked": { - "lastModified": 1675665690, - "narHash": "sha256-pmDzS6gDhYVqPq5nqoZzQRQ3BkKXebKbmNRYqeMjXLU=", - "owner": "MasterQ32", - "repo": "SDL.zig", - "rev": "6b33f1f4299ec8814e9fb3b206cda37791ced574", - "type": "github" - }, - "original": { - "owner": "MasterQ32", - "repo": "SDL.zig", - "rev": "6b33f1f4299ec8814e9fb3b206cda37791ced574", - "type": "github" - } - } - }, - "root": "root", - "version": 7 -} diff --git a/flake.nix b/flake.nix deleted file mode 100644 index d19d482e..00000000 --- a/flake.nix +++ /dev/null @@ -1,241 +0,0 @@ -{ - description = "rosettaboy nix flake"; - inputs = { - nixpkgs.url = github:NixOS/nixpkgs/nixos-22.11; - flake-utils.url = github:numtide/flake-utils; - flake-compat = { - url = "github:edolstra/flake-compat"; - flake = false; - }; - gitignore = { - url = "github:hercules-ci/gitignore.nix"; - inputs.nixpkgs.follows = "nixpkgs"; - }; - pre-commit-hooks = { - url = "github:cachix/pre-commit-hooks.nix"; - inputs.nixpkgs.follows = "nixpkgs"; - inputs.nixpkgs-stable.follows = "nixpkgs"; - inputs.flake-utils.follows = "flake-utils"; - inputs.flake-compat.follows = "flake-compat"; - inputs.gitignore.follows = "gitignore"; - }; - gomod2nix-src = { - url = "github:nix-community/gomod2nix"; - flake = false; - }; - nim-argparse = { - url = "github:iffy/nim-argparse"; - flake = false; - }; - php-sdl-src = { - url = "github:Ponup/php-sdl"; - flake = false; - }; - naersk = { - url = "github:nix-community/naersk"; - inputs.nixpkgs.follows = "nixpkgs"; - }; - zig-overlay = { - url = "github:mitchellh/zig-overlay"; - inputs.nixpkgs.follows = "nixpkgs"; - inputs.flake-utils.follows = "flake-utils"; - inputs.flake-compat.follows = "flake-compat"; - }; - zig-sdl = { - url = "github:MasterQ32/SDL.zig/6b33f1f4299ec8814e9fb3b206cda37791ced574"; - flake = false; - }; - zig-clap = { - url = "github:Hejsil/zig-clap/272d8e2088b2cae037349fb260dc05ec46bba422"; - flake = false; - }; - gb-autotest-roms = { - url = "github:shish/gb-autotest-roms"; - flake = false; - }; - cl-gameboy = { - url = "github:sjl/cl-gameboy"; - flake = false; - }; - }; - - outputs = { - nixpkgs, - flake-utils, - gitignore, - pre-commit-hooks, - gomod2nix-src, - nim-argparse, - php-sdl-src, - naersk, - zig-overlay, - zig-sdl, - zig-clap, - gb-autotest-roms, - cl-gameboy, - ... - }: flake-utils.lib.eachDefaultSystem (system: let - pkgs = nixpkgs.legacyPackages.${system}; - lib = pkgs.lib; - inherit (builtins) mapAttrs; - inherit (lib) hiPrio filterAttrs; - gomod2nix' = rec { - gomod2nix = pkgs.callPackage "${gomod2nix-src}" { inherit buildGoApplication mkGoEnv; }; - inherit (pkgs.callPackage "${gomod2nix-src}/builder" { inherit gomod2nix; }) buildGoApplication mkGoEnv; - }; - callPackage = pkgs.newScope { - inherit gb-autotest-roms cl-gameboy; - # inherit (gitignore.lib) gitignoreSource; - cleanSource = import ./utils/clean-source.nix { inherit (gitignore.lib) gitignoreFilterWith; inherit (lib) cleanSourceWith; }; - inherit php-sdl-src; - inherit nim-argparse; - inherit (gomod2nix') gomod2nix buildGoApplication; - naersk = pkgs.callPackage naersk {}; - zig = zig-overlay.packages.${system}.master-2023-02-06; - inherit zig-clap zig-sdl; - }; - - utils = callPackage ./utils/derivation.nix {}; - - mkC = {clangSupport ? false, ltoSupport ? false, debugSupport ? false}: - callPackage ./c/derivation.nix { - stdenv = if clangSupport then pkgs.clangStdenv else pkgs.stdenv; - inherit ltoSupport debugSupport; - }; - - mkCpp = {ltoSupport ? false, debugSupport ? false, clangSupport ? false}: - callPackage ./cpp/derivation.nix { - stdenv = if clangSupport then pkgs.clangStdenv else pkgs.stdenv; - inherit ltoSupport debugSupport; - }; - - mkGo = {...}: - callPackage ./go/derivation.nix { - }; - - mkNim = {debugSupport ? false, speedSupport ? false}: - callPackage ./nim/derivation.nix { - inherit debugSupport speedSupport; - inherit (pkgs.llvmPackages_14) bintools; - }; - - mkPhp = {opcacheSupport ? false}: - callPackage ./php/derivation.nix { - inherit opcacheSupport; - }; - - mkPxd = {}: - callPackage ./pxd/derivation.nix {}; - - mkPy = {mypycSupport ? false}: - callPackage ./py/derivation.nix { - inherit mypycSupport; - }; - - mkRs = {ltoSupport ? false, debugSupport ? false}: - callPackage ./rs/derivation.nix { - inherit ltoSupport debugSupport; - }; - - mkZig = {safeSupport ? false, fastSupport ? false}: - callPackage ./zig/derivation.nix { - inherit safeSupport fastSupport; - }; - - pre-commit-check = pre-commit-hooks.lib.${system}.run { - src = ./.; - hooks = { - actionlint.enable = true; - #deadnix.enable = true; - shellcheck.enable = true; - }; - }; - - in rec { - packages = rec { - inherit utils; - - c-debug = mkC { debugSupport = true; }; - c-lto = mkC { ltoSupport = true; }; - c-release = mkC { }; - c-clang-debug = mkC { debugSupport = true; clangSupport = true; }; - c-clang-lto = mkC { ltoSupport = true; clangSupport = true; }; - c-clang-release = mkC { clangSupport = true; }; - c = hiPrio c-release; - - cpp-release = mkCpp {}; - cpp-debug = mkCpp { debugSupport = true; }; - cpp-lto = mkCpp { ltoSupport = true; }; - cpp-clang-release = mkCpp { clangSupport = true; }; - cpp-clang-debug = mkCpp { debugSupport = true; clangSupport = true; }; - cpp-clang-lto = mkCpp { ltoSupport = true; clangSupport = true; }; - cpp = hiPrio cpp-release; - - go-release = mkGo {}; - go = hiPrio go-release; - - nim-release = mkNim {}; - nim-debug = mkNim { debugSupport = true; }; - nim-speed = mkNim { speedSupport = true; }; - nim = hiPrio nim-release; - - php-release = mkPhp {}; - php-opcache = mkPhp { opcacheSupport = true; }; - php = hiPrio php-release; - - py-release = mkPy {}; - py-mypyc = mkPy { mypycSupport = true; }; - py = hiPrio py-release; - - pxd-release = mkPxd {}; - pxd = hiPrio pxd-release; - - rs-debug = mkRs { debugSupport = true; }; - rs-release = mkRs { }; - rs-lto = mkRs { ltoSupport = true; }; - rs = hiPrio rs-release; - - zig-fast = mkZig { fastSupport = true; }; - zig-safe = mkZig { safeSupport = true; }; - zig = hiPrio zig-fast; - - # I don't think we can join all of them because they collide - default = pkgs.symlinkJoin { - name = "rosettaboy"; - paths = [ c cpp go nim php pxd py rs zig ]; - # if we use this without adding build tags to the executable, - # it'll build all variants but not symlink them - # paths = builtins.attrValues (filterAttrs (n: v: n != "default") packages); - }; - }; - - checks = let - # zig-safe is too slow - skip - packagesToCheck = filterAttrs (n: p: p.meta ? mainProgram && n != "zig-safe") packages; - in { - inherit pre-commit-check; - } // mapAttrs (_: utils.mkBlargg) packagesToCheck; - - devShells = let - shellHook = '' - export GB_DEFAULT_AUTOTEST_ROM_DIR=${gb-autotest-roms} - export GB_DEFAULT_BENCH_ROM=${cl-gameboy}/roms/opus5.gb - ''; - langDevShells = mapAttrs (name: package: pkgs.mkShell { - inputsFrom = [ package ]; - buildInputs = package.devTools or []; - inherit shellHook; - }) packages; - in langDevShells // { - default = pkgs.mkShell { - inputsFrom = builtins.attrValues langDevShells; - inherit (pre-commit-check) shellHook; - }; - # something wrong with using it in `inputsFrom` - py = pkgs.mkShell { - buildInputs = packages.py.devTools; - inherit shellHook; - }; - }; - }); -} diff --git a/go/.envrc b/go/.envrc deleted file mode 120000 index 8be2b97d..00000000 --- a/go/.envrc +++ /dev/null @@ -1 +0,0 @@ -../utils/.envrc \ No newline at end of file diff --git a/go/derivation.nix b/go/derivation.nix deleted file mode 100644 index 9d6d0bd2..00000000 --- a/go/derivation.nix +++ /dev/null @@ -1,30 +0,0 @@ -{ - buildGoApplication, - cleanSource, - pkg-config, - SDL2, - gomod2nix -}: - -buildGoApplication rec { - name = "rosettaboy-go"; - src = cleanSource { - inherit name; - src = ./.; - }; - modules = ./gomod2nix.toml; - - passthru.devTools = [ gomod2nix ]; - - buildInputs = [ SDL2 ]; - nativeBuildInputs = [ pkg-config ]; - - postInstall = '' - mv $out/bin/src $out/bin/rosettaboy-go - ''; - - meta = { - description = name; - mainProgram = name; - }; -} diff --git a/go/shell.nix b/go/shell.nix deleted file mode 100644 index eb19cd41..00000000 --- a/go/shell.nix +++ /dev/null @@ -1,8 +0,0 @@ -{ pkgs ? import {} } : pkgs.mkShell { - buildInputs = with pkgs; [ - go - SDL2 - - pkg-config - ]; -} diff --git a/nim/.envrc b/nim/.envrc deleted file mode 120000 index 8be2b97d..00000000 --- a/nim/.envrc +++ /dev/null @@ -1 +0,0 @@ -../utils/.envrc \ No newline at end of file diff --git a/nim/derivation.nix b/nim/derivation.nix deleted file mode 100644 index 54e94f11..00000000 --- a/nim/derivation.nix +++ /dev/null @@ -1,64 +0,0 @@ -{ - lib, - stdenvNoCC, - cleanSource, - llvmPackages_14, - nimPackages, - nim-argparse, - git, - cacert, - bintools, - debugSupport ? false, - speedSupport ? false -}: - -let - argparse = nimPackages.buildNimPackage rec { - pname = "argparse"; - version = "master"; - src = nim-argparse; - }; - - # Upstream `nimPackages.sdl2` is marked broken on macOS but it actually works - # fine: - sdl2 = nimPackages.sdl2.overrideAttrs (o: { - meta = o.meta // { - platforms = o.meta.platforms ++ lib.platforms.darwin; - }; - }); -in - -nimPackages.buildNimPackage rec { - name = "rosettaboy-nim"; - src = cleanSource { - inherit name; - src = ./.; - }; - - passthru = { - devTools = [ nimPackages.nim git cacert ]; - }; - - nimBinOnly = true; - - nimFlags = [] - ++ lib.optional debugSupport "-d:debug" - ++ lib.optional (!debugSupport) "-d:release" - ++ lib.optional (!speedSupport) "-d:nimDebugDlOpen" - ++ lib.optionals speedSupport [ "-d:danger" "--opt:speed" "-d:lto" "--mm:arc" "--panics:on" ] - ; - - buildInputs = [ argparse sdl2 ]; - - # Wants `lld` on macOS: - nativeBuildInputs = lib.optional stdenvNoCC.isDarwin llvmPackages_14.bintools; - - postInstall = '' - mv $out/bin/rosettaboy $out/bin/rosettaboy-nim - ''; - - meta = { - description = name; - mainProgram = name; - }; -} diff --git a/nim/shell.nix b/nim/shell.nix deleted file mode 100644 index 56a0717e..00000000 --- a/nim/shell.nix +++ /dev/null @@ -1,9 +0,0 @@ -{ pkgs ? import {} } : pkgs.mkShell { - buildInputs = with pkgs; [ - nim - SDL2 - pkgs.llvmPackages_15.bintools - - git cacert - ]; -} diff --git a/php/.envrc b/php/.envrc deleted file mode 120000 index 8be2b97d..00000000 --- a/php/.envrc +++ /dev/null @@ -1 +0,0 @@ -../utils/.envrc \ No newline at end of file diff --git a/php/derivation.nix b/php/derivation.nix deleted file mode 100644 index 87ed51e6..00000000 --- a/php/derivation.nix +++ /dev/null @@ -1,74 +0,0 @@ -{ - stdenv, - lib, - makeWrapper, - php, - php-sdl-src, - SDL2, - cleanSource, - opcacheSupport ? false -}@args: - -let - sdl = php.buildPecl { - pname = "sdl"; - version = "master"; - src = php-sdl-src; - buildInputs = [ SDL2 ]; - }; - - php = args.php.buildEnv { - extensions = ({ enabled, all }: enabled ++ [ sdl ] ++ lib.optional opcacheSupport all.opcache); - extraConfig = lib.optionalString opcacheSupport '' - opcache.enable_cli=1 - opcache.jit_buffer_size=100M - ''; - }; -in - -stdenv.mkDerivation rec { - name = "rosettaboy-php"; - - src = cleanSource { - inherit name; - src = ./.; - extraRules = '' - .phpstan.neon - .php-cs-fixer.dist.php - ''; - }; - - passthru = { - inherit php; - devTools = [ - sdl - php - php.packages.composer - php.packages.php-cs-fixer - ]; - }; - - buildInputs = [ php ]; - nativeBuildInputs = [ makeWrapper ]; - - installPhase = '' - runHook preInstall - - mkdir -p $out/bin $out/libexec/$name - cp src/* $out/libexec/$name - - # Note: we have to invoke the interpreter explicitly instead of letting the - # shebang on `main.php` take care of it for us because the interpreter is - # actually a shell script (because of the PHP extensions we're adding) which - # is problematic on macOS: https://stackoverflow.com/a/67101108 - makeWrapper ${lib.getBin php}/bin/php $out/bin/$name \ - --add-flags $out/libexec/$name/main.php - - runHook postInstall - ''; - - meta = { - description = name; - mainProgram = name; - }; -} diff --git a/php/shell.nix b/php/shell.nix deleted file mode 100644 index 32efcbc8..00000000 --- a/php/shell.nix +++ /dev/null @@ -1,25 +0,0 @@ -{ pkgs ? import { } }: - -let - sdl = pkgs.php81.buildPecl { - pname = "sdl"; - version = "655e40"; - src = pkgs.fetchFromGitHub { - owner = "Ponup"; - repo = "php-sdl"; - rev = "655e403b8a9681c418702a74833c68c1a4ae1bd5"; - sha256 = "1kfw1s4ip8y0zfyl61ipw3ql9c4d9f0bwwg398kfl9p8k3vc857h"; - }; - buildInputs = with pkgs; [ SDL2 ]; - }; - - php = pkgs.php81.withExtensions ({ enabled, all }: enabled ++ [ sdl ]); -in - -pkgs.mkShell { - buildInputs = [ - php - php.packages.composer - php.packages.php-cs-fixer - ]; -} diff --git a/pxd/.envrc b/pxd/.envrc deleted file mode 120000 index 8be2b97d..00000000 --- a/pxd/.envrc +++ /dev/null @@ -1 +0,0 @@ -../utils/.envrc \ No newline at end of file diff --git a/pxd/derivation.nix b/pxd/derivation.nix deleted file mode 100644 index 2a043a9f..00000000 --- a/pxd/derivation.nix +++ /dev/null @@ -1,35 +0,0 @@ -{ - python310, - cleanSource -}: - -let - python = python310; - pythonPackages = python.pkgs; - - runtimeDeps = with pythonPackages; [ setuptools pysdl2 cython_3 ]; - devDeps = with pythonPackages; [ black ]; -in - -pythonPackages.buildPythonApplication rec { - name = "rosettaboy-pxd"; - src = cleanSource { - inherit name; - src = ./.; - extraRules = '' - py_env.sh - ''; - }; - - passthru.python = python.withPackages (p: runtimeDeps ++ devDeps); - passthru.devTools = [ python ]; - - propagatedBuildInputs = runtimeDeps; - - dontUseSetuptoolsCheck = true; - - meta = { - description = name; - mainProgram = name; - }; -} diff --git a/pxd/shell.nix b/pxd/shell.nix deleted file mode 100644 index bffd8219..00000000 --- a/pxd/shell.nix +++ /dev/null @@ -1,13 +0,0 @@ -{ pkgs ? import {} } : pkgs.mkShell { - buildInputs = with pkgs; [ - (python310.withPackages (pypkgs: with pypkgs; [ - pysdl2 - cython_3 - setuptools - - black - ])).out - - SDL2 - ]; -} diff --git a/py/.envrc b/py/.envrc deleted file mode 120000 index 8be2b97d..00000000 --- a/py/.envrc +++ /dev/null @@ -1 +0,0 @@ -../utils/.envrc \ No newline at end of file diff --git a/py/derivation.nix b/py/derivation.nix deleted file mode 100644 index f1ca14f0..00000000 --- a/py/derivation.nix +++ /dev/null @@ -1,57 +0,0 @@ -{ - lib, - python311, - cleanSource, - fetchFromGitHub, - mypycSupport ? false -}: - -let - python = python311; - pythonPackages = python.pkgs; - - # We use `match` which is only supported in `mypy` v1 and newer: - # https://github.com/python/mypy/commit/d5e96e381f72ad3fafaae8707b688b3da320587d - # - # This hasn't made it's way into `nixpkgs` yet so we override for now: - # (!!! remove once nixpkgs has 1.0.0+) - mypy' = pythonPackages.mypy.overridePythonAttrs (old: rec { - version = "1.0.0"; - buildInputs = (old.buildInputs or []) ++ (with pythonPackages; [ psutil types-psutil ]); - src = fetchFromGitHub { - owner = "python"; - repo = "mypy"; - rev = "refs/tags/v${version}"; - hash = "sha256-/E2O6J+o0OiY2v/ogatygaB07D/Z5ZQ6mB0daEQqo+4="; - }; - }); - - runtimeDeps = with pythonPackages; [ setuptools pysdl2 ]; - devDeps = with pythonPackages; [ mypy' black ]; -in - -pythonPackages.buildPythonApplication rec { - name = "rosettaboy-py"; - src = cleanSource { - inherit name; - src = ./.; - extraRules = '' - py_env.sh - ''; - }; - - nativeBuildInputs = lib.optional mypycSupport mypy'; - - passthru.python = python.withPackages (p: runtimeDeps ++ devDeps); - passthru.devTools = [ python ]; - - propagatedBuildInputs = runtimeDeps; - - ROSETTABOY_USE_MYPYC = mypycSupport; - dontUseSetuptoolsCheck = true; - - meta = { - description = name; - mainProgram = name; - }; -} diff --git a/py/shell.nix b/py/shell.nix deleted file mode 100644 index a32c76dc..00000000 --- a/py/shell.nix +++ /dev/null @@ -1,11 +0,0 @@ -{ pkgs ? import {} } : pkgs.mkShell { - buildInputs = with pkgs; [ - (python310.withPackages (pypkgs: with pypkgs; [ - pysdl2 - setuptools - - mypy - black - ])).out - ]; -} diff --git a/rs/.envrc b/rs/.envrc deleted file mode 120000 index 8be2b97d..00000000 --- a/rs/.envrc +++ /dev/null @@ -1 +0,0 @@ -../utils/.envrc \ No newline at end of file diff --git a/rs/derivation.nix b/rs/derivation.nix deleted file mode 100644 index 711cc90e..00000000 --- a/rs/derivation.nix +++ /dev/null @@ -1,43 +0,0 @@ -{ - lib, - stdenv, - naersk, - SDL2, - pkg-config, - libiconv, - rustfmt, - rustc, - cargo, - cleanSource, - ltoSupport ? false, - debugSupport ? false -}: - -let - devTools = [ rustfmt rustc cargo ] ++ lib.optional stdenv.isDarwin [libiconv]; -in - -naersk.buildPackage rec { - name = "rosettaboy-rs"; - src = cleanSource { - inherit name; - src = ./.; - extraRules = '' - test_pgo.sh - ''; - }; - - buildInputs = [ SDL2 ]; - nativeBuildInputs = [ pkg-config ]; - - cargoBuildOptions = input: input ++ (lib.optional ltoSupport ["--profile release-lto"]); - - release = !debugSupport && !ltoSupport; - - passthru = { inherit devTools; }; - - meta = { - description = name; - mainProgram = name; - }; -} diff --git a/rs/shell.nix b/rs/shell.nix deleted file mode 100644 index 48d11157..00000000 --- a/rs/shell.nix +++ /dev/null @@ -1,9 +0,0 @@ -{ pkgs ? import {} } : pkgs.mkShell { - buildInputs = with pkgs; [ - rustc - cargo - SDL2 - - rustfmt - ] ++ (pkgs.lib.optional pkgs.targetPlatform.isMacOS pkgs.libiconv); -} diff --git a/shell.nix b/shell.nix deleted file mode 100644 index 5ec404c2..00000000 --- a/shell.nix +++ /dev/null @@ -1,10 +0,0 @@ -(import - ( - let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in - fetchTarball { - url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; - sha256 = lock.nodes.flake-compat.locked.narHash; - } - ) - { src = ./.; } -).shellNix \ No newline at end of file diff --git a/utils/.envrc b/utils/.envrc deleted file mode 100644 index a8599945..00000000 --- a/utils/.envrc +++ /dev/null @@ -1 +0,0 @@ -use flake ..#$(basename "$(pwd)") diff --git a/utils/clean-source.nix b/utils/clean-source.nix deleted file mode 100644 index 759e023e..00000000 --- a/utils/clean-source.nix +++ /dev/null @@ -1,35 +0,0 @@ -{ - cleanSourceWith, - gitignoreFilterWith -}: - -# see https://github.com/hercules-ci/gitignore.nix/blob/master/docs/gitignoreFilter.md - -let - customerFilter = src: extraRules: - let - # IMPORTANT: use a let binding like this to memoize info about the git directories. - srcIgnored = gitignoreFilterWith { - basePath = src; - extraRules = '' - .envrc - .gitignore - *.nix - build*.sh - format*.sh - '' + extraRules; - }; - in path: type: srcIgnored path type; -in - -{ - name, - src, - extraRules ? "" -}: - -cleanSourceWith { - filter = customerFilter src extraRules; - src = src; - name = name + "-source"; -} diff --git a/utils/derivation.nix b/utils/derivation.nix deleted file mode 100644 index 2610840d..00000000 --- a/utils/derivation.nix +++ /dev/null @@ -1,55 +0,0 @@ -{ - lib, - stdenvNoCC, - runCommand, - makeWrapper, - python3, - wget, - cacert, - parallel, - elfutils, - gb-autotest-roms, - cl-gameboy -}: - -stdenvNoCC.mkDerivation rec { - name = "rosettaboy-utils"; - - src = ./.; - - passthru = { - # we have to make $out or building will fail... - mkBlargg = pkg: runCommand "rosettaboy-checks-blargg-${pkg.name}" {} '' - echo ${pkg.name} - find ${gb-autotest-roms} -name '*.gb' \ - | ${parallel}/bin/parallel --will-cite --line-buffer --keep-order \ - "${lib.getExe pkg} --turbo --silent --headless --frames 200" \ - | tee -a $out - ''; - devTools = [ wget cacert parallel ] - ++ lib.optional (!stdenvNoCC.isDarwin) elfutils; - }; - - dontConfigure = true; - dontBuild = true; - - buildInputs = [ python3 ]; - nativeBuildInputs = [ makeWrapper ]; - - installPhase = '' - mkdir -p $out/bin $out/libexec/rosettaboy-utils/ - cp bench.py blargg.py cpudiff.py $out/libexec/rosettaboy-utils/ - - makeWrapper $out/libexec/rosettaboy-utils/bench.py $out/bin/rosettaboy-bench \ - --set-default "GB_DEFAULT_BENCH_ROM" "${cl-gameboy}/roms/opus5.gb" - - makeWrapper $out/libexec/rosettaboy-utils/cpudiff.py $out/bin/rosettaboy-cpudiff - - makeWrapper $out/libexec/rosettaboy-utils/blargg.py $out/bin/rosettaboy-blargg \ - --set-default "GB_DEFAULT_AUTOTEST_ROM_DIR" "${gb-autotest-roms}" - ''; - - meta = { - description = name; - }; -} diff --git a/utils/get-builds-for-lang.nix b/utils/get-builds-for-lang.nix deleted file mode 100644 index a727b625..00000000 --- a/utils/get-builds-for-lang.nix +++ /dev/null @@ -1,14 +0,0 @@ -{ - lang, - attr ? "checks", - system ? builtins.currentSystem, - prefix ? ".#${attr}.${system}.", - lib ? import -}: - -attrs: lib.pipe attrs.${system} [ - builtins.attrNames - (builtins.filter (x: x == lang || lib.hasPrefix "${lang}-" x)) - (builtins.map (x: "${prefix}${x}")) - (builtins.concatStringsSep "\n") -] diff --git a/utils/shell.nix b/utils/shell.nix deleted file mode 100644 index e8b1b2a5..00000000 --- a/utils/shell.nix +++ /dev/null @@ -1,5 +0,0 @@ -{ pkgs ? import {} } : pkgs.mkShell { - buildInputs = with pkgs; [ - wget cacert - ]; -} diff --git a/utils/shell.sh b/utils/shell.sh index d7411e90..cd040162 100755 --- a/utils/shell.sh +++ b/utils/shell.sh @@ -4,18 +4,4 @@ set -eu cd $(dirname $0) cd .. -if nix develop --help 2>/dev/null 1>/dev/null; then - if [ -n "$*" ]; then - nix develop . --command bash -c "$*" - else - nix develop . - fi -elif command -v nix-shell; then - if [ -n "$*" ]; then - nix-shell --run "$*" - else - nix-shell - fi -else - ./utils/shell-docker "$@" -fi +./utils/shell-docker "$@" diff --git a/zig/.envrc b/zig/.envrc deleted file mode 120000 index 8be2b97d..00000000 --- a/zig/.envrc +++ /dev/null @@ -1 +0,0 @@ -../utils/.envrc \ No newline at end of file diff --git a/zig/derivation.nix b/zig/derivation.nix deleted file mode 100644 index 36d5de61..00000000 --- a/zig/derivation.nix +++ /dev/null @@ -1,104 +0,0 @@ -{ - stdenv, - lib, - darwin, - libiconv, - zig, - pkg-config, - SDL2, - zig-sdl, - zig-clap, - autoPatchelfHook, - cleanSource, - safeSupport ? false, - fastSupport ? false -}: - -let - # `apple_sdk` defaults to `10_12` instead of `11_0` on `x86_64-darwin` but we - # need `CoreHaptics` to successfully link against `SDL2` and `CoreHaptics` is - # not available in `10_12`. - # - # So, we use `11_0`, even on x86_64. - inherit (darwin.apple_sdk_11_0) frameworks; -in - -stdenv.mkDerivation rec { - name = "rosettaboy-zig"; - src = cleanSource { - inherit name; - src = ./.; - extraRules = '' - lib - ''; - }; - - passthru = { - devTools = [ zig ]; - }; - - buildInputs = [ SDL2 ] - ++ lib.optionals stdenv.isDarwin (with frameworks; [ - IOKit GameController CoreAudio AudioToolbox QuartzCore Carbon Metal - Cocoa ForceFeedback CoreHaptics - ]) - ++ lib.optional stdenv.isDarwin libiconv - ; - - nativeBuildInputs = [ zig pkg-config ] - ++ lib.optional (!stdenv.isDarwin) autoPatchelfHook - ; - - dontConfigure = true; - dontBuild = true; - - # Unforunately `zig`'s parsing of `NIX_LDFLAGS` bails when it encounters any - # flags it does not expect. - # https://github.com/ziglang/zig/blob/fe6dcdba1407f00584725318404814571cdbd828/lib/std/zig/system/NativePaths.zig#L79 - # - # When `zig` sees the `-liconv` flag that's in `NIX_LDFLAGS` on macOS, it - # bails, causing it to miss the `-L` path for SDL. - # - # Really, this should be fixed in upstream (zig) but for now we just strip out - # the `-l` flags: - preInstall = lib.optionalString stdenv.isDarwin '' - readonly ORIGINAL_NIX_LDFLAGS=($NIX_LDFLAGS) - - NIX_LDFLAGS="" - for c in "''${ORIGINAL_NIX_LDFLAGS[@]}"; do - # brittle, bad, etc; this presumes `-l...` style args (no space) - if [[ $c =~ ^-l.* ]]; then - echo "dropping link flag: $c" - continue - else - echo "keeping link flag: $c" - NIX_LDFLAGS="$NIX_LDFLAGS $c" - fi - done - - export NIX_LDFLAGS - ''; - - ZIG_FLAGS = [] - ++ lib.optional fastSupport "-Doptimize=ReleaseFast" - ++ lib.optional safeSupport "-Doptimize=ReleaseSafe" - ; - - installPhase = '' - runHook preInstall - - export HOME=$TMPDIR - mkdir -p lib - cp -aR ${zig-sdl}/ lib/sdl - cp -aR ${zig-clap}/ lib/clap - zig build $ZIG_FLAGS --prefix $out install - mv $out/bin/rosettaboy $out/bin/rosettaboy-zig - - runHook postInstall - ''; - - meta = { - description = name; - mainProgram = name; - }; -} diff --git a/zig/shell.nix b/zig/shell.nix deleted file mode 100644 index 0e0fba5c..00000000 --- a/zig/shell.nix +++ /dev/null @@ -1,39 +0,0 @@ -{ - pkgs ? import {}, - # Right. Getting this to work took more effort than all the other shells put - # together. - # Things that didn't work: - # - Directly using the Zig package, pinned to any version. Nix is "known - # as the most up to date distribution", but this project uses a *nightly* - # build of Zig, so that's not there. - # - Building Zig from source. The latest version of Zig itself uses - # LLVM+Clang 15, which is brand new and also not yet in Nix repos. There - # was a brief window in late August 2022 where Zig could be built with - # LLVM 14, but the most recent commit that works with that doesn't work - # with this project. - # - Building LLVM 15 from source. I didn't even try this given the massive - # disk space and build time requirements. - zig-overlay ? pkgs.callPackage ((pkgs.fetchFromGitHub { - # Officially this information is published without past history at: - # https://ziglang.org/download/index.json - # 1. Update this URL with the latest commit. - # 2. Try to use it with `nix-shell`. It will error because the hash is - # wrong, and the right hash will be reported. Put the new hash here - # too. (I swear this is what the tutorials say to do.) - # 3. Change the "YYYY-MM-DD" key below to the new version, as long as - # it's in the `sources.json`. - owner = "mitchellh"; - repo = "zig-overlay"; - rev = "6a76f5697bcf291d1a7d88b0ef8189c2f5c9b38f"; - hash = "sha256-62E0b7i51LNHBsWbsi2ChG5gCqZfTb8/UaZren6DqXs="; - }) + "/default.nix") {}, - zig ? zig-overlay.master-2023-02-06 -}: - -pkgs.mkShell { - buildInputs = with pkgs; [ - pkg-config - SDL2 - zig - ]; -} From c7ddd64a82b167480022e4db3570fe3c737efb5d Mon Sep 17 00:00:00 2001 From: Shish Date: Fri, 29 Sep 2023 01:07:53 +0100 Subject: [PATCH 2/2] [all] Re-add nix It would be good if this could be made fast and simple Original commit changeset: effc1576a1fc --- .envrc | 1 + .github/workflows/common.yml | 103 +++++++++++++ README.md | 6 +- c/.envrc | 1 + c/derivation.nix | 44 ++++++ cpp/.envrc | 1 + cpp/derivation.nix | 41 +++++ cpp/shell.nix | 10 ++ default.nix | 10 ++ flake.lock | 283 ++++++++++++++++++++++++++++++++++ flake.nix | 241 +++++++++++++++++++++++++++++ go/.envrc | 1 + go/derivation.nix | 30 ++++ go/shell.nix | 8 + nim/.envrc | 1 + nim/derivation.nix | 64 ++++++++ nim/shell.nix | 9 ++ php/.envrc | 1 + php/derivation.nix | 74 +++++++++ php/shell.nix | 25 +++ pxd/.envrc | 1 + pxd/derivation.nix | 35 +++++ pxd/shell.nix | 13 ++ py/.envrc | 1 + py/derivation.nix | 57 +++++++ py/shell.nix | 11 ++ rs/.envrc | 1 + rs/derivation.nix | 43 ++++++ rs/shell.nix | 9 ++ shell.nix | 10 ++ utils/.envrc | 1 + utils/clean-source.nix | 35 +++++ utils/derivation.nix | 55 +++++++ utils/get-builds-for-lang.nix | 14 ++ utils/shell.nix | 5 + utils/shell.sh | 16 +- zig/.envrc | 1 + zig/derivation.nix | 104 +++++++++++++ zig/shell.nix | 39 +++++ 39 files changed, 1403 insertions(+), 2 deletions(-) create mode 100644 .envrc create mode 120000 c/.envrc create mode 100644 c/derivation.nix create mode 120000 cpp/.envrc create mode 100644 cpp/derivation.nix create mode 100644 cpp/shell.nix create mode 100644 default.nix create mode 100644 flake.lock create mode 100644 flake.nix create mode 120000 go/.envrc create mode 100644 go/derivation.nix create mode 100644 go/shell.nix create mode 120000 nim/.envrc create mode 100644 nim/derivation.nix create mode 100644 nim/shell.nix create mode 120000 php/.envrc create mode 100644 php/derivation.nix create mode 100644 php/shell.nix create mode 120000 pxd/.envrc create mode 100644 pxd/derivation.nix create mode 100644 pxd/shell.nix create mode 120000 py/.envrc create mode 100644 py/derivation.nix create mode 100644 py/shell.nix create mode 120000 rs/.envrc create mode 100644 rs/derivation.nix create mode 100644 rs/shell.nix create mode 100644 shell.nix create mode 100644 utils/.envrc create mode 100644 utils/clean-source.nix create mode 100644 utils/derivation.nix create mode 100644 utils/get-builds-for-lang.nix create mode 100644 utils/shell.nix create mode 120000 zig/.envrc create mode 100644 zig/derivation.nix create mode 100644 zig/shell.nix diff --git a/.envrc b/.envrc new file mode 100644 index 00000000..3550a30f --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.github/workflows/common.yml b/.github/workflows/common.yml index 41ab7ffd..240941d9 100644 --- a/.github/workflows/common.yml +++ b/.github/workflows/common.yml @@ -106,3 +106,106 @@ jobs: - name: Benchmark Test shell: bash run: ./all.py bench ${{ inputs.workdir }} --default --frames 10 + + nix: + name: Nix + runs-on: ubuntu-22.04 + if: ${{ !contains(github.event.head_commit.message, '[skip nix]') }} + outputs: + flake_status: ${{ steps.early.outputs.flake_status }} + shell_status: ${{ steps.early.outputs.shell_status }} + steps: + - name: Checkout + uses: actions/checkout@v3 + - id: early + name: Check if nix files exist + shell: bash + working-directory: ${{ inputs.workdir }} + run: | + flake_status="skip" + if [ -f "derivation.nix" ]; then + flake_status="found" + fi + echo "flake_status=${flake_status}" >> "${GITHUB_OUTPUT}" + + shell_status="skip" + if [ -f "shell.nix" ]; then + shell_status="found" + fi + echo "shell_status=${shell_status}" >> "${GITHUB_OUTPUT}" + + nix-flake: + name: Nix Flake + runs-on: ubuntu-22.04 + needs: nix + if: ${{ !contains(github.event.head_commit.message, '[skip nix flake]') && needs.nix.outputs.flake_status == 'found' }} + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + submodules: recursive + - name: Set Up Build Cache + uses: actions/cache@v3 + id: nix-cache + with: + path: /tmp/nix-store.nar + key: ${{ runner.os }}-${{ inputs.workdir }}-nix-store.nar-${{ hashFiles('flake.nix', 'flake.lock', format('{0}/derivation.nix', inputs.workdir)) }} + restore-keys: ${{ runner.os }}-${{ inputs.workdir }}-nix-store.nar- + - name: Install Deps + uses: cachix/install-nix-action@v15 + with: + nix_path: nixpkgs=channel:nixos-22.11 + # see: https://github.com/cachix/install-nix-action/issues/56#issuecomment-1300421537 + - name: "Import Nix Store Cache" + if: "steps.nix-cache.outputs.cache-hit == 'true'" + run: "nix-store --import < /tmp/nix-store.nar" + - name: Flake Build + shell: bash + run: | + nix eval .#packages --impure --raw \ + --apply '(import ./utils/get-builds-for-lang.nix { lang = "${{ inputs.workdir }}"; attr = "packages"; })' \ + | xargs nix build -L + - name: Flake Run + shell: bash + run: | + nix eval .#packages --impure --raw \ + --apply '(import ./utils/get-builds-for-lang.nix { lang = "${{ inputs.workdir }}"; prefix = ".#"; })' \ + | xargs -I{} nix run {} -- --help + - name: Flake Shell + shell: bash + working-directory: ${{ inputs.workdir }} + run: nix develop ..#${{ inputs.workdir }} --command bash -c './build.sh && ./rosettaboy-release --help' + - name: Flake Checks + shell: bash + run: | + nix eval .#checks --impure --raw \ + --apply '(import ./utils/get-builds-for-lang.nix { lang = "${{ inputs.workdir }}"; attr = "checks"; })' \ + | xargs nix build -L + - name: "Export Nix Store Cache" + shell: bash + # we could add devShells here but it might fill the cache fast... + run: | + nix eval .#packages --impure --raw \ + --apply '(import ./utils/get-builds-for-lang.nix { lang = "${{ inputs.workdir }}"; attr = "packages"; })' \ + | xargs -I{} bash -c "nix eval --raw {}; echo" \ + | xargs nix-store -qR \ + | xargs nix-store --export > /tmp/nix-store.nar + + nix-shell: + name: Nix Shell + runs-on: ubuntu-22.04 + needs: nix + if: ${{ !contains(github.event.head_commit.message, '[skip nix flake]') && needs.nix.outputs.shell_status == 'found' }} + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + submodules: recursive + - name: Install Nix + uses: cachix/install-nix-action@v15 + with: + nix_path: nixpkgs=channel:nixos-22.11 + - name: Run Shell + shell: bash + working-directory: ${{ inputs.workdir }} + run: nix-shell --pure --run './build.sh && ./rosettaboy-release --help' diff --git a/README.md b/README.md index 0902ca4c..c9e6685a 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,11 @@ that the user has the standard language dev kits installed (eg we assume anyone who wants to work on the Rust version will have Cargo installed; anyone who wants to work on Python will have virtualenv + pip; etc) -If you have Docker available, you can run `./utils/shell.sh` to create and run an environment with all necessary dev tools pre-installed. — `./build.sh && ./rosettaboy-release --headless --silent` should be able to pass tests for all languages. +If you have either Nix or Docker available, you can run `./utils/shell.sh` to create and run an environment with all necessary dev tools pre-installed. — `./build.sh && ./rosettaboy-release --headless --silent` should be able to pass tests for all languages. + +If you prefer Docker, you can use `./utils/shell-docker.sh` instead. + +If you prefer Nix, you can manually run `nix develop` or `nix-shell` instead. When run with an implementation as an argument, e.g. `nix develop .#py`, it will only provide what is needed for that language, and when run in the project root it will provide everything needed for all languages. Alternatively, there is also an integration with [nix-direnv](https://github.com/nix-community/nix-direnv). Benchmarks diff --git a/c/.envrc b/c/.envrc new file mode 120000 index 00000000..8be2b97d --- /dev/null +++ b/c/.envrc @@ -0,0 +1 @@ +../utils/.envrc \ No newline at end of file diff --git a/c/derivation.nix b/c/derivation.nix new file mode 100644 index 00000000..a0f48b61 --- /dev/null +++ b/c/derivation.nix @@ -0,0 +1,44 @@ +{ + lib, + stdenv, + cmake, + SDL2, + pkg-config, + cleanSource, + clang-tools ? null, + ltoSupport ? false, + debugSupport ? false +}: + +let + devTools = [ clang-tools ]; +in + +stdenv.mkDerivation rec { + name = "rosettaboy-c"; + src = cleanSource { + inherit name; + src = ./.; + extraRules = '' + .clang-format + ''; + }; + + passthru = { inherit devTools; }; + + enableParallelBuilding = true; + + buildInputs = [ SDL2 ]; + nativeBuildInputs = [ cmake pkg-config ]; + + cmakeFlags = [ ] + ++ lib.optional debugSupport "-DCMAKE_BUILD_TYPE=Debug" + ++ lib.optional (!debugSupport) "-DCMAKE_BUILD_TYPE=Release" + ++ lib.optional ltoSupport "-DENABLE_LTO=On" + ; + + meta = { + description = name; + mainProgram = name; + }; +} diff --git a/cpp/.envrc b/cpp/.envrc new file mode 120000 index 00000000..8be2b97d --- /dev/null +++ b/cpp/.envrc @@ -0,0 +1 @@ +../utils/.envrc \ No newline at end of file diff --git a/cpp/derivation.nix b/cpp/derivation.nix new file mode 100644 index 00000000..85a08d01 --- /dev/null +++ b/cpp/derivation.nix @@ -0,0 +1,41 @@ +{ + lib, + stdenv, + cmake, + SDL2, + fmt_8, + pkg-config, + cleanSource, + clang-format ? null, + ltoSupport ? false, + debugSupport ? false, +}: + +stdenv.mkDerivation rec { + name = "rosettaboy-cpp"; + src = cleanSource { + inherit name; + src = ./.; + extraRules = '' + .clang-format + ''; + }; + + buildInputs = [ SDL2 fmt_8 ]; + nativeBuildInputs = [ cmake pkg-config ]; + + passthru = { + devTools = [ clang-format ]; + }; + + cmakeFlags = [ ] + ++ lib.optional debugSupport "-DCMAKE_BUILD_TYPE=Debug" + ++ lib.optional (!debugSupport) "-DCMAKE_BUILD_TYPE=Release" + ++ lib.optional ltoSupport "-DENABLE_LTO=On" + ; + + meta = { + description = name; + mainProgram = name; + }; +} diff --git a/cpp/shell.nix b/cpp/shell.nix new file mode 100644 index 00000000..2740b043 --- /dev/null +++ b/cpp/shell.nix @@ -0,0 +1,10 @@ +{ pkgs ? import {} } : pkgs.mkShell { + buildInputs = with pkgs; [ + cmake + gnumake + SDL2 + fmt_8 + + clang-tools # Massive, and only used for format.sh. But I think it may share some dependencies with Nim, Zig (11/generic, I think), Rust. Painful because the other requirements are so small. + ]; +} diff --git a/default.nix b/default.nix new file mode 100644 index 00000000..e6195e86 --- /dev/null +++ b/default.nix @@ -0,0 +1,10 @@ +(import + ( + let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in + fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; + sha256 = lock.nodes.flake-compat.locked.narHash; + } + ) + { src = ./.; } +).defaultNix \ No newline at end of file diff --git a/flake.lock b/flake.lock new file mode 100644 index 00000000..70cb8549 --- /dev/null +++ b/flake.lock @@ -0,0 +1,283 @@ +{ + "nodes": { + "cl-gameboy": { + "flake": false, + "locked": { + "lastModified": 1479058009, + "narHash": "sha256-qvT5wbRCrngd5tUPaEWqEUj53mGwjz87+eibn3qWd3k=", + "owner": "sjl", + "repo": "cl-gameboy", + "rev": "cce73054d4bfde332e31a2938f4e0630c162cf6b", + "type": "github" + }, + "original": { + "owner": "sjl", + "repo": "cl-gameboy", + "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-utils": { + "locked": { + "lastModified": 1667395993, + "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "gb-autotest-roms": { + "flake": false, + "locked": { + "lastModified": 1658898280, + "narHash": "sha256-i5TkoWJ9f+OZYwr0oyaFigCkfPQkRBReYqBfXxcyXmM=", + "owner": "shish", + "repo": "gb-autotest-roms", + "rev": "367b7dc51aed4b84eea8097d7426fe2e3cd2f459", + "type": "github" + }, + "original": { + "owner": "shish", + "repo": "gb-autotest-roms", + "type": "github" + } + }, + "gitignore": { + "inputs": { + "nixpkgs": [ + "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" + } + }, + "gomod2nix-src": { + "flake": false, + "locked": { + "lastModified": 1662501203, + "narHash": "sha256-4BKeqCX2zwgBiTdlc2DjGQ0CttKm0vSw0r/bdFdM/PQ=", + "owner": "nix-community", + "repo": "gomod2nix", + "rev": "89cd0675b96775aa3ee86e7c0cf5bc238dd27976", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "gomod2nix", + "type": "github" + } + }, + "naersk": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1671096816, + "narHash": "sha256-ezQCsNgmpUHdZANDCILm3RvtO1xH8uujk/+EqNvzIOg=", + "owner": "nix-community", + "repo": "naersk", + "rev": "d998160d6a076cfe8f9741e56aeec7e267e3e114", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "naersk", + "type": "github" + } + }, + "nim-argparse": { + "flake": false, + "locked": { + "lastModified": 1669660759, + "narHash": "sha256-rtA0NJbA7aQglw/4U/fhX8GTlGf0eYoXfuzlJsA38Q8=", + "owner": "iffy", + "repo": "nim-argparse", + "rev": "98c7c99bfbcaae750ac515a6fd603f85ed68668f", + "type": "github" + }, + "original": { + "owner": "iffy", + "repo": "nim-argparse", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1675757258, + "narHash": "sha256-pIRer8vdsoherlRKpzfnHbMZ5TsAcvRlXHCIaHkIUbg=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "af96094e9b8eb162d70a84fa3b39f4b7a8b264d2", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-22.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "php-sdl-src": { + "flake": false, + "locked": { + "lastModified": 1672500911, + "narHash": "sha256-8BTE9pjoJuomSuNxvoBLjbBE8eA3BkO9+8CjG4kO3M0=", + "owner": "Ponup", + "repo": "php-sdl", + "rev": "655e403b8a9681c418702a74833c68c1a4ae1bd5", + "type": "github" + }, + "original": { + "owner": "Ponup", + "repo": "php-sdl", + "type": "github" + } + }, + "pre-commit-hooks": { + "inputs": { + "flake-compat": [ + "flake-compat" + ], + "flake-utils": [ + "flake-utils" + ], + "gitignore": [ + "gitignore" + ], + "nixpkgs": [ + "nixpkgs" + ], + "nixpkgs-stable": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1675688762, + "narHash": "sha256-oit/SxMk0B380ASuztBGQLe8TttO1GJiXF8aZY9AYEc=", + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "rev": "ab608394886fb04b8a5df3cb0bab2598400e3634", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "type": "github" + } + }, + "root": { + "inputs": { + "cl-gameboy": "cl-gameboy", + "flake-compat": "flake-compat", + "flake-utils": "flake-utils", + "gb-autotest-roms": "gb-autotest-roms", + "gitignore": "gitignore", + "gomod2nix-src": "gomod2nix-src", + "naersk": "naersk", + "nim-argparse": "nim-argparse", + "nixpkgs": "nixpkgs", + "php-sdl-src": "php-sdl-src", + "pre-commit-hooks": "pre-commit-hooks", + "zig-clap": "zig-clap", + "zig-overlay": "zig-overlay", + "zig-sdl": "zig-sdl" + } + }, + "zig-clap": { + "flake": false, + "locked": { + "lastModified": 1675698794, + "narHash": "sha256-p541p6rmQu6uh/uhd72fM/PWo8UeliCEn59g6WJsktQ=", + "owner": "Hejsil", + "repo": "zig-clap", + "rev": "272d8e2088b2cae037349fb260dc05ec46bba422", + "type": "github" + }, + "original": { + "owner": "Hejsil", + "repo": "zig-clap", + "rev": "272d8e2088b2cae037349fb260dc05ec46bba422", + "type": "github" + } + }, + "zig-overlay": { + "inputs": { + "flake-compat": [ + "flake-compat" + ], + "flake-utils": [ + "flake-utils" + ], + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1675746787, + "narHash": "sha256-62E0b7i51LNHBsWbsi2ChG5gCqZfTb8/UaZren6DqXs=", + "owner": "mitchellh", + "repo": "zig-overlay", + "rev": "6a76f5697bcf291d1a7d88b0ef8189c2f5c9b38f", + "type": "github" + }, + "original": { + "owner": "mitchellh", + "repo": "zig-overlay", + "type": "github" + } + }, + "zig-sdl": { + "flake": false, + "locked": { + "lastModified": 1675665690, + "narHash": "sha256-pmDzS6gDhYVqPq5nqoZzQRQ3BkKXebKbmNRYqeMjXLU=", + "owner": "MasterQ32", + "repo": "SDL.zig", + "rev": "6b33f1f4299ec8814e9fb3b206cda37791ced574", + "type": "github" + }, + "original": { + "owner": "MasterQ32", + "repo": "SDL.zig", + "rev": "6b33f1f4299ec8814e9fb3b206cda37791ced574", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 00000000..d19d482e --- /dev/null +++ b/flake.nix @@ -0,0 +1,241 @@ +{ + description = "rosettaboy nix flake"; + inputs = { + nixpkgs.url = github:NixOS/nixpkgs/nixos-22.11; + flake-utils.url = github:numtide/flake-utils; + flake-compat = { + url = "github:edolstra/flake-compat"; + flake = false; + }; + gitignore = { + url = "github:hercules-ci/gitignore.nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + pre-commit-hooks = { + url = "github:cachix/pre-commit-hooks.nix"; + inputs.nixpkgs.follows = "nixpkgs"; + inputs.nixpkgs-stable.follows = "nixpkgs"; + inputs.flake-utils.follows = "flake-utils"; + inputs.flake-compat.follows = "flake-compat"; + inputs.gitignore.follows = "gitignore"; + }; + gomod2nix-src = { + url = "github:nix-community/gomod2nix"; + flake = false; + }; + nim-argparse = { + url = "github:iffy/nim-argparse"; + flake = false; + }; + php-sdl-src = { + url = "github:Ponup/php-sdl"; + flake = false; + }; + naersk = { + url = "github:nix-community/naersk"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + zig-overlay = { + url = "github:mitchellh/zig-overlay"; + inputs.nixpkgs.follows = "nixpkgs"; + inputs.flake-utils.follows = "flake-utils"; + inputs.flake-compat.follows = "flake-compat"; + }; + zig-sdl = { + url = "github:MasterQ32/SDL.zig/6b33f1f4299ec8814e9fb3b206cda37791ced574"; + flake = false; + }; + zig-clap = { + url = "github:Hejsil/zig-clap/272d8e2088b2cae037349fb260dc05ec46bba422"; + flake = false; + }; + gb-autotest-roms = { + url = "github:shish/gb-autotest-roms"; + flake = false; + }; + cl-gameboy = { + url = "github:sjl/cl-gameboy"; + flake = false; + }; + }; + + outputs = { + nixpkgs, + flake-utils, + gitignore, + pre-commit-hooks, + gomod2nix-src, + nim-argparse, + php-sdl-src, + naersk, + zig-overlay, + zig-sdl, + zig-clap, + gb-autotest-roms, + cl-gameboy, + ... + }: flake-utils.lib.eachDefaultSystem (system: let + pkgs = nixpkgs.legacyPackages.${system}; + lib = pkgs.lib; + inherit (builtins) mapAttrs; + inherit (lib) hiPrio filterAttrs; + gomod2nix' = rec { + gomod2nix = pkgs.callPackage "${gomod2nix-src}" { inherit buildGoApplication mkGoEnv; }; + inherit (pkgs.callPackage "${gomod2nix-src}/builder" { inherit gomod2nix; }) buildGoApplication mkGoEnv; + }; + callPackage = pkgs.newScope { + inherit gb-autotest-roms cl-gameboy; + # inherit (gitignore.lib) gitignoreSource; + cleanSource = import ./utils/clean-source.nix { inherit (gitignore.lib) gitignoreFilterWith; inherit (lib) cleanSourceWith; }; + inherit php-sdl-src; + inherit nim-argparse; + inherit (gomod2nix') gomod2nix buildGoApplication; + naersk = pkgs.callPackage naersk {}; + zig = zig-overlay.packages.${system}.master-2023-02-06; + inherit zig-clap zig-sdl; + }; + + utils = callPackage ./utils/derivation.nix {}; + + mkC = {clangSupport ? false, ltoSupport ? false, debugSupport ? false}: + callPackage ./c/derivation.nix { + stdenv = if clangSupport then pkgs.clangStdenv else pkgs.stdenv; + inherit ltoSupport debugSupport; + }; + + mkCpp = {ltoSupport ? false, debugSupport ? false, clangSupport ? false}: + callPackage ./cpp/derivation.nix { + stdenv = if clangSupport then pkgs.clangStdenv else pkgs.stdenv; + inherit ltoSupport debugSupport; + }; + + mkGo = {...}: + callPackage ./go/derivation.nix { + }; + + mkNim = {debugSupport ? false, speedSupport ? false}: + callPackage ./nim/derivation.nix { + inherit debugSupport speedSupport; + inherit (pkgs.llvmPackages_14) bintools; + }; + + mkPhp = {opcacheSupport ? false}: + callPackage ./php/derivation.nix { + inherit opcacheSupport; + }; + + mkPxd = {}: + callPackage ./pxd/derivation.nix {}; + + mkPy = {mypycSupport ? false}: + callPackage ./py/derivation.nix { + inherit mypycSupport; + }; + + mkRs = {ltoSupport ? false, debugSupport ? false}: + callPackage ./rs/derivation.nix { + inherit ltoSupport debugSupport; + }; + + mkZig = {safeSupport ? false, fastSupport ? false}: + callPackage ./zig/derivation.nix { + inherit safeSupport fastSupport; + }; + + pre-commit-check = pre-commit-hooks.lib.${system}.run { + src = ./.; + hooks = { + actionlint.enable = true; + #deadnix.enable = true; + shellcheck.enable = true; + }; + }; + + in rec { + packages = rec { + inherit utils; + + c-debug = mkC { debugSupport = true; }; + c-lto = mkC { ltoSupport = true; }; + c-release = mkC { }; + c-clang-debug = mkC { debugSupport = true; clangSupport = true; }; + c-clang-lto = mkC { ltoSupport = true; clangSupport = true; }; + c-clang-release = mkC { clangSupport = true; }; + c = hiPrio c-release; + + cpp-release = mkCpp {}; + cpp-debug = mkCpp { debugSupport = true; }; + cpp-lto = mkCpp { ltoSupport = true; }; + cpp-clang-release = mkCpp { clangSupport = true; }; + cpp-clang-debug = mkCpp { debugSupport = true; clangSupport = true; }; + cpp-clang-lto = mkCpp { ltoSupport = true; clangSupport = true; }; + cpp = hiPrio cpp-release; + + go-release = mkGo {}; + go = hiPrio go-release; + + nim-release = mkNim {}; + nim-debug = mkNim { debugSupport = true; }; + nim-speed = mkNim { speedSupport = true; }; + nim = hiPrio nim-release; + + php-release = mkPhp {}; + php-opcache = mkPhp { opcacheSupport = true; }; + php = hiPrio php-release; + + py-release = mkPy {}; + py-mypyc = mkPy { mypycSupport = true; }; + py = hiPrio py-release; + + pxd-release = mkPxd {}; + pxd = hiPrio pxd-release; + + rs-debug = mkRs { debugSupport = true; }; + rs-release = mkRs { }; + rs-lto = mkRs { ltoSupport = true; }; + rs = hiPrio rs-release; + + zig-fast = mkZig { fastSupport = true; }; + zig-safe = mkZig { safeSupport = true; }; + zig = hiPrio zig-fast; + + # I don't think we can join all of them because they collide + default = pkgs.symlinkJoin { + name = "rosettaboy"; + paths = [ c cpp go nim php pxd py rs zig ]; + # if we use this without adding build tags to the executable, + # it'll build all variants but not symlink them + # paths = builtins.attrValues (filterAttrs (n: v: n != "default") packages); + }; + }; + + checks = let + # zig-safe is too slow - skip + packagesToCheck = filterAttrs (n: p: p.meta ? mainProgram && n != "zig-safe") packages; + in { + inherit pre-commit-check; + } // mapAttrs (_: utils.mkBlargg) packagesToCheck; + + devShells = let + shellHook = '' + export GB_DEFAULT_AUTOTEST_ROM_DIR=${gb-autotest-roms} + export GB_DEFAULT_BENCH_ROM=${cl-gameboy}/roms/opus5.gb + ''; + langDevShells = mapAttrs (name: package: pkgs.mkShell { + inputsFrom = [ package ]; + buildInputs = package.devTools or []; + inherit shellHook; + }) packages; + in langDevShells // { + default = pkgs.mkShell { + inputsFrom = builtins.attrValues langDevShells; + inherit (pre-commit-check) shellHook; + }; + # something wrong with using it in `inputsFrom` + py = pkgs.mkShell { + buildInputs = packages.py.devTools; + inherit shellHook; + }; + }; + }); +} diff --git a/go/.envrc b/go/.envrc new file mode 120000 index 00000000..8be2b97d --- /dev/null +++ b/go/.envrc @@ -0,0 +1 @@ +../utils/.envrc \ No newline at end of file diff --git a/go/derivation.nix b/go/derivation.nix new file mode 100644 index 00000000..9d6d0bd2 --- /dev/null +++ b/go/derivation.nix @@ -0,0 +1,30 @@ +{ + buildGoApplication, + cleanSource, + pkg-config, + SDL2, + gomod2nix +}: + +buildGoApplication rec { + name = "rosettaboy-go"; + src = cleanSource { + inherit name; + src = ./.; + }; + modules = ./gomod2nix.toml; + + passthru.devTools = [ gomod2nix ]; + + buildInputs = [ SDL2 ]; + nativeBuildInputs = [ pkg-config ]; + + postInstall = '' + mv $out/bin/src $out/bin/rosettaboy-go + ''; + + meta = { + description = name; + mainProgram = name; + }; +} diff --git a/go/shell.nix b/go/shell.nix new file mode 100644 index 00000000..eb19cd41 --- /dev/null +++ b/go/shell.nix @@ -0,0 +1,8 @@ +{ pkgs ? import {} } : pkgs.mkShell { + buildInputs = with pkgs; [ + go + SDL2 + + pkg-config + ]; +} diff --git a/nim/.envrc b/nim/.envrc new file mode 120000 index 00000000..8be2b97d --- /dev/null +++ b/nim/.envrc @@ -0,0 +1 @@ +../utils/.envrc \ No newline at end of file diff --git a/nim/derivation.nix b/nim/derivation.nix new file mode 100644 index 00000000..54e94f11 --- /dev/null +++ b/nim/derivation.nix @@ -0,0 +1,64 @@ +{ + lib, + stdenvNoCC, + cleanSource, + llvmPackages_14, + nimPackages, + nim-argparse, + git, + cacert, + bintools, + debugSupport ? false, + speedSupport ? false +}: + +let + argparse = nimPackages.buildNimPackage rec { + pname = "argparse"; + version = "master"; + src = nim-argparse; + }; + + # Upstream `nimPackages.sdl2` is marked broken on macOS but it actually works + # fine: + sdl2 = nimPackages.sdl2.overrideAttrs (o: { + meta = o.meta // { + platforms = o.meta.platforms ++ lib.platforms.darwin; + }; + }); +in + +nimPackages.buildNimPackage rec { + name = "rosettaboy-nim"; + src = cleanSource { + inherit name; + src = ./.; + }; + + passthru = { + devTools = [ nimPackages.nim git cacert ]; + }; + + nimBinOnly = true; + + nimFlags = [] + ++ lib.optional debugSupport "-d:debug" + ++ lib.optional (!debugSupport) "-d:release" + ++ lib.optional (!speedSupport) "-d:nimDebugDlOpen" + ++ lib.optionals speedSupport [ "-d:danger" "--opt:speed" "-d:lto" "--mm:arc" "--panics:on" ] + ; + + buildInputs = [ argparse sdl2 ]; + + # Wants `lld` on macOS: + nativeBuildInputs = lib.optional stdenvNoCC.isDarwin llvmPackages_14.bintools; + + postInstall = '' + mv $out/bin/rosettaboy $out/bin/rosettaboy-nim + ''; + + meta = { + description = name; + mainProgram = name; + }; +} diff --git a/nim/shell.nix b/nim/shell.nix new file mode 100644 index 00000000..56a0717e --- /dev/null +++ b/nim/shell.nix @@ -0,0 +1,9 @@ +{ pkgs ? import {} } : pkgs.mkShell { + buildInputs = with pkgs; [ + nim + SDL2 + pkgs.llvmPackages_15.bintools + + git cacert + ]; +} diff --git a/php/.envrc b/php/.envrc new file mode 120000 index 00000000..8be2b97d --- /dev/null +++ b/php/.envrc @@ -0,0 +1 @@ +../utils/.envrc \ No newline at end of file diff --git a/php/derivation.nix b/php/derivation.nix new file mode 100644 index 00000000..87ed51e6 --- /dev/null +++ b/php/derivation.nix @@ -0,0 +1,74 @@ +{ + stdenv, + lib, + makeWrapper, + php, + php-sdl-src, + SDL2, + cleanSource, + opcacheSupport ? false +}@args: + +let + sdl = php.buildPecl { + pname = "sdl"; + version = "master"; + src = php-sdl-src; + buildInputs = [ SDL2 ]; + }; + + php = args.php.buildEnv { + extensions = ({ enabled, all }: enabled ++ [ sdl ] ++ lib.optional opcacheSupport all.opcache); + extraConfig = lib.optionalString opcacheSupport '' + opcache.enable_cli=1 + opcache.jit_buffer_size=100M + ''; + }; +in + +stdenv.mkDerivation rec { + name = "rosettaboy-php"; + + src = cleanSource { + inherit name; + src = ./.; + extraRules = '' + .phpstan.neon + .php-cs-fixer.dist.php + ''; + }; + + passthru = { + inherit php; + devTools = [ + sdl + php + php.packages.composer + php.packages.php-cs-fixer + ]; + }; + + buildInputs = [ php ]; + nativeBuildInputs = [ makeWrapper ]; + + installPhase = '' + runHook preInstall + + mkdir -p $out/bin $out/libexec/$name + cp src/* $out/libexec/$name + + # Note: we have to invoke the interpreter explicitly instead of letting the + # shebang on `main.php` take care of it for us because the interpreter is + # actually a shell script (because of the PHP extensions we're adding) which + # is problematic on macOS: https://stackoverflow.com/a/67101108 + makeWrapper ${lib.getBin php}/bin/php $out/bin/$name \ + --add-flags $out/libexec/$name/main.php + + runHook postInstall + ''; + + meta = { + description = name; + mainProgram = name; + }; +} diff --git a/php/shell.nix b/php/shell.nix new file mode 100644 index 00000000..32efcbc8 --- /dev/null +++ b/php/shell.nix @@ -0,0 +1,25 @@ +{ pkgs ? import { } }: + +let + sdl = pkgs.php81.buildPecl { + pname = "sdl"; + version = "655e40"; + src = pkgs.fetchFromGitHub { + owner = "Ponup"; + repo = "php-sdl"; + rev = "655e403b8a9681c418702a74833c68c1a4ae1bd5"; + sha256 = "1kfw1s4ip8y0zfyl61ipw3ql9c4d9f0bwwg398kfl9p8k3vc857h"; + }; + buildInputs = with pkgs; [ SDL2 ]; + }; + + php = pkgs.php81.withExtensions ({ enabled, all }: enabled ++ [ sdl ]); +in + +pkgs.mkShell { + buildInputs = [ + php + php.packages.composer + php.packages.php-cs-fixer + ]; +} diff --git a/pxd/.envrc b/pxd/.envrc new file mode 120000 index 00000000..8be2b97d --- /dev/null +++ b/pxd/.envrc @@ -0,0 +1 @@ +../utils/.envrc \ No newline at end of file diff --git a/pxd/derivation.nix b/pxd/derivation.nix new file mode 100644 index 00000000..2a043a9f --- /dev/null +++ b/pxd/derivation.nix @@ -0,0 +1,35 @@ +{ + python310, + cleanSource +}: + +let + python = python310; + pythonPackages = python.pkgs; + + runtimeDeps = with pythonPackages; [ setuptools pysdl2 cython_3 ]; + devDeps = with pythonPackages; [ black ]; +in + +pythonPackages.buildPythonApplication rec { + name = "rosettaboy-pxd"; + src = cleanSource { + inherit name; + src = ./.; + extraRules = '' + py_env.sh + ''; + }; + + passthru.python = python.withPackages (p: runtimeDeps ++ devDeps); + passthru.devTools = [ python ]; + + propagatedBuildInputs = runtimeDeps; + + dontUseSetuptoolsCheck = true; + + meta = { + description = name; + mainProgram = name; + }; +} diff --git a/pxd/shell.nix b/pxd/shell.nix new file mode 100644 index 00000000..bffd8219 --- /dev/null +++ b/pxd/shell.nix @@ -0,0 +1,13 @@ +{ pkgs ? import {} } : pkgs.mkShell { + buildInputs = with pkgs; [ + (python310.withPackages (pypkgs: with pypkgs; [ + pysdl2 + cython_3 + setuptools + + black + ])).out + + SDL2 + ]; +} diff --git a/py/.envrc b/py/.envrc new file mode 120000 index 00000000..8be2b97d --- /dev/null +++ b/py/.envrc @@ -0,0 +1 @@ +../utils/.envrc \ No newline at end of file diff --git a/py/derivation.nix b/py/derivation.nix new file mode 100644 index 00000000..f1ca14f0 --- /dev/null +++ b/py/derivation.nix @@ -0,0 +1,57 @@ +{ + lib, + python311, + cleanSource, + fetchFromGitHub, + mypycSupport ? false +}: + +let + python = python311; + pythonPackages = python.pkgs; + + # We use `match` which is only supported in `mypy` v1 and newer: + # https://github.com/python/mypy/commit/d5e96e381f72ad3fafaae8707b688b3da320587d + # + # This hasn't made it's way into `nixpkgs` yet so we override for now: + # (!!! remove once nixpkgs has 1.0.0+) + mypy' = pythonPackages.mypy.overridePythonAttrs (old: rec { + version = "1.0.0"; + buildInputs = (old.buildInputs or []) ++ (with pythonPackages; [ psutil types-psutil ]); + src = fetchFromGitHub { + owner = "python"; + repo = "mypy"; + rev = "refs/tags/v${version}"; + hash = "sha256-/E2O6J+o0OiY2v/ogatygaB07D/Z5ZQ6mB0daEQqo+4="; + }; + }); + + runtimeDeps = with pythonPackages; [ setuptools pysdl2 ]; + devDeps = with pythonPackages; [ mypy' black ]; +in + +pythonPackages.buildPythonApplication rec { + name = "rosettaboy-py"; + src = cleanSource { + inherit name; + src = ./.; + extraRules = '' + py_env.sh + ''; + }; + + nativeBuildInputs = lib.optional mypycSupport mypy'; + + passthru.python = python.withPackages (p: runtimeDeps ++ devDeps); + passthru.devTools = [ python ]; + + propagatedBuildInputs = runtimeDeps; + + ROSETTABOY_USE_MYPYC = mypycSupport; + dontUseSetuptoolsCheck = true; + + meta = { + description = name; + mainProgram = name; + }; +} diff --git a/py/shell.nix b/py/shell.nix new file mode 100644 index 00000000..a32c76dc --- /dev/null +++ b/py/shell.nix @@ -0,0 +1,11 @@ +{ pkgs ? import {} } : pkgs.mkShell { + buildInputs = with pkgs; [ + (python310.withPackages (pypkgs: with pypkgs; [ + pysdl2 + setuptools + + mypy + black + ])).out + ]; +} diff --git a/rs/.envrc b/rs/.envrc new file mode 120000 index 00000000..8be2b97d --- /dev/null +++ b/rs/.envrc @@ -0,0 +1 @@ +../utils/.envrc \ No newline at end of file diff --git a/rs/derivation.nix b/rs/derivation.nix new file mode 100644 index 00000000..711cc90e --- /dev/null +++ b/rs/derivation.nix @@ -0,0 +1,43 @@ +{ + lib, + stdenv, + naersk, + SDL2, + pkg-config, + libiconv, + rustfmt, + rustc, + cargo, + cleanSource, + ltoSupport ? false, + debugSupport ? false +}: + +let + devTools = [ rustfmt rustc cargo ] ++ lib.optional stdenv.isDarwin [libiconv]; +in + +naersk.buildPackage rec { + name = "rosettaboy-rs"; + src = cleanSource { + inherit name; + src = ./.; + extraRules = '' + test_pgo.sh + ''; + }; + + buildInputs = [ SDL2 ]; + nativeBuildInputs = [ pkg-config ]; + + cargoBuildOptions = input: input ++ (lib.optional ltoSupport ["--profile release-lto"]); + + release = !debugSupport && !ltoSupport; + + passthru = { inherit devTools; }; + + meta = { + description = name; + mainProgram = name; + }; +} diff --git a/rs/shell.nix b/rs/shell.nix new file mode 100644 index 00000000..48d11157 --- /dev/null +++ b/rs/shell.nix @@ -0,0 +1,9 @@ +{ pkgs ? import {} } : pkgs.mkShell { + buildInputs = with pkgs; [ + rustc + cargo + SDL2 + + rustfmt + ] ++ (pkgs.lib.optional pkgs.targetPlatform.isMacOS pkgs.libiconv); +} diff --git a/shell.nix b/shell.nix new file mode 100644 index 00000000..5ec404c2 --- /dev/null +++ b/shell.nix @@ -0,0 +1,10 @@ +(import + ( + let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in + fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; + sha256 = lock.nodes.flake-compat.locked.narHash; + } + ) + { src = ./.; } +).shellNix \ No newline at end of file diff --git a/utils/.envrc b/utils/.envrc new file mode 100644 index 00000000..a8599945 --- /dev/null +++ b/utils/.envrc @@ -0,0 +1 @@ +use flake ..#$(basename "$(pwd)") diff --git a/utils/clean-source.nix b/utils/clean-source.nix new file mode 100644 index 00000000..759e023e --- /dev/null +++ b/utils/clean-source.nix @@ -0,0 +1,35 @@ +{ + cleanSourceWith, + gitignoreFilterWith +}: + +# see https://github.com/hercules-ci/gitignore.nix/blob/master/docs/gitignoreFilter.md + +let + customerFilter = src: extraRules: + let + # IMPORTANT: use a let binding like this to memoize info about the git directories. + srcIgnored = gitignoreFilterWith { + basePath = src; + extraRules = '' + .envrc + .gitignore + *.nix + build*.sh + format*.sh + '' + extraRules; + }; + in path: type: srcIgnored path type; +in + +{ + name, + src, + extraRules ? "" +}: + +cleanSourceWith { + filter = customerFilter src extraRules; + src = src; + name = name + "-source"; +} diff --git a/utils/derivation.nix b/utils/derivation.nix new file mode 100644 index 00000000..2610840d --- /dev/null +++ b/utils/derivation.nix @@ -0,0 +1,55 @@ +{ + lib, + stdenvNoCC, + runCommand, + makeWrapper, + python3, + wget, + cacert, + parallel, + elfutils, + gb-autotest-roms, + cl-gameboy +}: + +stdenvNoCC.mkDerivation rec { + name = "rosettaboy-utils"; + + src = ./.; + + passthru = { + # we have to make $out or building will fail... + mkBlargg = pkg: runCommand "rosettaboy-checks-blargg-${pkg.name}" {} '' + echo ${pkg.name} + find ${gb-autotest-roms} -name '*.gb' \ + | ${parallel}/bin/parallel --will-cite --line-buffer --keep-order \ + "${lib.getExe pkg} --turbo --silent --headless --frames 200" \ + | tee -a $out + ''; + devTools = [ wget cacert parallel ] + ++ lib.optional (!stdenvNoCC.isDarwin) elfutils; + }; + + dontConfigure = true; + dontBuild = true; + + buildInputs = [ python3 ]; + nativeBuildInputs = [ makeWrapper ]; + + installPhase = '' + mkdir -p $out/bin $out/libexec/rosettaboy-utils/ + cp bench.py blargg.py cpudiff.py $out/libexec/rosettaboy-utils/ + + makeWrapper $out/libexec/rosettaboy-utils/bench.py $out/bin/rosettaboy-bench \ + --set-default "GB_DEFAULT_BENCH_ROM" "${cl-gameboy}/roms/opus5.gb" + + makeWrapper $out/libexec/rosettaboy-utils/cpudiff.py $out/bin/rosettaboy-cpudiff + + makeWrapper $out/libexec/rosettaboy-utils/blargg.py $out/bin/rosettaboy-blargg \ + --set-default "GB_DEFAULT_AUTOTEST_ROM_DIR" "${gb-autotest-roms}" + ''; + + meta = { + description = name; + }; +} diff --git a/utils/get-builds-for-lang.nix b/utils/get-builds-for-lang.nix new file mode 100644 index 00000000..a727b625 --- /dev/null +++ b/utils/get-builds-for-lang.nix @@ -0,0 +1,14 @@ +{ + lang, + attr ? "checks", + system ? builtins.currentSystem, + prefix ? ".#${attr}.${system}.", + lib ? import +}: + +attrs: lib.pipe attrs.${system} [ + builtins.attrNames + (builtins.filter (x: x == lang || lib.hasPrefix "${lang}-" x)) + (builtins.map (x: "${prefix}${x}")) + (builtins.concatStringsSep "\n") +] diff --git a/utils/shell.nix b/utils/shell.nix new file mode 100644 index 00000000..e8b1b2a5 --- /dev/null +++ b/utils/shell.nix @@ -0,0 +1,5 @@ +{ pkgs ? import {} } : pkgs.mkShell { + buildInputs = with pkgs; [ + wget cacert + ]; +} diff --git a/utils/shell.sh b/utils/shell.sh index cd040162..d7411e90 100755 --- a/utils/shell.sh +++ b/utils/shell.sh @@ -4,4 +4,18 @@ set -eu cd $(dirname $0) cd .. -./utils/shell-docker "$@" +if nix develop --help 2>/dev/null 1>/dev/null; then + if [ -n "$*" ]; then + nix develop . --command bash -c "$*" + else + nix develop . + fi +elif command -v nix-shell; then + if [ -n "$*" ]; then + nix-shell --run "$*" + else + nix-shell + fi +else + ./utils/shell-docker "$@" +fi diff --git a/zig/.envrc b/zig/.envrc new file mode 120000 index 00000000..8be2b97d --- /dev/null +++ b/zig/.envrc @@ -0,0 +1 @@ +../utils/.envrc \ No newline at end of file diff --git a/zig/derivation.nix b/zig/derivation.nix new file mode 100644 index 00000000..36d5de61 --- /dev/null +++ b/zig/derivation.nix @@ -0,0 +1,104 @@ +{ + stdenv, + lib, + darwin, + libiconv, + zig, + pkg-config, + SDL2, + zig-sdl, + zig-clap, + autoPatchelfHook, + cleanSource, + safeSupport ? false, + fastSupport ? false +}: + +let + # `apple_sdk` defaults to `10_12` instead of `11_0` on `x86_64-darwin` but we + # need `CoreHaptics` to successfully link against `SDL2` and `CoreHaptics` is + # not available in `10_12`. + # + # So, we use `11_0`, even on x86_64. + inherit (darwin.apple_sdk_11_0) frameworks; +in + +stdenv.mkDerivation rec { + name = "rosettaboy-zig"; + src = cleanSource { + inherit name; + src = ./.; + extraRules = '' + lib + ''; + }; + + passthru = { + devTools = [ zig ]; + }; + + buildInputs = [ SDL2 ] + ++ lib.optionals stdenv.isDarwin (with frameworks; [ + IOKit GameController CoreAudio AudioToolbox QuartzCore Carbon Metal + Cocoa ForceFeedback CoreHaptics + ]) + ++ lib.optional stdenv.isDarwin libiconv + ; + + nativeBuildInputs = [ zig pkg-config ] + ++ lib.optional (!stdenv.isDarwin) autoPatchelfHook + ; + + dontConfigure = true; + dontBuild = true; + + # Unforunately `zig`'s parsing of `NIX_LDFLAGS` bails when it encounters any + # flags it does not expect. + # https://github.com/ziglang/zig/blob/fe6dcdba1407f00584725318404814571cdbd828/lib/std/zig/system/NativePaths.zig#L79 + # + # When `zig` sees the `-liconv` flag that's in `NIX_LDFLAGS` on macOS, it + # bails, causing it to miss the `-L` path for SDL. + # + # Really, this should be fixed in upstream (zig) but for now we just strip out + # the `-l` flags: + preInstall = lib.optionalString stdenv.isDarwin '' + readonly ORIGINAL_NIX_LDFLAGS=($NIX_LDFLAGS) + + NIX_LDFLAGS="" + for c in "''${ORIGINAL_NIX_LDFLAGS[@]}"; do + # brittle, bad, etc; this presumes `-l...` style args (no space) + if [[ $c =~ ^-l.* ]]; then + echo "dropping link flag: $c" + continue + else + echo "keeping link flag: $c" + NIX_LDFLAGS="$NIX_LDFLAGS $c" + fi + done + + export NIX_LDFLAGS + ''; + + ZIG_FLAGS = [] + ++ lib.optional fastSupport "-Doptimize=ReleaseFast" + ++ lib.optional safeSupport "-Doptimize=ReleaseSafe" + ; + + installPhase = '' + runHook preInstall + + export HOME=$TMPDIR + mkdir -p lib + cp -aR ${zig-sdl}/ lib/sdl + cp -aR ${zig-clap}/ lib/clap + zig build $ZIG_FLAGS --prefix $out install + mv $out/bin/rosettaboy $out/bin/rosettaboy-zig + + runHook postInstall + ''; + + meta = { + description = name; + mainProgram = name; + }; +} diff --git a/zig/shell.nix b/zig/shell.nix new file mode 100644 index 00000000..0e0fba5c --- /dev/null +++ b/zig/shell.nix @@ -0,0 +1,39 @@ +{ + pkgs ? import {}, + # Right. Getting this to work took more effort than all the other shells put + # together. + # Things that didn't work: + # - Directly using the Zig package, pinned to any version. Nix is "known + # as the most up to date distribution", but this project uses a *nightly* + # build of Zig, so that's not there. + # - Building Zig from source. The latest version of Zig itself uses + # LLVM+Clang 15, which is brand new and also not yet in Nix repos. There + # was a brief window in late August 2022 where Zig could be built with + # LLVM 14, but the most recent commit that works with that doesn't work + # with this project. + # - Building LLVM 15 from source. I didn't even try this given the massive + # disk space and build time requirements. + zig-overlay ? pkgs.callPackage ((pkgs.fetchFromGitHub { + # Officially this information is published without past history at: + # https://ziglang.org/download/index.json + # 1. Update this URL with the latest commit. + # 2. Try to use it with `nix-shell`. It will error because the hash is + # wrong, and the right hash will be reported. Put the new hash here + # too. (I swear this is what the tutorials say to do.) + # 3. Change the "YYYY-MM-DD" key below to the new version, as long as + # it's in the `sources.json`. + owner = "mitchellh"; + repo = "zig-overlay"; + rev = "6a76f5697bcf291d1a7d88b0ef8189c2f5c9b38f"; + hash = "sha256-62E0b7i51LNHBsWbsi2ChG5gCqZfTb8/UaZren6DqXs="; + }) + "/default.nix") {}, + zig ? zig-overlay.master-2023-02-06 +}: + +pkgs.mkShell { + buildInputs = with pkgs; [ + pkg-config + SDL2 + zig + ]; +}