From eeb7d900a488da04e5e26dfe901ec6fd2a39b2f0 Mon Sep 17 00:00:00 2001 From: "Daniel F. Murcia Rivera" Date: Mon, 1 Jul 2024 12:18:09 -0500 Subject: [PATCH] nix flakes support - support nix build - add dev env with direnv hook - update readme --- .envrc | 2 + .gitignore | 7 ++++ README.md | 7 ++++ build_pkg/default.nix | 63 +++++++++++++++++++++++++++++++ build_pkg/deps/default.nix | 15 ++++++++ build_pkg/filter.nix | 13 +++++++ flake.lock | 76 ++++++++++++++++++++++++++++++++++++++ flake.nix | 43 +++++++++++++++++++++ 8 files changed, 226 insertions(+) create mode 100644 .envrc create mode 100644 build_pkg/default.nix create mode 100644 build_pkg/deps/default.nix create mode 100644 build_pkg/filter.nix create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..bd5946e --- /dev/null +++ b/.envrc @@ -0,0 +1,2 @@ +# use https://github.com/nix-community/nix-direnv +use_flake ".#python311.dev_env" diff --git a/.gitignore b/.gitignore index 36f9861..d8551aa 100644 --- a/.gitignore +++ b/.gitignore @@ -143,3 +143,10 @@ cython_debug/ # For testing input/ output/ + +# nix build +result +.direnv + +# vs code +.vscode diff --git a/README.md b/README.md index 46569f1..8d5015a 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,10 @@ To install from GitHub: pip install -e git+https://github.com/quantum5/win2xcur.git +To install from nix flakes: + + nix profile install "github:quantum5/win2xcur#python311.bin" + ## Usage: `win2xcur` For example, if you want to convert [the sample cursor](sample/crosshair.cur) @@ -52,3 +56,6 @@ are using unconventional distros (e.g. Alpine) and are getting errors related to `wand`, please see the [Wand documentation on installation][wand-install]. [wand-install]: https://docs.wand-py.org/en/0.6.7/guide/install.html + +Nix installation will not interfere with your local python, try this +option if you have dependencies or python version conflicts. diff --git a/build_pkg/default.nix b/build_pkg/default.nix new file mode 100644 index 0000000..4b36c63 --- /dev/null +++ b/build_pkg/default.nix @@ -0,0 +1,63 @@ +{ + makesLib, + nixpkgs, + python_version, + src, +}: let + deps = import ./deps { + inherit nixpkgs python_version; + }; + + # Define the package requirements + build_required_deps = python_pkgs: { + runtime_deps = with python_pkgs; [ + numpy + wand + ]; + build_deps = with python_pkgs; [flit-core]; + test_deps = with python_pkgs; [ + mypy + pytest + pylint + ]; + }; + + # The pkg builder + bundle_builder = lib: pkgDeps: + makesLib.makePythonPyprojectPackage { + inherit (lib) buildEnv buildPythonPackage; + inherit pkgDeps src; + }; + + # Abstract builder to allow an alternative to override dependencies + build_bundle = builder: + # builder: Deps -> (PythonPkgs -> PkgDeps) -> (Deps -> PkgDeps -> Bundle) -> Bundle + # Deps: are the default project dependencies + # PythonPkgs -> PkgDeps: is the required dependencies builder + # Deps -> PkgDeps -> Bundle: is the bundle builder + builder deps build_required_deps bundle_builder; + + # Concrete bundle that uses python pkgs from the default + # i.e. the python nixpkg from the flake + bundle = build_bundle (default: required_deps: builder: builder default.lib (required_deps default.python_pkgs)); + + # Develompent environment + dev_env = let + template = makesLib.makePythonVscodeSettings { + env = bundle.env.dev; + bins = [ ]; + name = "win2xcur-env-dev-template"; + }; + hook = makesLib.makeScript { + name = "win2xcur-env-dev"; + entrypoint = "${template}/template"; + }; + in nixpkgs.mkShell { + packages = [bundle.env.dev]; + shellHook = "${hook}/bin/dev"; + }; + + # Executable application derivation + bin = deps.lib.toPythonApplication bundle.pkg; +in + bundle // {inherit build_bundle dev_env bin;} diff --git a/build_pkg/deps/default.nix b/build_pkg/deps/default.nix new file mode 100644 index 0000000..1eebd17 --- /dev/null +++ b/build_pkg/deps/default.nix @@ -0,0 +1,15 @@ +{ + nixpkgs, + python_version, +}: let + # Define required nixpkgs python builders + lib = { + buildEnv = nixpkgs."${python_version}".buildEnv.override; + inherit (nixpkgs."${python_version}".pkgs) buildPythonPackage toPythonApplication; + inherit (nixpkgs.python3Packages) fetchPypi; + }; + # Define the nixpkgs python packages overrides + python_pkgs = nixpkgs."${python_version}Packages"; +in { + inherit lib python_pkgs; +} diff --git a/build_pkg/filter.nix b/build_pkg/filter.nix new file mode 100644 index 0000000..a17f6ea --- /dev/null +++ b/build_pkg/filter.nix @@ -0,0 +1,13 @@ +let + metadata = (builtins.fromTOML (builtins.readFile ../pyproject.toml)).project; +in + path_filter: src: + path_filter { + root = src; + include = [ + "mypy.ini" + "pyproject.toml" + (path_filter.inDirectory metadata.name) + (path_filter.inDirectory "tests") + ]; + } diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..794f199 --- /dev/null +++ b/flake.lock @@ -0,0 +1,76 @@ +{ + "nodes": { + "makes": { + "inputs": { + "nixpkgs": "nixpkgs" + }, + "locked": { + "lastModified": 1718642562, + "narHash": "sha256-Zma+WJHFOOId/i/jrNamYxz7LTX2wPnnoTarlTop0Ws=", + "owner": "fluidattacks", + "repo": "makes", + "rev": "dcb3432e36a87b38e89530e6ca10d3dfa80c36ad", + "type": "github" + }, + "original": { + "owner": "fluidattacks", + "repo": "makes", + "type": "github" + } + }, + "nix_filter": { + "locked": { + "lastModified": 1710156097, + "narHash": "sha256-1Wvk8UP7PXdf8bCCaEoMnOT1qe5/Duqgj+rL8sRQsSM=", + "owner": "numtide", + "repo": "nix-filter", + "rev": "3342559a24e85fc164b295c3444e8a139924675b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "nix-filter", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1667092106, + "narHash": "sha256-ZQwHNd/RemupaV52ePNNv4Kp+LHzPjssEAumLijum4E=", + "owner": "nix-community", + "repo": "nixpkgs.lib", + "rev": "2c15a4c90737d33309daf869bae0daaa35eeb264", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nixpkgs.lib", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1719080280, + "narHash": "sha256-BBIherhYksOLQjlqh72/SvKLvdHgDyySaQD6rEm+n8Y=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "74d8dda4f86d6328a713575310ff7883547412c0", + "type": "github" + }, + "original": { + "owner": "nixos", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "makes": "makes", + "nix_filter": "nix_filter", + "nixpkgs": "nixpkgs_2" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..d51f8dd --- /dev/null +++ b/flake.nix @@ -0,0 +1,43 @@ +{ + description = "win2xcur is a tool to convert Windows .cur and .ani cursors to Xcursor format."; + inputs = { + makes.url = "github:fluidattacks/makes"; + nixpkgs.url = "github:nixos/nixpkgs"; + nix_filter.url = "github:numtide/nix-filter"; + }; + outputs = { + self, + nixpkgs, + nix_filter, + makes, + }: let + path_filter = nix_filter.outputs.lib; + src = import ./build_pkg/filter.nix path_filter self; + out = system: python_version: let + makesLib = makes.lib."${system}"; + pkgs = nixpkgs.legacyPackages."${system}"; + in + import ./build_pkg { + inherit src python_version makesLib; + nixpkgs = pkgs; + }; + supported = ["python39" "python310" "python311"]; + python_outs = system: + (builtins.listToAttrs (map (name: { + inherit name; + value = out system name; + }) + supported)) + // {build_with_python = out system; nixpkgs = nixpkgs.legacyPackages."${system}";}; + systems = [ + "aarch64-darwin" + "aarch64-linux" + "x86_64-darwin" + "x86_64-linux" + ]; + forAllSystems = nixpkgs.lib.genAttrs systems; + in { + packages = forAllSystems python_outs; + defaultPackage = self.packages; + }; +}