# Copyright (C) 2024 - 2024:
# The Trinitrix Project <benedikt.peetz@b-peetz.de, antifallobst@systemausfall.org, sils@sils.li>
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This file is part of Trinitrix.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program.
# If not, see <https://www.gnu.org/licenses/>.
{
  description = "A multi protocol chat client";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";

    treefmt-nix = {
      url = "github:numtide/treefmt-nix";
      inputs = {
        nixpkgs.follows = "nixpkgs";
      };
    };

    crane = {
      url = "github:ipetkov/crane";
      inputs = {
        nixpkgs.follows = "nixpkgs";
      };
    };
    rust-overlay = {
      url = "github:oxalica/rust-overlay";
      inputs = {
        nixpkgs.follows = "nixpkgs";
        flake-utils.follows = "flake-utils";
      };
    };

    # inputs for following
    systems = {
      url = "github:nix-systems/x86_64-linux"; # only evaluate for this system
    };
    flake-compat = {
      url = "github:edolstra/flake-compat";
      flake = false;
    };
    flake-utils = {
      url = "github:numtide/flake-utils";
      inputs = {
        systems.follows = "systems";
      };
    };
  };

  outputs = {
    self,
    nixpkgs,
    flake-utils,
    treefmt-nix,
    crane,
    rust-overlay,
    ...
  }:
    flake-utils.lib.eachDefaultSystem (system: let
      pkgs = import nixpkgs {
        inherit system;
        overlays = [(import rust-overlay)];
      };

      nightly = false;
      rust_minimal =
        if nightly
        then
          (pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.minimal)).override {
            extensions = ["rustc-codegen-cranelift-preview"];
          }
        else pkgs.rust-bin.stable.latest.minimal;
      rust_default =
        if nightly
        then
          (pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default)).override {
            extensions = ["rustc-codegen-cranelift-preview"];
          }
        else pkgs.rust-bin.stable.latest.default;

      nativeBuildInputs = with pkgs; [
        pkg-config
        mold-wrapped

        clang-tools
      ];
      buildInputs = with pkgs; [
        openssl
        lua54Packages.lua
      ];
      cargo_toml = craneLib.cleanCargoToml {cargoToml = ./Cargo.toml;};
      pname = cargo_toml.package.name;

      craneLib = (crane.mkLib pkgs).overrideToolchain rust_minimal;
      craneBuild = craneLib.buildPackage {
        inherit nativeBuildInputs buildInputs;

        src = let
          trixyFilter = path: _type: builtins.match ".*tri$" path != null;
          markdownOrCargo = path: type:
            (trixyFilter path type) || (craneLib.filterCargoSources path type);
        in
          pkgs.lib.cleanSourceWith {
            src = craneLib.path ./.;
            filter = markdownOrCargo;
          };

        doCheck = true;
      };

      manual = pkgs.stdenv.mkDerivation {
        name = "${pname}-manual";
        inherit (cargo_toml.package) version;

        src = ./docs;
        nativeBuildInputs = with pkgs; [pandoc];

        buildPhase = ''
          mkdir --parents $out/docs;

          pandoc "./${pname}.1.md" -s -t man > $out/docs/${pname}.1
        '';

        installPhase = ''
          install -D $out/docs/${pname}.1  $out/share/man/man1/${pname};
        '';
      };

      treefmtEval = import ./treefmt.nix {inherit treefmt-nix pkgs;};
    in {
      packages.default = pkgs.symlinkJoin {
        inherit (cargo_toml.package) name;

        paths = [manual craneBuild];
      };

      checks = {
        inherit craneBuild;
        formatting = treefmtEval.config.build.check self;
      };

      formatter = treefmtEval.config.build.wrapper;

      devShells.default = pkgs.mkShell {
        packages = with pkgs; [
          cocogitto

          yq

          rust_default
          cargo-edit

          valgrind
          licensure
        ];
        inherit nativeBuildInputs buildInputs;
      };
    });
}
# vim: ts=2