106 lines
3.4 KiB
Nix
106 lines
3.4 KiB
Nix
{
|
|
description = "A Python Project Template.";
|
|
|
|
inputs = {
|
|
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
|
};
|
|
|
|
outputs = {
|
|
self,
|
|
nixpkgs,
|
|
...
|
|
} @ inputs: let
|
|
supportedSystems = ["x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin"];
|
|
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
|
|
pkgs = forAllSystems (system: nixpkgs.legacyPackages.${system}.extend overlay);
|
|
|
|
overlay = final: prev: rec {
|
|
python3Packages = prev.python3Packages.overrideScope (pfinal: pprev: {
|
|
packageNameToDrv = x: builtins.getAttr (cleanPythonPackageName x) final.python3Packages;
|
|
});
|
|
|
|
cleanPythonPackageName = x: let
|
|
cleanName = builtins.match "([a-z,A-Z,0-9,_,-]+).*" x;
|
|
in
|
|
if cleanName != null
|
|
then builtins.elemAt cleanName 0
|
|
else builtins.warn "Could not determine package name from '${x}'" null;
|
|
};
|
|
|
|
pyproject = builtins.fromTOML (builtins.readFile ./pyproject.toml);
|
|
|
|
buildDependencies = forAllSystems (system: builtins.map pkgs.${system}.python3Packages.packageNameToDrv pyproject.build-system.requires);
|
|
runtimeDependencies = forAllSystems (system: builtins.map pkgs.${system}.python3Packages.packageNameToDrv pyproject.project.dependencies);
|
|
optionalDependencies = forAllSystems (system: builtins.mapAttrs (name: value: builtins.map pkgs.${system}.python3Packages.packageNameToDrv value) pyproject.project.optional-dependencies);
|
|
in {
|
|
# `nix build`
|
|
packages = forAllSystems (system: let
|
|
buildProject = {skipCheck ? false}:
|
|
pkgs.${system}.python3Packages.buildPythonPackage {
|
|
pname = pyproject.project.name;
|
|
version = pyproject.project.version;
|
|
src = ./.;
|
|
|
|
pyproject = true;
|
|
|
|
build-system = buildDependencies.${system};
|
|
|
|
dependencies = runtimeDependencies.${system};
|
|
|
|
optional-dependencies = optionalDependencies.${system};
|
|
|
|
nativeCheckInputs = optionalDependencies.${system}.dev;
|
|
|
|
checkPhase = let
|
|
dev = builtins.map (x: x.pname) optionalDependencies.${system}.dev;
|
|
in ''
|
|
${
|
|
if builtins.elem "pytest" dev && !skipCheck
|
|
then "pytest tests"
|
|
else ""
|
|
}
|
|
${
|
|
if builtins.elem "mypy" dev && !skipCheck
|
|
then "mypy src"
|
|
else ""
|
|
}
|
|
${
|
|
if builtins.elem "pylint" dev && !skipCheck
|
|
then "pylint src"
|
|
else ""
|
|
}
|
|
'';
|
|
};
|
|
in {
|
|
default = self.packages.${system}."${pyproject.project.name}";
|
|
"${pyproject.project.name}" = buildProject {skipCheck = false;};
|
|
quick = buildProject {skipCheck = true;};
|
|
});
|
|
|
|
# `nix fmt`
|
|
formatter = forAllSystems (system: pkgs.${system}.alejandra);
|
|
|
|
# `nix develop`
|
|
devShells = forAllSystems (system: rec {
|
|
default = venv;
|
|
|
|
venv = pkgs.${system}.mkShell {
|
|
shellHook = ''
|
|
if [ ! -d .venv/ ]; then
|
|
echo "Creating Virtual Environment..."
|
|
${pkgs.${system}.python3}/bin/python3 -m venv .venv
|
|
fi
|
|
|
|
alias activate='source .venv/bin/activate'
|
|
|
|
echo "Entering Virtual Environment..."
|
|
source .venv/bin/activate
|
|
'';
|
|
};
|
|
});
|
|
|
|
# NixOS Module
|
|
nixosModules.default = import ./nix/module.nix inputs;
|
|
};
|
|
}
|