Skip to content

Commit

Permalink
MacOS nix fixes
Browse files Browse the repository at this point in the history
Mac users reported issues that boiled down to `psycopg[binary]` not having a matching wheel. I thought that using the "pure Python installation" (just `psycopg`) would work, but this breaks even on Linux as the libpq dynamic library cannot be found (even when adding postgres to the nix shell packages or the build inputs of the Python packages). There is also `psycopg[c]` which compiles the extension at installation time. This seems to work and the compilation time is not too long either (on a Surface from this decade it took ~1min to compile the extension, same in a Mac VM).
  • Loading branch information
niklasmohrin authored Nov 11, 2024
2 parents 72d1785 + 0be15b9 commit 872ef2e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
devShells = forAllSystems (system:
let
pkgs = pkgsFor.${system};
extras = if pkgs.stdenv.isDarwin then [ "psycopg-c" ] else [ "psycopg-binary" ];
in
rec {
evap = pkgs.callPackage ./nix/shell.nix {
inherit (self.packages.${system}) python3;
poetry2nix = inputs.poetry2nix.lib.mkPoetry2Nix { inherit pkgs; };
pyproject = ./pyproject.toml;
poetrylock = ./poetry.lock;
inherit extras;
};
evap-dev = evap.override { poetry-groups = [ "dev" ]; };
default = evap-dev;
Expand Down
2 changes: 1 addition & 1 deletion nix/services.nix
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
fi
set -x
cp deployment/localsettings.template.py evap/localsettings.py
sed -i -e "s/\$SECRET_KEY/$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 32)/" evap/localsettings.py
sed -i -e "s/\$SECRET_KEY/$(head /dev/urandom | LC_ALL=C tr -dc A-Za-z0-9 | head -c 32)/" evap/localsettings.py
git submodule update --init
./manage.py migrate --noinput
./manage.py collectstatic --noinput
Expand Down
15 changes: 13 additions & 2 deletions nix/shell.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ pkgs, python3, poetry2nix, pyproject, poetrylock, poetry-groups ? [ ], extraPackages ? [ ], extraPythonPackages ? (ps: [ ]), ... }:
{ pkgs, lib ? pkgs.lib, python3, poetry2nix, pyproject, poetrylock, extras ? [ ], poetry-groups ? [ ], extraPackages ? [ ], extraPythonPackages ? (ps: [ ]), ... }:

let
# When running a nix shell, XDG_DATA_DIRS will be populated so that bash_completion can (lazily) find this completion script
Expand All @@ -17,11 +17,22 @@ let
poetry-env = poetry2nix.mkPoetryEnv {
python = python3;
# We pass these instead of `projectDir` to avoid adding the dependency on other files.
inherit pyproject poetrylock;
inherit pyproject poetrylock extras;
preferWheels = true;
overrides = poetry2nix.overrides.withDefaults (final: prev: {
# https://github.com/nix-community/poetry2nix/issues/1499
django-stubs-ext = prev.django-stubs-ext.override { preferWheel = false; };

psycopg = prev.psycopg.overridePythonAttrs (old: {
buildInputs = old.buildInputs or [ ]
++ lib.optionals pkgs.stdenv.isDarwin [ pkgs.openssl ];
propagatedBuildInputs = old.propagatedBuildInputs or [ ] ++ [ pkgs.postgresql ];
});

psycopg-c = prev.psycopg-c.overridePythonAttrs (old: {
nativeBuildInputs = old.nativeBuildInputs or [ ] ++ [ pkgs.postgresql ];
propagatedBuildInputs = old.propagatedBuildInputs or [ ] ++ [ final.setuptools ];
});
});
groups = poetry-groups;
checkGroups = [ ]; # would otherwise always install dev-dependencies
Expand Down
19 changes: 16 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ django-fsm = "~2.8.2"
Django = "~5.1.0"
mozilla-django-oidc = "~4.0.1"
openpyxl = "~3.1.5"
psycopg = { version = "~3.2.3", extras = ["binary"] }
psycopg = "~3.2.3"
psycopg-c = { version = "~3.2.3", optional = true }
psycopg-binary = { version = "~3.2.3", optional = true }
redis = "~5.2.0"
typing-extensions = "~4.12.2"
xlwt = "~1.3.0"
Expand All @@ -34,6 +36,10 @@ tblib = "~3.0.0"
xlrd = "~2.0.1"
typeguard = "~4.4.0"

[tool.poetry.extras]
psycopg-c = ["psycopg-c"]
psycopg-binary = ["psycopg-binary"]

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Expand Down

0 comments on commit 872ef2e

Please sign in to comment.