Skip to content

Commit

Permalink
feat: Support Nix for development
Browse files Browse the repository at this point in the history
- Install and enable Direnv to activate it when `cd`-ing into the project
- Cache Python packages in CI using the "linz" repository
- Lint and format the Nix code
- Create a symlink to the Python executable for easy IDE integration
  • Loading branch information
l0b0 committed Jun 17, 2024
1 parent 6c00b90 commit 9014f10
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
watch_file poetry.lock pyproject.toml
use nix
ln --force --no-target-directory --symbolic "$(which python)" python
10 changes: 7 additions & 3 deletions .github/workflows/format-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ jobs:
uses: actions/setup-python@65d7f2d534ac1bc67fcd62888c5f4f3d2cb2b236 # v4
with:
python-version: "3.10.6"
- uses: cachix/install-nix-action@8887e596b4ee1134dae06b98d573bd674693f47c # v26
- uses: cachix/cachix-action@18cf96c7c98e048e10a83abd92116114cd8504be # v14
with:
name: linz
authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"
- name: Format
run: nix-shell --pure --run 'pre-commit run --all-files'
- name: Install
run: |
pip install poetry
poetry install
- name: Format
run: |
poetry run pre-commit run --all-files
- name: Unit Tests
run: |
poetry run pytest --doctest-modules .
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.pyc
__pycache__
/.pytest_cache
/python
Thumbs.db
/.venv
/.vscode
24 changes: 24 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ repos:
stages: [commit]
types: [python]

- id: deadnix
name: deadnix
entry: deadnix
args: [--edit, --fail]
files: \.nix$
language: system
stages: [commit]

- id: gitlint
name: gitlint
entry: gitlint
Expand All @@ -34,6 +42,13 @@ repos:
types: [python]
require_serial: true

- id: nixfmt
name: nixfmt
entry: nixfmt
files: \.nix$
language: system
stages: [commit]

- id: pylint
name: pylint
entry: pylint
Expand All @@ -42,6 +57,15 @@ repos:
types: [python]
require_serial: true

- id: statix
name: statix
entry: statix
args: [check]
files: \.nix$
pass_filenames: false
language: system
stages: [commit]

- repo: https://github.com/pre-commit/mirrors-prettier
rev: a99a3fbe79a9d346cabd02a5e167ad0edafe616b # v2.3.0
hooks:
Expand Down
42 changes: 42 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
let
pkgs = import (builtins.fetchTarball {
name = "nixos-unstable-2024-06-05";
url = "https://github.com/nixos/nixpkgs/archive/57610d2f8f0937f39dbd72251e9614b1561942d8.tar.gz";
sha256 = "0k8az8vmfdk1n8xlza252sqk0hm1hfc7g67adin6jxqaab2s34n9";
}) { };
poetry2nix = import (builtins.fetchTarball {
name = "poetry2nix-2024.6.557458";
url = "https://github.com/nix-community/poetry2nix/archive/81662ae1ad31491eae3bb1d976fb74c71853bc63.tar.gz";
sha256 = "1zvlhzlc7mxr74qii3mkyn4iyd5rdivrm40yf7r7jvj9ry5gnbx9";
}) { inherit pkgs; };
poetryPackages = poetry2nix.mkPoetryPackages {
projectDir = ./.;
overrides = poetry2nix.overrides.withDefaults (
self: super: {
types-shapely = super.types-shapely.overridePythonAttrs (old: {
propagatedBuildInputs = old.propagatedBuildInputs or [ ] ++ [ self.setuptools ];
});
}
);
};
pythonWithPackages = poetryPackages.python.withPackages (_ps: poetryPackages.poetryPackages);
in
pkgs.mkShell {
packages = [
pythonWithPackages
pkgs.bashInteractive
pkgs.cacert
pkgs.deadnix
pkgs.gcc # To install Python debugging in IDEA
(pkgs.gdal.override { geos = pkgs.geos_3_11; })
pkgs.gitFull
pkgs.nixfmt-rfc-style
pkgs.nodejs
pkgs.poetry
pkgs.statix
pkgs.which
];
shellHook = ''
ln --force --no-target-directory --symbolic "${pkgs.lib.getExe pythonWithPackages}" python
'';
}

0 comments on commit 9014f10

Please sign in to comment.