Shuttler_AFE_PSU/flake.nix

156 lines
6.5 KiB
Nix

{
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
inputs.mozilla-overlay = { url = github:mozilla/nixpkgs-mozilla; flake = false; };
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, mozilla-overlay }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; overlays = [ (import mozilla-overlay) ]; };
formatter.x86_64-linux = nixpkgs.legacyPackages.x86_64-linux.nixfmt;
PCB = pkgs.stdenv.mkDerivation {
name = "production_files";
src = ./src;
nativeBuildInputs = [ pkgs.kicad pkgs.zip pkgs.python3 ];
buildPhase = ''
# kicad-cli requires the use of $HOME
TMP_DIR="$(mktemp --tmpdir -d kicad-shuttler_afe_psu.XXXXXXX)"
export HOME=$TMP_DIR
SCH=shuttler_afe_psu.kicad_sch
PCB=shuttler_afe_psu.kicad_pcb
# Get Revision Number from the Title Block in KiCAD Top Schematics
REV=$(cat $SCH | grep rev | cut -d'"' -f 2)
PREFIX=shuttler_afe_psu_$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,In1.Cu,In2.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"
'';
installPhase = ''
mkdir -p $out/production_files
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
'';
};
FP = pkgs.stdenv.mkDerivation {
name = "production_files";
src = ./Front_Panel;
nativeBuildInputs = [ pkgs.kicad pkgs.zip pkgs.python3 ];
buildPhase = ''
cd ./src
FP=Shuttler_AFE_Front_Panel.kicad_pcb
FP_Cover=Shuttler_AFE_Front_Panel_Cover.kicad_pcb
# Get Revision Number from the Title Block in KiCAD PCB
REV=$(cat $FP | grep rev | cut -d'"' -f 2)
FRONT_FP_PREFIX=Shuttler_AFE_Front_Panel_$REV
FRONT_PANEL_COVER_PREFIX=Shuttler_AFE_Front_Panel_Cover_$REV
# kicad-cli requires the use of $HOME
TMP_DIR="$(mktemp --tmpdir -d kicad-Shuttler_AFE_Front_Panel.XXXXXXX)"
export HOME=$TMP_DIR
export KICAD7_3DMODEL_DIR=${pkgs.kicad.libraries.packages3d}/share/kicad/3dmodels
kicad-cli pcb export step $FP --subst-models --force -o $FRONT_FP_PREFIX.step
kicad-cli pcb export dxf $FP -l 'F.Silkscreen,Edge.Cuts' -o $FRONT_FP_PREFIX.dxf
kicad-cli pcb export svg $FP -l 'F.Silkscreen,Edge.Cuts' --black-and-white --page-size-mode 2 --exclude-drawing-sheet -o $FRONT_FP_PREFIX.svg
kicad-cli pcb export step $FP_Cover --subst-models --force -o $FRONT_PANEL_COVER_PREFIX.step
kicad-cli pcb export dxf $FP_Cover -l 'F.Silkscreen,Edge.Cuts' -o $FRONT_PANEL_COVER_PREFIX.dxf
kicad-cli pcb export svg $FP_Cover -l 'F.Silkscreen,Edge.Cuts' --black-and-white --page-size-mode 2 --exclude-drawing-sheet -o $FRONT_PANEL_COVER_PREFIX.svg
'';
installPhase = ''
mkdir -p $out/production_files
cp *.step $out/production_files
cp *.dxf $out/production_files
cp *.svg $out/production_files
cp ../3D/* $out/production_files
'';
};
ENC_P = pkgs.stdenv.mkDerivation {
name = "production_files";
src = ./Enclosure_End_Panels;
nativeBuildInputs = [ pkgs.kicad pkgs.zip pkgs.python3 ];
buildPhase = ''
cd ./src
FRONT_FP=Shuttler_AFE_Enclosure_Front_Panel.kicad_pcb
REAR_FP=Shuttler_AFE_Enclosure_Rear_Panel.kicad_pcb
# Get Revision Number from the Title Block in KiCAD PCB
REV=$(cat $FRONT_FP | grep rev | cut -d'"' -f 2)
FRONT_FP_PREFIX=Shuttler_AFE_Front_Panel_$REV
REAR_FP_PREFIX=Shuttler_AFE_Front_Panel_Cover_$REV
# kicad-cli requires the use of $HOME
TMP_DIR="$(mktemp --tmpdir -d kicad-Shuttler_AFE_Front_Panel.XXXXXXX)"
export HOME=$TMP_DIR
export KICAD7_3DMODEL_DIR=${pkgs.kicad.libraries.packages3d}/share/kicad/3dmodels
kicad-cli pcb export step $FRONT_FP --subst-models --force -o $FRONT_FP_PREFIX.step
kicad-cli pcb export dxf $FRONT_FP -l 'F.Silkscreen,Edge.Cuts' -o $FRONT_FP_PREFIX.dxf
kicad-cli pcb export svg $FRONT_FP -l 'F.Silkscreen,Edge.Cuts' --black-and-white --page-size-mode 2 --exclude-drawing-sheet -o $FRONT_FP_PREFIX.svg
kicad-cli pcb export step $REAR_FP --subst-models --force -o $REAR_FP_PREFIX.step
kicad-cli pcb export dxf $REAR_FP -l 'F.Silkscreen,Edge.Cuts' -o $REAR_FP_PREFIX.dxf
kicad-cli pcb export svg $REAR_FP -l 'F.Silkscreen,Edge.Cuts' --black-and-white --page-size-mode 2 --exclude-drawing-sheet -o $REAR_FP_PREFIX.svg
'';
installPhase = ''
mkdir -p $out/production_files
cp *.step $out/production_files
cp *.dxf $out/production_files
cp *.svg $out/production_files
'';
};
in {
packages.x86_64-linux = {
"PCB" = PCB;
"Front_Panels" = FP;
"Enclosure_Panels" = ENC_P;
};
devShells.x86_64-linux.default =
let pkgs = nixpkgs.legacyPackages.x86_64-linux;
in pkgs.mkShell {
name = "kicad-dev-shell";
buildInputs = [ pkgs.kicad ];
};
};
}