# This is a nix flake. To use it, you need nix (with flake support) installed.
# If you do not have nix and do not intend to use it, you can ignore this file.
# Why should you use it? Using this flake will allow you to enter a developer shell
# which has all necessary packages for this repository already installed.
# The shell will allow you to code against the specific revisions of the dependencies
# pinned in this file.
# This is useful when git HEAD of your dependencies already contains breaking changes
# you should not (yet) code against.
# OTOH, if you should code against a newer, not yet released revision, you can specify
# this revision in this file.
# You can use this file in three ways:
# 1. Build: $ nix build
# this will build the package (it runs make for you in an environment with the dependencies installed)
# 2. Develop: $ nix develop
# This will drop you inside a shell in which you can develop and compile (and test) your code.
# It even starts and sets up the test database for you.
# 3. Use this repository in a new project
# Should you create a new project that depends on this project you are in luck. You can create a flake.nix
# in your repository and include this packages as a dependency just like the dependencies of this package are
# included here.

{
  inputs = {
    nixpkgs.url = "nixpkgs/release-26.05";
    systems.url = "github:nix-systems/default";
    gnunet.url = "git+https://git.gnunet.org/gnunet?rev=e8e7504b7ab43f7dffa25f273ff228069d8d6dd4";
    exchange.url = "git+https://git.gnunet.org/exchange?rev=b542c0c40c30e1ddd9b0528e913cceed8eef9c56";
    merchant.url = "git+https://git.gnunet.org/merchant?rev=8ca128478cf6dd8524572bf4fb344abde24ea34e";
    donau.url = "git+https://git.gnunet.org/donau?rev=014bf263a4f4631cfbab6486a3af069524e98ec6";
    self.submodules = true;
  };

  outputs = { self, nixpkgs, gnunet, exchange, donau, merchant, systems, ... } @ inputs:
    let
      supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
      forEachSystem = nixpkgs.lib.genAttrs supportedSystems;
      nixpkgsFor = forEachSystem (system: import nixpkgs { inherit system; });
    in
    {
      # This defines (installable) package derivations
      # For use in flakes that use this flake as input in order
      # to specify/use this package from git as a dependency
      packages = forEachSystem (system:
        let
          pkgs = nixpkgsFor.${system};
          gnunetpkgs = gnunet.packages.${system};
          exchangepkgs = exchange.packages.${system};
          donaupkgs = donau.packages.${system};
          merchantpkgs = merchant.packages.${system};
        in {
          anastasis = pkgs.stdenv.mkDerivation {
            name = "anastasis";
            src = ./.;
            nativeBuildInputs = [
              pkgs.gnumake
              pkgs.meson
              pkgs.ninja
              pkgs.pkg-config
             ];
            buildInputs = [
              pkgs.libtool
              pkgs.jansson
              pkgs.git
              pkgs.libmicrohttpd
              pkgs.libsodium
              pkgs.libgcrypt
              pkgs.curlWithGnuTls
              pkgs.qrencode
              gnunetpkgs.gnunet
              exchangepkgs.exchange
              donaupkgs.donau
              merchantpkgs.merchant
             ];
            preConfigure = ''
              patchShebangs --build contrib/check-prebuilt
              ./bootstrap
            '';
          };
        }
      );
      defaultPackage = forEachSystem (system: self.packages.${system}.anastasis);
      # This defines a development shell in which you can compile
      # (and use) exchange
       devShells = forEachSystem
        (system:
          let
            pkgs = nixpkgsFor.${system};
            gnunetpkgs = gnunet.packages.${system};
            exchangepkgs = exchange.packages.${system};
            donaupkgs = donau.packages.${system};
            merchantpkgs = merchant.packages.${system};
          in
          {
            default = pkgs.mkShell {
              packages = [
                pkgs.gcc
                pkgs.meson
                pkgs.ninja
                pkgs.gnumake
                pkgs.texinfo
                pkgs.pkg-config
                pkgs.libtool
                pkgs.jansson
                pkgs.git
                pkgs.gettext
                pkgs.postgresql
                pkgs.curlWithGnuTls
                gnunetpkgs.gnunet
                pkgs.codespell
                pkgs.clang-tools
                pkgs.uncrustify
                pkgs.jq #probably not needed
                pkgs.doxygen
                pkgs.typst
                pkgs.pdftk
                pkgs.qrencode
                exchangepkgs.exchange
                donaupkgs.donau
                merchantpkgs.merchant
              ];

              shellHook = ''
                echo "anastasis environment loaded."
                export CC=gcc
                export CFLAGS="-O"
               '';
            };
          });
    };
}
