Files
project-templates/build123d/default.nix
T

127 lines
3.9 KiB
Nix

{
flake-parts-lib,
inputs,
...
}: {
flake = {
templates = {
build123d = {
path = ./template;
description = "A python flake with a build132d/cadquery parametric cad setup";
};
};
flakeModules.build123d = {
imports = [
inputs.treefmt-nix.flakeModule
];
options.perSystem = flake-parts-lib.mkPerSystemOption ({
lib,
config,
pkgs,
self',
system,
...
}: let
cfg = config.krantz.build123d;
cq = inputs.cadquery.packages.${system};
pyEnv = cq.python3.withPackages (p:
with p; [
cadquery
build123d
]);
in {
options.krantz.build123d = {
enable = lib.mkEnableOption "executing a python build123d/cadquery file to get a 3d file";
objectName = lib.mkOption {
description = "What to name this object.";
type = lib.types.str;
example = "box";
};
builder = lib.mkOption {
description = "The path to python cad script that generates the 3d file.";
type = lib.types.path;
example = lib.literalExpression "./build3d.py";
};
outputFileName = lib.mkOption {
description = "The name of the 3d file the builder script will output. After the builder is run this file must appear in the current working directory.";
type = lib.types.str;
default = "output.step";
};
devShell = {
enable = lib.mkEnableOption "a build123d development environment" // {default = true;};
name = lib.mkOption {
description = "The name of the attribute in devShells to configure";
type = lib.types.str;
default = "default";
example = "build123d";
};
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 = "step";
};
};
};
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.${cfg.devShell.name} = lib.mkIf cfg.devShell.enable (pkgs.mkShell {
packages = [
pyEnv
];
}
// cfg.devShell.extraAttrs);
# Formatter for nix files, available through 'nix fmt'
treefmt = {
programs.alejandra.enable = lib.mkDefault true;
};
# Your custom packages
# Accessible through 'nix build', 'nix shell', 'nix run', etc
packages.${cfg.package.name} = lib.mkIf cfg.package.enable (pkgs.runCommand cfg.objectName {}
/*
bash
*/
''
RESULT_FILE_NAME=${cfg.objectName}.${lib.last (lib.splitString "." cfg.outputFileName)}
${pyEnv}/bin/python ${cfg.builder}
mkdir $out
mv ${cfg.outputFileName} $out/$RESULT_FILE_NAME
ln -s ./$RESULT_FILE_NAME $out/${cfg.outputFileName}
'');
};
});
};
};
}