From 585caaf32725ea36239f15d4081d8e5561201c1d Mon Sep 17 00:00:00 2001 From: Anish Lakhwara Date: Sun, 20 Nov 2022 18:29:07 +1000 Subject: [PATCH] add fastapi --- basic_test.py | 18 +++++++++++ flake.lock | 60 +++++++++++++++++++++++++++++++++++ flake.nix | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++ justfile | 4 +++ shell.nix | 15 +++++++++ 5 files changed, 183 insertions(+) create mode 100644 basic_test.py create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 justfile create mode 100644 shell.nix diff --git a/basic_test.py b/basic_test.py new file mode 100644 index 0000000..5785147 --- /dev/null +++ b/basic_test.py @@ -0,0 +1,18 @@ +import os +from pytest import mark + +def test_one(): + assert 2 + 2 == 4 + +def test_nonstandard_module(): + import numpy as np + assert np.linspace(1,2).shape == (50,) + +@mark.skipif(os.getenv('GITHUB_ACTIONS') != 'true', + reason = 'Not running on GHA') +def test_version(): + import sys + vi = sys.version_info + found = f'python{vi.major}{vi.minor}' + expected = os.getenv('EXPECTED_PY') + assert(found == expected) diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..473cc79 --- /dev/null +++ b/flake.lock @@ -0,0 +1,60 @@ +{ + "nodes": { + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1641205782, + "narHash": "sha256-4jY7RCWUoZ9cKD8co0/4tFARpWB+57+r1bLLvXNJliY=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "b7547d3eed6f32d06102ead8991ec52ab0a4f1a7", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "locked": { + "lastModified": 1642700792, + "narHash": "sha256-XqHrk7hFb+zBvRg6Ghl+AZDq03ov6OshJLiSWOoX5es=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "846b2ae0fc4cc943637d3d1def4454213e203cba", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1655200170, + "narHash": "sha256-/yzkX+10sJhYNIcTtZ5ObS+nh/HrJp01XLaubzbRDcU=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "9ff91ce2e4c5d70551d4c8fd8830931c6c6b26b8", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-22.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-compat": "flake-compat", + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..aaabde3 --- /dev/null +++ b/flake.nix @@ -0,0 +1,86 @@ +# The core functionality is provided here using flakes. Legacy support for +# `nix-shell` is provided by a wrapper in `shell.nix`. + +# TODO Hacking around the Qt problems +# TODO PyPI package not in nixpkgs + + +{ + description = "Python development environment"; + + inputs = { + + # Version pinning is managed in flake.lock. Upgrading can be done with + # something like + # + # nix flake lock --update-input nixpkgs + + nixpkgs .url = "github:nixos/nixpkgs/nixos-22.05"; + flake-utils .url = "github:numtide/flake-utils"; + flake-compat = { + url = "github:edolstra/flake-compat"; + flake = false; + }; + + }; + + outputs = { self, nixpkgs, flake-utils, ... }: + + # Option 1: try to support each default system + flake-utils.lib.eachDefaultSystem # NB Some packages in nixpkgs are not supported on some systems + + # Option 2: try to support selected systems + # flake-utils.lib.eachSystem ["x86_64-linux" "i686-linux" "aarch64-linux" "x86_64-darwin"] + (system: + + let + pkgs = import nixpkgs { + inherit system; + # Any overlays you need can go here + overlays = [ ]; + }; + + # ----- A Python interpreter with the packages that interest us ------- + python-with-all-my-packages = (python: + (python.withPackages (ps: [ + ps.pytest + ps.fastapi + ]))); + + in + rec { + + #devShell = self.devShells.${ system }.python310; # does not need `rec` + devShell = devShells.python310; + + devShells = + builtins.listToAttrs ( + builtins.map + ( + pythonVersion: { + name = pythonVersion; + value = pkgs.mkShell { + buildInputs = [ + (python-with-all-my-packages pkgs.${ pythonVersion }) + pkgs.just + pkgs.cowsay + ]; + packages = [ + pkgs.python3Packages.python-lsp-server + pkgs.lolcat + pkgs.exa + ]; + shellHook = + '' + export PS1="${pythonVersion} devshell> " + alias foo='cowsay Foo' + alias bar='exa -l | lolcat' + alias baz='cowsay What is the difference between buildIntputs and packages? | lolcat' + ''; + }; + } + ) [ "python37" "python38" "python39" "python310" ] + ); + } + ); +} diff --git a/justfile b/justfile new file mode 100644 index 0000000..0d6c3b0 --- /dev/null +++ b/justfile @@ -0,0 +1,4 @@ +# -*-Makefile-*- + +test colours='': + pytest -v {{colours}} diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..6924cad --- /dev/null +++ b/shell.nix @@ -0,0 +1,15 @@ +# Adapts flake.nix to support legacy `nix-shell` + +(import + ( + let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + in + fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; + sha256 = lock.nodes.flake-compat.locked.narHash; + } + ) + { + src = ./.; + }).shellNix