diff --git a/pkgs/applications/science/electronics/kicad/addons/default.nix b/pkgs/applications/science/electronics/kicad/addons/default.nix new file mode 100644 index 000000000000..5170e7efce36 --- /dev/null +++ b/pkgs/applications/science/electronics/kicad/addons/default.nix @@ -0,0 +1,5 @@ +{ kicad }: +{ + kikit = kicad.callPackage ./kikit.nix { addonName = "kikit"; }; + kikit-library = kicad.callPackage ./kikit.nix { addonName = "kikit-library"; }; +} diff --git a/pkgs/applications/science/electronics/kicad/addons/kikit.nix b/pkgs/applications/science/electronics/kicad/addons/kikit.nix new file mode 100644 index 000000000000..6e5fc5ad9678 --- /dev/null +++ b/pkgs/applications/science/electronics/kicad/addons/kikit.nix @@ -0,0 +1,52 @@ +# For building the multiple addons that are in the kikit repo. +{ stdenv +, bc +, kikit +, zip +, python3 +, addonName +, addonPath +}: +let + # This python is only used when building the package, it's not the python + # environment that will ultimately run the code packaged here. The python env defined + # in KiCad will import the python code packaged here when KiCad starts up. + python = python3.withPackages (ps: with ps; [ click ]); + kikit-module = python3.pkgs.toPythonModule (kikit.override { inherit python3; }); + + # The following different addons can be built from the same source. + targetSpecs = { + "kikit" = { + makeTarget = "pcm-kikit"; + resultZip = "pcm-kikit.zip"; + description = "KiCad plugin and a CLI tool to automate several tasks in a standard KiCad workflow"; + }; + "kikit-library" = { + makeTarget = "pcm-lib"; + resultZip = "pcm-kikit-lib.zip"; + description = "KiKit uses these symbols and footprints to annotate your boards (e.g., to place a tab in a panel)."; + }; + }; + targetSpec = targetSpecs.${addonName}; +in +stdenv.mkDerivation { + name = "kicadaddon-${addonName}"; + inherit (kikit-module) src version; + + nativeBuildInputs = [ python bc zip ]; + propagatedBuildInputs = [ kikit-module ]; + + buildPhase = '' + patchShebangs scripts/setJson.py + make ${targetSpec.makeTarget} + ''; + + installPhase = '' + mkdir $out + mv build/${targetSpec.resultZip} $out/${addonPath} + ''; + + meta = kikit-module.meta // { + description = targetSpec.description; + }; +} diff --git a/pkgs/applications/science/electronics/kicad/base.nix b/pkgs/applications/science/electronics/kicad/base.nix index 3403e410cf85..a2e5bbe72a56 100644 --- a/pkgs/applications/science/electronics/kicad/base.nix +++ b/pkgs/applications/science/electronics/kicad/base.nix @@ -69,6 +69,8 @@ stdenv.mkDerivation rec { patches = [ # upstream issue 12941 (attempted to upstream, but appreciably unacceptable) ./writable.patch + # https://gitlab.com/kicad/code/kicad/-/issues/15687 + ./runtime_stock_data_path.patch ]; # tagged releases don't have "unknown" diff --git a/pkgs/applications/science/electronics/kicad/default.nix b/pkgs/applications/science/electronics/kicad/default.nix index a49c813036d3..c6c66839e4bc 100644 --- a/pkgs/applications/science/electronics/kicad/default.nix +++ b/pkgs/applications/science/electronics/kicad/default.nix @@ -1,4 +1,6 @@ { lib, stdenv +, runCommand +, newScope , fetchFromGitLab , gnome , dconf @@ -11,6 +13,8 @@ , callPackages , librsvg , cups +, unzip +, jq , pname ? "kicad" , stable ? true @@ -18,6 +22,7 @@ , libngspice , withScripting ? true , python3 +, addons ? [ ] , debug ? false , sanitizeAddress ? false , sanitizeThreads ? false @@ -27,6 +32,14 @@ , symlinkJoin }: +# `addons`: https://dev-docs.kicad.org/en/addons/ +# +# ```nix +# kicad = pkgs.kicad.override { +# addons = with pkgs.kicadAddons; [ kikit kikit-library ]; +# }; +# ``` + # The `srcs` parameter can be used to override the kicad source code # and all libraries, which are otherwise inaccessible # to overlays since most of the kicad build expression has been @@ -106,6 +119,32 @@ let wxGTK = wxGTK32; python = python3; wxPython = python.pkgs.wxPython_4_2; + addonPath = "addon.zip"; + addonsDrvs = map (pkg: pkg.override { inherit addonPath python3; }) addons; + + addonsJoined = + runCommand "addonsJoined" + { + inherit addonsDrvs; + nativeBuildInputs = [ unzip jq ]; + } '' + mkdir $out + + for pkg in $addonsDrvs; do + unzip $pkg/addon.zip -d unpacked + + folder_name=$(jq .identifier unpacked/metadata.json --raw-output | tr . _) + for d in unpacked/*; do + if [ -d "$d" ]; then + dest=$out/share/kicad/scripting/$(basename $d)/$folder_name + mkdir -p $(dirname $dest) + + mv $d $dest + fi + done + rm -r unpacked + done + ''; inherit (lib) concatStringsSep flatten optionalString optionals; in @@ -113,6 +152,7 @@ stdenv.mkDerivation rec { # Common libraries, referenced during runtime, via the wrapper. passthru.libraries = callPackages ./libraries.nix { inherit libSrc; }; + passthru.callPackage = newScope { inherit addonPath python3; }; base = callPackage ./base.nix { inherit stable baseName; inherit kicadSrc kicadVersion; @@ -131,7 +171,7 @@ stdenv.mkDerivation rec { dontFixup = true; pythonPath = optionals (withScripting) - [ wxPython python.pkgs.six python.pkgs.requests ]; + [ wxPython python.pkgs.six python.pkgs.requests ] ++ addonsDrvs; nativeBuildInputs = [ makeWrapper ] ++ optionals (withScripting) @@ -164,6 +204,17 @@ stdenv.mkDerivation rec { "--set-default KICAD7_SYMBOL_DIR ${symbols}/share/kicad/symbols" "--set-default KICAD7_TEMPLATE_DIR ${template_dir}" ] + ++ optionals (addons != [ ]) ( + let stockDataPath = symlinkJoin { + name = "kicad_stock_data_path"; + paths = [ + "${base}/share/kicad" + "${addonsJoined}/share/kicad" + ]; + }; + in + [ "--set-default NIX_KICAD7_STOCK_DATA_PATH ${stockDataPath}" ] + ) ++ optionals (with3d) [ "--set-default KICAD7_3DMODEL_DIR ${packages3d}/share/kicad/3dmodels" diff --git a/pkgs/applications/science/electronics/kicad/runtime_stock_data_path.patch b/pkgs/applications/science/electronics/kicad/runtime_stock_data_path.patch new file mode 100644 index 000000000000..16f7e493c623 --- /dev/null +++ b/pkgs/applications/science/electronics/kicad/runtime_stock_data_path.patch @@ -0,0 +1,15 @@ +diff --git a/common/paths.cpp b/common/paths.cpp +index a74cdd9..790cc58 100644 +--- a/common/paths.cpp ++++ b/common/paths.cpp +@@ -151,6 +151,10 @@ wxString PATHS::GetStockDataPath( bool aRespectRunFromBuildDir ) + { + wxString path; + ++ if( wxGetEnv( wxT( "NIX_KICAD7_STOCK_DATA_PATH" ), &path ) ) { ++ return path; ++ } ++ + if( aRespectRunFromBuildDir && wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) ) + { + // Allow debugging from build dir by placing relevant files/folders in the build root diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index ba0a724f56d6..f05d13560401 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -39768,6 +39768,8 @@ with pkgs; with3d = false; }; + kicadAddons = recurseIntoAttrs (callPackage ../applications/science/electronics/kicad/addons {}); + librepcb = libsForQt5.callPackage ../applications/science/electronics/librepcb { }; ngspice = libngspice.override {