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 May 20, 2024
1 parent 3a8c454 commit 0a0edba
Show file tree
Hide file tree
Showing 5 changed files with 75 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 @@ -4,13 +4,28 @@
repos:
- repo: local
hooks:
- id: alejandra
name: alejandra (Nix)
entry: alejandra
files: \.nix$
language: system
stages: [commit]

- id: black
name: black
entry: black
language: system
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 Down Expand Up @@ -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
39 changes: 39 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
let
pkgs = import (
builtins.fetchTarball {
url = "https://github.com/nixos/nixpkgs/archive/33d1e753c82ffc557b4a585c77de43d4c922ebb5.tar.gz";
sha256 = "0s5rrp2spdqdc21h4faahq2h5giaznq8n757j20zl9ap8d7jk03i";
}
) {};
poetry2nix =
import (
builtins.fetchTarball {
url = "https://github.com/nix-community/poetry2nix/archive/291a863e866972f356967d0a270b259f46bf987f.tar.gz";
sha256 = "1mzsvkbxh5c1j82gsghfa3gc0amnsajygbw7n6wxn9mg48j5y45x";
}
) {
inherit pkgs;
};
poetryPackages = poetry2nix.mkPoetryPackages {
projectDir = ./.;
};
pythonWithPackages = poetryPackages.python.withPackages (_ps: poetryPackages.poetryPackages);
in
pkgs.mkShell {
packages = [
pythonWithPackages
pkgs.alejandra
pkgs.bashInteractive
pkgs.cacert
pkgs.deadnix
pkgs.gdal
pkgs.gitFull
pkgs.nodejs
pkgs.poetry
pkgs.statix
pkgs.which
];
shellHook = ''
ln --force --no-target-directory --symbolic "${pkgs.lib.getExe pythonWithPackages}" python
'';
}

0 comments on commit 0a0edba

Please sign in to comment.