{ flake-parts-lib, inputs, ... }: { flake = { templates = { cad = { path = ./template; description = "A python flake with a build132d/cadquery parametric cad setup"; }; }; flakeModules.cad = { imports = [ inputs.treefmt-nix.flakeModule ]; options.perSystem = flake-parts-lib.mkPerSystemOption ({ lib, config, pkgs, system, ... }: let cfg = config.krantz.cad; cq = inputs.cadquery.packages.${system}; pyEnv = cq.python3.withPackages (p: with p; [ cadquery build123d ]); outputExt = [ "step" "stl" "3mf" ]; in { options.krantz.cad = { enable = lib.mkEnableOption "executing a python build123d/cadquery file to generate 3d files"; builder = lib.mkOption { description = "The path to python cad script that generates the 3d file(s)."; type = lib.types.path; example = lib.literalExpression "./build3d.py"; }; outputName = lib.mkOption { description = '' The derivation's name that will also be used to rename the following default outputs. ${ # output.${ext} where ext is from the outputExt list lib.concatStringsSep "\n" (lib.map (ext: "* output.${ext}") outputExt) } If left empty no files will be renamed. ''; type = lib.types.str; default = ""; example = "box_for_stuf"; }; devShell = { enable = lib.mkEnableOption "a build123d/cadquery development environment" // {default = true;}; name = lib.mkOption { description = "The name of the attribute in devShells to configure"; type = lib.types.str; default = "default"; example = "cad"; }; extraAttrs = lib.mkOption { description = "Extra attributes to merge into the devshell. See https://nixos.org/manual/nixpkgs/stable/#sec-pkgs-mkShell"; type = lib.types.attrs; default = {}; example = { shellHook = '' export DEBUG=1 ''; }; }; }; package = { enable = lib.mkEnableOption "a package output for the 3d file" // {default = true;}; name = lib.mkOption { description = "The name of the attribute in packages to configure"; type = lib.types.str; default = "default"; example = "3dfiles"; }; }; formatter = { enable = lib.mkEnableOption "treefmt for formatting multiple types of files" // {default = true;}; nix = lib.mkEnableOption "formatting for nix files" // {default = true;}; }; }; config = lib.mkIf cfg.enable { # Dev Shell that lets you enter an environment with all the necessary utilites # Available through 'nix develop' # Also can be activated automatically if direnv is installed on the system with 'direnv allow' devShells = lib.mkIf cfg.devShell.enable { ${cfg.devShell.name} = pkgs.mkShell { packages = [ pyEnv ]; shellHook = '' export PYTHONPATH="${pyEnv}/${pyEnv.sitePackages}:$PYTHONPATH" ''; } // cfg.devShell.extraAttrs; }; # Your custom packages # Accessible through 'nix build', 'nix shell', 'nix run', etc packages = lib.mkIf cfg.package.enable { ${cfg.package.name} = pkgs.runCommand cfg.outputName {} /* bash */ '' mkdir $out cd $out ${pyEnv}/bin/python ${cfg.builder} ${ if cfg.outputName != "" then /* bash */ '' for ext in ${lib.concatStringsSep " " outputExt}; do if [[ -f output.$ext ]]; then mv output.$ext ${cfg.outputName}.$ext fi done '' else "" } ''; }; # Formatter for nix files, available through 'nix fmt' treefmt.programs = lib.mkIf cfg.formatter.enable { alejandra.enable = lib.mkIf cfg.formatter.nix true; }; }; }); }; }; }