diff --git a/.gitignore b/.gitignore index e91d255..a63d0e9 100644 --- a/.gitignore +++ b/.gitignore @@ -28,4 +28,4 @@ __pycache__ *.ses # Generated Production Files -production \ No newline at end of file +result \ No newline at end of file diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..b077b25 --- /dev/null +++ b/flake.lock @@ -0,0 +1,44 @@ +{ + "nodes": { + "kicad_bom_generator": { + "flake": false, + "locked": { + "lastModified": 1702353729, + "narHash": "sha256-NIM/GLC71VdGdMletBBv9lSPuHpgD9zzeGiVQLEAULA=", + "ref": "refs/heads/main", + "rev": "72686f5556785c9aa13678dc42757dddfb7d7c23", + "revCount": 2, + "type": "git", + "url": "https://git.m-labs.hk/linuswck/KiCAD_BOM_Generator.git" + }, + "original": { + "type": "git", + "url": "https://git.m-labs.hk/linuswck/KiCAD_BOM_Generator.git" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1702233072, + "narHash": "sha256-H5G2wgbim2Ku6G6w+NSaQaauv6B6DlPhY9fMvArKqRo=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "781e2a9797ecf0f146e81425c822dca69fe4a348", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-23.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "kicad_bom_generator": "kicad_bom_generator", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..aee8993 --- /dev/null +++ b/flake.nix @@ -0,0 +1,90 @@ +{ + inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11"; + inputs.kicad_bom_generator = { + url = "git+https://git.m-labs.hk/linuswck/KiCAD_BOM_Generator.git"; + flake = false; + }; + + outputs = { self, nixpkgs, kicad_bom_generator }: + let + pkgs = import nixpkgs { system = "x86_64-linux"; }; + + build = { variant }: + let + production_files = pkgs.stdenv.mkDerivation { + name = "production-files"; + src = ./kirdy_LD_adapter_${variant}; + + nativeBuildInputs = [ pkgs.kicad pkgs.zip pkgs.python3 ]; + + buildPhase = '' + # kicad-cli requires the use of $HOME + export HOME=/tmp + + if [ "${variant}" != "copper_plate" ]; then + SCH=kirdy_LD_adapter_${variant}.kicad_sch + PCB=kirdy_LD_adapter_${variant}.kicad_pcb + + # Get Revision Number from the Title Block in KiCAD Top Schematics + REV=$(cat $SCH | grep rev | cut -d'"' -f 2) + PREFIX=kirdy_LD_adapter_${variant}_$REV + + kicad-cli sch export python-bom $SCH -o $PREFIX"_bom".xml + export PYTHONPATH=${pkgs.kicad.base}/share/kicad/plugins + python ${kicad_bom_generator}/generate_bom_from_xml.py $PREFIX"_bom".xml $PREFIX"_bom".csv + + kicad-cli sch export pdf $SCH -o $PREFIX.pdf + kicad-cli pcb export pos $PCB --format csv --units mm -o $PREFIX"_pos".csv + + export KICAD7_3DMODEL_DIR=${pkgs.kicad.libraries.packages3d}/share/kicad/3dmodels + kicad-cli pcb export step $PCB --subst-models --force -o $PREFIX.step + + mkdir -p $PREFIX"_gerber_drill" + kicad-cli pcb export gerbers $PCB -l 'F.Cu,B.Cu,F.Paste,B.Paste,F.Silkscreen,B.Silkscreen,F.Mask,B.Mask,Edge.Cuts' --no-x2 --subtract-soldermask -o ./$PREFIX"_gerber_drill" + + # The additional trailing slash is due to a bug in the kicad-cli tool. https://gitlab.com/kicad/code/kicad/-/issues/14438 + kicad-cli pcb export drill $PCB -u mm --generate-map --map-format gerberx2 -o ./$PREFIX"_gerber_drill"/ + + zip -r -j $PREFIX"_gerber_drill" $PREFIX"_gerber_drill" + else + PCB=kirdy_LD_adapter_${variant}.kicad_pcb + + # Get Revision Number from the Title Block in KiCAD PCB + REV=$(cat $PCB | grep rev | cut -d'"' -f 2) + PREFIX=kirdy_LD_adapter_${variant}_$REV + + export KICAD7_3DMODEL_DIR=${pkgs.kicad.libraries.packages3d}/share/kicad/3dmodels + kicad-cli pcb export step $PCB --subst-models --force -o $PREFIX.step + fi + ''; + + installPhase = '' + mkdir -p $out/production_files + if [ ${variant} != "copper_plate" ]; then + cp $PREFIX"_bom".csv $out/production_files/$PREFIX"_bom".csv + cp $PREFIX.pdf $out/production_files/$PREFIX.pdf + cp $PREFIX"_pos".csv $out/production_files/$PREFIX"_pos.csv" + cp $PREFIX.step $out/production_files/$PREFIX.step + cp $PREFIX"_gerber_drill".zip $out/production_files/$PREFIX"_gerber_drill".zip + else + cp $PREFIX.step $out/production_files/$PREFIX.step + fi + ''; + }; + in { "LD_adapter_${variant}" = production_files; }; + + in rec { + formatter.x86_64-linux = nixpkgs.legacyPackages.x86_64-linux.nixfmt; + + packages.x86_64-linux = (build { variant = "type_1"; }) + // (build { variant = "type_2"; }) + // (build { variant = "copper_plate"; }); + + devShells.x86_64-linux.default = + let pkgs = nixpkgs.legacyPackages.x86_64-linux; + in pkgs.mkShell { + name = "kicad-dev-shell"; + buildInputs = [ pkgs.kicad ]; + }; + }; +}