# Mobile NixOS mruby fails tests in sandbox — strip checks + rebuild script-loader. Import before device config.
{ inputs, ... }: {
  flake.nixosModules.systemIdeapadMrubyOverlay =
    { config, lib, ... }:
    let
      cfg = config.chiasson.system.ideapadMrubyOverlay;
      mobileNixosSrc = inputs.mobile-nixos;
    in
    {
      options.chiasson.system.ideapadMrubyOverlay = {
        enable = lib.mkEnableOption ''
          Fix mruby for Mobile NixOS (no tests) + script-loader rebuild. Needs `mobile-nixos` input.
        '';
      };

      config = lib.mkIf cfg.enable {
        nixpkgs.overlays = lib.mkOrder 1000 [
          (final: prev:
            let
              fixMruby =
                drv:
                drv.overrideAttrs (old: {
                  doCheck = false;
                  preCheck = (old.preCheck or "") + ''
                    export SHELL="${final.bash}/bin/bash"
                    unset SOURCE_DATE_EPOCH
                  '';
                });
              mrubyFixed =
                prev.lib.makeOverridable (args: fixMruby (prev.mruby.override args)) { };
            in
            {
              mruby = mrubyFixed;
              mobile-nixos = (prev.mobile-nixos or { }) // {
                script-loader = final.callPackage "${mobileNixosSrc}/boot/script-loader" {
                  mruby = mrubyFixed;
                };
              };
            })
        ];
      };
    };
}
