# SPDX-FileCopyrightText: 2024–2025 toastal <toastal@posteo.net>
# SPDX-License-Identifier: MPL-2.0
{
  description = "w3m’s website";

  inputs = {
    # would be fine sticking to the next stable, but missing too many things on
    # nixos-24.11
    #nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-25.05";
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixpkgs-unstable";
  };

  outputs =
    { self, nixpkgs, ... }@inputs:
    let
      supportedSystems = nixpkgs.lib.systems.flakeExposed;

      nixpkgsFor = nixpkgs.lib.genAttrs supportedSystems (
        system:
        import nixpkgs {
          inherit system;
          overlays = [
            (import ./nix/overlays/default.nix)
            (import ./nix/overlays/dev-tools.nix)
          ];
        }
      );

      forAllSystems =
        fn:
        nixpkgs.lib.genAttrs supportedSystems (
          system:
          fn rec {
            inherit system;
            pkgs = nixpkgsFor.${system};
            inherit (pkgs) lib;
          }
        );
    in
    {
      overlays = {
        default = import ./nix/overlays/default.nix;
        dev-tools = import ./nix/overlays/dev-tools.nix;
      };

      packages = forAllSystems (
        { pkgs, ... }:
        {
          inherit (pkgs)
            open-props
            mplus-webfonts
            w3m-site_assets
            w3m-site
            w3m-site_scripts
            ;
          w3m-site-brotli-precompressed = pkgs.w3m-site.override {
            precompressBrotli = true;
          };
          default = pkgs.w3m-site;
        }
      );

      apps = forAllSystems (
        { pkgs, ... }:
        {
          dev-server = {
            type = "app";
            program = "${pkgs.w3m-site_scripts}/bin/dev-server.sh";
          };
        }
      );

      devShells = forAllSystems (
        { pkgs, ... }:
        {
          default = pkgs.w3m-site_dev-shell;
        }
      );

      formatter = forAllSystems ({ pkgs, ... }: pkgs.nixfmt-rfc-style);
    };
}
