anchor.diff:        rev = version;
         inherit sha256 name;
       };
  -    nativeBuildInputs = [
  -      cmake
  -    ];
  +    nativeBuildInputs = [ cmake ];
     } // attrs);
   
  -in stdenv.mkDerivation rec {
  +  # oce on aarch64 fails a test
  +  withOCC = (stdenv.isAarch64 && (withOCCT || oceSupport)) || (!stdenv.isAarch64 && withOCCT);
  +  withOCE = oceSupport && !stdenv.isAarch64 && !withOCC;
  +
  +in
  +stdenv.mkDerivation rec {
     pname = "kicad";
  -  series = "5.0";
  -  version = "5.1.4";
  +  version = "5.1.5";
   
  -  src = fetchurl {
  -    url = "https://launchpad.net/kicad/${series}/${version}/+download/kicad-${version}.tar.xz";
  -    sha256 = "1r60dgh6aalbpq1wsmpyxkz0nn4ck8ydfdjcrblpl69k5rks5k2j";
  +  src = fetchFromGitHub {
  +    owner = "KiCad";
  +    repo = "kicad-source-mirror";
  +    rev = version;
  +    sha256 = "15h3rwisjss3fdc9bam9n2wq94slhacc3fbg14bnzf4n5agsnv5b";
     };
  +
  +  # quick fix for #72248
  +  # should be removed if a better fix is merged
  +  patches = [
  +    (fetchpatch {
  +      url = "https://github.com/johnbeard/kicad/commit/dfb1318a3989e3d6f9f2ac33c924ca5030ea273b.patch";
  +      sha256 = "00ifd3fas8lid8svzh1w67xc8kyx89qidp7gm633r014j3kjkgcd";
  +    })
  +  ];
   
     postPatch = ''
       substituteInPlace CMakeModules/KiCadVersion.cmake \
  -      --replace no-vcs-found ${version}
  +      --replace "unknown" ${version}
     '';
   
  -  cmakeFlags = [
  -    "-DKICAD_SCRIPTING=ON"
  -    "-DKICAD_SCRIPTING_MODULES=ON"
  -    "-DKICAD_SCRIPTING_WXPYTHON=ON"
  -    # nix installs wxPython headers in wxPython package, not in wxwidget
  -    # as assumed. We explicitely set the header location.
  -    "-DCMAKE_CXX_FLAGS=-I${pythonPackages.wxPython}/include/wx-3.0"
  -    "-DwxPYTHON_INCLUDE_DIRS=${pythonPackages.wxPython}/include/wx-3.0"
  -  ] ++ optionals (oceSupport) [ "-DKICAD_USE_OCE=ON" "-DOCE_DIR=${opencascade}" ]
  -    ++ optional (ngspiceSupport) "-DKICAD_SPICE=ON";
  +  makeFlags = optional (debug) [ "CFLAGS+=-Og" "CFLAGS+=-ggdb" ];
   
  -  nativeBuildInputs = [
  -    cmake
  -    doxygen
  -    pkgconfig
  -    wrapGAppsHook
  -    pythonPackages.wrapPython
  -    lndir
  -  ];
  -  pythonPath = [ pythonPackages.wxPython ];
  -  propagatedBuildInputs = [ pythonPackages.wxPython ];
  +  cmakeFlags =
  +    optionals (scriptingSupport) [
  +      "-DKICAD_SCRIPTING=ON"
  +      "-DKICAD_SCRIPTING_MODULES=ON"
  +      "-DKICAD_SCRIPTING_PYTHON3=ON"
  +      "-DKICAD_SCRIPTING_WXPYTHON_PHOENIX=ON"
  +    ]
  +    ++ optional (!scriptingSupport)
  +      "-DKICAD_SCRIPTING=OFF"
  +    ++ optional (ngspiceSupport) "-DKICAD_SPICE=ON"
  +    ++ optionals (withOCE)
  +      [ "-DKICAD_USE_OCE=ON" "-DOCE_DIR=${opencascade}" ]
  +    ++ optionals (withOCC) [
  +      "-DKICAD_USE_OCC=ON"
  +      # this line is unneeded on unstable...
  +      "-DKICAD_USE_OCE=OFF"
  +      "-DOCC_INCLUDE_DIR=${opencascade-occt}/include/opencascade"
  +    ]
  +    ++ optionals (debug) [
  +      "-DCMAKE_BUILD_TYPE=Debug"
  +      "-DKICAD_STDLIB_DEBUG=ON"
  +      "-DKICAD_USE_VALGRIND=ON"
  +    ]
  +    ;
  +
  +  pythonPath =
  +    optionals (scriptingSupport)
  +    [ wxPython pythonPackages.six ];
  +
  +  nativeBuildInputs =
  +    [ cmake doxygen pkgconfig lndir ]
  +    ++ optionals (scriptingSupport)
  +      [ pythonPackages.wrapPython makeWrapper ]
  +  ;
   
     buildInputs = [
  -    libGLU libGL zlib libX11 wxGTK pcre libXdmcp glew glm libpthreadstubs
  -    cairo curl openssl boost
  -    swig (python.withPackages (ps: with ps; [ wxPython ]))
  -  ] ++ optional (oceSupport) opencascade
  -    ++ optional (ngspiceSupport) libngspice;
  +    libGLU libGL zlib libX11 wxGTK pcre libXdmcp gettext
  +    glew glm libpthreadstubs cairo curl openssl boost
  +  ]
  +  ++ optionals (scriptingSupport) [ swig python wxPython ]
  +  ++ optional (ngspiceSupport) libngspice
  +  ++ optional (withOCE) opencascade
  +  ++ optional (withOCC) opencascade-occt
  +  ++ optional (debug) valgrind
  +  ;
   
  -  # this breaks other applications in kicad
  -  dontWrapGApps = true;
  +  # 5.1.x fails the eeschema test
  +  # doInstallCheck = (!debug);
  +  # installCheckTarget = "test";
  +
  +  dontStrip = debug;
   
     passthru = {
  -    i18n = mkLib version "i18n" "1dk7wis4cncmihl8fnic3jyhqcdzpifchzsp7hmf214h0vp199zr" {
  -      buildInputs = [
  -        gettext
  -      ];
  +    symbols = mkLib "${version}" "symbols" "048b07ffsaav1ssrchw2p870lvb4rsyb5vnniy670k7q9p16qq6h" {
  +      meta.license = licenses.cc-by-sa-40;
  +    };
  +    templates = mkLib "${version}" "templates" "0cs3bm3zb5ngw5ldn0lzw5bvqm4kvcidyrn76438alffwiz2b15g" {
  +      meta.license = licenses.cc-by-sa-40;
  +    };
  +    footprints = mkLib "${version}" "footprints" "1c4whgn14qhz4yqkl46w13p6rpv1k0hsc9s9h9368fxfcz9knb2j" {
  +      meta.license = licenses.cc-by-sa-40;
  +    };
  +    i18n = mkLib "${version}" "i18n" "1rfpifl8vky1gba2angizlb2n7mwmsiai3r6ip6qma60wdj8sbd3" {
  +      buildInputs = [ gettext ];
         meta.license = licenses.gpl2; # https://github.com/KiCad/kicad-i18n/issues/3
       };
  -    symbols = mkLib version "symbols" "1lna4xlvzrxif3569pkp6mrg7fj62z3a3ri5j97lnmnnzhiddnh3" {
  -      meta.license = licenses.cc-by-sa-40;
  -    };
  -    footprints = mkLib version "footprints" "0c0kcywxlaihzzwp9bi0dsr2v9j46zcdr85xmfpivmrk19apss6a" {
  -      meta.license = licenses.cc-by-sa-40;
  -    };
  -    templates = mkLib version "templates" "1bagb0b94cjh7zp9z0h23b60j45kwxbsbb7b2bdk98dmph8lmzbb" {
  -      meta.license = licenses.cc-by-sa-40;
  -    };
  -    packages3d = mkLib version "packages3d" "0h2qjj8vf33jz6jhqdz90c80h5i1ydgfqnns7rn0fqphlnscb45g" {
  +    packages3d = mkLib "${version}" "packages3d" "0cff2ms1bsw530kqb1fr1m2pjixyxzwa81mxgac3qpbcf8fnpvaz" {
         hydraPlatforms = []; # this is a ~1 GiB download, occupies ~5 GiB in store
         meta.license = licenses.cc-by-sa-40;
       };
     };
   
  -  modules = with passthru; [ i18n symbols footprints templates ];
  +  # TODO, figure out how to skip this step? (since we're not making the 3D models optional)
  +  modules = with passthru; [ i18n symbols footprints templates ]
  +  ++ optionals (with3d) [ packages3d ];
   
     postInstall = ''
       mkdir -p $out/share
  
positive.code:     done
    '';
  
    preFixup = ''
      buildPythonPath "$out $pythonPath"
      gappsWrapperArgs+=(--set PYTHONPATH "$program_PYTHONPATH")
  
      wrapGApp "$out/bin/kicad" --prefix LD_LIBRARY_PATH : "${libngspice}/lib"
    '';
  
    meta = {
      description = "Free Software EDA Suite";
      homepage = http://www.kicad-pcb.org/;
      license = licenses.gpl2;
      maintainers = with maintainers; [ berce ];
      platforms = with platforms; linux;
      broken = stdenv.isAarch64;
    };
  }
  
negative.code: { lib, stdenv, fetchFromGitHub, cmake, libGLU, libGL, zlib, wxGTK
  , libX11, gettext, glew, glm, cairo, curl, openssl, boost, pkgconfig
  , doxygen, pcre, libpthreadstubs, libXdmcp, makeWrapper, gnome3
  , gsettings-desktop-schemas, librsvg, hicolor-icon-theme, lndir, cups
  , fetchpatch
  
  , oceSupport ? true, opencascade
  , withOCCT ? false, opencascade-occt
  , ngspiceSupport ? true, libngspice
  , scriptingSupport ? true, swig, python, pythonPackages, wxPython
  , debug ? false, valgrind
  , with3d ? true
  }:
  
  assert ngspiceSupport -> libngspice != null;
  