diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3a125c0..d3ed3fa 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -90,6 +90,14 @@ local development. 7. Submit a pull request through the GitHub website. + +# Nix +If you're using [nix](https://nix.dev/) package manager you can use included `flake.nix` by : + +```console +$ nix develop +``` + # Pull Request Guidelines Before you submit a pull request, check that it meets these guidelines: diff --git a/flake.lock b/flake.lock index a400313..98a819c 100644 --- a/flake.lock +++ b/flake.lock @@ -18,6 +18,85 @@ "type": "github" } }, + "lix-unit": { + "inputs": { + "mdbook-nixdoc": [ + "pyproject-nix", + "mdbook-nixdoc" + ], + "nix-github-actions": [ + "pyproject-nix", + "nix-github-actions" + ], + "nixpkgs": [ + "pyproject-nix", + "nixpkgs" + ], + "treefmt-nix": [ + "pyproject-nix", + "treefmt-nix" + ] + }, + "locked": { + "lastModified": 1727322567, + "narHash": "sha256-scZo6AyJTxTK9wYW0HmWDzLxVxOoFr7/XkIVJCmmOe4=", + "owner": "adisbladis", + "repo": "lix-unit", + "rev": "59c489dbc5b27a83fadc94fde2c2b69abb4c0e80", + "type": "github" + }, + "original": { + "owner": "adisbladis", + "repo": "lix-unit", + "type": "github" + } + }, + "mdbook-nixdoc": { + "inputs": { + "nix-github-actions": [ + "pyproject-nix", + "nix-github-actions" + ], + "nixpkgs": [ + "pyproject-nix", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1708395692, + "narHash": "sha256-smf0VmxGbjJDZqKvxxG3ZVqubgbVwAWG26wPo+BT/A0=", + "owner": "adisbladis", + "repo": "mdbook-nixdoc", + "rev": "d6a71b114b9221c0b4f20d31b81766d072cc26be", + "type": "github" + }, + "original": { + "owner": "adisbladis", + "repo": "mdbook-nixdoc", + "type": "github" + } + }, + "nix-github-actions": { + "inputs": { + "nixpkgs": [ + "pyproject-nix", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1720066371, + "narHash": "sha256-uPlLYH2S0ACj0IcgaK9Lsf4spmJoGejR9DotXiXSBZQ=", + "owner": "nix-community", + "repo": "nix-github-actions", + "rev": "622f829f5fe69310a866c8a6cd07e747c44ef820", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nix-github-actions", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1727348695, @@ -34,10 +113,35 @@ "type": "github" } }, + "pyproject-nix": { + "inputs": { + "lix-unit": "lix-unit", + "mdbook-nixdoc": "mdbook-nixdoc", + "nix-github-actions": "nix-github-actions", + "nixpkgs": [ + "nixpkgs" + ], + "treefmt-nix": "treefmt-nix" + }, + "locked": { + "lastModified": 1727951589, + "narHash": "sha256-/gPhr5thmc/Li2L+OoOyGe5Sef6j3Zzp44zRcbokysM=", + "owner": "nix-community", + "repo": "pyproject.nix", + "rev": "ed7903195e72b2fd00dfaa3be9e9bcab8f2957ad", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "pyproject.nix", + "type": "github" + } + }, "root": { "inputs": { "flake-utils": "flake-utils", - "nixpkgs": "nixpkgs" + "nixpkgs": "nixpkgs", + "pyproject-nix": "pyproject-nix" } }, "systems": { @@ -54,6 +158,27 @@ "repo": "default", "type": "github" } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": [ + "pyproject-nix", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1727252110, + "narHash": "sha256-3O7RWiXpvqBcCl84Mvqa8dXudZ1Bol1ubNdSmQt7nF4=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "1bff2ba6ec22bc90e9ad3f7e94cca0d37870afa3", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index f587293..46afc93 100644 --- a/flake.nix +++ b/flake.nix @@ -1,13 +1,94 @@ { - description = "Python dev shell flake"; + description = "Wrapper to run programs with different env"; inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; flake-utils.url = "github:numtide/flake-utils"; + pyproject-nix.url = "github:nix-community/pyproject.nix"; + pyproject-nix.inputs.nixpkgs.follows = "nixpkgs"; }; - outputs = { nixpkgs, flake-utils, ... }: + outputs = { self, nixpkgs, flake-utils, pyproject-nix, }: flake-utils.lib.eachDefaultSystem (system: - let pkgs = nixpkgs.legacyPackages.${system}; - in { devShells = { default = import ./shell.nix { inherit pkgs; }; }; }); + let + pkgs = nixpkgs.legacyPackages.${system}; + pypkgs = pkgs.python3Packages; + python = pkgs.python3.override { + packageOverrides = self: super: { + runenv = runenv; + hatch = pkgs.hatch; + ruff = pkgs.ruff; + mypy = pkgs.mypy; + black = pkgs.black; + }; + }; + project = + pyproject-nix.lib.project.loadPyproject { projectRoot = ./.; }; + + runenv = python.pkgs.buildPythonPackage rec { + pname = "runenv"; + version = "1.2.2"; + + src = python.pkgs.fetchPypi { + inherit pname version; + sha256 = ""; + }; + + doCheck = false; + checkInputs = [ ]; + propagatedBuildInputs = [ pypkgs.feedparser ]; + + meta = with pkgs.lib; { + homepage = "https://github.com/onjin/runenv"; + description = "Wrapper to run programs with different env"; + license = licenses.mit; + }; + }; + + in { + packages = { + runenv = let + # Returns an attribute set that can be passed to `buildPythonPackage`. + attrs = project.renderers.buildPythonPackage { + inherit python; + extras = [ ]; + }; + # Pass attributes to buildPythonPackage. + # Here is a good spot to add on any missing or custom attributes. + in python.pkgs.buildPythonPackage (attrs // { + # Because we're following main, use the git rev as version + version = + if (self ? rev) then self.shortRev else self.dirtyShortRev; + }); + default = self.packages.${system}.runenv; + }; + devShells = { + default = let + # Returns a function that can be passed to `python.withPackages` + arg = project.renderers.withPackages { + inherit python; + extras = [ "develop" ]; + }; + + # Returns a wrapped environment (virtualenv like) with all our packages + pythonEnv = python.withPackages arg; + + # Create a devShell like normal. + in pkgs.mkShell { + packages = [ + self.packages.${system}.runenv + pythonEnv + pkgs.gnumake + pkgs.hatch + pkgs.ruff + pkgs.mypy + pkgs.black + pypkgs.keyrings-alt + ]; + shellHook = '' + export PYTHONPATH="$(pwd):$PYTHONPATH" + ''; + }; + }; + }); } diff --git a/pyproject.toml b/pyproject.toml index fd343f1..75629cc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,6 +35,14 @@ Documentation = "https://github.com/onjin/runenv#readme" Issues = "https://github.com/onjin/runenv/issues" Source = "https://github.com/onjin/runenv" +[project.optional-dependencies] +develop = [ + "hatch", + "black", + "ruff", + "mypy", +] + [tool.hatch.version] path = "runenv/__about__.py" diff --git a/shell.nix b/shell.nix deleted file mode 100644 index 0a8803d..0000000 --- a/shell.nix +++ /dev/null @@ -1,34 +0,0 @@ -{ pkgs ? import { } }: -let - - # set your python version here - pythonPkg = pkgs.python310; - pythonEnv = pythonPkg.withPackages (pythonPkgs: - with pythonPkgs; [ - pip - virtualenv - # add nix python packages to install with python - ]); - lib-path = with pkgs; lib.makeLibraryPath [ libffi openssl stdenv.cc.cc ]; - -in pkgs.mkShell { - packages = [ pkgs.bashInteractive ]; - buildInputs = with pkgs; [ pythonEnv ]; - shellHook = '' - - # Allow the use of wheels. - SOURCE_DATE_EPOCH=$(date +%s) - - # Augment the dynamic linker path - export "LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${lib-path}" - - # Create local venv - VENV=$PWD/.venv - if test ! -d $VENV; then - virtualenv $VENV - fi - source $VENV/bin/activate - export PYTHONPATH=$VENV/${pythonEnv.sitePackages}/:$PYTHONPATH - export PATH=$VENV/bin:$PATH - ''; -}