{
  description = "Critterfolio";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, utils }:
    utils.lib.eachDefaultSystem (system:
      let
        pkgs = import nixpkgs {
          inherit system;
          config = {
            allowUnfree = true;
            android_sdk.accept_license = true;
          };
        };

        rmlui = pkgs.stdenv.mkDerivation rec {
          pname = "rmlui";
          version = "master";

          src = pkgs.fetchFromGitHub {
            owner = "mikke89";
            repo = "RmlUi";
            rev = "master";
            hash = "sha256-vfFtjNHw7coXWrGgtcFyuIsu5rctW5dBJoOt8MTZwDU=";
          };

          nativeBuildInputs = [
            pkgs.cmake
            pkgs.pkg-config
          ];

          buildInputs = [
            pkgs.freetype
            pkgs.lunasvg
            pkgs.lua
          ];

          cmakeFlags = [
            "-DRMLUI_LUA_BINDINGS=ON"
            "-DRMLUI_LUA_BINDINGS_LIBRARY=lua"
            "-DRMLUI_SVG_PLUGIN=ON"
            "-DBUILD_SHARED_LIBS=ON"
            "-DBUILD_SAMPLES=OFF"
            "-DBUILD_TESTING=OFF"
            "-DNO_FONT_INTERFACE_DEFAULT=OFF"
          ];
        };

        buildDeps = with pkgs; [
          cmake
          pkg-config
          gcc
          clang-tools
          lua-language-server
        ];

        runtimeDeps = with pkgs; [
          sdl3
          sdl3-image
          libGL
          wayland
          libxkbcommon
          rmlui
          freetype
          lunasvg
          lua
          sqlite.dev
          sqlitecpp
          toml11
        ];

        ndkVersion = "26.3.11579264";

        androidComposition = pkgs.androidenv.composeAndroidPackages {
          platformVersions = [ "34" ];
          buildToolsVersions = [ "34.0.0" ];
          includeNDK = true;
          ndkVersions = [ ndkVersion ];
          cmakeVersions = [ "3.22.1" ];
          abiVersions = [ "arm64-v8a" ];
          includeEmulator = false;
        };

        androidSdkRoot = "${androidComposition.androidsdk}/libexec/android-sdk";
        androidNdkRoot = "${androidSdkRoot}/ndk/${ndkVersion}";

        androidDeps = [
          androidComposition.androidsdk
          pkgs.gradle
          pkgs.jdk17
          pkgs.ninja
        ];
      in
      {
        devShells.default = pkgs.mkShell {
          nativeBuildInputs = buildDeps ++ androidDeps;
          buildInputs = runtimeDeps;

          shellHook = ''
            export CP_PATH="${rmlui}/include"
            export LIBRARY_PATH="${pkgs.libGL}/lib:${rmlui}/lib"
            export LD_LIBRARY_PATH="$LIBRARY_PATH:$LD_LIBRARY_PATH"

            export ANDROID_HOME="${androidSdkRoot}"
            export ANDROID_SDK_ROOT="${androidSdkRoot}"
            export ANDROID_NDK_HOME="${androidNdkRoot}"
            export ANDROID_NDK_ROOT="${androidNdkRoot}"

            echo "Env ready (desktop + Android NDK ${ndkVersion}, arm64-v8a)"
          '';
        };

        packages.default = pkgs.stdenv.mkDerivation {
          pname = "critterfolio";
          version = "0.1.0";
          src = ./.;

          nativeBuildInputs = buildDeps ++ [ pkgs.makeWrapper ];
          buildInputs = runtimeDeps;

          cmakeFlags = [
            "-DCMAKE_BUILD_TYPE=Release"
          ];

          postInstall = ''
            wrapProgram $out/bin/critterfolio \
              --prefix LD_LIBRARY_PATH : "${pkgs.lib.makeLibraryPath runtimeDeps}"
          '';
        };
      }
    );
}
