{ inputs = {
    nixpkgs.url = "nixpkgs/nixos-22.11";

    yokutango = {
      url = "git+https://git.nani.wtf/h7x4/yokutango?ref=master";
      flake = false;
    };

    noto-cjk-jp-otf-regular = {
      url = "https://github.com/notofonts/noto-cjk/raw/main/Sans/OTF/Japanese/NotoSansCJKjp-Regular.otf";
      flake = false;
    };
  };

  outputs = { self, nixpkgs, yokutango, noto-cjk-jp-otf-regular }: let
    system = "x86_64-linux";
    pkgs = nixpkgs.legacyPackages.${system};
    inherit (pkgs) lib;

    nativeBuildInputs = with pkgs; [
      self.packages.${system}.yokutango2tex
      texlive.combined.scheme-full
    ];

    FONTCONFIG_FILE = pkgs.makeFontsConf { 
      fontDirectories = with pkgs; [
        noto-fonts
        self.packages.${system}.noto-sans-cjk-jp
      ];
    };

    mkYokutango-pdf = { paper ? "a4paper" }: let
        template = lib.fileContents ./template.tex;
        y2t-output-d = pkgs.runCommand "yokutango2tex-output" {} ''
          ${self.packages.${system}.yokutango2tex}/bin/yokutango2tex ${self.packages.${system}.yokutango-json} > $out
        '';
        y2t-output = lib.fileContents y2t-output-d;
        combined = pkgs.writeText ''yokutango.tex'' (builtins.replaceStrings [
          "% --- %"
          "$paper$"
        ] [
          y2t-output
          paper
        ] template);
      in pkgs.runCommand "yokutango-pdf" { inherit nativeBuildInputs FONTCONFIG_FILE; } ''
        mkdir $out
        ln -s ${combined}      yokutango.tex
        ln -s ${combined} $out/yokutango.tex
        xelatex yokutango.tex
        xelatex yokutango.tex
        ln -s ${./2page-booklet-wrapper.tex} yokutango-book.tex
        xelatex yokutango-book.tex
        mv yokutango.pdf yokutango-book.pdf $out
      '';
  in {
    devShells.${system}.default = pkgs.mkShell {
      inherit nativeBuildInputs FONTCONFIG_FILE;
    };

    packages.${system} = {
      default = self.packages.${system}.yokutango-pdf-a5;

      noto-sans-cjk-jp = pkgs.runCommandLocal "noto-sans-cjk-jp" {} ''
        mkdir -p $out/share/fonts/opentype/noto-cjk
        ln -s ${noto-cjk-jp-otf-regular} $out/share/fonts/opentype/noto-cjk/NotoSansCJKjp-Regular.otf
      '';

      yokutango-json = pkgs.runCommandLocal "yokutango-json" {} ''
        ln -s ${yokutango}/json $out
      '';

      yokutango-test = let
        template = lib.fileContents ./template.tex;
        body = lib.fileContents ./testContent.tex;
        combined = pkgs.writeText ''yokutango-test.tex'' (builtins.replaceStrings ["% --- %"] [body] template);
      in pkgs.runCommand "yokutango-pdf" { inherit nativeBuildInputs FONTCONFIG_FILE; } ''
        mkdir $out
        ln -s ${combined}      yokutango-test.tex
        ln -s ${combined} $out/yokutango-test.tex
        xelatex yokutango-test.tex
        xelatex yokutango-test.tex
        mv yokutango-test.pdf $out
      '';

      yokutango2tex = pkgs.writers.writeHaskellBin "yokutango2tex" {
        libraries = with pkgs.haskellPackages; [ aeson bytestring ];
      } (lib.fileContents ./yokutango2tex.hs);

      yokutango-pdf-a5 = mkYokutango-pdf { paper = "a5paper"; };
      yokutango-pdf-a4 = mkYokutango-pdf { paper = "a4paper"; };
    };
  };
}