{ inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11"; inputs.kicad_bom_generator = { url = "git+https://git.m-labs.hk/M-Labs/KiCAD_BOM_Generator.git"; flake = false; }; outputs = { self, nixpkgs, kicad_bom_generator }: let pkgs = import nixpkgs { system = "x86_64-linux"; }; build_copper_plate = { }: let copper_plate = pkgs.stdenv.mkDerivation { name = "production-files"; src = ./kirdy_LD_adapter_copper_plate; nativeBuildInputs = [ pkgs.kicad ]; PCB = "kirdy_LD_adapter_copper_plate.kicad_pcb"; buildPhase = '' # kicad-cli requires the use of $HOME export HOME=$(mktemp -d tmp.XXXX) # Get Revision Number from the Title Block in KiCAD PCB REV=$(cat $PCB | grep rev | cut -d'"' -f 2) PREFIX=kirdy_LD_adapter_copper_plate_$REV export KICAD7_3DMODEL_DIR=${pkgs.kicad.libraries.packages3d}/share/kicad/3dmodels kicad-cli pcb export step $PCB --subst-models --force -o $PREFIX.step ''; installPhase = '' mkdir -p $out cp $PREFIX.step $out/$PREFIX.step ''; }; in rec { "LD_adapter_copper_plate_step" = copper_plate; }; build_PCB = { variant }: let bom = pkgs.stdenv.mkDerivation { name = "bom"; src = ./kirdy_LD_adapter_${variant}; nativeBuildInputs = [ pkgs.kicad pkgs.python3 ]; SCH = "kirdy_LD_adapter_${variant}.kicad_sch"; PCB = "kirdy_LD_adapter_${variant}.kicad_pcb"; buildPhase = '' # kicad-cli requires the use of $HOME export HOME=$(mktemp -d tmp.XXXX) # Get Revision Number from the Title Block in KiCAD PCB 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 ''; installPhase = '' mkdir -p $out cp $PREFIX"_bom".csv $out/$PREFIX"_bom".csv ''; }; step = pkgs.stdenv.mkDerivation { name = "step"; src = ./kirdy_LD_adapter_${variant}; nativeBuildInputs = [ pkgs.kicad pkgs.python3 ]; SCH = "kirdy_LD_adapter_${variant}.kicad_sch"; PCB = "kirdy_LD_adapter_${variant}.kicad_pcb"; buildPhase = '' # kicad-cli requires the use of $HOME export HOME=$(mktemp -d tmp.XXXX) # Get Revision Number from the Title Block in KiCAD PCB REV=$(cat $SCH | 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 ''; installPhase = '' mkdir -p $out cp $PREFIX.step $out/$PREFIX.step ''; }; pos = pkgs.stdenv.mkDerivation { name = "pos"; src = ./kirdy_LD_adapter_${variant}; nativeBuildInputs = [ pkgs.kicad ]; SCH = "kirdy_LD_adapter_${variant}.kicad_sch"; PCB = "kirdy_LD_adapter_${variant}.kicad_pcb"; buildPhase = '' # kicad-cli requires the use of $HOME export HOME=$(mktemp -d tmp.XXXX) # Get Revision Number from the Title Block in KiCAD PCB REV=$(cat $SCH | grep rev | cut -d'"' -f 2) PREFIX=kirdy_LD_adapter_${variant}_$REV kicad-cli pcb export pos $PCB --format csv --units mm -o $PREFIX"_pos".csv ''; installPhase = '' mkdir -p $out cp $PREFIX"_pos".csv $out/$PREFIX"_pos.csv" ''; }; gerber_and_drill = pkgs.stdenv.mkDerivation { name = "gerber_and_drill"; src = ./kirdy_LD_adapter_${variant}; nativeBuildInputs = [ pkgs.kicad pkgs.zip ]; SCH = "kirdy_LD_adapter_${variant}.kicad_sch"; PCB = "kirdy_LD_adapter_${variant}.kicad_pcb"; buildPhase = '' # kicad-cli requires the use of $HOME export HOME=$(mktemp -d tmp.XXXX) # 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 mkdir -p $PREFIX"_gerber" 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" mkdir -p $PREFIX"_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"_drill"/ ''; installPhase = '' mkdir -p $out cp -r $PREFIX"_gerber" $out/ cp -r $PREFIX"_drill" $out/ ''; }; pdf = pkgs.stdenv.mkDerivation { name = "pdf"; src = ./kirdy_LD_adapter_${variant}; nativeBuildInputs = [ pkgs.kicad ]; SCH = "kirdy_LD_adapter_${variant}.kicad_sch"; buildPhase = '' # kicad-cli requires the use of $HOME export HOME=$(mktemp -d tmp.XXXX) # 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 pdf $SCH -o $PREFIX.pdf ''; installPhase = '' mkdir -p $out cp $PREFIX.pdf $out/$PREFIX.pdf ''; }; all_prod_files = pkgs.runCommand "LD_adapter_${variant}_all_prod_files" { buildInputs = [ ]; } '' mkdir -p $out ln -s ${pdf}/$(ls ${pdf} | grep "pdf") $out ln -s ${bom}/$(ls ${bom} | grep ".csv") $out ln -s ${pos}/$(ls ${pos} | grep ".csv") $out ln -s ${step}/$(ls ${step} | grep ".step") $out ln -s ${gerber_and_drill}/$(ls ${gerber_and_drill} | grep "gerber") $out ln -s ${gerber_and_drill}/$(ls ${gerber_and_drill} | grep "drill") $out ''; in { "LD_adapter_${variant}_step" = step; "LD_adapter_${variant}_gerber_and_drill" = gerber_and_drill; "LD_adapter_${variant}_bom" = bom; "LD_adapter_${variant}_pos" = pos; "LD_adapter_${variant}_pdf" = pdf; "LD_adapter_${variant}_all_prod_files" = all_prod_files; }; in rec { formatter.x86_64-linux = nixpkgs.legacyPackages.x86_64-linux.nixfmt; packages.x86_64-linux = (build_PCB { variant = "type_1"; }) // (build_PCB { variant = "type_2"; }) // (build_copper_plate { }); devShells.x86_64-linux.default = let pkgs = nixpkgs.legacyPackages.x86_64-linux; in pkgs.mkShell { name = "kicad-dev-shell"; buildInputs = [ pkgs.kicad ]; }; }; }