{ pkgs }:
{ palette }:
let
  rendersvg = pkgs.runCommand "rendersvg" { } ''
    mkdir -p $out/bin
    ln -s ${pkgs.resvg}/bin/resvg $out/bin/rendersvg
  '';
in
pkgs.stdenv.mkDerivation {
  name = "generated-gtk-theme-nix-rice";
  src = pkgs.fetchFromGitHub {
    owner = "nana-4";
    repo = "materia-theme";
    rev = "d7f59a37ef51f893c28b55dc344146e04b2cd52c";
    sha256 = "sha256-PnpFAmKEpfg3wBwShLYviZybWQQltcw7fpsQkTUZtww=";
  };
  buildInputs = builtins.attrValues {
    inherit (pkgs)
      sassc
      bc
      which
      meson
      optipng
      ninja
      ;
    inherit (pkgs.nodePackages)
      sass
      ;
    inherit (pkgs.gtk4) dev;
    inherit rendersvg;
  };
  phases = [
    "unpackPhase"
    "installPhase"
  ];
  installPhase = ''
    HOME=/build
    chmod 777 -R .
    patchShebangs .
    mkdir -p $out/share/themes
    mkdir bin
    sed -e 's/handle-horz-.*//' -e 's/handle-vert-.*//' -i ./src/gtk-2.0/assets.txt

    cat > /build/gtk-colors << EOF
      BTN_BG=${palette.util.bg}
      BTN_FG=${palette.bright.yellow}
      FG=${palette.util.fg}
      BG=${palette.util.bg}
      HDR_BTN_BG=${palette.util.bg}
      HDR_BTN_FG=${palette.util.fg}
      ACCENT_BG=${palette.normal.cyan}
      ACCENT_FG=${palette.normal.yellow}
      HDR_FG=${palette.bright.yellow}
      HDR_BG=${palette.util.bg}
      MATERIA_SURFACE=${palette.normal.black}
      MATERIA_VIEW=${palette.bright.black}
      MENU_BG=${palette.util.bg}
      MENU_FG=${palette.util.fg}
      SEL_BG=${palette.normal.yellow}
      SEL_FG=${palette.normal.cyan}
      TXT_BG=${palette.util.bg}
      TXT_FG=${palette.util.fg}
      WM_BORDER_FOCUS=${palette.normal.yellow}
      WM_BORDER_UNFOCUS=${palette.normal.black}
      UNITY_DEFAULT_LAUNCHER_STYLE=False
      NAME="nix-rice"
      MATERIA_STYLE_COMPACT=False
    EOF

    echo "Changing colours:"
    ./change_color.sh -o nix-rice /build/gtk-colors -i False -t "$out/share/themes"
    chmod 555 -R .
  '';
}
